#### Archived

This topic is now archived and is closed to further replies.

# How to move sprites in a circle

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

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites

  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();}

##### Share on other sites

  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();}

##### Share on other sites
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

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631753
• Total Posts
3002098
×