How I've reduce the number of physics objects required for "solid" tiles in my project

Hi everyone,

The map in my game is top down 2D, and I’m using the physics engine for collision detection between sprites and “solid” tiles e.g. buildings etc. Until now, I’ve been adding a physics object for every tile with the “physics=true” property set in Tiled i.e.:

local ifPhysicsProperty = function (event)         mte.physics.addBody(event.target, "static", {friction = 0.5, bounce = 0.0, density = 1}) end

mte.addPropertyListener("physics", ifPhysicsProperty)

However, I noticed there were a lot of unnecessary physics objects being created, as adjacent tiles would ideally be part of a single physics object rather than separate objects.

So I’ve made a function which will rope together adjacent solid tiles and create a single physics object. I’d be happy to post the code if anyone’s interested.

For example this:

[sharedmedia=core:attachments:2380]

Can be simplified to this:

[sharedmedia=core:attachments:2381]

11 physics objects reduced to 4. In some of my other maps this approach has cut down on the number of physics object by about 10x.

I just wanted to share this in case anyone else has been curious about doing the same thing!

  • Anthony

I would be interested in this please, since I have an insane amount of physics objects everywhere. I’m not sure if this saves on performance or anything wouldn’t it still be the same physics on screen just less smaller boxes? This would work very nice if could specify a certain layer, since I got “walls” stacked up on each other with being the higher layers wasted physics drawn

I’d be interested in seeing this myself! It looks like a good way to improve performance and reduce overhead, particularly in maps with many physics objects.

Unfortunately I don’t have an answer on whether or not it improves performance. Maybe Dyson can shed some light on it?

I’ve put the code on Bitbucket: https://bitbucket.org/anthonygore/luagridtools/

Let me know how you go with it!

I would be interested in this please, since I have an insane amount of physics objects everywhere. I’m not sure if this saves on performance or anything wouldn’t it still be the same physics on screen just less smaller boxes? This would work very nice if could specify a certain layer, since I got “walls” stacked up on each other with being the higher layers wasted physics drawn

I’d be interested in seeing this myself! It looks like a good way to improve performance and reduce overhead, particularly in maps with many physics objects.

Unfortunately I don’t have an answer on whether or not it improves performance. Maybe Dyson can shed some light on it?

I’ve put the code on Bitbucket: https://bitbucket.org/anthonygore/luagridtools/

Let me know how you go with it!