尽可能整齐地处理碰撞过程中的速度矢量

时间:2010-04-02 21:55:39

标签: c# xna collision

我正在尝试创建一种处理两个对象之间所有可能碰撞的好方法。通常一个人会移动并击中另一个,然后应该“反弹”。

到目前为止我所做的事情(我创造了一个典型的游戏,你有一个棋盘并用砖块反弹球)是检查矩形是否相交,如果它们相交,则反转Y速度。

这是一个非常丑陋和临时的解决方案,从长远来看不会起作用,因为这种处理在游戏中很常见,我真的很想找到一个很好的方法来为未来的项目做这件事。任何链接或有用的信息都表示赞赏。

以下是我现在的碰撞处理功能。

protected void collision()
    {
        #region Boundaries
        if (bal.position.X + bal.velocity.X >= viewportRect.Width ||
            bal.position.X + bal.velocity.X <= 0)
        {
            bal.velocity.X *= -1;
        }
        if (bal.position.Y + bal.velocity.Y <= 0)
        {
            bal.velocity.Y *= -1;
        }
        #endregion
        bal.rect = new Rectangle((int)bal.position.X+(int)bal.velocity.X-bal.sprite.Width/2, (int)bal.position.Y-bal.sprite.Height/2+(int)bal.velocity.Y, bal.sprite.Width, bal.sprite.Height);
        player.rect = new Rectangle((int)player.position.X-player.sprite.Width/2, (int)player.position.Y-player.sprite.Height/2, player.sprite.Width, player.sprite.Height);

        if (bal.rect.Intersects(player.rect))
        {
            bal.position.Y = player.position.Y - player.sprite.Height / 2 - bal.sprite.Height / 2;
            if (player.position.X != player.prevPos.X)
            {
                bal.velocity.X -= (player.prevPos.X - player.position.X) / 2;
            }

            bal.velocity.Y *= -1;
        }
        foreach (Brick b in brickArray.list)
        {
            b.rect.X = Convert.ToInt32(b.position.X-b.sprite.Width/2);
            b.rect.Y = Convert.ToInt32(b.position.Y-b.sprite.Height/2);
            if (bal.rect.Intersects(b.rect))
            {
                b.recieveHit();
                bal.velocity.Y *= -1;
            }
        }
        brickArray.removeDead();
    }

1 个答案:

答案 0 :(得分:1)

以下是一些可能有用的建议。

首先,所有对象,例如PlayerBal(不确定是什么)和Brick都有一些相似的界面 - 它们都有sprite(定义对象的大小),rect(对象的边界框),position(当前位置),velocity(当前速度)。这表明您可以创建一个公共基类来封装此功能。

另请注意,您始终会根据当前rectposition重新计算sprite。这表明rect实际上应该是隐藏重新计算的属性(总是相同的!)

因此,您可以从定义这样的公共基类开始(我将遵循.NET标准编码样式并使用属性和CamelCase名称):

class GameObject {
  Point Position { get; set; } 
  Vector Velocity { get; set; }
  Sprite Sprite { get; set; }
  Rectangle Rect { 
    get { 
      // Ecnapsulated calculation of the bounding rectangle
      return new Rectangle
        ( (int)Position.X + (int)Velocity.X - Sprite.Width/2, 
          (int)Position.Y + (int)Velocity.Y - Sprite.Height/2, 
          Sprite.Width, Sprite.Height);   
    }
}    

如果您现在将此类型用作所有游戏对象的基类,您应该能够写下这个:

protected void Collision() {      
    #region Boundaries        
    if (bal.Position.X + bal.Velocity.X >= viewportRect.Width ||        
        bal.Position.X + bal.Velocity.X <= 0)        
        bal.Velocity.X *= -1;        
    if (bal.Position.Y + bal.Velocity.Y <= 0)        
        bal.Velocity.Y *= -1;        
    #endregion

    if (bal.Rect.Intersects(player.Rect)) {      
        bal.Position.Y = player.Position.Y - player.Sprite.Height/2 
                       - bal.Sprite.Height/2;      
        if (player.Position.X != player.PrevPos.X)      
            bal.Velocity.X -= (player.PrevPos.X - player.Position.X) / 2;      
        bal.Velocity.Y *= -1;      
    }      
    foreach (Brick b in brickArray.list) {      
        if (bal.Rect.Intersects(b.Rect)) {      
            b.RecieveHit();      
            bal.Velocity.Y *= -1;      
        }      
    }      
    brickArray.RemoveDead();      
}      

将函数拆分为两个是一个好主意 - 一个检查balplayer,另一个检查砖块。