# Bodies, Rest, and Motion

July 13, 1999
by Raman Pfaff

#### Newton's Laws and Euler's Method

When I first started playing with Macromedia Director I found it an amazing tool for displaying the motion of particles2 which are governed by mathematical equations, and I created a web site with many demonstrations that I regularly use when teaching introductory physics in the classroom. As other Director users have stumbled on to the site, the most common question I get is as follows:

How do you control the motion of the golf ball? I have very little math background, and don't know much about physics.

If you had taken physics with me you might have learned the answer to the questions, but luckily we have the web, so I'll give you a quick introduction to the math, physics, and Lingo necessary to simulate the real world motion of a football tossed through the air, a feather being dropped, or a golf ball being launched from the tee. I'd like to point out that there is no way I could summarize a full semester of physics in this short article (I can hear some of you breathing a sigh of relief now), and I will focus on a non-calculus version of physics, and use the simplest approximation possible to simulate real world motion.

#### Where fore art thou?

One of the first things that people do when learning to control motion with Director is to use Lingo to move the particle a fixed number of pixels with every exitFrame. A typical example of this would be

on exitFrame
set the locH of sprite 1 = 5 + the locH of sprite 1
end

This code would move the sprite 5 pixels to the right on every exitFrame. The particle would never move up or down, just to the right. If we wanted to move the particle up or down we would have used locV rather than locH. This small piece of code has already introduced us to two of the three terms that you we'll need to know about - position and velocity. In Director, the position of a particle is called the location (or loc). If we have a sprite on the stage you could type into the message window and get this response

put the loc of sprite 1
-- point(35,85)
put the locH of sprite 1
-- 35
put the locV of sprite 1
-- 85

The sprites have a horizontal and vertical position. In physics we prefer to have a well-defined coordinate system (not just horizontal and vertical), and it is common to call the horizontal axis the x-axis, and the vertical axis the y-axis, so to describe the position of a particle I would refer to point(x,y). It should be mentioned that the position is always relative to an origin. In math the origin generally has a zero for its x and y values, and in Director the origin is the upper left corner of the stage - point(0,0).

#### Warp Speed.

In the earlier code we moved our particle 5 pixels to the right every exitFrame. Average velocity is defined as the change in position divided by the change in time. In this case our velocity would be

(although the units would certainly confuse most physics teachers). When we discussed the position of the particle we used the x and the y locations, and the same must be done for the velocity. Since our particle was moving horizontally at 5 pixels/exitFrame, this would correspond to Vx (the component of velocity in the x direction). For this particle Vy = 0 (since it was not moving up or down). Using our definition of velocity we can now say:

where the little triangular symbol is the greek letter delta. It is used to represent "change in," so y means change in y, and t means change in time. (Are those physics memories creeping back in yet...numbered equations, boring formulae, etc.)

#### Step on it!

Position and velocity have now been introduced, and in a classroom those concepts are generally understood rather well, but confusion can often set in when acceleration is introduced. Average acceleration is defined as the change in velocity divided by the change in time, and just as we did with the position and velocity, we must deal with the x and y acceleration individually. In mathematical terms we could write

where once again the little symbols represent the "change in," and in this case we are looking at the change in velocity in the x direction and the y direction.

#### I do not think you realize the gravity of your situation. - Spock

Lets now talk about this in terms of throwing a ball straight into the air. You initially throw the ball up and it has a certain velocity upward. Take a moment to visualize the motion in your mind. Did the ball change direction? Did the ball change its velocity during the flight? Was the ball ever motionless? The answer to all three is yes. Gravity is what causes the ball to return to the ground because it causes things to accelerate towards the surface of the Earth. Gravity only acts in one direction (the vertical direction, the y-axis, etc.) and can be considered a constant value. If we rearrange the y-component of Equations 2 and 3 we can say

where Vy is the amount of change in the y-velocity during the time t. When speaking we would say the ball is initially moving up, but when you catch it later, the ball was moving down. In physics we choose a direction to be positive, and when the ball is moving that way it has a positive direction, and opposite of that would be a negative direction. The position, velocity and acceleration must be defined according to the same schemes. With Direction, the x-value (locH) increases as you move to the right, so we will call that the positive direction, but the y-value (locV) increases as you move to down (towards the bottom part of your screen).

We will define down to be the positive y direction. With those directions in mind, when I throw a ball up, the initial y-velocity is negative (we'll say -50 m/s), and Ay is positive (lets say +9.8 m/s/s). Rather than considering our chunk of time to be an exitFrame, we'll now choose a value of 0.1 second. Lets say our ball was initially at the location of point(40,100) on the stage.

With all those initial conditions, after one tenth of a second I would find that

Vy = 9.8 * 0.1 = 0.98 m/s and
y = -50 * 0.1 = -5 m

The new velocity and position after the small time interval are found to be

Vy = Vy + Vy = -50 + 0.98 = -49.02 m/s and
y = y + y = 100 + (-5) = 95 m

from which you can see that the ball is now moving more slowly than it was initially, and it has moved upward on the stage (although the y-value has decreased). Spend a few moments watching the bouncing ball.

The approximation I just showed is known as the Euler's method3. In the real world, time is a continuous event, and does not occur in small chunks of t's. For real motion we use the equations above in the limit where t goes to zero. Using a value of t = 0.0000000000001 to do calculations is not reasonable, since the code would plug and chug for a long time before anything appeared to change on the stage (and round-off errors would accumulate). On the other hand, assuming a t of 10 seconds would not be reasonable if you only expect the ball to be in the air for 2 seconds. Finding a reasonable value to balance calculation speed vs. accuracy is often a challenge.

#### Enough Physics...show me the Lingo!

We now have a wealth of knowledge related to simulating real world motion with Euler's method, so let's try to put this in terms of a behavior. Since we have talked about position, velocity, acceleration, and small time increments I will define all these to be properties

property spriteNum, x, y, Vx, Vy, Ax, Ay, dTime

When the sprite starts up, I want it to know what its position is, so I add this to the beginSprite handler

on beginSprite me
set x = float(the locH of sprite spriteNum)
set y = float(the locV of sprite spriteNum)
end

You'll notice that I used the float() command for the x and y values. When simulating motion, it is often difficult to survive in the world of integers. The floating point variables will certainly slow the code, but when trying to simulate actual motion of particle systems it is necessary. In the behavior, the user selects the initial velocities and accelerations, so the sprite will have those values associated with it.

For Euler's method (recall Equations 2, 3, and 4) I place the calculations in the exitFrame handler, and include both x and y directions:

on exitFrame me
-- the small d represents the "delta"
-- (change) from the equations

-- find the changes in velocity
set dVx = Ax * dTime
set dVy = Ay * dTime

-- find the changes in position
set dx = Vx * dTime
set dy = Vy * dTime

-- set the new velocities and position
set Vx = Vx + dVx
set Vy = Vy + dVy
set x = x + dx
set y = y + dy

-- and now move the sprite to the
-- calculated x and y position
set the loc of sprite spriteNum = point(x,y)

end

The three particles on this demonstration have different initial conditions. Click on them and notice how the motion differs.

#### Don't drag me down...

In the previous section we had a constant acceleration - that due to gravity. The vast majority of introductory physics problems assume gravity is the only acceleration on the particle, but in the real world we have this quantity called air which tends to oppose the direction of motion. Think about what happens when you stick your hand out the window of a car as it travels down the road. If you are travelling with a velocity of 5 miles per hour (mph) and you stick your hand out the window, you notice a bit of a tug on your hand caused by the air. That tug you feel is called a force (which is proportional to acceleration).

When you are moving with a velocity of 70 mph that tug on your hand becomes much larger. If a particle is in a vacuum (no air), we know that Ay = g (where g is the acceleration due to gravity: accepted value of 9.8 m/s/s), and Ax = 0, since gravity acts downward. If air is added to the situation, the added acceleration must be opposite the direction of motion (in other words, be opposite the direction of the velocity), and is commonly written as

where c is a constant value that you can choose. The larger the value of c, the larger the "drag force." The negative sign in front of the cV terms are due to the fact that the drag force must be opposite the direction of motion.

To incorporate the drag forces into my Lingo, I decide to add two properties to my behavior and now have

property spriteNum, x, y, Vx, Vy, Ax, Ay
property dTime, gravity, dragC

and since the acceleration will change over time (since the velocity will change), I add the lines to the exitFrame handler to calculate the new acceleration as time passes:

on exitFrame me
-- the small d represents the "delta"
-- (change) from the equations
set Ax = -dragC * Vx
set Ay = gravity - dragC * Vy

-- find the changes in velocity
-- the rest of the code follows from
-- the earlier example...

end

To easily demonstrate this fact, click start to drop the feather and the hammer at the same time. The only difference between the two particles is the drag coefficient (dragC) which depends on many things; including (but not limited to) the mass and size of the particle, and the density of air.

#### Applications and Final Thoughts

This simulation used Euler's method to perform the calculations. There are other methods which are more precise, but this one is elegant and simple. This code can be used to describe the motion of a sky diver (the dragC value could be dynamically changed to simulate the opening of the parachute), or the projectile motion of a missile (please don't sink my battleship). In free code (download below) I have added several other features, such as constraining the motion to a rectangle, and bouncing the particles from the interior of the rectangle 4. See references for additional information on that subject. I've also added a property so that you can start the motion via Lingo, and a "coefficient of elasticity" parameter which determines the percentage of velocity you lose when you make contact with a wall.

For additional methods that can be used to more accurately simulate motion, feel free to search the web for the keywords 'Runge-Kutta' or 'Euler-Richardson' and see what pops up.

Sample movies are available for download in Mac or PC format.

References

1 The title of a movie made in 1993 starring Bridget Fonda, Eric Stoltz, Phoebe Cates, and Tim Roth. It is unfortunate that when you study physics your first thought is of Newton's laws when you hear the title.

2 In physics we like to treat everything as a small spherical object, or particle. Consider a particle to be anything that you want moving on your stage; a cow, a UFO, a hammer, a feather, etc.

3 Information about this method can be found in most any computational methods book, or introductory physics book that includes discussion of computational methods.

4 A Multimedia Handyman article on randomly bouncing sprites

Raman Pfaff earned his Ph.D. in nuclear physics, spent some time as a professor, but is now having fun as Director of Multimedia Development for 3MP in Charlottesville, VA. In a recent dream sequence he was stuck in an infinite repeat loop but decided to see a movie, grab a latte, and watch a Redwings game instead. He can be reached at pfaff@explorescience.com. Feel free to learn a bit more at http://www.explorescience.com/ and http://www.exploremath.com/

Copyright 1997-2018, Director Online. Article content copyright by respective authors.