Game Development Reference
In-Depth Information
Handling Collisions
Given a Box2D world and some moving bodies, sooner or later two of them will
collide. Physics games rely on collisions for most gameplay features. When a bird
smashes pig's castle in Angry Birds, it is thanks to collisions; when the idol falls
down the totem and breaks on the ground, that's due to collisions.
Box2D already does all the required tasks to solve collisions and run the simulation
without any coding on our part. By the way, in some situations, we need to interact
with collisions for a gameplay purpose.
Think about Angry Birds, hitting hard on a wooden wall will break it, but normal
Box2D collision routines do not manage wooden wall breaking. Moreover, if the
Totem Destroyer idol hits the ground, the level fails but again Box2D just manages
the collisions without caring about the gameplay rules.
That's why sometimes we need to analyze collisions, and luckily Box2D allows us
to do it thanks to contacts : objects created by Box2D to manage collisions between
two fixtures.
In this chapter you will learn how to handle collisions using contacts, and among
other things:
• Creating a custom contact listener
• Determining which bodies are colliding
• Determining how hard was a collision
• Looping through all collisions involving a body
By the end of the chapter, you will be able to manage any kind of collision, as well as to
complete the Angry Birds and Totem destroyer levels by managing in-game collisions.
Search WWH ::




Custom Search