The basic set of features that the Box2D library offers might be sufficient for simple games. However, sometimes you need to know when objects collide. For instance, in action games you usually shoot things and need to know what object was hit by your projectile.
Fortunately, the Box2D library offers an interface for collision detection in the form of callbacks. Using callbacks in the Lua language might not be straightforward because of the Lua language design. LuaBox2D contains a simple interface to set up callback functions for collision detection.
This recipe shows you how to define your own callback function in the Lua environment and how to process events when collision occurs.
First of all, you'll need a World object, some static walls, and at least one dynamic object. You can use the createWall
and createBullet
functions from the Setting up bullets recipe.