Collision Detection with Advanced Shapes


In this chapter we have developed frame-independent collision-detection logic and scripts for circle-line collisions (or point-line collisions, if you set the radius to 0). This is much more powerful than you might realize. With this knowledge, you can create simple or complicated shapes without any extra-fancy math. For instance, think of an octagon. Ordinarily you might not know how to detect a collision between a circle and an octagon. Well, why not put eight lines together and run a detection script for each of those lines? Suddenly, circle-octagon collision detection is a very easy thing! The shape in question doesn't have to be regular, either; you can create a star shape, a triangle, or even the shape of a house; the detection works because each line is treated separately.

Many shapes can be created with line segments. Collision detection is performed on each line segment separately.

graphics/05fig24.gif

There is one problem you may encounter: multiple collisions at once. Imagine a rectangle shape and a ball colliding with the corner. It is likely that your script will detect two separate collisions. This is fine if you just want to know if a collision took place, but if you want to use a collision reaction (covered in the next chapter), then which line should the circle react to? The answer is simple: Keep track of the frames variable for each collision. The lowest one is the collision that occurred first. You can then make the circle react to the appropriate collision.

If you are interested in developing your own collision-detection scripts for shapes not covered here (for instance, point-ellipse collisions), follow this simple formula:

  1. List the conditions that must be met for a collision to take place.

  2. Figure out how to determine if each condition is met.

You may have to pick up a book on geometry or trigonometry if you are looking for the equations that define more complicated objects such as ellipses or toroids (doughnut shapes). See Appendix E for book suggestions.

Collision detection is a fundamental requirement for most games, however simple they may be. With what you've learned in this chapter, you can detect collisions between almost any two objects. Combine all this knowledge with what you'll gain in the next chapter, and you will soon be creating advanced games like pinball and billiards!



Macromedia Flash MX Game Design Demystified(c) The Official Guide to Creating Games with Flash
Macromedia Flash MX Game Design Demystified: The Official Guide to Creating Games with Flash -- First 1st Printing -- CD Included
ISBN: B003HP4RW2
EAN: N/A
Year: 2005
Pages: 163
Authors: Jobe Makar

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net