Hello everybody, I need to design a swarm system with n-bodies and I have an issue:

when 2 objects get close to each other (distance less than r_max) they start interacting and enter a formation. The formation I want is one in which one object moves and the other orbits around it. The orbiting object has to always stay within the r_max distance from the other object and cannot move faster than d_max per time_step.

I cannot use the global position of the objects for the calculations, but I know the distance between the 2 objects and the relative angle between the 2.

So far I have this formula:

f(x,y) = e^(-alpha*dist^2)

dx = alpha*f(x,y)*sin(theta)*dist;

dy = -alpha*f(x,y)*cos(theta)*dist;

I return those dx and dy which are going to be added to the global position outside of this function.

If I use this formula, however, the f(x,y) becomes smaller and smaller or larger and larger depending on alpha so the object doesn't stay in orbit.

Can anyone help me getting the object to move at a maximum speed of d_max per time_step and staying in an orbit with a distance that never exceeds r_max?

For semplicity we can assume that the object that doesn't orbit moves at a speed way lower than d_max per time_step

Thank you very much in advance