Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So before we go and actually implement collision detection into our game, I wanted to go over a couple of the options which you have for collision detection. So the simplest is called bounding box collision detection and you can see in this example here, I have my alien and I have my hero ship, and essentially, there are rectangles around them that denote the bounds. So anytime I overlap the two, we can see they turn red here. This is the simplest form of collision detection that's actually useful for a lot of things, because remember in our game, the aliens are moving downwards.
So if we want to test between the aliens and the ship, this is actually pretty good, and for the most case since things are moving so fast, this would actually work fairly well. Now obviously, if you had a big graphic that had all kinds of intricate shapes, and you wanted to really test in a more accurate way, then things would break down. But this is the simplest and the fastest way to do collision detection and you can see the code at the bottom, essentially every display object in Starling has a Bounds property which contains this rectangle, and you can use the intersects method of the rectangle class to check if it's overlapping the bounds of another Starling display object.
So again, this is the simplest form of collision detection. The next one is distance-based collision detection. So in distance or sphere-based collision detection, you can think of there being a circle around all of your objects, and essentially, you are testing the difference between the center of both circles. So if the distance between the center of both of these circles is within a certain amount, then it will determine that a collision has happened. This is a more accurate way to do things, and all of this is adjustable.
You can adjust the numbers to create any type of effects that you want. This is usually considered the best approach and it actually works for the vast majority of cases in which you need collision detection, and this is the method that we're going to be using in our game. But you can see at the bottom, essentially you find out the distance between the center of both of these circles and if it's less than the two radiuses added together, then a collision has occurred. And again, you can manipulate those numbers if you want.
Now this brings up a point about pixel perfect collision detection and that's something you want to avoid at all cost, particularly when you are running games on the GPU. Pixel perfect collision detection first of all is not needed in 99% of cases, and if it is needed, then you might want to consider not running a game on the GPU because it's such a CPU intensive process. Once your textures are uploaded to the GPU, you don't have any way to read the pixel data in order to do good collision detection.
All of that has to happen on the CPU. So definitely, avoid pixel perfect collision detection, particularly when you are running games in Starling, and this distance-based approach will work in the vast majority of cases.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92201 Viewers
82 Video lessons · 104060 Viewers
71 Video lessons · 75397 Viewers
56 Video lessons · 106768 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.