Single tile Physics - Collision

Is it possible to make one single tile a physics object and detect collision with it?

In corona examples, we have something like this:

local function collisionDetect( self, event ) if ( event.phase == "began" ) then print( self.myName .. ": collision began with " .. event.other.myName ) elseif ( event.phase == "ended" ) then print( self.myName .. ": collision ended with " )--.. event.other.myName ) end end box.collision = collisionDetect; box:addEventListener( "collision" );

how can i do that with one tile? 

Yes easy, and keeps coding very minimal.

when you use Tiled to create your maps and load them in through MTE you can set the tile you use to have properties.

So on your spritesheet in tiled set the “wall” tile to:

key :       value:
physics    true

Will make it have physics set to static as default.

Now to detect collision, you can set a new variable to your object like object.type = “wall” or object.name = “wall”

and inside your normal collisionDetect just look for a .type == “wall” and it will detect that wall.

Helpful tool would also set a new local variable in the function:

local agro = event.object1 local hit = event.object2

And proceed inside your began phase with a properly nested detection…remember they come in at any order and need to check both instances!

 

if (agro.type == “player” or hit.type == "player) then
            if (agro.type == “wall” or hit.type == “wall”) then

                     --FOUND WALL DETECTION

Good luck!

Hey @Azmar, by set a new variable to the tile like: object.name = “wall”, you meant in the spritesheet in tiled? Like physics = true? Or in the code? If is in the code, where do i set that?

And when you say “inside your normal collisionDetect”… how can i add an event listener to my tile? 

In the example i gave: 

box.collision = collisionDetect; box:addEventListener( "collision" );

I saw mte.addPropertyListener(), but that only run one time when the map is loaded. I want to one of the tiles to be: “isSensor = true” in the collision. Is this possible to set? How?

Thank you for the help!

I think I set you down the wrong track, you could create objects on your map and easily call them in to give them all the properties you want or set all the properties in tiled to them by looking at the MTE documentation. And that all works fine, but with my experience on MTE it is very amazing but has a few minor bugs, and you will come across this bug and there are some threads in the history on it. It happens when creating objects and using them for all these properties while loading/changing maps through MTE etc…hard to describe. Just saving you a huge headache that doesn’t make sense just do what I’m gonna suggest next.

If you want a wall that is just a physics object sure do it in tiled but if it has any specific properties or special collision rules just create it in corona and do the normal add sprite functions and after do what you want.

//mte.add sprite (monster…)

//physics.addBody(…)

//monster.isSensor = true;

monster.type = “wall”; —is how you set collision

It may be tedious and a lot of code where you can do it in Tiled, but believe me the bug will come for you :slight_smile:

I like to do just:

Runtime:addEventListener("collision", onCollision)

To handle most my collisions, as there are so many to handle when you get further in the project…that may be incorrect if anyone wants to correct me on that. And I use the above code now to handle everything.

My 2 cents on working 5+ months full time on MTE.

Oh, got it! That’s why my physics properties wasn’t working…

Indeed it’s a lot of code to do all manually but works :smiley:

Thanks for pointing me to the right direction, Azmar!

Yes easy, and keeps coding very minimal.

when you use Tiled to create your maps and load them in through MTE you can set the tile you use to have properties.

So on your spritesheet in tiled set the “wall” tile to:

key :       value:
physics    true

Will make it have physics set to static as default.

Now to detect collision, you can set a new variable to your object like object.type = “wall” or object.name = “wall”

and inside your normal collisionDetect just look for a .type == “wall” and it will detect that wall.

Helpful tool would also set a new local variable in the function:

local agro = event.object1 local hit = event.object2

And proceed inside your began phase with a properly nested detection…remember they come in at any order and need to check both instances!

 

if (agro.type == “player” or hit.type == "player) then
            if (agro.type == “wall” or hit.type == “wall”) then

                     --FOUND WALL DETECTION

Good luck!

Hey @Azmar, by set a new variable to the tile like: object.name = “wall”, you meant in the spritesheet in tiled? Like physics = true? Or in the code? If is in the code, where do i set that?

And when you say “inside your normal collisionDetect”… how can i add an event listener to my tile? 

In the example i gave: 

box.collision = collisionDetect; box:addEventListener( "collision" );

I saw mte.addPropertyListener(), but that only run one time when the map is loaded. I want to one of the tiles to be: “isSensor = true” in the collision. Is this possible to set? How?

Thank you for the help!

I think I set you down the wrong track, you could create objects on your map and easily call them in to give them all the properties you want or set all the properties in tiled to them by looking at the MTE documentation. And that all works fine, but with my experience on MTE it is very amazing but has a few minor bugs, and you will come across this bug and there are some threads in the history on it. It happens when creating objects and using them for all these properties while loading/changing maps through MTE etc…hard to describe. Just saving you a huge headache that doesn’t make sense just do what I’m gonna suggest next.

If you want a wall that is just a physics object sure do it in tiled but if it has any specific properties or special collision rules just create it in corona and do the normal add sprite functions and after do what you want.

//mte.add sprite (monster…)

//physics.addBody(…)

//monster.isSensor = true;

monster.type = “wall”; —is how you set collision

It may be tedious and a lot of code where you can do it in Tiled, but believe me the bug will come for you :slight_smile:

I like to do just:

Runtime:addEventListener("collision", onCollision)

To handle most my collisions, as there are so many to handle when you get further in the project…that may be incorrect if anyone wants to correct me on that. And I use the above code now to handle everything.

My 2 cents on working 5+ months full time on MTE.

Oh, got it! That’s why my physics properties wasn’t working…

Indeed it’s a lot of code to do all manually but works :smiley:

Thanks for pointing me to the right direction, Azmar!