Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Hour 18. Building a Simple Game > Detecting Collisions

Detecting Collisions

If you try different combinations of magnitude and direction, you should notice that once the ball leaves the screen, it is gone for good. To prevent this from happening, you need to add collision detection to the game logic. Numerous advanced collision detection algorithms are available, but for this game, you use a simple method for determining collisions, which initially detects a collision with the walls or sides of the screen.


Try It Yourself: Detecting Border Collisions

To detect a collision between the ball and the sides of the screen, you need to compare the current location of the ball and the coordinates of the screen sides. To keep the gameplay more realistic, you want to detect a collision with the outer diameter of the ball. By completing the following steps, you will implement the code necessary to detect and respond to a collision with the screen edges. If you closed Visual Studio, repeat the previous exercise first. Also, be sure to keep Visual Studio open at the end of this exercise because you will use this application later.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial