Jump to content
  • Advertisement
Sign in to follow this  
similar

Breakout game collision corner obstacles bug?

This topic is 1410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My friends I made some parts of this game but I have two problems in this game.

Problem about distinguishing ball collision with obstacles

problem is related to complex guidelines that I used them for simulation of ball collision with board or ball collision with blocks. These guidelines are not accurate especially when the ball meets with block corner or bottom. If it is not better for collision distinguish I use a better code. I wrote this code. Are you think that are there better way?

thanks

 private Point MouseDownLocation;

int step = 2;
int stepleft = 2;


bool flagBottom;
bool flagTop;

private void Form1_Load(object sender, EventArgs e)
{

    timer1.Interval = 10;
    timer1.Enabled = true;

}

private void timer1_Tick(object sender, EventArgs e)
{


    ball.Top += step;
    ball.Left += stepleft;

    //board simulate collision
    bool collisonX = ball.Location.X + ball.Width > board.Location.X && ball.Location.X < board.Location.X + board.Width;
    bool collisonY = ball.Top + ball.Height == board.Location.Y || ball.Top + ball.Height - 1 == board.Location.Y;

    //board2(button1) simulate collision
    bool collisonX2 = ball.Location.X + ball.Width > board2.Location.X && ball.Location.X < board2.Location.X + board2.Width;
    bool collisonY2 = ball.Top + ball.Height == board2.Location.Y || ball.Top + ball.Height - 1 == board2.Location.Y;

    //Collision the ball with under buttons 
    bool collsionButtonY = ball.Top - ball.Height == board2.Location.Y || ball.Top - ball.Height == board2.Location.Y - 1;

    //collision leftwall 
    bool leftWall = ball.Left == 0 || ball.Left == -1 || ball.Left == 1;
    //collision rightwall 
    bool topWall = ball.Top == 0 || ball.Top == -1 || ball.Top == 1;

    bool bottomWall = collisonX && collisonY;
    bool toppWall = collisonX2 && collisonY2;

    //collision 
    bool barrier = collisonX2 && collsionButtonY;

    //rightwall
    bool rightWall = ball.Left + ball.Width == this.ClientSize.Width || ball.Left + ball.Width == this.ClientSize.Width - 1;
    // sidewall = collision rightwall or leftwall 
    bool sideWall = leftWall || rightWall;

    //Check the ball hit the ground 
    bool check = ball.Top + ball.Height < this.ClientSize.Height;


    //if topWall true,This means that the ball is hit to the topwall

    if (topWall)
    {
        flagBottom = false;
        flagTop = true;
        if (stepleft > 0)
        {
            step = 2;
        }
        else if (stepleft < 0)
        {
            step = 2;
        }



    }
    //if bottomWall true,This means that the ball is hit to the board

    else if (bottomWall)
    {
        flagBottom = true;
        flagTop = false;
        if (stepleft > 0)
        {
            step = step * -1;

        }
        else if (stepleft < 0)
        {

            step = step * -1;
        }


    }
    //if barrier true and flagbottom true,This means that the ball is hit to the board2(button1)

    else if (barrier && flagBottom)
    {

        if (stepleft > 0)
        {

            step = step * -1;

        }
        else if (stepleft < 0)
        {
            step = step * -1;

        }
    }
    //if toppWall true and flagTop true,This means that the ball is hit to The top button is hit 

    else if (toppWall && flagTop)
    {
        if (stepleft > 0)
        {
            step = step * -1;
        }
        else if (stepleft < 0)
        {
            step = step * -1;

        }
    }
    else if (sideWall)
    {
        //if leftwall true,This means that the ball is hit to the left side wall
        if (leftWall)
        {
            if (flagTop)
            {
                stepleft = 2;
            }
            else if (flagBottom)
            {

                stepleft = 2;
            }
        }
        //if rightWall true,This means that the ball is hit to the left side wall
        else if (rightWall)
        {
            if (flagTop)
            {
                stepleft = -2;
            }
            else if (flagBottom)
            {
                stepleft = -2;
            }

        }


    }
        //check if ckeck==ture,this mean the ball is hit the ground
    else if (!check)
    {
        timer1.Enabled = false;
    }





}



 private void board_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        MouseDownLocation = e.Location;
    }

}

private void board_MouseMove(object sender, MouseEventArgs e)
{

    if (e.Button == MouseButtons.Left)
    {
        board.Left = e.X + board.Left - MouseDownLocation.X;

    }



}

Share this post


Link to post
Share on other sites
Advertisement
Well, for one, if you want to use the ball position, use the center of the ball and its radius. All of this adding and subtracting width is confusing, and it gives you a square collision bound rather than a circle.
You might need to do a bit more math here to get accurate collisions.

Can you link to some screenshots of exactly what you're trying to do, and explain the consequences of collision a little better?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!