游戏物理-非常基本的碰撞分辨率与verlet集成



我正在学习如何做一些非常基本的物理东西,以供自己娱乐,但是我遇到了一个奇怪的问题。

我使用的时间校正verlet积分方法如http://www.lonesock.net/article/verlet.html所述,到目前为止,它工作得很好。动作和东西看起来都很好。

我的问题发生在碰撞解决方面。我可以很好地解决地形问题,因为地形不会移动。我只是将玩家的当前位置设置为一个安全点,事情似乎很顺利。然而,当我试图解决另一个精灵(物理演员等)时,事情似乎到处都是。

我的分辨率代码如下所示:

    void ResolveCollision(Sprite* entity1, Sprite* entity2)
    {
        Vec2D depth = GetCollisionDepth(entity1, entity2);
        if (GetSpritePhysical(entity1) && GetSpritePhysical(entity2))
        {
            /* Two physical objects. Move them apart both by half the collision depth. */
            SetPosition(entity1, PointFromVector(AddVectors(
                VectorFromPoint(GetPosition(entity1)), ScalarMultiply(0.5f, depth))));
            SetPosition(entity2, PointFromVector(AddVectors(
                VectorFromPoint(GetPosition(entity2)), ScalarMultiply(-0.5f, depth))));
        }
        else if (GetSpritePhysical(entity1) && !GetSpritePhysical(entity2))
        {
            SetPosition(entity1, PointFromVector(AddVectors(
                VectorFromPoint(GetPosition(entity1)), ScalarMultiply(-1.0f, depth))));
        }
        else if (!GetSpritePhysical(entity1) && GetSpritePhysical(entity2))
        {
            SetPosition(entity2, PointFromVector(AddVectors(
                VectorFromPoint(GetPosition(entity2)), depth)));
        }
        else
        {
            /* Do nothing, why do we have two collidable but nonphysical objects... */
        }
    }

可以看到,有三种情况,取决于碰撞的精灵是否是物理的。

这很可能是第一个有问题的案例;我在这里使用相同的函数来获得穿透深度,我在我的(看似有效的)地形碰撞中。

为什么碰撞的精灵会飞得到处都是?这个问题把我难住了。

一旦将它们分开,就可以求解它们的速度/动量。你是怎么做到的?

最有可能的是,为了确定分离的方向,你使用一个从每个中心出发的向量。穿透越深,这个向量(碰撞法线)可能越不准确。这会导致意想不到的结果。

效果更好的是不必将它们分开,而是在移动它们之前计算碰撞点的位置,然后只将它们移动到那么远…然后解速度…然后在剩下的时间片中将它们移动到新的方向。当然,这有点复杂,但结果更准确。

最新更新