我正在編寫3D XNA遊戲,我正在努力理解如何實現我的兩個玩家之間的動量衝突。基本上這個概念是讓任何一個玩家都擊中另一個並試圖將對手擊倒關卡(比如粉碎兄弟遊戲)來得分。C#碰撞編程 - 動量
//Declared Variables
PrimitiveObject player1body;
PrimitiveObject player2body;
Vector3 player1vel = Vector3.Zero;
Vector3 player2vel = Vector3.Zero;
//Created in LoadContent()
PrimitiveSphere player1 = new PrimitiveSphere(tgd, 70, 32);
this.player1vel = new Vector3(0, 135, -120);
this.player1body = new PrimitiveObject(player1);
this.player1body.Translation = player1vel;
this.player1body.Colour = Color.Blue;
PrimitiveSphere player2 = new PrimitiveSphere(tgd, 70, 32);
this.player2vel = new Vector3(0, 135, 120);
this.player2body = new PrimitiveObject(player2);
this.player2body.Translation = player2vel;
this.player2body.Colour = Color.Red;
//Update method code for collision
this.player1vel.X = 0;
this.player1vel.Y = 0;
this.player1vel.Z = 0;
this.player2vel.X = 0;
this.player2vel.Y = 0;
this.player2vel.Z = 0;
if (player1body.BoundingSphere.Intersects(player2body.BoundingSphere))
{
this.player2vel.Z += 10;
}
正如你可以看到我做的是檢查PLAYER1的邊界球,當它與玩家2的那麼球員相交2將被推回在Z軸上,現在顯然這只是不會工作,並不是非常直觀,而我的問題就在於我試圖弄清楚如何提出解決方案,我想要發生的是當任何一個玩家與另一個玩家發生碰撞時,他們基本上會交換向量,讓它產生反彈效果I我正在尋找並且不僅僅影響一個座標軸,而是兩個座標軸X & Z.
感謝您抽出時間閱讀,我會感謝任何人都能想到的解決方案。
備註: PrimitiveSphere如果您想知道是使用(圖形設備,球體的直徑,tesselation)。
我想你的意思是'this.player2vel = p1v;' – SLuck49
感謝你提供的信息,這真的很有用,我會在稍後將這個信息應用到爆炸中,但不幸的是,當玩家現在應該碰撞時,其他與您的解決方案,這是奇怪的,因爲我看到它應該如何工作。 – Kyle
@ShawnHolzworth,謝謝你,我做到了。 – davidsbro