Use a rotation matrix. This code will move from a point (x,y) by theta radians to a new point (px, px)
Point Rotate(x, y, theta)
int px = (x * Math.Cos(theta)) - (y * Math.Sin(theta));
int py = (y * Math.Cos(theta)) + (x * Math.Sin(theta));
return new Point(px, py);
end
The matrix used above,
[cosθ - sinθ][x]
[cosθ + sinθ][y]
Will move a point around the circle clockwise when using graphical coordinates.
I did the exact same thing last week. You can animate this by finding the total theta that you wish to move and then divide that by the number of frames (or steps). Now, start every move at some arbitrary point (for example, (0, radius)) and increment some counter totalSteps and move always beginning from that initial point. If you simply move the point itself each frame you will accumulate some error, but if you always move from the initial point by the current increment, stopping when the increment == totalTheta, it will be perfect. Let me know if that makes sense.
Maybe I should illustrate a bit more. Let's say you have a method "BeginMove":
double totalTheta = 0;
double increment = 0;
double currentTheta = 0;
bool moving = false;
void BeginMove()
{
totalTheta = (2 * Math.PI) / numObjects;
increment = totalTheta / steps;
currentTheta = 0;
moving = true;
}
Now you have a method which updates the move every frame:
void Update
{
if (!moving) return;
// do a min/max to ensure that you never pass totalTheta when incrementing.
// there will be more error handling, but this is the basic idea.
currentTheta += increment;
SomeObject.Location = Rotate(0, radius, currentTheta);
moving = (currentTheta < totalTheta);
}
There will obviously be more logic here depending upon your exact situation, but the idea is:
- Find the total theta to move.
- Find the increment (totalTheta / steps)
- Maintain a running total of how much you have moved already.
- Increment the running total by the angle increment before each move.
- Start each move from the same (arbitrary) point on the circle and rotate by the running total.
- Repeat until the running total == total theta.