Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have multiple objects (circles) and I want to place them in a circle, with even distance between each one, and have them orbit the center of the screen.

If I remove one object, I want the orbit radius to reduce and the other circles, as a result, end up getting closer and closing the circle again.

Below are some images to help explain what I'm trying to achieve.

A. is the objects evenly distributed around the center.

enter image description here

B. is after destroying one of the objects, the "circle of objects" need to close in and evenly distribute the objects once again.

enter image description here

C. is just after another object has been destroyed.

enter image description here

I know that the solution is some "basic" math, but it's been a while, and I'm super rusty.

Thanks for the help, if anything further is needed please let me know!!



share|improve this question
up vote 9 down vote accepted

There are 360 degrees (2π radians) in a circle. Divide that by the number of objects, and that tells you the correct angle between the objects, for even spacing.

If you want to keep the objects the same distance apart no matter how many objects are in the circle, we need to calculate the distance out from the center at which points are that distance apart. That's just trigonometry; we make a triangle from the center to two adjacent objects. Split that into two right triangles, and use the cosin of the half angle to determine the distance from the center to one of the objects.

We can then use the angle and distanceFromCenter to place each object.

So if that calculated angle is a, then: (pseudocode)

float distanceBetweenObjects = 50.0; // or however far apart you want them to be
float distanceFromCenter = (distanceBetweenObjects*0.5)/cos(a*0.5);
float accumulatedAngle = 0.0;
foreach( object )
    object.position.x = cos(accumulatedAngle) * distanceFromCenter;
    object.position.y = sin(accumulatedAngle) * distanceFromCenter;
    accumulatedAngle += a;

Note that this assumes that the center of the screen is 0,0. If the center of your screen is some other coordinate, then add that to the calculations above. Also note that this doesn't take into account the size of the objects; it's merely trying to place their midpoints a certain distance apart. So your "sistanceBetweenObjects" value needs to include the size of the objects.

share|improve this answer
Do you have a way of shrinking the radius as the objects are removed? – Luis Estrada Jan 1 '13 at 7:02
@LuisEstrada I've edited the answer to calculate a distance from center, based on the desired distance between objects. – Trevor Powell Jan 1 '13 at 19:00
Hey @TrevorPowell, Thanks so much for the awesome answer. One quick problem. The objects don't seem to get evenly distributed no matter how many objects I instantiate. That's even after adding in the dimensions of the objects into both the distanceFromCenter and distanceBetweenObjects. Here's a link to the function as it stands currently. P.S. I adjusted positioning to account for the center of the screen not being 0,0. Thanks again! Cheers!! – Nicholas Jan 1 '13 at 23:23
Bearing in mind that I don't know AS3, the code appears fine to me (apart from the additional +62 on the distanceFromCenter calculation, which shouldn't cause any problems other than putting the objects further out from the center than would be required for the requested distanceBetweenObjects). I've verified that the code I provided does calculate object positions correctly, and the code you're showing appears to be correct.. is there anything else in your codebase which might be moving the objects around, after they've been set up? – Trevor Powell Jan 1 '13 at 23:48
I don't believe so. I will double check right now. So if the dimensions of the object is 62, and let's say that I want 10px between each object, I would just make the distanceBetweenObjects = 72, and that should dynamically keep them evenly spaced yeah? And I shouldn't need to add the dimensions to the distanceFromCenter? It's odd. I'll try it again and maybe take two screenshots of what's happening. I can also paste code in c++ if you rather. – Nicholas Jan 2 '13 at 0:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.