2

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

我正在使用http://www.lonesock.net/article/verlet.html上描述的时间校正的 verlet 集成方法,到目前为止,它工作得很好。动作和东西看起来都不错。

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

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

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... */
    }
}

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

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

为什么碰撞的精灵会飞得满地都是?我对这个问题感到很困惑。任何帮助都会很棒。

4

1 回答 1

1

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

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

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

于 2011-04-17T13:19:20.790 回答