Collision between multiple objects

假如想象 提交于 2019-12-05 17:56:18

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:

Then pick u2:

Done :

One possible solution that might be robust against the problem you described (totally untested):

  1. Move your object for a full time-step dt

  2. Check for collisions with other objects, this might be more than one object

  3. Calculate the 'time of impact' by interpolation, which is some real number that is smaller than the time step. Do this for each object you collided with, and choose the minimum one. This gives you the time to the first collision t_col < dt.

  4. Redo the last step, but now move the object only for t_col so that it exactly hits the object and then start flipping velocities and other collision related physics. You can now either finish the step here if you are lazy (probably ok, since dt should be small), or continue to move for another dt - t_col and see if you hit something else.

This is not something I invented just now, but is similar to the zero-cross detection that Simulink uses to simulate exactly this kind of discontinuous problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!