我正在學習如何爲我自己的娛樂做一些非常基本的物理材料,但我遇到了一個奇怪的問題。與verlet集成的非常基本的碰撞分辨率
我正在使用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... */
}
}
正如人們所看到的,有三種情況,由碰撞精靈是否是物理的或沒有確定。
這很可能是第一個有這個問題的案例;而且我正在使用相同的函數來獲取穿透深度,以使我處於我的(看似正在工作的)地形碰撞中。
爲什麼碰撞的精靈會飛到所有的地方?我很困惑這個問題。任何幫助都會很棒。