7
\$\begingroup\$

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!!

Cheers!

o7

\$\endgroup\$

1 Answer 1

9
\$\begingroup\$

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)/sin(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.

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Do you have a way of shrinking the radius as the objects are removed? \$\endgroup\$ Jan 1, 2013 at 7:02
  • 2
    \$\begingroup\$ @LuisEstrada I've edited the answer to calculate a distance from center, based on the desired distance between objects. \$\endgroup\$ Jan 1, 2013 at 19:00
  • \$\begingroup\$ 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. pastebin.com/qnXHuBZE P.S. I adjusted positioning to account for the center of the screen not being 0,0. Thanks again! Cheers!! \$\endgroup\$
    – Nicholas
    Jan 1, 2013 at 23:23
  • \$\begingroup\$ 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? \$\endgroup\$ Jan 1, 2013 at 23:48
  • \$\begingroup\$ 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. \$\endgroup\$
    – Nicholas
    Jan 2, 2013 at 0:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .