Page 1 of 1

character movement and angles

Posted: Mon Feb 25, 2019 3:27 pm
by hellboytwosix
Hi im trying to make a simple RTS like game.

Current state is that the character can move when iam click on a place on the screen, it moves in its on pace. i can rotate by manually changing the var.

The problem is that i need the character to turn i the direction that its moving in.

I have read about trigonometri an have some basic knowledge

It more of a math/logical problem but im hoping u can help

// David

Re: character movement and angles

Posted: Tue Feb 26, 2019 1:31 am
by keharriso
Let's say you have character coordinates at (cx, cy) and target coordinates at (tx, ty).

Then you get:

Code: Select all

dx = tx - cx
dy = ty - cy
r = math.atan2(dy, dx)
You also want to set the ox/oy arguments to love.graphics.draw so that it rotates around the center.

EDIT: If you don't have tx or ty and just have a velocity – say, (vx, vy) – then use:

Code: Select all

r = math.atan2(vy, vx)