简体   繁体   English

试图为突破游戏定义桨的位置,以创建更好的球碰撞物理

[英]trying to define location on paddle for breakout game to create better ball collision physics

So I'm trying to figure out how to split the rectangle of my paddle up so i can define different points on the paddle at which the ball can be hit. 所以我试图弄清楚如何将桨的矩形分开,以便可以在桨上定义可以击球的不同点。 I want to make it so that if the ball is hit on either the left or right side of the paddle, then the ball direction will change slightly rather than following the exact same path every time. 我要这样做,以便如果在桨的左侧或右侧击中球,则球的方向将略有变化,而不是每次都遵循完全相同的路径。

Here is my paddle class (I started writing the code I would need in the isboxcolliding method but got stuck and confused): 这是我的桨类(我开始编写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;
        }
    }
}

} }

any help would be greatly apreciated! 任何帮助将不胜感激!

You do not necessarily need to split the collision rectangle up. 您不必拆分碰撞矩形。 You can check if the collision is on the left or right side by having the following information. 您可以通过获取以下信息来检查碰撞是在左侧还是在右侧。

  • The collision point or the centre position of the ball when the collision occurs, lets call it Vector2 collisionPoint 发生碰撞时的碰撞点或球的中心位置,我们将其Vector2 collisionPoint
  • Centre position of the paddle, lets call it Vector2 paddleCentrePosition 桨的中心位置,将其Vector2 paddleCentrePosition

This only works if the paddle does not rotate. 这仅在桨叶不旋转时才有效。

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*/
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM