【问题标题】:trying to define location on paddle for breakout game to create better ball collision physics尝试为突破游戏定义桨上的位置以创建更好的球碰撞物理
【发布时间】:2017-01-13 00:34:16
【问题描述】:

所以我想弄清楚如何将我的桨的矩形分开,这样我就可以在桨上定义可以击球的不同点。我想这样做,如果球在球拍的左侧或右侧被击中,那么球的方向会稍微改变,而不是每次都遵循完全相同的路径。

这是我的 paddle 类(我开始在 isboxcolliding 方法中编写我需要的代码,但被卡住并感到困惑):

class Ball
{
    Texture2D texture;
    Vector2 position;
    Vector2 speed;
    Rectangle bounds;
    Rectangle screenBounds;
    Random rnd = new Random();
    bool collisionWithBrick; // Prevents rapid brick removal.



    public Ball(Texture2D texture, Rectangle screenbounds) // Constructor
    {
        this.texture = texture;
        this.screenBounds = screenbounds;
    }

    public int Width
    {
        get { return texture.Width; }
    }

    public int Height
    {
        get { return texture.Height; }
    }

    public Rectangle Bounds
    {
        get
        {
            bounds = new Rectangle((int)position.X, (int)position.Y,
                Width, Height);

            return bounds;
        }
    }

    public void SetStartBallPosition(Rectangle paddle)
    {
        position.X = paddle.X + (paddle.Width - Width) / 2;
        position.Y = paddle.Y = paddle.Y - Height;
        bounds = Bounds;

        if (rnd.Next(0, 2) < 1)
            speed = new Vector2(-200.0f, 200.0f); // Move ball left.
        else
            speed = new Vector2(200.0f, -200.0f); // Move Ball Right.
    }

    internal void Deflection(MultiBallBrick multiBallBrick)
    {
        if (!collisionWithBrick)
        {
            speed.Y *= -1;
            collisionWithBrick = true;
        }
    }

    internal void Deflection(ReinforcedBrick reinforcedBrick)
    {
        if (!collisionWithBrick)
        {
            speed.Y *= -1;
            collisionWithBrick = true;
        }
    }


    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.Silver);
    }


    // Check if our ball collides with the right paddle.
    public void IsBoxColliding(Rectangle paddle)
    {

        if (bounds.Intersects(paddle))
        {
            int center = paddle.Center.X - bounds.Center.X;


            if (center > )
            {
                speed.Y *= -1;



                speed.X = speed.X * 1.5f;
            }
        }

        /*if (paddle.Intersects(bounds)) // Bounds = our ball
        {
            position.Y = paddle.Y - Height;
            speed.Y *= - 1; // Reverse the direction of the ball.
        }*/
    }

    public void Update(GameTime gameTime)
    {
        collisionWithBrick = false;
        position += speed * (float)gameTime.ElapsedGameTime.TotalSeconds;


        // Check to see if the ball goes of the screen.
        if (position.X + Width > screenBounds.Width) // Right side
        {
            speed.X *= -1;
            position.X = screenBounds.Width - Width;
        }

        if (position.X < 0) // Left side
        {
            speed.X *= -1;
            position.X = 0;
        }

        if (position.Y < 0) // Top
        {
            speed.Y *= -1;
            position.Y = 0;
        }
    }

    public bool OffBottom()
    {
        if (position.Y > screenBounds.Height)
            return true;

        return false;
    }

    public void Deflection(Brick brick)
    {
        if (!collisionWithBrick)
        {
            speed.Y *= -1;
            collisionWithBrick = true;
        }
    }
}

}

任何帮助将不胜感激!

【问题讨论】:

    标签: c# xna


    【解决方案1】:

    您不一定需要拆分碰撞矩形。您可以通过以下信息检查碰撞是在左侧还是右侧。

    • 发生碰撞时球的碰撞点或中心位置,我们称之为Vector2 collisionPoint
    • 桨的中心位置,我们称之为Vector2 paddleCentrePosition

    这仅在桨叶不旋转时有效。

    Vector2 difference = collisionPoint - paddleCentrePosition;
    if(difference.x > 0) {
        /*Right side of the paddle*/
    }
    else if(difference.x < 0) {
        /*Left side of the paddle*/
    }
    else{
        /*The exact same position*/
    }
    

    【讨论】:

    • 谢谢,这真的很有帮助。这就是我的想法,但无法理解它。不过有一件事,我的第二层有一个多球积木,由于某种原因,第二个产生的球实际上不再与桨相互作用(实际上也没有与积木相互作用)。知道为什么会发生这种情况吗?我似乎不明白为什么,因为第二个球被设置为像第一个球一样碰撞(它之前碰撞过)。
    • @ESuth 不知道。检查是否正在调用 isboxcolliding。进行控制台打印。
    • 它肯定被调用了。当多砖被击中时,我基本上只是在生成另一个版本的球类。我不需要为多球创建一个全新的课程吗?
    • @ESuth Multibrick 是一块可以生成球的砖块?你是什​​么意思另一个版本的球类?继承了ball类的类?如果您要覆盖该函数,那么您必须记住调用基函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多