swissnetizen

How to check if on Floor using P2?

Recommended Posts

For future reference, this is how to detect if the entity is on the floor (I think):

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

from: http://phaser.io/examples/v2/p2-physics/tilemap-gravity

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.