Showing posts with label Collision Detection. Show all posts
Showing posts with label Collision Detection. Show all posts

Saturday, May 24, 2014

The ABCs of Android game development: Detect collisions

In the fourth installment of his five-part app developer series, William J. Francis provides the code to be able to detect when the asteroid and the ship collide in this Android game.

The goal of the Android game we are developing in this series is to adjust the flying saucer’s momentum in order to avoid getting smashed to bits by the asteroid. While simplistic, the project incorporates all the essential aspects of a video game: a canvas, sprites, animation, collision detection, and user input.

In our last tutorial we managed to get our sprites moving around the screen. (If you're new to this series, catch up on parts one and two, respectively: Prepare the canvas and Load and display sprites.) Now we need to be able to detect when the asteroid and the ship collide. There are basically two techniques for collision detection: bounding algorithms and image masking. Bounding is easier and takes up fewer CPU cycles than image masking; however, bounding is not normally accurate enough for arcade type games. Just what is the difference? Well, consider our two images. I have them both on a 50x50 pixel background, but the contents of those sprites don't fill the entire 50x50 pixel grid. Take a look at the illustrations in Figure A and Figure B.

Figure A

Friday, May 23, 2014

Intro to Collision Detection: Collision Detection Basics

In this lesson, we will be discussing a few methods of collision detection and how one would go about implementing them.

What exactly does collision detection describe?
To put simply, collision detection is a way of checking for object interaction.

In most real-time games (both old and new), collision detection is an integral element that holds everything together. As such, a poor collision detection system can make the gaming experience painfully frustrating and, in worst scenarios, may even "break" your game.
No one likes broken games.

You should make every effort to implement as fluid and accurate a collision detection system as needed by your game.

Thursday, May 22, 2014

Touch Events and Sprite Collision Detection. Android Game Programming

In this tutorial we are going to handle the touch event in the screen and check if the (x,y) coordinates collision with any of our characters sprites.

The first thing to do is add this method in the view to handle each touch in the screen. For each sprite in the sprites list we are going to ask if the (x,y) touch coordinates have a collision. If the collision exists, we remove the sprite from the sprites list. We iterate backwards the sprite list to avoid errors in the next iteration when we remove a sprite.