Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My friends I made some parts of this game but I have two problems in this game.

1)Problem about distinguishing ball collision with obstacles

1-1)A first 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?

2)How I can do a work that if the ball collision with any obstacle, the obstacle report the collision.

My aim is about using of events. Are you thinking that I make obstacles as runtime? If I make as a runtime, how I can make collision event for them?

With best regards

C#
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;

           }



       }
Posted
Comments
BillWoodruff 29-Sep-14 3:10am    
I think to make your game responsive you are going to need to use regions for the "ball" and do hit-detection using a fast technique. See:

http://www.codeproject.com/Articles/2134/Collision-A-C-Game-part-pixel-perfect-collision

1 solution

1) There are many different ways of detecting and responding to collisions and which one to use depends on your current scenario.
For a breakout game I would try to model the paddle and blocks as polygons (made up from lines) and the ball as a circle.
Then using vector math you can figure out where two lines (the side of the polygon and the trajectory of the ball) intersect[^].

2. I don't think you want the individual obstacle to report the collision.
I understand you want to use events, but for a game such as breakout you're probably better off just iterating over the list of collidable items and pass them to a collision checker.

I wrote an article on game programming[^] where I implemented a breakout game, it's in java but the collision calculations might still be useful to you.

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900