This post will discuss an alternative to hand calculating collision filters for Box2D physics in Corona. The goal of the alternative is to take a somewhat painful and time consuming manual task and to automate it.
Setting up collisions: The Old Way
If you have ever set up collision detection using the Corona SDK, then you know that it goes something like this:
- Create a list of object types for your game and assign each one a number (objNum):
-
1 - player_ship
-
2 - asteroid
-
3 - alien_ship
- Denote/Mark which object collides with which other object type:
-
1 - player_ship - Collides with: 2, 3
-
2 - asteroid - Collides with: 1, 2, 3
-
3 - alien_ship - Collides with: 1, 2, 3
- Calculate the categoryBits for each numbered object type (2 ^ objNum-1)
-
1 - player_ship - categoryBits: 2^0 == 1
-
2 - asteroid - categoryBits: 2^1 == 2
-
3 - alien_ship - categoryBits: 2^2 == 4
- Calculate collision maskBits for each object type based by adding the categoryBits of each object type it collides with:
-
1 - player_ship - maskBits: 2 + 4 = 6
-
2 - asteroid - maskBits: 1 + 2 + 4 = 7
-
3 - alien_ship - maskBits: 1 + 2 + 4 = 7
-
Create the collision filters with the above information:[lua]local playerShipCollisionFilter = { categoryBits = 1, maskBits = 6}
local asteroidCollisionFilter = { categoryBits = 2, maskBits = 7}
local enemyShipCollisionFilter = { categoryBits = 3, maskBits = 7}[/lua] -
Use the collision filters when making physics bodies:[lua]-- Create a new asteroid
local asteroid = display.newImageRect( “asteroid.png”, 32, 32 )
physics.addBody( asteroid, { filter=asteroidCollisionFilter } )[/lua]
Please note, if this example seems familiar, that is because it uses some of the same values as Brent Sorrentino used in his wonderful forum post on calculating collisions: http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart
I think you will probably agree with me when I say, “Yuck!” I am personally way to lazy (and mistake prone) to write that kind of code every time I want to set up collisions. Even with Brent’s wonderful graph it is, as I said above, painful and time-consuming.
So, how do we do this better?
Setting up collisions: The New Way
I am currently working on a a free starter kit for Corona SDK, and although it is still in its infancy, I have high hopes for it. I am calling this free starter kit SSKCorona. i.e. “[the] Super Starter Kit [for] Corona [SDK]”.
However super SSKCorona may or may not be at this point, it does have a few gizmos in it that I am proud of. One of these gizmos is the collision calculator class.
Using the collisionCalculator, you can turn the above code into this code:[lua]-- Create blank calculator and configure it
local myCC = ssk.ccmgr:newCalculator()
myCC:addName( “player_ship” )
myCC:addName( “asteroid” )
myCC:addName( “alien_ship” )
– player_ship collides with: asteroid and alien_ship
myCC:collidesWith( “player_ship”, “asteroid”, “alien_ship” )
– asteroid collides with: asteroid, player_ship, and alien_ship
myCC:collidesWith( “asteroid”, “asteroid”, “player_ship”, “alien_ship” )
– alien_ship collides with: asteroid, player_ship, and alien_ship
myCC:collidesWith( “alien_ship”, “asteroid”, “player_ship”, “alien_ship” )
– Get the masks when you need them to create them to create physics bodies
– Create a new asteroid
local asteroid = display.newImageRect( “asteroid.png”, 32, 32 )
physics.addBody( asteroid, { filter = myCC:getCollisionFilter( “asteroid” ) } )[/lua]
Later when you’re done with the calculator, or want to create a new collision configuration, simply throw away the old calculator like this:
[lua]myCC = nil[/lua]
The calculator has a number of advantages over the manual method, but the most obvious are:
-
Easy to configure - Personally I prefer using names for object types instead of numbers.
-
No math errors - The calculator does the math for you and double checks itself so there are no errors. (If you’ve done this a few times, you know how frustrating miscalculating a mask can be.)
-
Fast - The only thing that should take any appreciable time when using the calculator is deciding what to name the collision types.
How do you get it and use it in your project?
There are two ways to start using the calculator in your projects:
- Use all of SSKCorona in your project.
-
Go to the SSKCorona gitHub and download SSKCorona
-
Grab the ‘ssk’ folder and all of it contents and copy the folder to your project folder.
-
Add these two lines to main.cs
[lua]local globals = require( “ssk.globals” ) – Load just the calculator
require(“ssk.loadSSK”)[/lua]
- Start using it as shown above
- Use just the calculator in your project.
-
Go to the SSKCorona gitHub and download just the calculator
(https://github.com/roaminggamer/SSKCorona/blob/master/ssk/classes/c_collisionCalculator.lua) -
Put the file c_collisionCalculator.lua in your project directory
-
Add this line to main.cs
[lua]ccMgr = require( “c_collisionCalculator” ) – Load Standard Globals[/lua]
- Start using it as shown above except replace ‘ssk.ccMgr’ with just ‘ccMgr’
Is there more?
Yes. There is more, but as I said above, SSKCorona is still somewhat under construction. That said, I am very much interested in feedback and requests. Please visit the resource page here. Also, if you want to get updates, you can sign up for my RSS feed here (upper-right part of page) and read more about SSKCorona here .
Lastly, I really mean it when I say I am looking for feedback and requests. Soon, I will be using SSKCorona to write some tutorials. If you have any topics you would like to see covered please write me directly here:

Cheers!
[import]uid: 110228 topic_id: 32723 reply_id: 332723[/import]
[import]uid: 52491 topic_id: 32723 reply_id: 130761[/import]