valueerror Posted May 22, 2014 Share Posted May 22, 2014 http://test.xapient.net/phaser/ALL/moving-platform-push.html in this live example you can see what i mean.. the interesting thing is, that the platform will push the player into the ground, but not into another platform.. the root of this is the following function that uses the onPresolve equations to find out if the player is touching the moving platform from below and disables the collision completely.. it seems to stop working when the player is on the ground and not moving upwards.. this function allows you to jump through the platform but land on it...game.physics.p2.world.on("preSolve", onPresolve);here is the code of the function :function onPresolve(presolve){ if (presolve.contactEquations[0]){ c = presolve.contactEquations[0] f = presolve.frictionEquations[0]; var yAxis = p2.vec2.fromValues(0, 1); var y = p2.vec2.dot(c.normalA, yAxis); if (c.bodyB.parent.sprite && c.bodyB.parent.sprite.key == 'platform' && y >= 0){ //if moving upwards c.enabled = false //disable contactEquation f.enabled = false //disable frictionEquation (solves the stuckInPlatform problem) if (c.bodyA.parent.velocity.destination[1] < 15 ){ // velocity < 15 - still inside the platform c.bodyA.parent.velocity.destination[1]-= 0.5; // course correction! } } }}if someone please could help me think why this is happening - i would very much appreciate it.. thx! btw.: it would be better if this function would cycle through all contact equations and find out if bodyA is the player first - in this example the player is the only other object so this works with out the for-loop... Link to comment Share on other sites More sharing options...
valueerror Posted May 22, 2014 Author Share Posted May 22, 2014 oke.. i played around with it and accidentally found the solution ^^ i thought it would be ok to just use the one contact equation because i already tried to cycle through all equations and it didn't change anything... but what i did not see was that i had a well placed "return" in my forloop the moment i found my player and disabled the contact equation.. i thought it would not be necessary to go through the other equations and it would save cpu time.. wrong! in this special case where the player stands on the ground and is being pushed by the platform there are two contact equations and i have to do the calculations for every one of them and definitely not stop after the first one.. so this is the working function function onPresolve(presolve){ for (var i = 0; i < presolve.contactEquations.length; i++) { //cycle through all contact equations c = presolve.contactEquations[i] f = presolve.frictionEquations[i]; yAxis = p2.vec2.fromValues(0, 1); y = p2.vec2.dot(c.normalA, yAxis); if (c.bodyA.parent && c.bodyA.parent.sprite && c.bodyA.parent.sprite.name === 'mario'){ // check for player if (c.bodyB.parent.sprite && c.bodyB.parent.sprite.key == 'platform' && y >= 0){ //check for platform and if player is moving upwards c.enabled = false //disable contactEquation if (f) f.enabled = false //disable frictionEquation (solves the stuckInPlatform problem) if there is friction if (c.bodyA.parent.velocity.destination[1] < 15 ){ // velocity < 15 - still inside the platform c.bodyA.parent.velocity.destination[1]-= 0.5; // course correction! } } } }} Link to comment Share on other sites More sharing options...
valueerror Posted May 22, 2014 Author Share Posted May 22, 2014 i still don't know if it would be better to also do the whole check agains bodyB and see if bodyB is mario.. until now i couldn't find a problem with the approach above... Link to comment Share on other sites More sharing options...
Recommended Posts