Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Finding the movement angle?

Author
Message
Angel Eyes
19
Years of Service
User Offline
Joined: 16th May 2004
Location:
Posted: 3rd Mar 2011 02:40
Hi, just wondering if anyone can help with this.

I want to move a small sprite from one place on the screen to where the mouse pointer is, easy uh?

Well I cant figure out the rotation needed so that the sprite points towards the mouse.


Here is a little picture that shows you what I mean.



So if the point A is where the sprite is, and I want it to move to wherever the mouse is (point B in the picture) then I need the program to set the rotation of the sprite to point towards the mouse so that when I use the move sprite command it should move the sprite along the line to B.

At least I think it should!

Can anyone give me a simple short bit of code to do this please?
PrimalBeans
13
Years of Service
User Offline
Joined: 14th Oct 2010
Location: The sewer.... hunting alligatiors.
Posted: 4th Mar 2011 05:54 Edited at: 4th Mar 2011 05:55
Hmm i think what you would need is a variable in your character structure or whatever that kept track of the character rotation in radians.

From there you would need to be able to calculate the target angle by determining slope.


here is an image that can help you break things down a bit. The sprite position and the target position can be defined as a right triangle as seen here. this will help you determine the movement on the x and y axis.

now to determine the rotation of the sprite you just need this link: http://www.mathsisfun.com/algebra/trig-finding-angle-right-triangle.html

using trig to find the angle in radians and then comparing it to the current angle of the sprite you can determine the amount or rotation to apply.
go0d luck!

Attachments

Login to view attachments
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 4th Mar 2011 07:22
Hi,

Here's a little bit of code that calculates the angle between the mouse and the sprite and rotates it accordingly. I hope it helps.



To learn about how the atanfull command works, you should read the wikipedia article on atan2 (the rest of the computer world calls DBPro's atanfull atan2).

Angel Eyes
19
Years of Service
User Offline
Joined: 16th May 2004
Location:
Posted: 4th Mar 2011 16:05
Primal & Kira, thanks for that, its very helpful.

I used the routine and changed it so that now my gun turret rotates to the mouse and when I fire the shell follows the path to the mouse.

So thats one problem out of the way.. now for the other 9999.
Dashiznitz
12
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 19th Jun 2011 00:53
could you post your routine.. I have done this as well but there's a little drift as the sprite does not travel the exact line between the start and end points.

-Trying to get better
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 22nd Jun 2011 01:54
In case you're curious about the math behind the atanfull command, I wrote a function years ago that performs the same operation.
http://forum.thegamecreators.com/?m=forum_view&t=27159&b=6

Quote: "could you post your routine.. I have done this as well but there's a little drift as the sprite does not travel the exact line between the start and end points."

Are you storing the angle as a float instead of an integer? If the movement appears slightly off, it sounds like a small precision issue from storing the atanfull angle as an integer.

Login to post a reply

Server time is: 2024-03-29 14:41:05
Your offset time is: 2024-03-29 14:41:05