   Collision Detection/Resolution
  I am making a 2D top-down game and I am looking for some help in regards to the collision resolution between two moving (circular) objects.Basically, what I am trying to achieve is the effect of large groups of enemies which "cluster" behind one another naturally rather than moving through or on top of each other. I'm not looking for anything sophisticated like boids or actual grouping mechanics-- just enough to make it so enemies collide properly.Using a bounding circle approximation is accurate enough for my purposes, and so  actually detecting a collision is simple. What I need is to only allow the objects to move far enough until there is a collision and then stop.Thank you for any help or assistance you have.   
  You probably want to have something like an update movement method that gets called every frame.Then you want to have the ability to query collisions that happened between frames while in there, then you handle it by bouncing back or whatever.You will have to have something that keeps track of each object that has collisions to help with the checking.You can get infinitely more elaborate, but that is the basic formula you can't easily get away from.   ;Quote:You probably want to have something like an update movement method that gets called every frame.Right. I already have such methods in place.Quote:Then you want to have the ability to query collisions that happened between frames while in there, then you handle it by bouncing back or whatever.Well, I am currently looping through all objects after I determine where they would move to (not having moved yet) two at a time (using two nested for loops) and checking for collisions. If the initial check comes back true, I need to handle where to move one or both back to. This is what I need help with.Quote:You will have to have something that keeps track of each object that has collisions to help with the checking.Really? Could you elaborate on this please.Thanks for the reply.   ;  You generally would have some way to declare an object can collide with other objects. If all your game objects collide, then you just keeping them all in a list is fine.In your case it sounds pretty simple. So you loop through and for any object that collided, stop moving. You must have some kind of variable saying what the destination is or something, right? It should be easy enough to stop.Maybe the key element you are missing is you have to have the updates happen at the proper time. That is, you have two locations in a sense. The actual location as of last frame and the location for this frame. The engine calculates the movement, and determines what collides with what given the movement speeds and directions of the relavent objects. Then the object itself checks to see if it collided when it's time to update. If it collided, you do nothing in this case. You don't change position at all. If you did collide, you update to the new position.It can get kind of complicated because often things move fast so if you aren't colliding now is it because you moved too fast? But in your case it seems pretty simple.   ;  basic distance constraints.Quote:for (int i = 0; i < count; i ++){    for(int j = i+1; j < count; j ++)    {    Vector delta = (sphere.m_centre - sphere[j].m_centre);    float radius = (sphere.m_radius + sphere[j].m_radius);    float delta_length_squared = delta.dot_product(delta);    float radius_squared  = radius * radius;    if(delta_length_squared < radius_squared){   float delta_length = sqrt(delta_length_squared);   Vector push = delta * ((radius - delta_length) / delta_length);   sphere.m_centre += (push * 0.5f);   sphere[j].m_centre -= (push * 0.5f);    }    }}pushes sphere away if they intersect. Both spheres are pushed equal amount. That amount can be modulated by mass ratios, or size ratios (move the bigger sphere less, as it will generate more problems once it is interfered with). Quote:   // use sphere masses as collision restitution ratios   float a = 1.0f / sphere.m_mass;   float b = 1.0f / sphere[j].m_mass;// use sphere radii as collision restitution ratios//   float a = 1.0f / sphere.m_radius;//   float b = 1.0f / sphere[j].m_radius;sphere.m_centre += push * (a / (a + b));   sphere[j].m_centre -= push * (b / (a + b));;Quote:Original post by oliiibasic distance constraints....pushes sphere away if they intersect. Both spheres are pushed equal amount. That amount can be modulated by mass ratios, or size ratios (move the bigger sphere less, as it will generate more problems once it is interfered with). Ah, thanks for the help! I am actually trying to determine the exact time t at which they collide (to adjust velocity vectors accordingly).This way I can make them just touch each other without "pushing" each other away...   