Jump to content

Collision with Ninja physic


Recommended Posts


i'm trying to detect collision with objects to collect using the ninja physic model like this :

game.physics.startSystem(Phaser.Physics.NINJA); game.physics.ninja.gravity = 0;map = game.add.tilemap('map');map.addTilesetImage('tiles', 'tiles');//create layer with bordervar slopeMap = { '1':0, '2':1, '3':2, '4':3, '5':4, '15':14, '16':15, '17':16, '18':17, '19':18, '20':19, '21':20, '22':21, '23':22, '24':23, '25':24 , '26':25, '27':26, '28':27, '29':28, '30':29 };    layerBorder = map.createLayer('border');   tiles  = game.physics.ninja.convertTilemap(map, layerBorder, slopeMap); //create layer with objects to collect     layerObjs  = map.createLayer('objs');   mins = game.physics.ninja.convertTilemap(map, layerObjs, {'32':1,'33':1,'34':1,'35':1,'36':1});//add player
player = game.add.sprite (game.world.centerX, 100, 'player');
game.physics.ninja.enableCircle(player, player.width/2);
player.body.bounce = 0.1;
game.physics.ninja.enableBody( player );


collision with the border works but with objects it doesn't work

map.setTileIndexCallback([32,33,34,35,36,36], collisionHandler, this);

 so i try something like that in the update()

for (var i = 0; i < mins.length; i++) {       var hit = player.body.circle.collideCircleVsTile(mins[i].tile);       if (hit) onHitObj (player, mins[i].tile );   }
but i'm in trouble to know which object is it ....
i'm a bit lost, can help me ?




Link to comment
Share on other sites


I have used a similar approach in my code and it worked like a charm!

I used the latest phaser version now available.


 Please note that: player.body.circle.collideCircleVsTile returns

 false - if the objects collide

 undefined - if the objects do not collide


I don't know if that was made on purpose of it's a bug.

Link to comment
Share on other sites


IIRC I have used your same code. But yesss it worked for me!

I had two layers in my game. I have created them with "tiled" (a free Qt-based application).


Here is the way how I detect the collision in the update() function:


  for (var i = 0; i < this.obstacleTiles.length; i++)

            var mTile = this.obstacleTiles.tile
            var isColliding = (this.sprite1.body.circle.collideCircleVsTile(mTile)) !== undefined;
            if (isColliding) console.log("collision detected!");
I have created obstacleTiles in the create function as follow
obstacleTiles = game.physics.ninja.convertTilemap(this.map, layerObstacles, obstacleMap);
Link to comment
Share on other sites

thx for ur quick reply ,)

i was not enough clear : collision is detected but i didn't success to access to the object "collected"

for (var i = 0; i < mins.length; i++) {  var o   = mins[i],      hit = player.body.circle.collideCircleVsTile(mins[i].tile);       if (hit) {           o.scale.x = 2;          o.kill();       }}

neither kill() or scale.x works ? ,(,(

Link to comment
Share on other sites

mins is a  Phaser.Physics.Ninja.Body object.

Not really sure you can scale it!


If you use chrome

1. go to  http://examples.phaser.io/_site/view_full.html?d=ninja%20physics&f=ninja+tilemap.js&t=ninja%20tilemap

2. press F12

3. type "tiles" (it's the name of the variable in that code)

4. click the first object and you can see the properties of the object.

you can set in real-time the properties of the object.


Good luck!

Link to comment
Share on other sites


I played a little bit with the console in your game and I think I found this solution:


To remove a tile from the Ninja physics world you have to perform these steps:


1. Remove the body - with something like map.layers[1].bodies[0].destroy()

Note: when you do this. this will generate an Error in  Phaser.Physics.Ninja.Circle.collideCircleVsTile But you should be able to work it around!


2. Remove the tile itself from the map -     map.removeTileWorldXY(416, 416, 64, 64, layerObjs)


                                                                   This will remove your first "enemy" from your game.

                                                                    x and y = 416.

                                                                    w and h are 64.


That's the solution I found.


Link to comment
Share on other sites


  • Recently Browsing   0 members

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