 170
|
Chapter 5, 3D and Physics
#38 Use Acceleration to Simulate Gravity and Friction
HACK
Final Thoughts
Although basic, our engine is a good base upon which to build a number of
more advanced 3D engines:
By joining up our points via lines, we can build a 3D vector engine or
wireframe viewer
[Hack #85].
By adding z-buffering, we can create a more compelling effect that more
fully represents 3D depth (and allows us to, say, use spheres with specu-
lar highlights instead of black dots).
Real-time 3D is one of the most processor-intensive things you can do with
the Flash Player. As seen here, keeping it simple can produce fast effects.
HACK
#38
Use Acceleration to Simulate Gravity and
Friction Hack #38
Many real-world phenomena, such as gravity and friction, affect the velocity
of objects over time. Model such changes by a simple acceleration equation
to create realistic motion under simulated physical conditions.
Some physical effects are hard to get right in scripted animation unless you
know the underlying math. Acceleration calculations are easier than they
might appear. Like all forces acting on masses, gravity and friction cause
acceleration (or deceleration). When you are performing iterative calcula-
tions (which you are always doing when you are animating sprites over
time), the math works out to be surprisingly easy.
When modeling real-world movement, you often have an accelerating force
and a decelerating force acting on the same body (such as gravity and air
friction acting on a falling body). If the forces are balanced (such as when
the thrust of an airplane counteracts the air resistance), the velocity of the
object is constant (acceleration is zero). The trick is simply adding the forces
together to determine the overall force on the object.
A force such as gravity provides constant acceleration, meaning that the
velocity of the object changes over time (just as your car speeds up if you
provide enough gas to accelerate). The velocity of an object at any time is
equal to its existing velocity, plus the effect of acceleration over the time
interval, such as:
newV = oldV + (acceleration * time)
Likewise, the position of an object at any time is equal to its old position,
plus the effect of its velocity over the time interval, such as:
newPos = oldPos + (newV * time) Use Acceleration to Simulate Gravity and Friction #38
Chapter 5, 3D and Physics
|
171
HACK
If we recalculate the position and velocity often enough that the speed
doesn’t change during each iteration, and we use 1 as the time interval so the
units drop out, we can simplify the position equation as:
newPos = oldPos + newV
In words, this means, “To calculate the new position, take the old position
and add the current velocity.” For example, if I drive at 60 miles per hour
(mph) for one hour, I’ve traveled 60 miles from my original position. There-
fore, in each frame of the animation, we add a small amount to the position
in the direction of the velocity vector.
At the end of an interval, we recalculate the velocity to account for accelera-
tion. The velocity calculation can likewise be simplified as:
newV = oldV + acceleration
In words, this means, “To calculate the current velocity, take the old veloc-
ity and add the acceleration.” For example, if I’m driving at 60 mph and I
accelerate by 10 mph per second for one second, I’m now traveling at 70
mph. Therefore, in each frame of the animation, we add a small amount to
the velocity in the direction of the acceleration vector (usually pointing
down in the case of gravity).
Acceleration due to Earth’s gravity is approximately 32 feet per second
squared (or 9.8 meters per second squared), but if you aren’t providing an
accurate physics calculation, you can use any constant value for the accelera-
tion that makes your animation run at the rate you’d like.
Resting friction (the friction acting on a body at rest) tends to be greater
than rolling friction (the friction acting on, say, a rolling ball). Air friction
generally increases in proportion to the object’s speed, so we use a simple
coefficient of friction to approximate the resulting force as a fraction of the
current velocity.
The following code generates a number of particles
[Hack #33] and animates
them falling under the acceleration of gravity and being resisted by air friction:
function fall( ) {
// Add acceleration due to gravity
this.speedY += GRAVITY;
// Reduce the speed due to friction
this.speedY *= FRICTION;
// Assume both forces work exclusively in the Y direction
this._y += this.speedY;
// Make the clip bounce up when it hits the "floor" (a line)
if (this._y > 400) {
this._y = 400;
this.speedY = -this.speedY * ELASTICITY;
}
}

Get Flash Hacks now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.