character movement and angles

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
hellboytwosix
Prole
Posts: 1
Joined: Mon Feb 25, 2019 3:08 pm

character movement and angles

Post 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
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: character movement and angles

Post 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)
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot] and 5 guests