Jump to content

Trigger (with P2)


pandavigoureux29
 Share

Recommended Posts

Hi,

 

this may be a stupid question, but I wasn't able to find anything on how to use a P2.Body as a trigger. 

 

Maybe it's my notion of "Trigger" that is wrong. I use this term because it's called like that in Unity.

 

I mean here a body that sends a collision event but doesn't block anything.  

 

I know I could make a rectangle and check at each update if there's an object in it, but using the onBeginContact and onEndContact of P2.Body would seems more homogenous in my system.

 

Thanks

Link to comment
Share on other sites

A trigger is called sensor in P2 (like in Box2D).

 

You enable a body as a sensor with sprite.body.data.sensor = true. You have to access the original P2 Body with 'data' as the P2 Wrapper in Phaser currently doesn't support this.

 

As an alternative you can use the contact equation during a contact event and set its flag to eq.enabled = false. This will prevent the collision and allows you to have a body that reports a collision all the time where you can cherry pick your positives to collide or not to collide (as opposed to a non colliding mask/group setting). With this technique you can also calculate other properties of a collision (force, direction) and disable or enable the collision depending on the results.

Link to comment
Share on other sites

Thanks for the reply.

 

The bodies I have on my sprites don't have this "sensor" value in their "data". 

I also can't find it in the doc ;

http://schteppe.github.io/p2.js/docs/classes/Body.html

 

In order to be sure I attempted to set it to true but this hasn't changed anything.

 

I'll have a closer look a the equation stuff and come back to let you know

Link to comment
Share on other sites

Talking about STATIC bodies, I tried using 

sprite.body.motionState = Phaser.Physics.P2.Body.STATIC

but I noticed that it didn't automatically set the mass to 0, which was making a lot of funny behaviours.

 

For now I'm just setting the mass to 0 myself, but I would've expected this to be automatic (this seems to be P2 related though)

Link to comment
Share on other sites

You should use

//Phaser.Physics.P2.Body#staticbody.static = true

This will set the mass to zero as well. The motionState in p2 seems to be more like a private variable at the moment. It's only a property and not a real setter which could update the mass accordingly. Use the static setter of the Phaser Body Wrapper or if you have to handle P2 bodies manually: Just do both assignments when you have to change. If you do not have to change the type later provide the property mass:0 during initialisation. This would set the motionState correctly to static.

Link to comment
Share on other sites

  • 3 weeks later...

As an alternative you can use the contact equation during a contact event and set its flag to eq.enabled = false. This will prevent the collision and allows you to have a body that reports a collision all the time where you can cherry pick your positives to collide or not to collide (as opposed to a non colliding mask/group setting). With this technique you can also calculate other properties of a collision (force, direction) and disable or enable the collision depending on the results.

 

this is exactly what i need.. unfortunately i have absolutely no idea how to use this...

 

i want to create a body that does not collide with a specific object when the body is moving upwards but still collide with the very same object if the body is moving downwards...

Link to comment
Share on other sites

What about using a special shape for feet, that you would deactivate when the upper body of the player is colliding with the moving object.

 

Then if the player comes from up, it will land on the object, but if its upper body is colliding with the object, the feet shape will not physically collide ( and so not preventing the player to go through the platform ). 

 

Just trying to help. Sometimes even a unfitting idea can lead to a good solution :)

Link to comment
Share on other sites

in fact i've already tried adding a second shape to my player and set it as sensor..  this sensor would if colliding with my "jumpthroughbutlandon" object "deactivate" the main shape for the time of the collision and reactivate it on endContact

 

the only thing i just can't figure out how to detect if the sensor or the main shape is colliding.. they act as one body and "onBeginContact" events for example seem to work only on the body - therefore on all shapes at the same time..  hmm.. maybe i'm thinking in circles right now but the solution seems so near and far at the same time ;-)

Link to comment
Share on other sites

If you are using P2's physics - well, this thread is about P2 so I hope you are :) -, you have the information about the colliding shapes. 

 

Assuming you are doing something like this :

sprite.body.onBeginContact.add(onBeginContact, this);

The callback onBeginContact should be like that :

function onBeginContact(_body2, _shapeA, _shapeB, _equation)

The _equation is a ContactEquation , and it also holds shapeA and shapeB properties.

 

If you keep a reference to the shapes you are adding to your body, this should be a piece of cake

Link to comment
Share on other sites

@pandavigoureux :   that's exactly what i needed.. thx for that!

i am now able to find out which of my sensors reported the collision !!!  :)

 

playersensor_up = player.body.addRectangle(10,4,0,-20);playersensor_up.sensor=true;player.body.onBeginContact.add(function(body1,shapeA,shapeB) {  if(shapeA===playersensor_up){ console.log("touchingup");}    } , this);
Link to comment
Share on other sites

i'm going to make an example for a jump through moving platform with the use of 2 additional sensors - right now ^^  (and if it's good make a pull request)

 

 

the way to do is would be...  get a github account - fork the phaser-examples repository - pull down the repo to your computer - create a new js file for your example (copy one of the existing and change it) - make the commit and then on github a pull request ..  :huh:

Link to comment
Share on other sites

That does not seems too bad, except for the little "bump" when the platform arrives at the top.

 

I've also noticed in your Mario game ( I've done some research ) that the velocity.x stays the same when you jump and release the directionnal key. This is quite hard to get used to :)

Link to comment
Share on other sites

That does not seems too bad, except for the little "bump" when the platform arrives at the top.

 

I've also noticed in your Mario game ( I've done some research ) that the velocity.x stays the same when you jump and release the directionnal key. This is quite hard to get used to :)

 

 

interesting..    you mean the "friction" of the air should slow down mario when in the air ?  i do not remember how the original mario controls were like...

 

(the bump is hard to avoid because of the fast directional change of the platform.. the only way to go would be to ease out and ease in the movement..  but this would only be possible with tweens and i already went down this path and decided not to go with tweens because of several other obstacles...

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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