Jump to content

P2 Physics : how to make a player collide with world bounds, but not enemies ?


Owumaro
 Share

Recommended Posts

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 :

 

post-15956-0-53081800-1440790998.png

 

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

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

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 set

game.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

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 bounds

Calling 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

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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