2013-07-16 123 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. 
} 

建立一个像样的定向碰撞引擎是一个不同的事情,但这种技术将处理基础知识(并摆脱那种地狱般的弹跳)。

希望这不是太冗长/不会错过任何重要的东西,我真的希望它有所帮助 - 我在很长一段时间内与你一样面对同样的问题,我知道它有多令人沮丧是!

我期待着看到别人的技术来处理这个!