GameDev.netThe Physics of Racing, Part 8: Simulating Car Dynamics with a Computer Program

The Physics of Racing, Part 8: Simulating Car Dynamics with a Computer Program
This month, we begin writing a computer program to simulate the physics of racing. Such a program is quite an ambitious one. A simple racing video game, such as "Pole Position," probably took an expert programmer several months to write. A big, realistic game like "Hard Drivin" probably took three to five people more than a year to create. The point is that the topic of writing a racing simulation is one that we will have to revisit many times in these articles, assuming your patience holds out. There are many `just physics' topics still to cover too, such as springs and dampers, transients, and thermodynamics. Your author hopes you will find the computer programming topic an enjoyable sideline and is interested, as always, in your feedback. We will use a computer programming language called Scheme. You have probably encountered BASIC, a language that is very common on personal computers. Scheme is like BASIC in that it is The first thing we need to do is create a (new-race-car) This is an example of an Everything in Scheme is an expression (that's why Scheme is simple). Every expression has a value. The value of the expression above is the new data structure itself. We need to give the new data structure a name so we can refer to it in later expressions: (define car-161 (new-race-car)) This expression illustrates two Scheme features. The first is that expressions can contain sub-expressions inside them. The inside expressions are called We will have another data structure (with the same format) for (define car-161 (new-race-car)) (define car-240 (new-race-car)) (define car-70 (new-race-car)) The state of a race car consists of several numbers describing the physics of the car. First, there is the car's position. Imagine a map of the course. Every position on the map is denoted by a pair of coordinates, and . For elevation changes, we add a height coordinate, . The position of the center of gravity of a car at any time is denoted with expressions such as the following: (race-car-x car-161) (race-car-y car-161) (race-car-z car-161) Each of these expressions performs The next pieces of state information are the three components of the car's velocity. When the car is going in any direction on the course, we can ask "how fast is it going in the direction, ignoring its motion in the and directions?" Similarly, we want to know how fast it is going in the direction, ignoring the and directions, and so on. Decomposing an object's velocity into separate components along the principal coordinate directions is necessary for computation. The technique was originated by the French mathematician Descartes, and Newton found that the motion in each direction can be analyzed independently of the motions in the other directions at right angles to the first direction. The velocity of our race car is retrieved via the following expressions: (race-car-vx car-161) (race-car-vy car-161) (race-car-vz car-161) To end this month's article, we show how velocity is computed. Suppose we retrieve the position of the car at simulated time and save it in some variables, as follows: (define x1 (race-car-x car-161)) (define y1 (race-car-y car-161)) (define z1 (race-car-z car-161)) and again, at a slightly later instant of simulated time, : (define x2 (race-car-x car-161)) (define y2 (race-car-y car-161)) (define z2 (race-car-z car-161)) We have used (define vx (/ (- x2 x1) (- t2 t1))) (define vy (/ (- y2 y1) (- t2 t1))) (define vz (/ (- z2 z1) (- t2 t1))) The nesting of expressions is one level deeper than we have seen heretofore, but these expressions can be easily analyzed. Since they all have the same form, it suffices to explain just one of them. First of all, the (/ (- x2 x1) (- t2 t1)) In normal mathematical notation, this expression would read and in most computer languages, it would look like this: (x2 - x1) / (t2 - t1) We can immediately see this is the velocity in the direction: a change in position divided by the corresponding change in time. The Scheme version of this expression looks a little strange, but there is a good reason for it: consistency. Scheme requires that all operations, including everyday mathematical ones, appear in the first position in a parenthesized expression, immediately after the left parenthesis. Although consistency makes mathematical expressions look strange, the payback is simplicity: all expressions have the same form. If Scheme had one notation for mathematical expressions and another notation for non-mathematical expressions, like most computer languages, it would be more complicated. Incidentally, Scheme's notation is called Polish notation. Perhaps you have been exposed to Hewlett-Packard calculators, which use reverse Polish, in in which the operator always appears in the So, to analyze the expression completely, it is a division expression (/ ...) whose two arguments are nested subtraction expressions (- ...) (- ...) The whole expression has the form (/ (- ...) (- ...)) which, when the variables are filled in, is (/ (- x2 x1) (- t2 t1)) After a little practice, Scheme's style for mathematics becomes second nature and the advantages of consistent notation pay off in the long run. Finally, we should like to store the velocity values in our data structure. We do so as follows: (set-race-car-vx! car-161 vx) (set-race-car-vy! car-161 vy) (set-race-car-vz! car-161 vz) The converted by: rck@home.net
Thu Sep 29 14:10:24 PDT 1994
© 1999-2011 Gamedev.net. All rights reserved. |