Jump to content

Body collision detection with Phaser.Physics.P2


Recommended Posts

Hey guys,


I am developing this draft game demo and I need some advice/ feedback on some tips to make the collision a bit better.


I'm using loadPolygon to load a custom body shapes for this sprite object and responding to collisions using  onBeginContact to applyImpulse to the ball on a SPACEKEY.onDown 

The following is the code


player.body.collides(ballCollisonGroup, this.headerBall, player)

headerBall(bodyA, bodyB, shapeA, shapeB, equation)
      let duration = 10;
      let localX = equation[0].contactPointA[0];
      let localY = equation[0].contactPointA[1];
      let forceX = 0.12 * this.headMass;
      let forceY = 0.8 * this.headMass;

      if(this.headerKey.isDown) // only if see is down apply additional force
  		duration = Math.min(this.headerKey.duration, 500); // apply time based on how long key pressed
        forceY = (1 * this.headMass).toFixed(2); // apply force of 1 velocity x mass of head (.53)
        forceX = (1 * this.headMass).toFixed(2);
        let impulse = [(forceX * duration), (forceY * duration )]; // calculate impulse
        bodyA.applyImpulse(impulse, localX, localY ); // apply it to ball




I'm not sure if I am doing it right as I am not verse in Math Physics but i would like some feedback on the following: -

  1. Is my formula accuarate?
  2. Can I set varying impulses based on the contact shape?
  3. There is a sort of glitch if the ball comes into contact with his shoes and the ball ends up in the middle of his body which gets stuck. How can I avoid this?
  4. The key down condition seems to be glitchy is there anyway to make this a bit more "safe" to detect and capture? I need to apply some force to the ball ONLY when the space key is pressed.

Thank you in advance for your feedback.




Link to comment
Share on other sites

  • 9 months later...

  • Recently Browsing   0 members

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