0
\$\begingroup\$

I'm a beginner in game development and as a little project I thought I should try to do a top-down racer game much like the good old Slicks n Slide.

I've seen several tutorials and questions about steering and car physics but most is more complicated than what I need and uses box2d with wheels turning and so on.

I want it to keep simple and only want three buttons. Accelerate, turn left and turn right. And my "car" is for now just a circle :-)

My question is how to do the steering. If someone can explain it simple to me I would be very thankful. I know I need to use sin and cos to get the steering angle but I haven't figured out how.

\$\endgroup\$
1
\$\begingroup\$

For a very minimalistic car you won't need anything other than two values: velocity and direction. Both values can be just a floating point number.

When steering left/right, adjust the direction, which could be as simple as being the angle the car is facing. Accelerating or breaking will modify the velocity.

if (key_left)
    direction -= 0.001;
else if (key_right)
    direction += 0.001;

if (key_up)
    velocity += 0.001;
else if (key_down)
    velocity -= 0.001;

To move the car, you'd use simple trigonometry:

pos.x += velocity * cos(direction);
pos.y += velocity * sin(direction);

Of course you can modify this further, e.g. only adjusting direction based on velocity (so you can't turn while standing still). In addition, you'll most likely want to add some friction as well:

velocity *= 0.99;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.