get what angle an object is moving at ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

get what angle an object is moving at ?

Post by Vrx8 »

for example.. i move my mouse to top left corner from the middle of the screen.. then my mouse is moving at 45 degree angle, but how do i get the value ?
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: get what angle an object is moving at ?

Post by erasio »

Generally you would want to have discrete steps with some time in between because of minor inaccuracies and the really small change in position.

The solution is trigonometry!

The formula is:

Code: Select all

angle = math.atan2(dx, dy)
Where dx and dy are the difference between pointA and pointB.

Code: Select all

 
dx = math.abs(x1 - x2)
dy = math.abs(y1 - y2) 
Edit: oh. Minor mistake by me. The angle will be in radians (a number between 0 and 2pi) . If you need degrees you need to translate the angle from radians to degree.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: get what angle an object is moving at ?

Post by zorg »

And thankfully, lua has functions for that: math.deg and math.rad convert from radians to degrees and vice versa.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: get what angle an object is moving at ?

Post by ivan »

Yep, erasio is on the right path except that atan2 accepts the y coordinate first:

Code: Select all

angle = math.atan2(dy, dx)
This is important since in trig you expect an angle of 0 to point East and to increase counter-clockwise
so that both math.pi and -math.pi point West. Therefore 2pi or math.pi*2 or 360 degrees actually points East.
Also, note that atan2 ALWAYS returns a value between -math.pi and math.pi.

To get the "vector" between two points we use:

Code: Select all

dx = x1 - x2
dy = y1 - y2
without the math.abs()

In short the code should be
headingAngle = math.atan2(finalY - initialY, finalX - initialX)
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

Re: get what angle an object is moving at ?

Post by Vrx8 »

erasio wrote: Wed Sep 13, 2017 11:03 am Generally you would want to have discrete steps with some time in between because of minor inaccuracies and the really small change in position.

The solution is trigonometry!

The formula is:

Code: Select all

angle = math.atan2(dx, dy)
Where dx and dy are the difference between pointA and pointB.

Code: Select all

 
dx = math.abs(x1 - x2)
dy = math.abs(y1 - y2) 
Edit: oh. Minor mistake by me. The angle will be in radians (a number between 0 and 2pi) . If you need degrees you need to translate the angle from radians to degree.
thx
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: get what angle an object is moving at ?

Post by erasio »

Oh man. I really am not at the point where I can do this from the top of my head yet.

Listen to Ivan. Everything he said is absolutely right.

Math.abs would just ignore half of the angles (what the heck was I thinking?) and yes indeed. Y is first for atan2.
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests