Quick QuickBox2D Tip II – Collision Detection

Once again, I found myself playing around with the excellent QuickBox2D library the other day and whipped up something I needed but thought might help others as well. Actually this tip ain’t all that quick, but it’s not a massive undertaking and could come in quite handy. In any case, we’ll take a look at a way of detecting collisions with QuickBox2D. Now, of course as far as animations go, QB2D (or Box2D, actually) handles all the collisions for you behind the scenes so you don’t really need to think about it. But what if you wanted to create a pool game where the balls click when knocking against each other, or you wanted to remove a game object when it collides with something bad, or – well there are any number reasons you might want to check for collisions when working with a 2D physics engine.

I’m going to assume that if you’re using QuickBox2D, you already have Box2D in your classpath. Now, in Box2D the item responsible for handling custom collision detection is the Box2D.Dynamics.b2ContactListener class which looks like this:

ActionScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

/*

* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com

*

* This software is provided 'as-is', without any express or implied

* warranty. In no event will the authors be held liable for any damages

* arising from the use of this software.

* Permission is granted to anyone to use this software for any purpose,

* including commercial applications, and to alter it and redistribute it

* freely, subject to the following restrictions:

* 1. The origin of this software must not be misrepresented; you must not

* claim that you wrote the original software. If you use this software

* in a product, an acknowledgment in the product documentation would be

* appreciated but is not required.

* 2. Altered source versions must be plainly marked as such, and must not be

* misrepresented as being the original software.

* 3. This notice may not be removed or altered from any source distribution.

*/

packageBox2D.Dynamics{

importBox2D.Collision.*;

importBox2D.Collision.Shapes.*;

importBox2D.Dynamics.Contacts.*;

importBox2D.Dynamics.*;

importBox2D.Common.Math.*;

importBox2D.Common.*;

/// Implement this class to get collision results. You can use these results for

/// things like sounds and game logic. You can also get contact results by

/// traversing the contact lists after the time step. However, you might miss

/// some contacts because continuous physics leads to sub-stepping.

/// Additionally you may receive multiple callbacks for the same contact in a

/// single time step.

/// You should strive to make your callbacks efficient because there may be

We’ll be extending that class, so you won’t really need to worry anything about it (you don’t even need to worry about those ‘virtual’ method attributes – we’ll just extend right over top of that and Flash won’t even care one way or the other).

Before we extend the contact listener, though, let’s first create a collision event for QuickBox2D. To do so, create a new package/directory inside the com.actionsnippet package named ‘collisions’. Within there, create another directory named ‘events’. Finally, inside that events directory, we’ll create the event class named QBCollisionEvent shown below.

Notice that this event sports four types (ADD, REMOVE, PERSIST, and RESULT), to mirror the different methods of the b2ContactListener class (read the comments in that class to get an idea of what each is and does). Also, check out that there are two body properties in this event (body1 and body2) which are instances of the b2Body object from Box2D. Obviously, in any given collision, there are at least two objects involved. These two properties just give quick and easy access to those two objects. You can use these to determine which items collided and, if necessary, get details like velocity, mass, position, etc. There’ll be an example down below to see what I mean.

Now that we have our event done, move up one directory to com.actionsnippet.collisions and we’ll create the QBContactListener class which will extend the b2ContactListener and implement IEventDispatcher (in order to dispatch our QBCollisionEvent).

Not much to say about this class except that, like the b2ContactListener, an instance of this class must be assigned to the m_contactListener property of the main b2World instance. In QuickBox2D terms, the main b2World instance can be reached through the ‘w’ property of the main sim (i.e. QuickBox2D) instance. But let’s put it all together and make a quick example, as it’s starting to sound a lot more complicated than it really is:

5 Comments:

Thank you! Thank you! Thank you! You saved me from hours of coding. Is it ok if I use the 2 classes in my final school exam?

Narcis ·May 12, 2010

Hey Narcis,
Glad that helped out. I need to start posting quick licensing conditions in my class file headers, but, yes, anything I post on this blog is free for the taking (as-is and with no guarantee, warranties, or money back).

you are very good.. I am an as3 noob. I just started programming in as3 and quickbox2d was very easy for me to learn. I got struck in the mid of my project. Your code for custom collision in quickbox has helped me a lot. Thank you very much indeed. Hope to get more tutorial and help from you..