Sign in to follow this  
Hikaru

2D-movement (floating, circulate)

Recommended Posts

hello! I'm having trouble to calculate the right movement for my NPCs. It's easy to make them go up/down/left/right but some of my npcs should have the ability to float and circulate on the screen.. this shouldn't be so hard since everything is in 2D. I know I need to play around with sin & cos but does anyone know a nice formula on this? or maybe a tutorial? thanks in advance //Hikaru

Share this post


Link to post
Share on other sites
A circling movement around (0;0) can be described as follows:

x(t) = r * sin(omega*t + phi)
y(t) = r * cos(omega*t + phi)

r is the radius

omega is the circular frequency (the higher, the faster the movement)

phi determines at which angle the movement starts:
0: north
pi/2: east
pi: south
3pi/2: west

Share this post


Link to post
Share on other sites
To elaborate on that: Have the NPC in question "remember" the centre and radius of the circle it should travel around, and its current angle. (A good convention is to have a zero angle represent travel to the right, with increasing angle changing counter-clockwise: this corresponds to how mathematicians normally think about it with their sins and coses. I'm pretty sure Fred's equations correspond to that equation, in which case the equivalences given for phi values are wrong :) ) To move it a distance 'd' around the circle, you should increase the angle by d/r radians (you should store the angles in radians, rather than doing lots of back and forth conversion. If you must, accept a degree value in the NPC constructor and convert it right away for storage). Thus the 'phi' in Fred's equation is simply the starting angle, and 'omega' is d/(r*t).

Share this post


Link to post
Share on other sites
If the radius of the circle is going to be the same for an NPC, you might want to consider calculating it all at the start and storing it. So, if you're okay with single degree increments, or less/more, whatever, you can calc it all at the start and store it in an array and then use that position you get from that as an offset from the radius of the circle (the NPC's current position). That way you don't have to always be calculating sin and cos, although it's likely that in your case continually calling sin and cos will not make one iota of difference in the speed. But, if you can save some computing power for something else, why not?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
A good convention is to have a zero angle represent travel to the right, with increasing angle changing counter-clockwise: this corresponds to how mathematicians normally think about it with their sins and coses. I'm pretty sure Fred's equations correspond to that equation, in which case the equivalences given for phi values are wrong :)

If you want that behavior, simply switch the sines and cosines.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this