多个物体之间的碰撞 [英] Collision between multiple objects

查看:164
本文介绍了多个物体之间的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个简单的物理系统,很有趣,但是遇到了一个困扰我的问题.

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

现在的基本算法是:

  1. 移动对象
  2. 检查碰撞
  3. 如果发生碰撞
    • 将对象移动最小距离以解决碰撞.
    • 根据法线,质量等调整速度
  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.

移动的对象一步一步平移以与其中一个物体碰撞.

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

我的反应是找到我可以移动的最小距离,以使它们不再碰撞.在这种情况下,这意味着将动态物体垂直向下移动.但是,现在它正在与另一个盒子碰撞.

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.

我在那个盒子上重复了同样的事情,试图移动动态盒子,使其不再碰撞,但是将它推回到第一个盒子中.这将永远重复.我的算法从根本上有缺陷吗?

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?

推荐答案

检测到碰撞后,与其向下移动,不如向后移动.这样,您可以保证,如果我们假设初始状态没有碰撞,那么最终您必须最终结束没有碰撞的状态.

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.

我们需要找出要缩小(缩放)v使其适合对象交点的量.收缩的v将具有正确的幅度,因此,如果我们沿-v的方向向后移动该幅度,则将不再有相交.

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.

让我们假设一个交集由x_intersectiony_intersection组成.若要找出我们需要向后移动多少以不再相交,我们需要缩放原始的v = (v_x, v_y)向量.如果x_intersection是较小的交集,则将v缩放为x_intersection / v_x,然后将对象移回-v * x_intersection / v_x.这意味着我们后退-(x_intersection, x_intersection * v_y/v_x).如果y_intersection是较小的交集,则将v缩放为y_intersection / v_y,然后将对象向后移动-v * y_intersection / v_y = -(y_intersection * v_x/v_y, y_intersection).

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. 通过某些移动矢量v
  2. 移动对象
  3. 检查所有碰撞
  4. 如果发生碰撞

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

  • 对于所有碰撞对象,找到最小比例尺v,我们需要根据该最小比例尺移回.该比例可以计算为两个比率中的最小比率

  • 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
    

  • 将对象向矢量方向移动,该矢量是通过以最小比例缩放负移动方向而获得的.那就是v2 = (min_o*-v),其中v2是我们用于后退的向量.

  • 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.

    例如:首先选择w:

    然后选择u2:

    完成:

    这篇关于多个物体之间的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆