pandavigoureux29 Posted April 10, 2014 Share Posted April 10, 2014 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 More sharing options...
JP91 Posted April 10, 2014 Share Posted April 10, 2014 maybe setPostBroadphaseCallback(callback,context)? Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted April 10, 2014 Author Share Posted April 10, 2014 I'm not sure this is what I'm looking for, although I don't fully understand what it does. It seems too global and not specific to a body.Basically what I want is being able to tell a body it can be crossed over, but still trigger onBeginContact event Link to comment Share on other sites More sharing options...
george Posted April 11, 2014 Share Posted April 11, 2014 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. pandavigoureux29, Mefteg and schteppe 3 Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted April 11, 2014 Author Share Posted April 11, 2014 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 pandavigoureux29 1 Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted April 11, 2014 Author Share Posted April 11, 2014 Found it ! I was directly trying to access a possible "sensor" value on the body, which doesn't exists. The thing is, this is a P2.Shape property. So, for example :sprite.body.data.shapes[0].sensor = true;Thanks for your help Ido 1 Link to comment Share on other sites More sharing options...
george Posted April 11, 2014 Share Posted April 11, 2014 Oh sorry. Yes, sensor is OF COURSE a property of a fixture and not of the body. I wrote some hours ago a class for fixtures to make this more easy with complex polygons.https://github.com/photonstorm/phaser/pull/704 I do not know what my brain was doing when I told you do use body.data.sensor I guess it's too early here. Sorry for the confusion. Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted April 11, 2014 Author Share Posted April 11, 2014 No problem. You led me straight to the solution. Thanks for your link, I'll have a look at that. Link to comment Share on other sites More sharing options...
Wavertron Posted April 11, 2014 Share Posted April 11, 2014 Nice, I had been using a lot of STATIC bodies, but it never really felt like the right way.Testing out sprite.body.data.shapes[0].sensor = true; just now is looking pretty sweet. doco ref: http://schteppe.github.io/p2.js/docs/classes/Shape.html#property_sensor Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted April 11, 2014 Author Share Posted April 11, 2014 Talking about STATIC bodies, I tried using sprite.body.motionState = Phaser.Physics.P2.Body.STATICbut 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 More sharing options...
george Posted April 11, 2014 Share Posted April 11, 2014 You should use//Phaser.Physics.P2.Body#staticbody.static = trueThis 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 More sharing options...
pandavigoureux29 Posted April 11, 2014 Author Share Posted April 11, 2014 Yes I wanted to make use of the defined static properties for clarity, but it seems like I'll have to stick with static/dynamic boolean properties of the body. Link to comment Share on other sites More sharing options...
valueerror Posted May 2, 2014 Share Posted May 2, 2014 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 More sharing options...
pandavigoureux29 Posted May 2, 2014 Author Share Posted May 2, 2014 I guess checking body.velocity.y isn't enough for you? Link to comment Share on other sites More sharing options...
valueerror Posted May 2, 2014 Share Posted May 2, 2014 no it isn't.. since the player will sometimes land on the other object (which is moving up and down) the player velocity will sometimes be -y and sometimes +y so at one point the player would fall through the object - i can't use velocity... Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted May 2, 2014 Author Share Posted May 2, 2014 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 More sharing options...
valueerror Posted May 2, 2014 Share Posted May 2, 2014 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 More sharing options...
pandavigoureux29 Posted May 2, 2014 Author Share Posted May 2, 2014 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 More sharing options...
valueerror Posted May 3, 2014 Share Posted May 3, 2014 @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 More sharing options...
pandavigoureux29 Posted May 3, 2014 Author Share Posted May 3, 2014 Glad I could help. Actually I thought about making an example about sensors and this kind of stuff, because it's really not well documented, in my opinion. But I don't know the process to do this. Link to comment Share on other sites More sharing options...
valueerror Posted May 3, 2014 Share Posted May 3, 2014 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 .. Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted May 3, 2014 Author Share Posted May 3, 2014 Thanks. However if you are on this I don't see the point in making the same example. I have one showing how to make a wall jump but I"m not sure if this could fit as an example Link to comment Share on other sites More sharing options...
valueerror Posted May 3, 2014 Share Posted May 3, 2014 walljump would be very interesting since this is one basic and very common platformer move ! here is the first draft of the jumpthrough example : http://test.xapient.net/phaser/ALL/moving-jumpthrough-platform.html Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted May 3, 2014 Author Share Posted May 3, 2014 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 More sharing options...
valueerror Posted May 3, 2014 Share Posted May 3, 2014 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 More sharing options...
Recommended Posts