我正在学习如何为自己的娱乐做一些非常基本的物理东西,但我遇到了一个奇怪的问题。
我正在使用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... */
}
}
可以看出,有三种情况,取决于碰撞的精灵是否是物理的。
这很可能是第一个有问题的案例;我在这里使用相同的功能来获得穿透深度,就像我在(看似有效的)地形碰撞中一样。
为什么碰撞的精灵会飞得满地都是?我对这个问题感到很困惑。任何帮助都会很棒。