Jump to content

How can i disable collision between a body and tilemap ?


askariwa
 Share

Recommended Posts

I am trying to modify the p2.js example (http://phaser.io/examples/v2/p2-physics/tilemap).
I have added a group named explosions which i want to have it's members to collide with the ship but not with the tilemap.
From what i've read in the forum, i need to use the collision groups but i cannot understand how (in this case). Using them for the ship is easy and think that using a loop could be ok for the explosions group but for the tilemap it's not that easy.
I have read nearly all posts in this forum but i didn't find anything for this case. 

Any hint ?


function preload() {

    game.load.tilemap('map', 'assets/tilemaps/maps/collision_test.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
    game.load.image('walls_1x2', 'assets/tilemaps/tiles/walls_1x2.png');
    game.load.image('tiles2', 'assets/tilemaps/tiles/tiles2.png');
    game.load.image('ship', 'assets/sprites/player.png');
	game.load.atlas('explosion', 'assets/atlas/explosions.png', 'assets/atlas/explosions.json');
}

var ship;
var map;
var layer;
var cursors;
var result;
var tilesCollisionGroup;
var shipCollisionGroup;
var explosionsCollisionGroup;

function create() {
    game.physics.startSystem(Phaser.Physics.P2JS);

	tilesCollisionGroup   = this.physics.p2.createCollisionGroup();    
	shipCollisionGroup  = this.physics.p2.createCollisionGroup();
	explosionsCollisionGroup  = this.physics.p2.createCollisionGroup();

	explosions = game.add.physicsGroup(Phaser.Physics.P2JS);
	explosions.createMultiple(5, 'explosion');
	//  explosions.body.setCollisionGroup(explosionsCollisionGroup);	?
	//	explosions.body.collides([shipCollisionGroup]);   ?

    map = game.add.tilemap('map');

    map.addTilesetImage('ground_1x1');
    map.addTilesetImage('walls_1x2');
    map.addTilesetImage('tiles2');
	
    layer = map.createLayer('Tile Layer 1');

    layer.resizeWorld();

    map.setCollisionBetween(1, 12);

    game.physics.p2.convertTilemap(map, layer);

	// how can i define tilesCollisionGroup ?
	
    ship = game.add.sprite(200, 200, 'ship');
    game.physics.p2.enable([ship],true);
	ship.body.clearShapes();
	ship.body.loadPolygon('physicsData', 'player');
	ship.body.fixedRotation = false;
	ship.body.setCollisionGroup(shipCollisionGroup);  
	ship.body.collides(tilesCollisionGroup);
	
    game.camera.follow(ship);

    game.physics.p2.setBoundsToWorld(true, true, true, true, false);

    cursors = game.input.keyboard.createCursorKeys();
	
	game.input.onTap.add(addexplosion, this);

}

 

Link to comment
Share on other sites

I have finally defined the collision groups as i had to, at least i think :mellow:

 

var ship;
var map;
var layer;
var cursors;
var result;
var tilesCollisionGroup;
var shipCollisionGroup;
var explosionsCollisionGroup;

function create() {

    game.physics.startSystem(Phaser.Physics.P2JS);

	tilesCollisionGroup   = game.physics.p2.createCollisionGroup();    
	shipCollisionGroup  = game.physics.p2.createCollisionGroup();
	explosionsCollisionGroup  = game.physics.p2.createCollisionGroup();

    ship = game.add.sprite(200, 200, 'ship');
    game.physics.p2.enable([ship],true);
	ship.body.clearShapes();
	ship.body.loadPolygon('physicsData', 'player');
	ship.body.fixedRotation = false;
	ship.body.setCollisionGroup(shipCollisionGroup);    
	ship.body.collides([tilesCollisionGroup]);	
    game.camera.follow(ship);
	
	explosions = game.add.group(); 
	explosions.enableBody = true;
	explosions.physicsBodyType = Phaser.Physics.P2JS;
	explosions.createMultiple(5, 'explosion');
	explosionTouchSound = game.add.audio('explosionTouchSound');

    map = game.add.tilemap('map');
    map.addTilesetImage('ground_1x1');
    map.addTilesetImage('walls_1x2');
    map.addTilesetImage('tiles2');
     
    layer = map.createLayer('Tile Layer 1');

    layer.resizeWorld();
	game.physics.p2.convertTilemap(map, layer);

// ********* 1 : insert members of tilesCollisionGroup

	for (var i = 0; i < map.layer.bodies.length; i++) 
	{        
		var tileBody = map.layer.bodies;        
		tileBody.setCollisionGroup(tilesCollisionGroup);   
	    tileBody.collides([shipCollisionGroup]);	
	} 

    //  By default the ship will collide with the World bounds,
    //  however because you have changed the size of the world (via layer.resizeWorld) to match the tilemap
    //  you need to rebuild the physics world boundary as well. The following
    //  line does that. The first 4 parameters control if you need a boundary on the left, right, top and bottom of your world.
    //  The final parameter (false) controls if the boundary should use its own collision group or not. In this case we don't require
    //  that, so it's set to false. But if you had custom collision groups set-up then you would need this set to true.
    game.physics.p2.setBoundsToWorld(true, true, true, true, false);

    //  Even after the world boundary is set-up you can still toggle if the ship collides or not with this:
    // ship.body.collideWorldBounds = false;
	
    cursors = game.input.keyboard.createCursorKeys();
	
	game.input.onTap.add(addexplosion, this);

}

Still, now the explosions do not collide with the tilemap but neither the ship does (even if i defined the collision between their collision groups).

Any help?

Link to comment
Share on other sites

16 hours ago, stvrbbns said:

rtileBody = map.layer.bodies;;s

In your for-loop, you didn't actually use the increment. Also, you're never using explosionsCollisionGroup.

I was using explosionsCollisionGroup after the code i've posted (i didn't wanted to make a long post) that's why but i just didn't saw missing the increment during the loop :blink: !

I am going to study your example, which seems to be exactly what i was trying to do.

Thank you for sharing this stvrbbns and for your help :)

Link to comment
Share on other sites

On 3/24/2016 at 4:52 AM, askariwa said:

I was using explosionsCollisionGroup after the code i've posted (i didn't wanted to make a long post) that's why but i just didn't saw missing the increment during the loop :blink: !

I am going to study your example, which seems to be exactly what i was trying to do.

Thank you for sharing this stvrbbns and for your help :)

Happily, you're welcome.

I don't know without seeing all of your code, but the main problem I found out I was having (and is resolved in my example) is the necessary order of function calls:

  1. map.createLayer()
  2. layer.resizeWorld()
  3. game.physics.p2.setBoundsToWorld()
  4. game.physics.p2.createCollisionGroup()

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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