Jump to content

Detecting the collision side


Aldus
 Share

Recommended Posts

Hello guys!

 

I'm trying to implement some way to detect the side of the tile the player is colliding and wich tile it is.

 

For example,if the player jump and collide with the UNDERSIDE of the tile he should stop ,like a normal collision, but if the player step ON it ,the tile should fall down. 

 

 

Another thing,if i have a platform built with a array of tiles,how can i detect wich tile the player is colliding?

 

 

I hope someone know or have the same issue, thanks!

 

Link to comment
Share on other sites

So the way I'd do this is with arcade physics, maybe you can do it with P2 but I'm not sure how.

First enable arcade physics on the game, your collision tilemap layer, and sprite.

 

//game is the global game reference, sprite is the eference to the sprite you want to collidegame.physics.startSystem(Phaser.Physics.ARCADE);game.physics.enable(sprite, Phaser.Physics.ARCADE);//tilemap is your tilemap.  If you don't dedicate a layer to collision, you can use the layer that does collide.collisionLayer = tileMap.createLayer('collision');//lazy collision setup, make sure you only collide on solid tilestileMap.setCollisionBetween(1, 500, true, collisionLayer);

Then when this is done, in your update loop, collide the layer with the sprite with a reference to a handler function:

 

update: function(){    game.physics.arcade.collide(sprite, collisionLayer, levelCollisionHandler, null, this);}

Make sure you have your level collision handler defined:

 

//When colliding with a tilemap layer, you always get the tile that collided with your spritelevelCollisionHandler: function(spriteThatCollided, tileThatCollided){     //use this to detect the side that the sprite collided on, alternatively spriteThatCollided.body.onFloor()     if(spriteThatCollided.body.blocked.bottom){         tileThatCollided.fall() //you'd need to define fall() or some logic here to do the falling     }}

Hope this helps, let me know if you have any questions!

Link to comment
Share on other sites

Thanks for reply CudaBear,but in case im not using a tilemap, i have a platform generator that create a new array of tiles starting at the left side of canvas until the right side,so how could i do that with remote objects,not static one like tileMap you used.

Link to comment
Share on other sites

Oh, in that case Aldus you should create your platforms as sprites, enable physics on them as usual, then use this line:

 

//sprite is the reference to your platform spritesprite.body.immovable = true;sprite.body.allowGravity = false;

to prevent it from moving.  Then, in the collision listener, just like with the tilemap, except collide the two sprites instead of the sprite and the tilemap.  You can then use:

if(platformSprite.body.touching.up == true){    platformSprite.body.immovable = false;    platformSprite.body.allowGravity = true;}

to make it move.

Link to comment
Share on other sites

That make sense, but i just figure it out, i  was thinking wrong,i dont need each tile reference to do this,

 

Here is my solution : 

 

Created 2 groups,group1  for  normalTiles and group2 for  brokenTiles(that will fall),

At the collisionCallBack method with the broken ones i dont need to test each tile to make it fall,if i use the whole group it will work, did'nt know that

 

 

But thanks anyway!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...