Jump to content

[P2] Is player on ground; onBeginContact & End not sufficient


Recommended Posts

I have added events to my player with onBeginContact and onEndContact. They fire off fine, but it is possible for the player to get stuck at certain points. I have even added a timer to make sure the player is still considered onGround if he should be in a tiny jump of skips over an uneven surface:

new function() {        function colStart(bodyB, shapeA, shapeB, contactEquations) {            groundTimer.removeAll();            groundTimer.stop();            player.astronaut.isOnGround = true;            player.astronaut.canJump = true;        }        function colEnd(bodyB, shapeA, shapeB) {            function onTimeOut() {                player.astronaut.isOnGround = false;                player.astronaut.canJump = false;            };                groundTimer.removeAll();                groundTimer.add(750, onTimeOut, this);                groundTimer.start();                        }        player.astronaut.sprite.body.onBeginContact.add(colStart, this);        player.astronaut.sprite.body.onEndContact.add(colEnd, this);    }();

A simplified version with just setting canJump & onGround to true/false on "onBeginContact" / "onEndContact" doesn't work any better.


He get's stuck, so at some point the onEndContact must fire after the onBeginContact, or the onBeginContact doesn't fire at all.


How would I fix this? It's game-breaking to have my character become stuck.


Link to comment
Share on other sites

Thanks, but I can't seem to get this function to report true when they touch. By doing "player.astronaut.sprite.body.data.overlaps(planets[0].sprite.body)" it doesn't fail, but it just gives false each time. Without the ".data" the method gives an exception.


If it would work, it seems like it could be the best solution indeed.


I am using Phaser version 2.1.0 by the way

Link to comment
Share on other sites

i don't quite get what it is that you want.. i assume you just want a reliable check if the player is on ground or not..  if so - this is your method:


(it takes a phaser p2 body and returns true on ground)

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];        if (c.bodyA === someone.data || c.bodyB === someone.data)        {            var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis            if (c.bodyA === someone.data) d *= -1;            if (d > 0.5) result = true;        }    } return result;}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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