START VOL. 5 NO. 2 / OCTOBER 1990 / PAGE 100
By Charles Clark
Programmers of games or animated graphics frequently find it necessary to imitate the movement of physical objects such as missiles or spaceships. To make them appear realistic, it helps to have a mathematical model for these motions, an equation or equations that express how such things move. This article and the accompanying programs demonstrate a variety of motions, providing the equations needed to simulate them.
This article has five BASIC programs--PHYS1.BAS, PHYS2.BAS, PHYS3.BAS, PHYS4.BAS and PHYS5.BAS. Each program runs separately, so you can type in only the listings for effects you'd like to try. PHYS1.BAS is in Listing 1, PHYS2.BAS is in Listing 2, and so on. Type in each listing separately, check it with TYPO II, and be sure to SAVE a copy to disk before you run it.
These are very simple demos without extensive built-in instructions, so they might be a bit confusing at first. If you don't enter the right numbers, you may not be able to tell that the program is doing anything. Read each relevant section of this article to see what the valid entries are, and you'll also find some good sample values to try as a test. If a program appears to lock up, or won't accept your input at some point, just press [BREAK], then type RUN to try again.
According to Newton's First Law of motion, a body either remains at rest, or moves uniformly in a straight line at constant velocity unless acted on by an outside force. In other words, once a body is set in motion it keeps that motion. It is no longer necessary to exert any force on it to keep it moving. Mathematically, this can be written:
X = X0+V0*T
where
X0 = initial position (distance units).
V0 = initial velocity (distance per unit time)
T = time
X = position at time T (distance units)
You must remember to use a consistent set of units. If your distances are given in feet, and time in seconds, then velocity must be given in feet per second. As long as you're consistent, you can use any units you like--miles per hour, kilometers per minute, or even furlongs per fortnight.
To see this thrilling equation in action, RUN PHYS1.BAS. Enter a starting position, such as 0. Then try entering several velocity values ranging from 10 to 100. The program calculates and plots the object's position every 0.1 seconds. If the object leaves the screen it wraps around and reenters at its starting point. Press any key to restart the program, or press [BREAK] to return to BASIC.
Note that the object's speed is constant. Its position is strictly a function of time. That is, as time increases, its distance from a fixed point increases proportionately. This very simple equation doesn't allow for gravity, friction, or any of the other forces that would normally affect an object's motion. No object on Earth could move in this manner, but it might be appropriate in the near-vacuum of deep space where there is little gravitational pull or friction.
When an outside force acts upon a body, the body moves with accelerated motion, according to Newton's Second Law of motion. In other words, that body's velocity (speed) is no longer constant. Many common motions that we experience can be mathematically represented by equations derived from Newton's Second Law. The position of an object can be calculated if its acceleration can be calculated at any given time.
In reality, difficulties arise from the fact that a variety of forces are present in nature. These forces are not necessarily constant either, and may depend on such factors as time, the object's velocity, or the object's distance from the force itself. As a result of all these factors, the acceleration of an object in reality could continually change, complicating the motion.
For our purposes, we'll consider a single force that can be considered constant--Earth's gravity. When an object is under the influence of gravity very near the surface of the Earth, the object's acceleration can be considered a constant 32 feet per second, at each second. This is true for an object regardless of its mass.
The position (height) of the object at any time is found by the following equation:
Y = Y0+V0*T-G*T*T/2
where
Y0 = initial height
V0 = initial upward velocity
T = time
G = gravitational constant
Y = height at time T
Other useful equations of the motion are:
YMAX = V0*V0/2/G+Y0
TMAX = -V0/G
VTERM = -SQR(V0*V0+2*G*Y0)
TTERM = (VTERM-V0)/-G
where
YMAX = object's peak height
TMAX = time object reaches peak height
VTERM = terminal velocity as object reaches ground level
TTERM = time when object reaches ground level
To test this equation, RUN PHYS2.BAS and enter a few trial initial upward (positive) velocities and initial heights. The program calculates and plots the object's position every 0.1 seconds.
Don't worry if the object leaves the screen. It will reappear as it comes back down. (If it takes too long you can always press [BREAK] and RUN the program again.) The program calculates and prints the maximum height and the time it takes to get there. It also prints the terminal velocity and the length of time the object was in flight.
Try entering an initial velocity of 60 and an initial height of 0. The peak height reached will be 56.25 feet in 1.87 seconds. Note the terminal velocity (-60 feet per second) and time of flight (3.75 seconds). Now enter an initial velocity of 0 and an initial height of 56.25 feet. The terminal velocity in both cases is exactly the same, since both objects were stationary (velocity=0) at the same peak height. Note also that the time of the second flight is exactly half that of the first. It takes just as long for an object to reach its peak as it does for it to fall from that height. Also, the terminal velocity of an object fired upward from ground level is exactly the same as its upward velocity. Try entering other numbers and you'll see that this is true for any initial conditions.
Note that the program ignores wind resistance, though if we were dropping feathers, say, it would make a significant difference. The program also makes no allowance for initial velocities greater than escape velocity (the velocity required for an object to escape Earth's gravitational pull, approximately 36,000 feet per second).
You can change the gravitational constant in line 1030 to any number you wish. This way you could examine, say the motion of an object on Jupiter (G=84.5) or on the moon (G=5).
If an object is fired at an angle instead of directly upward, an additional component must be added to the equation of the motion. The vertical component of the motion is expressed as:
Y = Y0+V0*SIN(A)*T-G*T*T/2
nearly identical to the equation for the freely falling body. Here A is the angle (in degrees from the horizontal) at which the projectile is fired. The horizontal component of the motion can be written:
X = X0+V0*COS(A)*T
Note the similarity to the equation derived from Newton's First Law of motion.
To see the trajectories that result from different firing angles, RUN PHYS3.BAS. Enter an initial upward velocity (0 to 100 feet per second), a starting height (0 to 60 feet) and an angle (0 to 90 degrees). The program calculates and plots the position of the projectile every 0.15 seconds. The peak altitude, terminal velocity and the range are printed when the projectile lands.
Choose an initial velocity (60 feet per second works well) with a starting height of 0. Run the program several times, varying only the angle. Start at 90 and decrease the angle by 5 degrees each time. If you don't erase the projectile's trail between runs, you can easily judge which angle gives the projectile the greatest range.
Another useful motion occurs when an attractive force varies proportionately to the object's distance from the force. As the object moves further away, the force's effect becomes greater and greater. Eventually the force becomes so great that the object begins moving towards it. In that case, the force decreases as the object gets closer.
When the object reaches the force, the force's effect is zero, but the object's speed allows it to over shoot the force and move away. The force increases once again, and the motion repeats itself. This movement about a fixed point is called harmonic motion. It simulates the motion of springs, pendula, musical instruments, and other vibrating or swinging objects.
Mathematically, the position of the object if found as follows:
S = S0*COS(T*SQR(K/M))
where
S0 = initial distance from force
T = time
K = the force constant
M = the mass of the object
S = distance from force at time T
In addition, the period of the oscillation can be calculated as follows:
P = 2*PI*SQR(M/K)
where
P = period
PI = 3.142
The program PHYS4.BAS demonstrates this motion. When you RUN the program, vertical bars will appear on the screen, each bar representing a length of 10 centimeters. Enter a starting position between 10 and 70 centimeters. Choose an initial mass for the object (100 grams or so). You can examine the effect of varying the object's weight by running the program several times. Or try varying the force constant K in line 3030 to see its effect on the object.
The velocity of the object is greatest at the central point (0 cm) and gradually slows to 0. The object then reverses direction and moves back to the center. In nature, forces such as friction would slow the object down, causing its swings to decrease in distance and time, eventually going to zero.
When two bodies are under the influence of their mutual gravitational attraction, Newton discovered that the force involved was proportional to the product of their masses and inversely proportional to the square of their distance. Basically, this provides a model for the motion of an object in orbit around a much larger object, such as a planet or star. It is not possible to write a simple, single equation to demonstrate this motion exactly. In any case, the gravitational force of our object would be so weak that it's not practical to show all the forces, motions and distances in proper scale onscreen.
However, some simple approximations are possible, and can be scaled to fit on a computer screen. The following equations are used to simulate the motion:
R = SQR(X*X+Y+Y)
F = K/(R*R*R)
where
X = screen x-coordinate
Y = screen y-coordinate
R = distance between objects
K = scaled gravitational constant
F = acceleration caused by force
The x and y components of the acceleration are respectively:
AX = AX-(X*F)
AY = AY-(Y*F)
Finally, the x and y-coordinates are incremented by:
X = X+AX
Y = Y+AY
These equations can be seen in operation with PHYS5.BAS. When RUN, the program first requests the x and y-coordinates of the object in orbit. It will accept onscreen coordinates only (x-coordinates from -80 to 79 and y-coordinates from -39 to 40). Next, x and y acceleration components are requested (ranging from 1.5 to 1.5).
The program then calculates the position of the object in orbit around the central object. If the object leaves the screen, you can press [BREAK] and RUN the program again, or wait for the object to (possibly) come back onscreen.
For some safe orbits, try entering values of 30,0,0,1.00 or 60,0,0,0.5 (highly elliptical orbits) and 30,0,0,1.5 (nearly circular orbit). The gravitational constant K in line 4030 can be changed if you wish to experiment with its magnitude.
Note that an orbital object moves quickly when it is close to the central body and slows down as its path takes it further away. Be careful not to enter initial accelerations that are too high or too low. If the objects come too close together (usually because the input acceleration was too low) you won't see anything happening. If the input acceleration is too high the object will leave the screen (and the orbit) and not come back.
All these equations form a basis for many of the motions you might want to simulate. Innovative programmers can add additional refinements such as the effects of friction, wind velocity or wind resistance for even more realism. With joystick control of velocity or acceleration, you could add that realistic touch to your games and simulations.
Charles Clark lives in Newark, Delaware. This is his first appearance in Antic.