Jump to content

P2 physics constant velocity


4orbidd3n
 Share

Recommended Posts

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

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

  • 4 weeks later...

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

 Share

  • Recently Browsing   0 members

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