Hikaru 122 Report post Posted May 1, 2006 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 0 Share this post Link to post Share on other sites
Fred304 382 Report post Posted May 1, 2006 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 radiusomega is the circular frequency (the higher, the faster the movement)phi determines at which angle the movement starts:0: northpi/2: eastpi: south3pi/2: west 0 Share this post Link to post Share on other sites
Zahlman 1682 Report post Posted May 1, 2006 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). 0 Share this post Link to post Share on other sites
Adam Hamilton 271 Report post Posted May 1, 2006 Remember that omega is 2 * pi * f (where f is in cycles per second) 0 Share this post Link to post Share on other sites
Hikaru 122 Report post Posted May 2, 2006 Thanks guys! 0 Share this post Link to post Share on other sites
Endar 668 Report post Posted May 2, 2006 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? 0 Share this post Link to post Share on other sites
Fred304 382 Report post Posted May 2, 2006 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. 0 Share this post Link to post Share on other sites