Public Group

# 2D-movement (floating, circulate)

This topic is 4522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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)

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 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 on other sites
Remember that omega is 2 * pi * f (where f is in cycles per second)

Thanks guys!

##### 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 on other sites
Quote:
 Original post by ZahlmanA 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.

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632562
• Total Posts
3007098

×