The Physics of Motion
How to program realistic-Iooking movements!
By Charles Clark
Learn some physics and discover some new programming possibilities for your games and animations with these five short BASIC demos that run on all 8-bit Atari computers. Each demo shows you how to program and display realistic movements onscreen, from simple straight-line motion to the paths of objects in orbit.
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
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
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
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))
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)
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)
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.
ZO 10 REM PROGRAM LISTING #1 UD 20 REM UNIFORM MOTION TR 30 REM BY CHARLES CLARK LK 35 REM (C) 1990, ANTIC PUBLISHING, INC VJ 40 DIM T $ (10) WA 100 GRAPHICS 7 OA 110 SETCOLOR 4, 0, 0 : SETC0L0R 2, 3, 12 SA 120 SETCOLOR 1, 0, 0: SETCOLOR 0, 0, 8 EM 130 COLOR 3 : PL0T 0,40 JF 200 TRAP 200 : PRINT CHR$ (125) : POKE 764, 255 PJ 210 PRINT "Initial Position (ft) : "; OT 220 INPUT X0 VU 230 PRINT "Initial Velocity (ft/sec) : "; OB 240 INPUT V0 CK 300 PRINT CHR$ (125) : P0KE 752, 1 IN 310 T=0: X = X0 ZM 320 POKE 657, 2 : POKE 656,0 : PRINT "UNIFORM MOTION: X=X0+V0*T; JK 330 POKE 657, 2 : POKE 656, 2 : PRINT "X0="; X0;" ft"; HF 340 POKE 657, 2 : POKE 656, 3 : PRINT "V0="; V0;" ft/sec; BP 350 POKE 657, 30 : POKE 656, 2 : PRINT "ft"; NW 360 POKE 657, 30 : POKE 656,3 : PRINT "sec" YP 370 T = T + 0 . 1 : X = X0 + V0*T HB 380 XL = XP : XP = XP + V0*0.1 WY 390 IF XP > 159 THEN XP = XP - 159 : GOTO 390 NW 400 COLOR 0 : PLOT XL, 40 SD 410 COLOR 3 : PLOT XP, 40 420 POKE 657, 20 : POKE 656, 2 : PRINT "X="; INT (X*10) / 10; HQ 430 T$ = STR$ (T):IF INT (T) = T THEN T$ (LEN (T$)+ 1, LEN (T$) + 2) = ".0" UB 440 POKE 657, 20 : POKE 656, 3 : PRINT "T = "; T$; EH 460 IF PEEK (764) = 255 THEN 340 KU 470 XP= 0 : GOTO 100
ZZ 1000 REM PROGRAM LISTING #2 BZ 1010 REM FREELY FALLING BODY JR 1020 REM BY CHARLES CLARK QI 1025 REM (c) 1990, ANTIC PUBLISHING, IN C. MU 1030 G = 32 EZ 1040 OPEN #1,4,0, "K:" EW 1100 GRAPHICS 7 UZ 1110 SETCOLOR 4,12,6 : SETCOLOR 2,3,12 SZ 1120 SETCOLOR 1,0,0:SET COLOR 0,0,0 TS 1130 COLOR 1:PLOT 0,79:DRAWTO 159,79 XP 1140 FOR 1=8 TO 78 STEP 10:PL0T 0,I:DR AWTO 60,I:PL0T 98,I:DRAWT0 159,I:NEXT I PB 1150 PLOT 0,8=DROWTO 0,79:PL0T 159,8:D RAWTO 159,79 HR 1160 COLOR 1:PLOT 88,78 QY 1200 TRAP 1200:PRINT CHR$(125):POKE 75 2,1 VT 1210 PRINT "Initial Velocity (ft/sec):";: INPUT V0 FL 1220 PRINT "Initial Height (ft): ";:IN PUT Y0 SH 1230 TRAP 1300:COLOR 0:PLOT 80,78:COLD R 1:PLOT 80,78-Y0:FOR DELAY=0 TO 200:N EXT DELAY:YL=Y0 GG 1300 T=0.1 GD 1310 Y=Y0+V0*T-G*T*T/2 FM 1330 COLOR 0:TRAP 1340:PLOT 80,78-YL KC 1340 IF Y<=0 THEN T=T-0.1:GOTO 1380 HA 1350 COLOR 2:TRAP 1360:PLOT 80,78-Y LS 1360 FOR DELAY=0 TO 20:NEXT DELAY GK 1370 YL=Y:T=T+0.1:GOTO 1310 IN 1380 COLOR 2:PLOT 80,78 DM 1400 TRAP 40000:PRINT CHR$(125):POKE 752,1 WP 1410 YMAX=INT ((V0*V0/2/G+Y0)*100)/100 VC 1420 TMAX=INT (V0/G*100)/100 DF 1430 VTERM=INT((-SQR(V0*V0+2*G*Y0))*10 0)/100 WE 1440 TTERM=INT((VTERM-V0)/-G*100)/100 IM 1450 PRINT "MAX. Alt.: ";YMAX;" ft at ";tmax;" sec" YN 1460 PRINT "Terminal Velocity: ";VTERM BU 1470 PRINT "Time of Flight: ";TTERM;" SEC";" [RETURN]"; VT 1480 POKE 752,1:GET #1,R:PRINT CHR$(125) IF 1490 PRINT :PRINT ,"Again ? [Y/N] " KL 1500 GET #1,R ZS 1510 IF CHR$(R)="Y" THEN 1160 LQ 1520 IF CHR$(R)="N" THEN GRAPHICS 0:CL OSE #1:END PP 1530 GOTO 1500
BB 2000 REM PROGRAM LISTING #3 BT 2010 REM PROJECTILE TRAJECTORY JS 2020 REM BY CHARLES CLARK QJ 2025 REM (C)1990, ANTIC PUBLISHING, IN C. UX 2030G=32:X0=0 DH 2040 DEG FD 2050 OPEN #1, 4, 0, "K:" FN 2060 GRAPHICS 7 JC 2070 SETCOLOR 4, 0, 0:SETCOLOR 2,3,6 KT 2080 SETCOLOR 1,12,2:SETCOLOR 0,0,12 VO 2090 COLOR 2:FOR I=8 TO 78 STEP 10:PLOT 0,I:DRAWTO 159,I:NEXT I:PLOT 0,79:DR AWTO 159,79 DW 2095 FOR I=0 TO 159 STEP 10:PLOT I,8:D RAWTO I,78:NEXT I:PLOT 159,8:DRAWTO 159,78 TV 2100 TRAP 2100:PRINT CHR$(125):POKE 75 2,1:PRINT "Initial Velocity (ft/sec): ";"INPUT V0 FH 2110 PRINT "Initial Height (ft): ";:IN PUT Y0 BC 2120 PRINT "Angle of Departure (deg): ";:INPUT D:TRAP 40000 ZH 2130 S=SIN(D):C=COS(D) NT 2140 COLOR 1:TRAP 2200:PLOT X0,78-Y0:T RAP 40000 CQ 2200 T=0.15 BM 2210 X=X0+V0*C*T NJ 2220 Y=Y0+V0*S*T/2 TE 2250 TRAP 2275 AL 2260 IF Y<=0 THEN T=T-0.15:GOTO 2400 OF 2270 PLOT X,78-Y PZ 2275 FOR DELAY=0 TO 15:NEXT DELAY HY 2280 T=T+0.15:GOTO 2210 DN 2400 TRAP 40000:PRINT CHR$(125):POKE 752,1 WF 2410 TMAX=V0*S/G NV 2420 YMAX=Y0+V0*S*TMAX-G*TMAX*TMAX/2 IE 2430 TTERM=(-V0*S-SQR(VB*VB*S*S+2*G*Y0))/-G ZO 2432 XTERM=X0+V0*C*TTERM XB 2433 TRAP 2434:PLOT XTERM,78:TRAP 40000 CC 2434 VTERM-V0*S-G*TTERM JH 2440 PRINT "Max. Alt.: ";INT (YMAX*100) /100;" ft at ";INT (TMAX*100)/100;" sec" CY 2450 PRINT "range: ";INT (XTERM*100)/10 0;" ft at ";INT(TTERM*100)/100;" sec" LO 2452 PRINT :PRINT "TERM. Vel.:";INT (VTERM*100)/100; " ft/sec [RETURN]"; FT 2460 GET #1,R:PRINT CHR$(125) :POKE 752,1 RE 2470 PRINT :PRINT ,"Again ? [Y/N]" LI 2480 GET #1,R AI 2490 IF CHR$(R)="Y" THEN 2600 LL 2500 IF CHR$(R)="N" THEN GRAPHICS 0:CL OSE #1:END TJ 2510 GOTO 2488 TO 2600 PRINT CHR$(125):POKE 752,1 VA 2610 PRINT :PRINT " Erase trail ? [y/n]" KU 2620 GET #1,R AA 2630 IF CHR$(R)="Y" THEN 2060 NC 2640 IF CHR$(R)="N" THEN 2100 RV 2650 GOTO 2620
CD 3000 REM PROGRAM LISTING #4 JQ 3010 REM HARMONIC MOTION JT 3020 REM BY CHARLES CLARK QK 3025 REM (C)1990,ANTIC PUBLISHING, IN C. EZ 3030 K=100 EO 3040 RAD CV 3050 DIM T$(10),S$(5) EY 3100 GRAPHICS 7 PI 3110 SETCOLOR 4,10,2:SETCOLOR 2,2,12 UH 3120 SETCOLOR 1,0,0:SETCOLOR 0,1,0 SI 3130 TRAP 3130:PRINT CHR$(125):POKE 752,1:POKE 764,255 IN 3140 COLOR 3:PLOT 80,0:DRAWTO 80,38 KT 3150 PLOT 80,42:DRAWTO 80,80 AV 3160 FOR I=0 TO 150 STEP 10 IL 3170 PLOT I,42:DRAWTO I,50:NEXT I GZ 3200 PRINT "Initial Position (cm): "; RB 3210 INPUT 50 QK 3220 PRINT "Mass (g) : "; QN 3230 INPUT M TJ 3300 PRINT CHR$ (125) : POKE 752, 1 OY 3310 T=0 : S=S0 : SKM=SQR(K/M) : P=INT (6.284*SKM*100+0.5)/100 YL 3312 POKE 657, 2 : POKE 656, 0 : PRINT "HAKM ONIC MOTION: S=S0*COS(T*SQR(K/M)) TE 3320 POKE 657, 2:POKE 656,1 : PRINT "AMPLitude=; 50;CM; Period=;P; Sec; TA 3330 POKE 657, 2:POKE 656,2 : PRINT "Mass =;M;gm; SU 3340 POKE 657, 2:POKE 656,3 : PRINT "K=; K; AG 3342 POKE 657, 30:POKE 656,2 : PRINT "cm; EQ 3344 POKE 657, 30:POKE 656,3 : PRINT "sec; SR 3348 COLOR 3:TRAP 3350 : PLOT 50+80,40 HZ 3350 T=T+0.1 : SL=5 VS 3360 S=S0*COS(T*SKM) TU 3370 TRAP 3392 YN 3380 COLOR 0:PLOT SL + 80,40 IJ 3390 COLOR 3:PLOT S+80,40 BH 3392 S$=" " : S$=STR$(S) PM 3394 IF S>0 THEN S$(5)= " GH 3400 POKE 657,20 : POKE 656,2 : PRINT "S=; S$; CQ 3405 T$= " : T$=STR$(T) KJ 3410 POKE 657,20:POKE 656,3:PRINT "T=;T$ AG 3500 IF PEEK(764) = 255 THEN 3350 OL 3510 GOTO 3100
DF 4000 REM PROGRAM LISTING #5 DC 4010 REM GRAVITATIONAL ATTRACTION JU 4020 REM BY CHARLES CLARK QL 4020 REM BY (c)1900, ANTIC PUBLISHING, INC. QD 4030 K=75 FJ 4040 GRAPHICS 7 LC 4050 SETCOLOR 4,0,0:SETCOLOR 2,10,0 MM 4060 SETCOLOR 1,7,10:SETCOLOR 2,10,0 YR 4070 COLOR 2:PLOT 80,40 AZ 4080 PRINT CHR$(125):POKE 752,1:TRAP 4080 VD 4090 PRINT "Initial x coordinate: ";:INPUT X WR 4100 PRINT "Initial y coordinate: ";:INPUT Y VH 4110 COLOR 1:PLOT 80+X, 40-Y RC 4120 PRINT CHR$(125):POKE 752,1:TRAP 4110 QE 4130 PRINT "Initial x acceleration: ";:INPUT AX SX 4140 PRINT "Initial y acceleration: ";:INPUT AY TV 4150 PRINT CHR$ (125):POKE 752,1 FU 4160 R=SQR(X*X+Y*Y):F=K/(R*R*R) RB 4170 AX=AX-(X*F) : AY=AY-(Y*F) VE 4180 TRAP 4190:COLOR 0:PLOT 80+X,40-Y; TRAP 40000 KW 4190 X=X+AX : Y=Y+AY RQ 4200 TRAP 4210:COLOR 1:PLOT 80+X,40-Y: TRAP 40000 RR 4210 GOTO 4160