Verlet 集成的非常基本的碰撞解决方案

问题描述 投票:0回答:1

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

我正在使用 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... */
        }
    }

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

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

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

game-physics verlet-integration
1个回答
1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.