2013-07-16 78 views
0

我知道這個問題可能會被問及很多,我很抱歉。但是我在比賽中碰到了一段時間的麻煩,我想要一些幫助。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; 
          } 

         } 

但問題是,如果玩家不與矩形相交我設定的重力上,所以他不斷地爲他碰撞與下降固體,然後放在上面不與它相撞。我需要知道的是:我怎樣才能避免這種情況?有沒有其他方法可以將球員的重力設置爲開啓狀態,而不會讓球員連續朝向球員下落,只能將球員放回球門上再次下降?

任何幫助表示讚賞。

+2

這個問題可能更適合我們的[gamedev網站](http://gamedev.stackexchange.com)。讓我知道你是否希望我標記這個遷移。 –

回答

0

我解決這個問題的方式可能不是最佳的(實際上我確信它可能不是最佳方式),但它迄今爲止在我所有的2D平臺項目中都適用於我。

我開始爲sprite類定義第二個矩形。該矩形與主邊界框具有相同的寬度和X座標,但會略高(在我的情況下爲2或3)。您還需要抵消它使兩個矩形的底部邊緣是內聯,來說明:

Rectangle boundingRect = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); 
Rectangle gravityRect = new Rectangle((int)boundingRect.X, (int)boundingRect.Y - 3, texture.Width, texture.Height + 3); 

Sprite類也需要一個布爾跟蹤如果玩家要下降的。還有一個要跟蹤它是否穩定(在初始化過程中,您明確地指定了它)。

public bool isGrounded = false; 
bool isSolid; 

在我Game1類的頂部,我宣佈2個整數:

int gravityTotalI = 0; 
int gravityCounterI = 0; 

當初始化我的精靈,我通常他們都添加到列表中。所以,我可以這樣做:

foreach (Sprite s in spriteList) 
{ 
    if (s.isSolid) 
    { 
     gravityTotalI++; 
    } 
} 

現在,我用邏輯的該位在的Game1更新方法:

foreach (Sprite s in spriteList) 
{ 
    if (!s.Equals(player) 
    { 
     if (player.boundingRect.Intersects(s.boundingRect) || player.boundingRect.Intersects(s.gravityRect)) 
     { 
      player.isGrounded = true; 
      gravityCounterI = 0; 
     } 
     else 
     { 
      gravCounterI++; 
      if (gravCounterI >= gravTotalI) 
      { 
       player.isGrounded = false; 
       gravCounterI = 0; 
      } 
     } 

     if (player.boundingRect.Intersects(s.boundingRect)) 
     { 
      player.position.Y -= 2f; //set the resistance of the platform here 
     } 
    } 
} //end of foreach loop. 
if (!player.isGrounded) 
{ 
    player.position.Y += 2f; //set the force of gravity here. 
} 

建立一個像樣的定向碰撞引擎是一個不同的事情,但這種技術將處理基礎知識(並擺脫那種地獄般的彈跳)。

希望這不是太冗長/不會錯過任何重要的東西,我真的希望它有所幫助 - 我在很長一段時間內與你一樣面對同樣的問題,我知道它有多令人沮喪是!

我期待着看到別人的技術來處理這個!