Performance : Local Collision vs Runtime Collision ?

Hey there !

I’m still on my way on making performances better for my game, and I’ve come across this problem : I’m intensely using physics, which causes smartphones to get hot really fast.

I’m using a general “Runtime Collision Listener” (Runtime:addEventListener(“collision”, self)) for several generic stuff (like collision between ground and players, ground and particle), and Local Collision Listener (object:addEventListener(“collision”, onLocalCollision)) for “external items” (like items that aren’t loaded in every level, special bonus, enemies, etc).

The Runtime Collision listener looks pretty heavy, with a lot of “if / then / else / end”.

… So, I was wondering : what would be better ? To have only “local collision listener” or having only “Local Collision Listener”. Or, is mixing both kind of collision listener is just OK ?

BONUS QUESTION : I’m also using collision filters to avoid having useless collision detections (for exemple, parts of a ragdoll that shouldn’t collide on eachother). I’ve read that it would reduce the “bandwith” : is it truely effective ?

Thanks a lot in advance !

I’m not sure the value of using a runtime collisions listener, then again, I don’t use physics much, but I’m not sure what the runtime check does that the local collision would not.

Rob

Hey Rob ! 

So, even if you’re not sure, you’re saying that there might not be any difference between local and Runtime collision listeners ?

Right now, I’ve tried simplifying my Runtime Collision Listener (by commenting a lot of if / then / else) and I don’t see (or can’t see) any performance difference…

It depends on the size of your porject and the number of physic objects, that listen for a collision.

You said it yourself, your global listener function is very if/elseif heavy, which is neither good coding practice nor the most performant way to do it.

If have more than a hand full of objects listening for a collision, you should use one collision function for each type of object (depending on your gam logic).

For example:

local objectBox01 = display.newRect(0, 0, 20, 20) physics.addBody(objectBox01, {...}) local objectBox02 = display.newRect(0, 0, 20, 20) physics.addBody(objectBox02, {...}) local objectBall = display.newCircle(0, 0, 10) physics.addBody(objectBall, {...}) local function boxCollision(event) --do stuff with the boxes end local function ballCollision(event) --do something with the ball end objectBox01:addEventListener("collision", boxCollision) objectBox02:addEventListener("collision", boxCollision) objectBall:addEventListener("collision", ballCollision)

If you are using something like a class model (with metatables) you can use one collision function for each class.

Hi @evanspro,

This question comes up often enough that, awhile back, I added a description to each type of collision handling in the guide… not so much from a low-level performance perspective, but rather a description of which general scenario is best for each type of handling. That, in my opinion, is the biggest factor in deciding which type to use.

Here’s a link to the guide:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Brent

@torbenratzlaff

I’m using class model (metatables), and I’m actually using local collision for each of them.

But as I said, I’m also using a Runtime collision listener (or “Global collision listener”, as written in the docs) : it’s used for “most common collisions” (player collides with ground, particles collide with ground, etc). For now, I’ve cleaned this listener and removed some conditions. But I have no idea if it changes anything to the performance… I’ll keep it that way anyway, since it looks cleaner than before.

@Brent Sorrentino

Yep, it does comes up often… I even asked almost the same question two years ago, when I was learning from scratch :slight_smile: But I just wanted to be sure to do it the best way I could. And I guess I won’t be able to do much since I’m using both global and local collision listeners for the different case scenarios.

Thanks a lot everybody !

EDIT : Just wanted to add that, managing collision filter does seem to have a lot of impact on performances !

I’m not sure the value of using a runtime collisions listener, then again, I don’t use physics much, but I’m not sure what the runtime check does that the local collision would not.

Rob

Hey Rob ! 

So, even if you’re not sure, you’re saying that there might not be any difference between local and Runtime collision listeners ?

Right now, I’ve tried simplifying my Runtime Collision Listener (by commenting a lot of if / then / else) and I don’t see (or can’t see) any performance difference…

It depends on the size of your porject and the number of physic objects, that listen for a collision.

You said it yourself, your global listener function is very if/elseif heavy, which is neither good coding practice nor the most performant way to do it.

If have more than a hand full of objects listening for a collision, you should use one collision function for each type of object (depending on your gam logic).

For example:

local objectBox01 = display.newRect(0, 0, 20, 20) physics.addBody(objectBox01, {...}) local objectBox02 = display.newRect(0, 0, 20, 20) physics.addBody(objectBox02, {...}) local objectBall = display.newCircle(0, 0, 10) physics.addBody(objectBall, {...}) local function boxCollision(event) --do stuff with the boxes end local function ballCollision(event) --do something with the ball end objectBox01:addEventListener("collision", boxCollision) objectBox02:addEventListener("collision", boxCollision) objectBall:addEventListener("collision", ballCollision)

If you are using something like a class model (with metatables) you can use one collision function for each class.

Hi @evanspro,

This question comes up often enough that, awhile back, I added a description to each type of collision handling in the guide… not so much from a low-level performance perspective, but rather a description of which general scenario is best for each type of handling. That, in my opinion, is the biggest factor in deciding which type to use.

Here’s a link to the guide:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Brent

@torbenratzlaff

I’m using class model (metatables), and I’m actually using local collision for each of them.

But as I said, I’m also using a Runtime collision listener (or “Global collision listener”, as written in the docs) : it’s used for “most common collisions” (player collides with ground, particles collide with ground, etc). For now, I’ve cleaned this listener and removed some conditions. But I have no idea if it changes anything to the performance… I’ll keep it that way anyway, since it looks cleaner than before.

@Brent Sorrentino

Yep, it does comes up often… I even asked almost the same question two years ago, when I was learning from scratch :slight_smile: But I just wanted to be sure to do it the best way I could. And I guess I won’t be able to do much since I’m using both global and local collision listeners for the different case scenarios.

Thanks a lot everybody !

EDIT : Just wanted to add that, managing collision filter does seem to have a lot of impact on performances !