Jump to content



Recommended Posts

With the magic canJump() from this labs-demo for example:



function checkIfCanJump() {    var yAxis = p2.vec2.fromValues(0, 1);    var result = false;    for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++)    {        var c = game.physics.p2.world.narrowphase.contactEquations[i];        if (c.bodyA === player.body.data || c.bodyB === player.body.data)        {            var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis            if (c.bodyA === player.body.data) d *= -1;            if (d > 0.5) result = true;        }    }        return result;}

If anybody can tell me exactly how and why this works, it would be really appreciated!


I can use it, and it works fine - but I do not completely understand it.

(Also it fails, if the body is asleep - at least I think that might be a problem, because asleep bodies do not come up in the contactequations?)


In my test http://jppresents.net/static/yavi I had to set the hearts to not sleep to make it work all the time.

(Ok, in the current version the hearts aren't on screen long enough to even think about going to p2-sleep but still.)

Link to comment
Share on other sites

ok... lets start.. as far as i understand it works this way:

first you define the Yaxis.. a vector.. we need this because what we want to find out happens on that direction.. you could switch the numbers 0 and 1 to define the Xaxis if you need something like touchingLeft or touchingRight..

then we cycle through all the contactequations in the narrowphase.. in this phase its already sure that a collision will happen and you cant cancel it anymore.. (you could if you would use onpresolve instead of narrowphase where bodies will collide but the exact equations are not done yet)

contactEquations is an array of equations...you pick one at the time ... c

an equation has allways 2 bodies.. you the check if one of them is your player

then it gets a bit blurry... all i know is that d is something like the direction the object is moving atm. and if d is bigger than 0 and its bodyA the object is moving upwards.. in this function d is multiplied by -1 and then compared with 0.5 ...so if its bigger it is definitely moving very straight down and we have found a touchDown.. thats all we wanted to know... if it would be smaller than -0.5 we would have a touchUp ;)

its not working for sleeping objects because they are not calculated every step in the narrowphase anymore because they already had their contactequations for all their reactions on the collission and they are not moving so we save power and gain performance.. thats what sleep is for....

Link to comment
Share on other sites

yeah, you get fuzzy at the same point in the function where it loses me too ;)


the d thing.. what does it mean exactly? because it can't really be movement, because nothing moves while the player stands on the floor. (But checkCanJump results in true of course, because we can jump from the ground)


If somebody could shed light on that part, that would be great :)

Link to comment
Share on other sites

  • 1 year later...

This is quite an old post but I wanted to show you guys how I solved this issue for myself:




phaser-p2Helper makes it easy to keep track and manage p2.js physics object interactions. It is an advanced helper to keep track on the objects between the onBeginContact and onEndContact signal.



Link to comment
Share on other sites


  • Recently Browsing   0 members

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