2D-movement (floating, circulate)

Started by
5 comments, last by Fred304 17 years, 11 months ago
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
Advertisement
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
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).
Remember that omega is 2 * pi * f (where f is in cycles per second)
Thanks guys!
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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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.

This topic is closed to new replies.

Advertisement