2D Car Drifting

Started by
2 comments, last by Hodgman 12 years, 8 months ago
Hey all,

I've got pretty much the most basic overhead 2d car physics going on:


velocity.x = Math.cos(angle) * speed;
velocity.y = Math.sin(angle) * speed;

position.x += velocity.x * deltaTime;
position.y += velocity.y * deltaTime;


Pretty basic -- change speed to go faster / slower / reverse and increase / decrease angle to turn.

Everything works, but it feels pretty boring to drive.

How would I go about drifting? Nothing too realistic, just something fun and arcadey where the more you turn, the more you slide unless you counter-turn.

Thanks!
Advertisement
instead of allowing your car to move in the direction it's facing, you can take in account that it still has momentum going in the direction it used to be facing? Ie just because you turn the wheel doesn't mean you're going to go in that direction.

Your car has an angle that it is facing, and an angle that it is moving. So gradually alter the cars moving angle to match the facing angle. Hopefully that makes sense... I'm currently in the middle of a dumb aurgument and yes as I write this they are getting even angrier and my smile is getting bigger... so I'm having a hard time concentrating.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.

instead of allowing your car to move in the direction it's facing, you can take in account that it still has momentum going in the direction it used to be facing? Ie just because you turn the wheel doesn't mean you're going to go in that direction.


That sounds like spaceship physics rather than car physics. Cars DO go in the direction they face unless you turn so fast the momentum is enough to lose friction. Once that happens the above method should be fine enough. But mostly it boils down to switching between the two types of friction.
f@dzhttp://festini.device-zero.de

That sounds like spaceship physics rather than car physics. Cars DO go in the direction they face unless you turn so fast the momentum is enough to lose friction. Once that happens the above method should be fine enough. But mostly it boils down to switching between the two types of friction.
"Spaceship physics" is still what's required to make it act like a real car. Cars go in the direction they're facing because there is friction constantly pushing back against 'slide' movements. So if you create a slippery 'spaceship' and then add tire friction, you've got a car.
e.g. quick idea of basic physics, this time starting with force, not velocitycar.force = (0,0)

for each wheel
slideFrictionAmount = 1.0 - abs( dot( wheel.forward, car.velocity ) );//1 when perpendicular, 0 when parallel
car.force += -velocity * tweakableNumber;//what percentage of sideways movement is converted into an opposing force
if( wheel.powered )
car.force += wheel.forward * tweakableNumber2;//engine power

car.acceleration = car.force / car.mass
car.velocity += car.acceleration * deltaTime
car.position += car.velocity * deltaTime
That said, the friction behaviour of rubber tires is quite complex, and the above model will require a lot of tweaking to get something 'fun'... Just cheating by switching between two 'modes', like Trienco suggested, may be a lot easier to achieve a result with wink.gif More complex car games simply use magic.

This topic is closed to new replies.

Advertisement