Sign in to follow this  
GroZZleR

2D Car Drifting

Recommended Posts

GroZZleR    820
Hey all,

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

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

position.x += velocity.x * deltaTime;
position.y += velocity.y * deltaTime;
[/code]

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!

Share this post


Link to post
Share on other sites
freeworld    341
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.

Share this post


Link to post
Share on other sites
Trienco    2555
[quote name='freeworld' timestamp='1313976733' post='4852085']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
Hodgman    51324
[quote name='Trienco' timestamp='1313988993' post='4852128']
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.
[/quote]"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 velocity[code]car.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[/code]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 [img]http://public.gamedev.net/public/style_emoticons/default/wink.gif[/img] More complex car games simply use [url="http://en.wikipedia.org/wiki/Hans_B._Pacejka#The_Pacejka_.22Magic_Formula.22_tire_models"]magic[/url].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this