How to move sprites in a circle

Started by
3 comments, last by CTRL_ALT_DELETE 22 years, 10 months ago
How do I move move a sprite that has an x and y velocity, in a circle at a constant speed. Something with vectors??? I am not fimiliar with this type of math.
Advertisement
What you mean your not familiar with this math; there is no math for moving a rect. in a circle. Just set 4 vertices. And move it based on those. Have vertex 1.x = obj.width + obj.width/2 ; and vertex1.y = obj.height + obj.height/2; Get the point where to put them? That would be slightly above the obj in the middle, just make three other vertices around the rect. of the object (or square) Then increment/decrement the x & y values based on the vertices you have plotted. Theres no advanced math for this.
bad!

  int angle = 0;while(true){    angle = (angle+1)%360;    sprite.xpos = 100+100*cos(2*PI*angle/360.0);        sprite.ypos = 100+100*sin(2*PI*angle/360.0);    sprite.render();}  

  int angle = 0;while(true){    angle = (angle+1)%360;    sprite.xpos = 100.0+100.0*cos(2.0*PI*angle/360.0);        sprite.ypos = 100.0+100.0*sin(2.0*PI*angle/360.0);    sprite.render();}  
The Anonymous Poster gave a reasonably good implementation (optimizations lurk, as always). The theory is as follows: imagine a vector reaching out from the center of the circle to its circumference; this vector will move with a constant angular velocity (your increment) and a variable linear acceleration due to the rotation. Say the circle is centered about (x,y) and the vector is of length l . The position (vx,vy) of the sprite - the tip of the vector - is defined by the simple trigonometric opposite and adjacent sides of the right angle triangle formed by the x-axis, the y-axis and the vector itself. Given the angle t in radians :
    vx = l * cos(t);vy = l * sin(t);  


Provided t is measured in the counterclockwise direction from the positive x-axis. Degree to radian angle conversions can be done as

radian = PI * degree / 180;

and sine and cosine values can be precomputed for all integer degree values (0 <= degree < 360). Throw all this into the preceding loop from the Anonymous Poster and voila!

Edited by - Oluseyi on June 8, 2001 8:45:37 PM

This topic is closed to new replies.

Advertisement