Owumaro Posted August 28, 2015 Share Posted August 28, 2015 Hi, I have a 2D world with P2 physics. It has a rectangle sprite to define the ground, it has gravity enabled and the player is standing on the ground. By default, the world bounds are the size of the game, and the player collides with each bound. This works as expected. Here is a screenshot that could help understanding the context : I would like to add enemies that come from outside of the game, and cross the game. I've managed to do this using kinematic body, however gravity/collision with the ground don't work, so it's only great for bullets or enemies like this. Now I'm trying to add enemies that walk on the ground and jump, but they're hitting the walls like the player. Naturally I fell in the collideWorldBounds trap, cf :http://www.html5gamedevs.com/topic/11624-collideworldboundsfalse-disables-all-collisions/ So now I'm trying to disable collision with the walls only for enemies but can't find a solution. Any idea ? I've also tried to push my world bounds and add rectangles on each side that would collide only with the player, but it was quite twisted and I didn't understand how to properly set the collisions to only affect the player (same issue as before). Thanks in advance Link to comment Share on other sites More sharing options...
tips4design Posted August 28, 2015 Share Posted August 28, 2015 Post some code. What collisions groups have you created? Link to comment Share on other sites More sharing options...
Owumaro Posted August 29, 2015 Author Share Posted August 29, 2015 I currently haven't created any collision group. Here is my create function :// Enable P2 physicsgame.physics.startSystem(Phaser.Physics.P2JS)game.physics.p2.gravity.y = 200game.physics.p2.setImpactEvents(true)// Backgroundbackground = game.add.sprite(0, 0, 'level1')// Groundground = game.add.sprite(100, 140, 'ground')ground.width = 300ground.height = 20game.physics.p2.enable(ground, debug)ground.body.static = true// Playerplayer = game.add.sprite(100, 103, 'character1')game.physics.p2.enable(player, debug)player.body.fixedRotation = true// Enemy enemy = game.add.sprite(50, 103, 'enemy')game.physics.p2.enable(enemy, debug)enemy.body.fixedRotation = true I will try to make a jsfiddle with a working code to test things easier. Link to comment Share on other sites More sharing options...
tips4design Posted August 29, 2015 Share Posted August 29, 2015 Well, you should use collision groups, it makes everything much cleaner and easier to understand. Link to comment Share on other sites More sharing options...
jackfreak Posted August 30, 2015 Share Posted August 30, 2015 Use collision groups as tips4design said. It will give you better control over what collides with what: http://phaser.io/examples/v2/p2-physics/collision-groups Another way could be setting enemy.body.collideWorldBounds = false; EDIT: I just saw that you already tried collideWorldBounds =/ Link to comment Share on other sites More sharing options...
Owumaro Posted August 30, 2015 Author Share Posted August 30, 2015 Thanks for the advice, I'm slowly getting closer to my goal It took me some time to understand how the collisionGroup works. What I understood is that if you want a sprite A to collide with a sprite B, you have to :- Create collision groups GA & GB- Set A to GA, B to GB- Call A.body.collides(GB) AND B.body.collides(GA) I thought there would be at least way to say "GA collides with GB" in one line, but I didn't find that. So now I have control on my collisions which I had not before. Still I can't manage to set if a sprite needs to collide with world bounds or not.Currently none of my sprites collide with the world bounds, and if I setgame.physics.p2.updateBoundsCollisionGroup()Then ALL of my sprites will collide with the world bounds. Is there a way to get the default world bound collision group, to manually set who should collide with it ? Link to comment Share on other sites More sharing options...
tips4design Posted August 30, 2015 Share Posted August 30, 2015 create the group that you want to collide with world bounds call game.physics.p2.updateBoundsCollisionGroup() create the other groupUpdateBoundsCollisionGroup will make the groups already existent to collide with the world bounds. Link to comment Share on other sites More sharing options...
Owumaro Posted August 30, 2015 Author Share Posted August 30, 2015 That didn't work... Even if I put the game.physics.p2.updateBoundsCollisionGroup() before creating any collisionGroup, all my sprites collide with the bounds. Edit : I saw that the updateBoundsCollisionGroup method takes a boolean parameter : If true the Bounds will be set to use its own Collision Group. Not sure if I understand this right but I guess it creates a collisionGroup for the bounds. But it doesn't tell how to get the actual group... Edit 2 : I finally did it !! The key is :- activate : game.physics.p2.updateBoundsCollisionGroup()To make every dynamic objects collide with world bounds - call sprite.body.collideWorldBounds = false on each sprite that shouldn't collide with the boundsCalling this usually removes all the collisions (sprite vs bounds as well as sprite vs sprite), but since I'm using a custom collisionGroup it still collides with what I manually set. Many thanks for the help tips4design 1 Link to comment Share on other sites More sharing options...
Recommended Posts