JakeCake Posted October 17, 2014 Share Posted October 17, 2014 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 More sharing options...
george Posted October 18, 2014 Share Posted October 18, 2014 There is a new method in P2 Body#overlaps. You can use it to test if two bodies are overlapping whenever you want independent of onBeginContact & onEndContact. This might help you to test if the player's on the ground or not when a jump is requested? Regards George Link to comment Share on other sites More sharing options...
JakeCake Posted October 18, 2014 Author Share Posted October 18, 2014 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 More sharing options...
valueerror Posted October 18, 2014 Share Posted October 18, 2014 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 More sharing options...
Recommended Posts