Jump to content

Phaser2 Pysics p2


ElizaTom
 Share

Recommended Posts

  • 3 weeks later...

Sorry if this is a bit late, Try looking at the p2 "platformer material" example, specifically at this snippet here, I added some of my own comments to explain everything:

function checkIfCanJump() {

    var result = false;
    //Search through all collisions going on right now
    for (var i=0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++)
    {
        var c = game.physics.p2.world.narrowphase.contactEquations[i];
        //is this a collision that involves the player?
        if (c.bodyA === player.body.data || c.bodyB === player.body.data)
        {
            //gets the dot product of the yaxis and the normal
            var d = p2.vec2.dot(c.normalA, yAxis);
            //if bodyA is the player we need to flip it so it points upward
            if (c.bodyA === player.body.data)
            {
                d *= -1;
            }
            //The normal is pointing upward so that means we are on the ground *YAY!*
            if (d > 0.5)
            {
                result = true;
            }
        }
    }
    
    return result;

}

If you don't know what a normal is I can give you a quick explanation. Basically, a normal vector is a vector (a straight line pointing at some angle over a distance) that is perpendicular to the surface. We then take the dot product of the normal vector and the y-Axis. The dot-product basically gives a higher number the closer they are to being perpendicular (Disclaimer: I would look up dot Product online to find a more mathematically correct answer). This allows us to see if we are standing on flat ground.

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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