4orbidd3n Posted September 19, 2014 Share Posted September 19, 2014 Hello everyone, I've been developing small games using phaser and this forums have been a great help in finding solutions for problems I've encountered along the way, so thank you all for the great community that has been developing around phaser. I'm not a good programmer so I've been learning a lot lately.Now I've come with a problem that I'm having a hard time dealing with. I'm using p2 as the physics engine for the game I'm currently working on, because I need to use a polygonal shape as a boundary for the level. Here is the main idea for the game : http://jogosgalp.wiz.homedns.org/powerrefinery/game_oldsystem.html The idea is to push the balls to the correct slot using the disc to get points.The concept changed in the meantime because we realized that it wouldn't be fun enough to play. So we are changing the main mechanics. We ditched the disc and the idea right now is to be able to drag any ball and throw it and allow the dragged ball to collide with the other balls that are running in the game. All of the balls should keep a constant velocity and when we release the dragged ball it should also keep the constant velocity instead of inherit the velocity from the mouse drag when we let it go.The phaser examples like the breakout game covers almost all that I need but uses the arcade physics. Here is what I've been using to test the behavior I want: http://jogosgalp.wiz.homedns.org/powerrefinery/dragtest.html Although I can keep the velocity using a contact material for the world with the sprites, I can't find a way to nullify the exchange of force between the sprites when they collide with each other. I also need to restrict the velocity for the dragged ball on release. After exhausting other options, I only have 2 ideas on possible solutions for this: - Using the p2 callbacks to correct the sprites (using something like onEndContact) to overwrite the new velocities added by the engine so they maintain the direction, but have the right velocity. I would also need to apply this for the release function of the dragging behavior. - Rewriting the ContactEquation so the objects would react the way I need them to. My question is, is it possible to do what I want using p2? And if so what would be the best approach to solve this? Thanks Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted September 19, 2014 Share Posted September 19, 2014 Hi I'm not sure to see the real problem here. If you want to know if you can affect a value to sprite.body.velocity.x and sprite.body.velocity.y with P2, the answer is yes. But if you already know how to use onEndContact you should already know that, so I think I didn't get the real issue. Link to comment Share on other sites More sharing options...
4orbidd3n Posted September 19, 2014 Author Share Posted September 19, 2014 Hi pandavigoureux29,Yes I've already messed around with the velocity of the bodies by using onEndontact. My issue resides on trying to keep the velocity. In my test I'm using a moveforward(200) as the initial speed for the balls.What I want is when the balls collide for them to keep the same momentum, meaning that they should be able to move at the same speed as I stated initialy. I tried to understand if there's was a value that I could easily extract from the equation and reapply in an inverse matter on both the velocities so the balls would just change direction on collision and be able the mantain the same speed, but couldn't find a solution like that.Was I able to explain my problem a little better? Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted September 19, 2014 Share Posted September 19, 2014 Well this could help you :http://schteppe.github.io/p2.js/docs/classes/ContactEquation.html Using contactPointA ( or B, I'm not sure of the order, you may have to compare the bodies in collision with the bodyA & bodyB ) from the contact equation, you may be able to find the direction to push the balls to. Link to comment Share on other sites More sharing options...
4orbidd3n Posted September 19, 2014 Author Share Posted September 19, 2014 Thanks for the reply.I've tried using the multiplier given by the contactEquation on the velocities to see if that would do it but didn't work. I'll check your approach to see if I can solve my problem. Link to comment Share on other sites More sharing options...
4orbidd3n Posted October 17, 2014 Author Share Posted October 17, 2014 Hi again guys, After some time out to work on other projects I´ve resumed my work on this game and came with this solution. A working example: http://jogosgalp.wiz.homedns.org/powerrefinery/dragtest.html For anyone that needs to use p2 physics and want the objects to have a constant velocity when they collide with each other, here's what I did://I want every object that is dynamic to be taken into account, so I call the global p2 onendcontact event.game.physics.p2.onEndContact.add(blockHit, this);function blockHit (bodyA, bodyB, shapeA, shapeB, equation) { var constantvelocity = 200; //Check to see if the body is not static. I only want to affect dynamic objects of course if (bodyA.type != 2){ /*Using the elements of the moveForward method. I need to change the angle in this method, because the velocity is the one determining the direction of the objects.*/ var magnitude = bodyA.parent.world.pxmi(-constantvelocity); //the atan method calculates the angle based on the position of the object between 2 points. The velocity provides this. var getanglebyvelocity = Math.atan2(bodyA.parent.data.velocity[1], bodyA.parent.data.velocity[0]); //apply this calculation like it's done with moveForward, but using the new angle that is calculated above bodyA.parent.data.velocity[0] = magnitude * Math.cos(getanglebyvelocity); bodyA.parent.data.velocity[1] = magnitude * Math.sin(getanglebyvelocity); } //repeat the process for the second body if (bodyB.type != 2){ var magnitude = bodyB.parent.world.pxmi(-constantvelocity); var getanglebyvelocity = Math.atan2(bodyB.parent.data.velocity[1], bodyB.parent.data.velocity[0]); bodyB.parent.data.velocity[0] = magnitude * Math.cos(getanglebyvelocity); bodyB.parent.data.velocity[1] = magnitude * Math.sin(getanglebyvelocity); }} Link to comment Share on other sites More sharing options...
Recommended Posts