ARTICLE : Simple Math For Moving Objects
by Tom Nally

Home

Maintaining checkbox states
Tidbits from the community
Liberty Basic 4
New Alternate Forum
Using the Tsunami Database
Wizard Framework
Links to LB Sites
Update on 10th Anniversary Contest
Extracting Icons And Saving Them As Bitmaps
Applying Symbolic Logic
QuadClicks
Simple Math For Moving Objects
Event Driven Programming - Part 2
The Beginners Series - Part 1

 

It goes without saying that Liberty BASIC is a language that is rich in graphics capabilities. If we use graphics for games, simulations, or any kind of miscellaneous amusement or diversion, we will probably be writing code to move our graphics objects around the screen. Fortunately, LB's extensive set of sprite commands allows programmers to do this easily.

The intent of this article is to discuss the basic equations of motion of an object. I'll start off with a discussion of objects moving at a constant velocity, and end up with a discussion of acceleration. (The ideas and equations in this article are also important to understanding projectile motion, too, but we'll have to leave that discussion for another time.)

An object has velocity if its position changes during a time interval. In fact, the average velocity of an object is the object's change in position during a particular time interval, divided by the length of that time interval.

Equation [F-1] given below is the general expression for average velocity using variables. Say that an object's position at time t1 is x1. Say also that it's position at time t2 is x2. Given that, the object's average velocity is defined as

v = (x2 - x1) / (t2 - t1) ----Equation [F-1]

Note that if x1 and x2 are given in units of feet, and t1 and t2 are given in units of seconds, then the velocity will be in ft/sec. At this point, one might ask why the position differential is defined as (x2 - x1) instead of (x1 - x2), and why is the time interval defined as (t2 - t1) rather than (t1 - t2)? My understanding is that differentials are usually defined as the final value minus the initial value, instead of vice versa. At least that was how it was explained to me in college physics some 20 years ago or so.

Let's do an example involving real numbers. Say that t1 = 15 seconds. At time t1 a car's position is 352 feet. Say also that t2 = 47 seconds. At time t2, the same car's position is 778 feet. The car's average velocity during that time period would be

Example 1:
----------

v = (778ft - 352ft) / (47sec - 15sec)
v = 426ft/32sec
v = 13.3 ft/sec

In the example above, the time interval under consideration (t2 - t1) was 32 seconds. As the time interval under consideration gets shorter and shorter, we are able to report the object's velocity much more frequently. An object's "instantaneous velocity" is the object's velocity as the time interval under consideration shrinks to near zero. In layman's language, instantaneous velocity means "the object's velocity at a particular instant". ` The readout from your car's speedometer is more-or-less an indication of "instantaneous velocity".

Just as velocity is defined as the change in an object's position during a time interval, acceleration is defined as the change in an object's velocity during a particular time interval.

For example, if at time t5 an object had a velocity of v5, and at time t6 the same object had a velocity of v6, then the object's average acceleration during the time interval between t5 and t6 would be defined in Equation [F-2] below:

a = (v6 - v5) / (t6 - t5) ----Equation [F-2]

So, what are the units of the quantity known as acceleration? Well, if velocity is given in ft/sec, and time is given in seconds, then the units of acceleration are ft/sec^2 in this case. Actually acceleration can be expressed in any number of units, as long as it consists of a length unit divided by a time unit squared.

Let's do another example with real numbers. Say that t5 = 54 seconds, and the car's velocity at t5 is v5 = 21ft/sec. Say also that t6 = 68 seconds, and the car's velocity at t6 is v6 = 14ft/sec. The car's average acceleration during that time interval would be

Example 2:
----------

a = (14ft/sec - 21ft/sec) / (68sec - 54sec)
a = (-7ft/sec) / (14sec)
a = -0.5ft/sec^2

Note above that the "acceleration" has a negative value. When acceleration is negative, we call that "deceleration", and obviously it means that the car is slowing down. How rapidly is it slowing down? Well, our result essentially tells us that every second, the car is losing one-half foot/sec of velocity. Or, sometimes we would say it this way: "minus 1/2 foot per second per second".

The two simple equations for velocity and accerlation help us define those quantities, but we need to know how we can use them to calculate the position of an object at any given time, t.

The basic equation for the position of an object undergoing acceleration is not too complicated. We will call this Equation, [F-3]:

x = x0 + v0*t + (1/2)*a*t^2 ----Equation [F-3]

where,

x is the object's position at time t,
x0 is the object's postion at time 0,
V0 is the object's velocity at time 0,
a is the acceleration of the object (constant for this discussion), and
t is the moment in time where we seek the object's position.

Note that if x0 = 0 and v0 = 0, then the equation simplifies to

x = (1/2)*a*t^2

Likewise, if x0 = 0 and a = 0, the equation simplifies to

x = v0*t

This latter form is identical to the distance formula for a moving object that we learned back in grammar school: d = r*t, for distance = rate * time.

Okay, let's use our Equation in a real-world example.

Example 3:
----------
Given:
x0 = 25
v0 = 2ft/sec
a = 0.2ft/sec^2

Find the object's location at t = 55 seconds?

Answer:

x = 25 + (2ft/sec)*55 + (1/2)*(0.2ft/sec^2)*(55sec)^2
x = 437.5 feet

In other words, 437.5 feet is the location of the object if we track it starting at an initial location of 25 feet, and an initial velocity of 2ft/sec, while it is accelerating at 0.2ft/sec^2.

So, Example 3 shows us how to find the position of an accelerating object at any given time, t, as long as we know the object's position at time=0 (which we call that "x0") and its velocity at time=0 (which we call v0). How would I use this information in a program to show, say, an accelerating sprite?

The way I would do this is to put Equation [F-3] in some kind of a loop. The loop control variable (or "index" variable) might be "t", and the object's position, x, would be calculated each time through the loop as t changes. Here is a little code snippet below which would accomplish this.

'In this code snippet, the window name
'is presumed to be #main, while the
'graphic box is presumed to be gbox1.
'The sprite has been given the name "bluesquare"

x0 = 15    'x position at time=0
v0 = 5     'velocity in the x-direction at time=0
a  = 0.75  'acceleration in the x-direction

for t = 0 to 20 step 0.25
	x = x0 + v0*t + (1/2)*a*t^2
	print #main.gbox1, "spritexy bluesquare "; x; " 50"
        print #main.gbox1, "drawsprites"
next t

'end of snippet

In the snippet above, the position of bluesquare is calculated every one-fourth of a second starting at time = 0 seconds and ending at time = 20 seconds. If this snippet was inserted into a program, the programmer would indeed see the bluesquare accelerating from left to right across the GraphicBox. (Actually, the same code could also be used to move the bluesquare at constant velocity across the GraphicBox. All the user would have to do is to set a = 0.)

Additional helpful information about the motion of the object can be derived from Equation [F-3]. Those who have taken the first calculus course may remember that velocity is the derivative of the position equation with respect to time, or

v = dx/dt

The derivative of Equation [F-3] with respect to "t" is Equation [F-4] shown below.

v = v0 + a*t ----Equation [F-4]

What equation [F-4] gives us is the instantaneous velocity of the object at any time, t. For instance, in the code snippet provided above, say that in addition to moving the bluesquare across the GraphicBox, we wanted to report the bluesquare's velocity at each time interval when it's position is being calculated. We could do that by adding two lines inside the loop:

'This revised snippet assumes that a TextBox
'has been defined, and has been given the
'name txtSpeed.

for t = 0 to 20 step 0.25
	x = x0 + v0*t + (1/2)*a*t^2
	print #main.gbox1, "spritexy bluesquare "; x; " 50"
        print #main.gbox1, "drawsprites"
	v = v0 + a*t
	print #main.txtSpeed, "Velocity = "; v; " ft/sec"
next t

'end of snippet

To provide a demonstration of what acceleration and deceleration look like, I've provided a little demo program called "AccDonuts2.bas" (and "AccDonuts2.tkn"), with associated bitmap files and wave files [ed: you will find these packed in the zip file called donuts.zip in the newsletter archive]. This demo features a donut delivery van which accelerates for 200 feet, then runs at constant speed for 200 feet, then decelerates for 200 feet. When you run the program for the first time, set the acceleration of the donut van to 10ft/sec^2. On subsequent runs, adjust the acceleration up or down as desired.

Because this program is not automatically sensitive to the different speeds of our computers, the program provides a method--albeit imperfect--for the user to manually adjust the speed of the demo. Click the button called "Global System Speed...". Users with computers that operate around 300mHz or less (like mine) will probably want to select the fastest setting. If your computer operates at 1gHz or so, you may want to try the slower settings.

Finally, here are two other things to know if you use Equations [F-1] through [F-4] to program moving objects:

1) I framed this entire discussion as if our object was moving in a horizontal direction. However, there is no reason why Equations [F-1] through [F-4] cannot apply to the vertical direction, too.

2) If your initial velocity ("v0") and your acceleration ("a") both have the same sign (both positive or both negative), then the programmed object will increase its velocity in the same direction as its initial velocity. However, if "v0" and "a" are opposite in sign, then the object will slow down and eventually reverse course, like a ball tossed into the air.


Happy programming!

Home

Maintaining checkbox states
Tidbits from the community
Liberty Basic 4
New Alternate Forum
Using the Tsunami Database
Wizard Framework
Links to LB Sites
Update on 10th Anniversary Contest
Extracting Icons And Saving Them As Bitmaps
Applying Symbolic Logic
QuadClicks
Simple Math For Moving Objects
Event Driven Programming - Part 2
The Beginners Series - Part 1