Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

System to detect on-ground with p2 physics


JakeCake
 Share

Recommended Posts

Hi, I have this character that I need to play a walking animation for while on-ground, but a jumping animation when not. I am using "body.onBeginContact()" & "body.onEndContact()" to set a variable "onground" = true/false to choose which animation should play. Since my terrain consist of a bunch of polygon, making for a somewhat edgy ground, my character will needless to say sometime bounce a little bit off the ground, calling these two events very quickly in succession.

 

This happens quite a lot, meaning the player very quickly, just while running along the ground, switches between the two animation modes, which is my first problem.

 

In addition to this problem, there seems to be times when "body.onEndContact()" is called but "body.onBeginContact()" is not called afterwards, so even though the character is standing on the ground he is not considered to be colliding, so I think I need some other function to check for collision as this is erroneous.

 

I am asking anyone here if they have had the same problem and found a good solution. A good solution would in my books be something that can tell if two bodies are touching or have touched within the last few frames or x milliseconds.

 

Link to comment
Share on other sites

use the narrophase equations of p2 physics to find out if a collision (downwards) will happen - if so return true (used to find out if player is on ground)

combine this with a timer that is set to a time in the future (the current time + 500ms) everytime touchingDown returns false and only change the animation if the current time has reached this point

( for example if(game.time.current < timer ){changeAnimation}  )

function touchingDown(someone) {    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];  // cycles through all the contactEquations until it finds our "someone"        if (c.bodyA === someone.body.data || c.bodyB === someone.body.data)        {            var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis            if (c.bodyA === someone.body.data) d *= -1;            if (d > 0.5) result = true;        }    } return result;}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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