2011-04-17 20 views
2

我正在學習如何爲我自己的娛樂做一些非常基本的物理材料,但我遇到了一個奇怪的問題。與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... */ 
    } 
} 

正如人們所看到的,有三種情況,由碰撞精靈是否是物理的或沒有確定。

這很可能是第一個有這個問題的案例;而且我正在使用相同的函數來獲取穿透深度,以使我處於我的(看似正在工作的)地形碰撞中。

爲什麼碰撞的精靈會飛到所有的地方?我很困惑這個問題。任何幫助都會很棒。

回答

1

一旦將它們分開,就可以解決它們的速度/動量。你最近怎麼樣?

最有可能的是,要確定分離的方向,可以使用從每個中心開始的向量。穿透越深,這個向量(碰撞正常)可能越不準確。這可能會導致意外的結果。

更好的辦法是不必將它們分開,而是在移動它們之前計算碰撞點的位置,然後只移動它們那麼遠......然後解析速度......然後將它們移入剩下的時間片的新方向。當然,這有點複雜,但結果更準確。