我知道這個問題可能會被問及很多,我很抱歉。但是我在比賽中碰到了一段時間的麻煩,我想要一些幫助。XNA 2D平臺變形器碰撞和重力
首先,遊戲是2D平臺遊戲。每個固體都放在一個列表中。我有碰撞檢測此代碼對我的作品相當不錯:
if (player.rectangle.Intersects(rect))
{
player1Collision = true;
colSolid = solid;
colRectangle = rect;
}
if (player1Collision)
{
Vector2 pos = player.position;
Vector2 pLeft = new Vector2(player.BoundingBox.Left, 0);
Vector2 pRight = new Vector2(player.BoundingBox.Right, 0);
Vector2 pTop = new Vector2(0, player.BoundingBox.Top);
Vector2 pBottom = new Vector2(0, player.BoundingBox.Bottom);
Vector2 sLeft = new Vector2(colSolid.BoundingBox.Left, 0);
Vector2 sRight = new Vector2(colSolid.BoundingBox.Right, 0);
Vector2 sTop = new Vector2(0, colSolid.BoundingBox.Top);
Vector2 sBottom = new Vector2(0, colSolid.BoundingBox.Bottom);
if (player.rectangle.Intersects(colRectangle))
{
if (player.velocity.X > 0 && Vector2.Distance(pRight, sLeft) < player.texture.Width/2)//left
{
player.velocity.X = 0f;
pos.X = colSolid.BoundingBox.Left - player.BoundingBox.Width;
}
else if (player.velocity.X < 0 && Vector2.Distance(pLeft, sRight) < player.texture.Width/2)//right
{
player.velocity.X = 0f;
pos.X = colSolid.BoundingBox.Right;
}
if (player.velocity.Y > 0 && Vector2.Distance(pBottom, sTop) < player.texture.Height/ 2) //top
{
player.velocity.Y = 0f;
player.gravityOn = false;
pos.Y = colSolid.BoundingBox.Top - player.BoundingBox.Height;
}
else if (player.velocity.Y < 0 && Vector2.Distance(pTop, sBottom) < player.texture.Height/2)//bottom
{
player.velocity.Y = 0f;
pos.Y = colSolid.BoundingBox.Bottom;
}
player.position = pos;
}
else
{
player.gravitySpeed = 0.15f;
player.gravityOn = true;
}
}
但問題是,如果玩家不與矩形相交我設定的重力上,所以他不斷地爲他碰撞與下降固體,然後放在上面不與它相撞。我需要知道的是:我怎樣才能避免這種情況?有沒有其他方法可以將球員的重力設置爲開啓狀態,而不會讓球員連續朝向球員下落,只能將球員放回球門上再次下降?
任何幫助表示讚賞。
這個問題可能更適合我們的[gamedev網站](http://gamedev.stackexchange.com)。讓我知道你是否希望我標記這個遷移。 –