Configuring Collisions - "Using your words"

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:

  1. Create a list of object types for your game and assign each one a number (objNum):
  • 1 - player_ship

  • 2 - asteroid

  • 3 - alien_ship

  1. 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

  1. 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

  1. 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

  1. 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]

  2. 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:

  1. 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
  1. Use just the calculator in your project.

[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]

My personal word for you and your work (SSK) is: Thanks for doing this for all of us - the Corona`s Developers.

Being it (this topic) stick* or not does not really matter as just your kindness on doing it is something awesome surely IMO.
Thanks and my best wishes for you keeping it all up as well.
Cheers,
Rodrigo.
@RSCdev [import]uid: 89165 topic_id: 32723 reply_id: 130106[/import]

My personal word for you and your work (SSK) is: Thanks for doing this for all of us - the Corona`s Developers.

Being it (this topic) stick* or not does not really matter as just your kindness on doing it is something awesome surely IMO.
Thanks and my best wishes for you keeping it all up as well.
Cheers,
Rodrigo.
@RSCdev [import]uid: 89165 topic_id: 32723 reply_id: 130106[/import]

@RSCdev,

Thanks! Also, I’ll be putting out weekly video updates for folks who want to know how SSKCorona is changing and maturing. Be sure to watch the video and subscribe to the RoamingGamer YouTube channel (actually I think you did.)

First weekly SSKCorona Update video

Cheers,

Ed [import]uid: 110228 topic_id: 32723 reply_id: 130518[/import]

@RSCdev,

Thanks! Also, I’ll be putting out weekly video updates for folks who want to know how SSKCorona is changing and maturing. Be sure to watch the video and subscribe to the RoamingGamer YouTube channel (actually I think you did.)

First weekly SSKCorona Update video

Cheers,

Ed [import]uid: 110228 topic_id: 32723 reply_id: 130518[/import]

Thanks for working on this Ed! I suppose my collision chart has helped many people in the past 1.5-2 years, but as you say, it takes time and there is some potential for math errors. I hope you can optimize your solution and supply it to all Corona devs.

Best of luck!
Brent Sorrentino [import]uid: 9747 topic_id: 32723 reply_id: 130523[/import]

Thanks for working on this Ed! I suppose my collision chart has helped many people in the past 1.5-2 years, but as you say, it takes time and there is some potential for math errors. I hope you can optimize your solution and supply it to all Corona devs.

Best of luck!
Brent Sorrentino [import]uid: 9747 topic_id: 32723 reply_id: 130523[/import]

@Brent,

Your collision chart has definitely helped people. I used your example because it is the best and clearest manual approach to setting up collision filters. That said, I have always been a lazy programmer. i.e. If I can automate a thing at low cost I will.

As far as getting this into peoples hands, I am trying. I am working on docs and examples which should go some ways to helping people make the leap.

Thanks for your comments and your contributions to the community,

Ed

[import]uid: 110228 topic_id: 32723 reply_id: 130558[/import]

@Brent,

Your collision chart has definitely helped people. I used your example because it is the best and clearest manual approach to setting up collision filters. That said, I have always been a lazy programmer. i.e. If I can automate a thing at low cost I will.

As far as getting this into peoples hands, I am trying. I am working on docs and examples which should go some ways to helping people make the leap.

Thanks for your comments and your contributions to the community,

Ed

[import]uid: 110228 topic_id: 32723 reply_id: 130558[/import]

I can’t really add anything else to what Brent and Rodrigo have already said, so I’ll simply stick with “+1” :wink: [import]uid: 52491 topic_id: 32723 reply_id: 130761[/import]

I can’t really add anything else to what Brent and Rodrigo have already said, so I’ll simply stick with “+1” :wink: [import]uid: 52491 topic_id: 32723 reply_id: 130761[/import]