Sign in to follow this  
sebflash

2d car physics / simplest way for position change in curve

Recommended Posts

Hi, I'm searching for the simplest way to simulate the position change in a curve in a 2d car racing game. The car has position at x,y and movement change/speed (=velocity) is at vx,vy. So, if car is driving down vy is positive and vx is zero. If car is driving to right vx is positive, vy is zero. But, do you now a smart and simple way how to change vx, vy if the car is steering to left/right in a curve? I have found a lot of tutorials on net, but most of them are just too complicated for what I need. BR, Sebastian

Share this post


Link to post
Share on other sites
If the car's orientation is represented as a single angle, you can then compute the velocity as:
vx = cos(angle) * speed;
vy = sin(angle) * speed;
You could then steer the car with two direction keys, one which decreased the angle and one which increased it.

That's about the simplest control scheme possible. Is that what you're looking for?

Share this post


Link to post
Share on other sites
E.g. you may simulate an acceleration perpendicular to the current velocity. For that purpose first get the perpendicular, e.g. as
vp := perp(v) = ( vy , -vx )

Then scale it by a factor f that designated how much the steering wheel is rotated. Use 0 for straigt forward and e.g. 5% and 10% for wide and sharp curves to the right resp. -5% and -10% for such curves to the left:
vp * f

Add this to the current velocity to yield in the new one
v' := v + vp * f = ( vx + f * vy , vy - f * vx )

EDIT: And at last, scale the resulting velocity so that its magnitude becomes the required value, e.g.
v' * | v | / | v' |

EDIT 2: Okay it's simple, but not as simple as jyk's solution.

[Edited by - haegarr on September 9, 2006 5:32:56 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
If the car's orientation is represented as a single angle, you can then compute the velocity as:
vx = cos(angle) * speed;
vy = sin(angle) * speed;
You could then steer the car with two direction keys, one which decreased the angle and one which increased it.

That's about the simplest control scheme possible. Is that what you're looking for?


Thanks. Just tested, works good enough for what i need!

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