Treat entire layer as single physics body

Is it possible to create a physics body from an entire layer in a Tiled map, instead of having bodies for all the individual tiles? Want to know because for collision detection, as my character walks along a platform, I don’t want new collisions every time the character reaches a new tile.

This is not readily possible. Even if it was, creating a complicated physics shape requires a multi-body physics object, and multibody physics objects return new collision events for collisions with each element of the multibody. The effect would be the same, but even less predictable. 

Have you tried checking event.object2 or event.other and running your code only when relevant? In this way you could choose to ignore collisions with tiles and only respond to other objects at your discretion. 

Thanks for the quick reply. Yes I have done that before but I was looking to implement the a small “foot” physics body and check whether or not that foot was beginning or ending a collision with the ground to toggle whether or not my character can jump (as described here: https://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/)  

It’s all good though, I’ve been using velocity checks instead which seems to work fine.

Ah, I see what you’re trying to do. Something other users and I have done before is to call physics.queryRegion() on a space beneath and narrower than the player object, much in the same way as using a foot body. The function returns a list of display objects with physics bodies intersecting the box you define in its arguments. It’s a simple matter of calling the function when the jump button is pressed, checking for floor objects, and either performing the jump or not. http://docs.coronalabs.com/api/library/physics/queryRegion.html

This has the advantage of avoiding the possible undesirable side effects of using velocity checks, and not requiring any finagling with collision detection at all.

Oh that makes sense. But given that the floor is a tile, how would I check that it is a floor tile given only the display object? It seems like I can only set properties of the Tile Object in Tiled.

The properties you set for a tile in Tiled are stored in the tile’s display object when it is spawned by the engine. 

local hits = physics.queryRegion( box\_upper\_left.x, box\_upper\_left.y, box\_lower\_right.x, box\_lower\_right.y ) if ( hits ) then -- There's at least one hit. print( "Hit count: " .. tostring( #hits ) ) -- Output all the results. for key,object in ipairs(hits) do print( "Object position: ", object.x, object.y ) end else -- There's no hit. end

Given the above example from Corona, the properties created in Tiled would be stored in object.properties. If you gave a tile an isFloor property in Tiled, you could retrieve it at object.properties[“isFloor”]. You’ll want to check that object.properties exists first, otherwise you’ll get an error if any of your tiles have no properties set in Tiled.

Things are simpler if you only need to check for any kind of tile at all beneath the player. You can check whether object.tile or object.index exist. If so, it is a tile.

Thank you very much! I did not know about object.properties.

This is not readily possible. Even if it was, creating a complicated physics shape requires a multi-body physics object, and multibody physics objects return new collision events for collisions with each element of the multibody. The effect would be the same, but even less predictable. 

Have you tried checking event.object2 or event.other and running your code only when relevant? In this way you could choose to ignore collisions with tiles and only respond to other objects at your discretion. 

Thanks for the quick reply. Yes I have done that before but I was looking to implement the a small “foot” physics body and check whether or not that foot was beginning or ending a collision with the ground to toggle whether or not my character can jump (as described here: https://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/)  

It’s all good though, I’ve been using velocity checks instead which seems to work fine.

Ah, I see what you’re trying to do. Something other users and I have done before is to call physics.queryRegion() on a space beneath and narrower than the player object, much in the same way as using a foot body. The function returns a list of display objects with physics bodies intersecting the box you define in its arguments. It’s a simple matter of calling the function when the jump button is pressed, checking for floor objects, and either performing the jump or not. http://docs.coronalabs.com/api/library/physics/queryRegion.html

This has the advantage of avoiding the possible undesirable side effects of using velocity checks, and not requiring any finagling with collision detection at all.

Oh that makes sense. But given that the floor is a tile, how would I check that it is a floor tile given only the display object? It seems like I can only set properties of the Tile Object in Tiled.

The properties you set for a tile in Tiled are stored in the tile’s display object when it is spawned by the engine. 

local hits = physics.queryRegion( box\_upper\_left.x, box\_upper\_left.y, box\_lower\_right.x, box\_lower\_right.y ) if ( hits ) then -- There's at least one hit. print( "Hit count: " .. tostring( #hits ) ) -- Output all the results. for key,object in ipairs(hits) do print( "Object position: ", object.x, object.y ) end else -- There's no hit. end

Given the above example from Corona, the properties created in Tiled would be stored in object.properties. If you gave a tile an isFloor property in Tiled, you could retrieve it at object.properties[“isFloor”]. You’ll want to check that object.properties exists first, otherwise you’ll get an error if any of your tiles have no properties set in Tiled.

Things are simpler if you only need to check for any kind of tile at all beneath the player. You can check whether object.tile or object.index exist. If so, it is a tile.

Thank you very much! I did not know about object.properties.