5

i've already asked something similar, but now i've the problem to manage and realize a "realistic" steering for a simple 2d (top-down) car racing game.

How can i do a "realistic" steering for the car ? (i use c# but another language is welcome;)) Using Sin and Cos ? If yes, how ? Thanks in advance!

crowne
  • 8,456
  • 3
  • 35
  • 50
stighy
  • 7,260
  • 25
  • 97
  • 157

4 Answers4

11

I'm on my lunch break so I can't do tremendous justice to the "best" answer, but the pseudocode looks something like this:

y_change = sin(rotation) * speed;
x_change = cos(rotation) * speed;

car.x += x_change;
car.y += y_change;

you would execute this code in every frame; rotation would be controlled by your steering input, and speed would be controlled by your acceleration input.

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
  • Thank you for your answer. For rotation you mean the angle (or degree) ? For example, if my car is in X=10, Y=20 and my speed is 30 (30 pixel per second), and rotation is 15 degree... can i use your tips ? – stighy Oct 25 '10 at 19:42
  • Thanks thanks and again..thanks.. it works with a little change: at screen, Y axes is inverted, so simply y_change = y_change * (-1)... and, because i've angle expressed in degree, i've to convert it in radiant (PI/180)*myangle... ;) – stighy Oct 25 '10 at 20:45
1

You will probably want to use a physics engine that someone else has already created. I've heard good things about the XNA Physics API.

I would imagine that you will have to use sine and cosine, but that is the just the tip of a VERY large iceberg...

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • 1
    I don't think he'll need a full-blown physics engine for a birds-eye-view 2D game. – Brian Driscoll Oct 25 '10 at 17:22
  • You might be right. I guess it depends on how realistic he wants his steering to be. I'd be curious to see how complex steering algorithms that take momentum, under-steer, resistance, fish tailing or any number of other factors are... – Abe Miessler Oct 25 '10 at 17:31
  • About physics engine, thanks but for my needed i think a basic algo is enough! – stighy Oct 25 '10 at 19:43
  • The "2d (top-down)" kind of modifies "realistic" to mean "not jerky" – NotMe Oct 26 '10 at 19:51
  • 1
    I would disagree with that. There are a lot of things you can do in 2d to make a driving experience more realistic. – Abe Miessler Oct 26 '10 at 20:16
  • I am with @Abe here. There is nothing about 2d that implies low quality or unsophisticated. I have not used it but [box2d](https://github.com/erincatto/box2d) like like it would be great for this. (Not top-down, but these 2d side-scroller vehicles are pretty evocative: https://www.youtube.com/watch?v=0awh5MFJwQo) – Craig Reynolds Jul 31 '20 at 18:52
1

Brian Driscoll’s helpful answer ten years ago is all you need to know about this for non-demanding applications. I often use Euler integration of position via velocity vector as modified by accelerations from a controller.

An interesting sidelight for wheeled vehicles is that they do not rotate around their center of gravity. A typical car rotates about a point along the line through the rear axle, but offset well to the side.

diagram of Ackermann steering geometry

This concept is important for real vehicles. Their steering mechanism tries to conform to Ackermann steering geometry to minimize wear due to slip. In simulated vehicles these considerations are import for modeling instantaneous curvature and predicting future path.

Craig Reynolds
  • 675
  • 7
  • 16
0

Algorithm is:

  1. Record how someone's else drives (using dev. version of game)

  2. (Optional) Split up recording into snippets for variety of usual situations.

  3. Replay recordings in game. (Using suitable snippets and possibly interpolate trajectory between them).

You may also try fuzzy logic and simple steering unit model.

Model would be:

x = integrate horiz_velocity by t
horiz_velocity = intergate steering_angle by t
and steering_angle = fuzzy_steering_function(...)
Vovanium
  • 3,798
  • 17
  • 23