# Phaser P2 Physics: How do I make my object "drive" like a car?

## Recommended Posts

Hi,

I'm making a game where the player controls a Mars Rover from a top-down perspective.  I'm using the WASD control scheme and I'm trying to make it so the rover moves like a car.  If you aren't sure what I mean, imagine looking down at a car performing a K-turn and you might know what I mean.

I got this to work with Arcade Physics, even got it to work with groups.  Now I don't know how to get it to work with P2 Physics.

Here is my code:

`function MovePlayer(player, cursors, game){	//Player Basic Movement Controls		//WASD declerations might be making things slower fyi.	var W = game.input.keyboard.addKey(Phaser.Keyboard.W);    var Al =  game.input.keyboard.addKey(Phaser.Keyboard.A);    var S = game.input.keyboard.addKey(Phaser.Keyboard.S);    var D = game.input.keyboard.addKey(Phaser.Keyboard.D);    	 if (Al.isDown)    {		//player.body.rotateLeft(100);		player.body.angularVelocity = -5;    }    else if (D.isDown)    {		//player.body.rotateRight(100);		player.body.angularVelocity = 5;    }    else    {		player.body.setZeroRotation();    }    if (W.isDown)    {    	player.body.thrust(450);    }    else if (S.isDown)    {        player.body.reverse(300);    }    else    {    	player.body.damping = 0.75;    }        if (player.body.velocity > 1.0)    {    	player.body.velocity = 1.0;    } }`

Right now the rover moves like it's in a slip'n'slide.  Holding down forward and left or forward and right keys results in the rover spinning around in place.  Imagine an object facing the center of a circle and just rotating around the center.  Same thing using the reverse key.  I don't know how to get it to work right.

Any ideas?

##### Share on other sites

Have a look at this for inspiration:

##### Share on other sites

Yeah thanks, so the complete demo available there looks like what I want (except I'm using 2D mode, not whatever 2.5D mode they are using), but that page doesn't have any of the code showing how to do it.

##### Share on other sites

Javascript is completely open source Just look at the sources with Developer Tools or whatever...

##### Share on other sites

Oh, shoot, I forgot about that!

So I looked at the code with developer tools and am still confused as to how they did it.  When I did this in Arcade physics I used VelocityfromAngle.  Does anyone know if there is an equivalent to VelocityfromAngle in P2?