Book Image

Lua Game Development Cookbook

By : Mario Kasuba, Mário Kašuba
Book Image

Lua Game Development Cookbook

By: Mario Kasuba, Mário Kašuba

Overview of this book

Table of Contents (16 chapters)
Lua Game Development Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Detecting object collision


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.

Getting ready

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.

How to do it…

Object collision can be detected with the ContactListener object. It provides...