Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collision between multiple objects

I'm writing a simple physics system for fun, and I've run into a problem that has me stuck.

The basic algorithm now is:

  1. Move an object
  2. Check for collisions
  3. If there was a collision
    • Move the object the minimum distance to resolve the collision.
    • Adjust the velocity based on the normals, masses, etc

I have one moving body moving toward two, static, massless, bodies.

Figure One

The moving object is translated in one step to collide with one of the bodies.

Figure Two

I respond by finding the smallest distance I can move so that they are no longer colliding. In this case, that means moving the dynamic body straight down. However, now it is colliding with another box.

Figure Three

I repeat the same thing with that box, trying to move the dynamic box so that it is no longer colliding, but that pushes it back into the first box. This repeats forever. Is my algorithm fundamentally flawed?

like image 465
user1174528 Avatar asked Sep 02 '13 18:09

user1174528


People also ask

What are the types of collision detection?

Two forms of collision detection: Continuous: very expensive. Simulate solid objects in real life. Discrete: objects will end up with penetrating each other.

Which method is used to collision detection between two rectangles?

Axis-Aligned Bounding Box One of the simpler forms of collision detection is between two rectangles that are axis aligned — meaning no rotation. The algorithm works by ensuring there is no gap between any of the 4 sides of the rectangles. Any gap means a collision does not exist.

What is code collision?

code / collision is a new kind of event for coders. coders go head-to-head in a variety of competitions. All you code is the strategy. join an event or host your own. play virtually or in person.

How do you detect a 2d collision?

The most basic way to approach this type of 2d collision detection is to utilize a concept known as Axis-aligned bounding box. Axis-aligned bounding box, or AABB, detects if two non-rotational polygons are overlapping each other.


1 Answers

Instead of moving down once a collision has been detected it would be better to move back in the direction you came from. That way you have a guarantee that eventually you must end up in a state where there are no collisions if we assume that the initial state had no collisions.

We need to find out by how much we need to shrink (scale) v to fit it into the object intersection. The shrunk v will have the right magnitude, so that if we move backwards in the direction of -v by that magnitude, then we will no-longer intersect.

Let's assume an intersection consists of a x_intersection and a y_intersection component. To find out by how much we need to move backwards to no longer intersect we need to scale the original v = (v_x, v_y) vector. If the x_intersection is the smaller intersection then we scale v by x_intersection / v_x and move our object back by -v * x_intersection / v_x. This means we move back by -(x_intersection, x_intersection * v_y/v_x). If the y_intersection is the smaller intersection then we scale v by y_intersection / v_y and move our object backwards by -v * y_intersection / v_y = -(y_intersection * v_x/v_y, y_intersection).

So I would say the steps in your algorithm could be:

  1. Move object by some move vector v
  2. Check for all collisions
  3. If there was a collision

    • For all collision objects find the minimum scaling of v by which we we would need to move back. This scaling can be computed as the minimum of two ratios

      given v = (v_x, v_y)
      min_i = min(x_intersection / v_x, y_intersection / v_y)    
      
    • Find the minimum scaling ration across all objects.

      min_o = min(min_i) for all i
      
    • Move object back in direction of vector obtained by scaling the negative move direction with the minimum ratio. That is v2 = (min_o*-v) where v2 is the vector we use to move back.

  4. Go back to step 2

For example: first pick w:

w

Then pick u2:

u2

Done :

enter image description here

like image 198
cyon Avatar answered Sep 30 '22 09:09

cyon