The first section of the script defines variables
initializes thier values. When the movie loads
these values are set as the initial values of
the variables and constants used in the rest
of the script. Some of the "var" are constant
such as "floor" and some change thier value (e.g."Vy")
as the sections of the script are processed.
var gravity = 5;
var elasticity = 0.85;
var angle = 15;
var floor = 250;
var Vy = 0;
var Vx = 15;
This sets the initial
position of the movie clip "ball_mc" to the
coordinates (20,20) on stage.
ball_mc._x = 20;
ball_mc._y = 20;
The nest section creates a function that is called each time the movie clip enters
a new frame.
ball_mc.onEnterFrame = function() {
This sets the values of the vertical velocity (Vy) to add acceleration
due to the force of gravity (gravity) to the previous value of Vy each time the
function is called.
Vy = Vy + gravity;
The value of the vertical velocity Vy is then added to
the vertical position of the movie clip "ball_mc" (as
refered to by the function). Another way of writing this would be: "this._y
= this._y + Vy/4". It has the same effect but is just less conscice. In this
way the position is re-evaluated each time the movie enters a new frame changing
by the amount "Vy" each time which is itself changin by the amount "gravity".
this._y += Vy/4;
Note: Dividing the velocity by 4 is just a simple scaling tweak used to adjust
the simulated motion.
The horizontal velocity Vx which has no acceleration
and is set to 15 is added to the horizontal position
of "ball_mc" to create horizontal motion.
this._x += Vx;
The movie clip is then
roated by the amount "angle" each time a new
frame is entered creating a more natural roll
motion to the ball.
this._rotation += angle;
The ball must hit the floor and change direction.
To do this a simple conditional statement is set
up to control the direction of the velocity of
the ball. If the vertical position of the ball
is greater than the vertical position of the floor
(175 pixels) the starting poition of the ball is
reset to equal the value floor then the direction
of the velocity is reversed. However each time
the ball collides with the floor it must not only
change direction but also reduce its starting velocity
by a fixed amount to creae the required dampening
effect (due to the simulated elastic compressibility
of the ball). To achiev this the vertical velociuty
Vy is multiplied by negative 1 and the constant
"elasticity".
if (this._y > floor) {
this._y = floor;
Vy = -Vy*elasticity;
}
To create the effect of the ball being trapped
in a box with walls at the edge of the stage
anothert conditional statement is created to
call script each time the balls horizonal position
exceeds the horizontal position of the walls.
If the position of "ball_mc"
is greater than the stage width minus the width
of the movie clip "ball_mc" or if the psoition
of ball_mc" is less
than the width of "ball_mc" ( the calcualted
edges of the stage when the size of the ball
is taken in account) the velocity of the movie
clip is reversed.
if ((this._x > (Stage.width-this._width/2))||(this._x <
this._width/2)){
Reversing the horizontal
velocity would just make the ball bounce left
and right forever to create a more relaistic
motion each time a wall is hit the starting
velocity of the ball is reduced by the constant
factor "elasticity" causing a dampening effect
on the horizontal motion.
Vx = -Vx*elasticity;
In addition to vertical
and horizontal, the rotational motion of the
ball must also be modified each time the ball
collided with a wall. The rotational velocity
is reduaced as with the the other velocities
usign the the constant factor "elasticity". It
is also reversed each time it hits either side
of the stage to give a simple friction and angular
momentum effect.
angle = -angle*elasticity
Note: The motion
of the ball towards the end is "jittery".
This is due to the rounding of values used in the
calculation of the position of the ball. Over time
erros can build up causing the ball to continue
to move spontaniously.
This simulation of
the motion of a bouncing ball is not physically
accurate. Instead it tries to mimic the characteristics
of a bouncing ball using simple actionscript
animation techniques. To learn how to create
a more physically accurate animation with
actionscript consistent with the laws and
conventions of equations of motion
see [1D Boucing Ball Using "Real" Physics] & [2D
Bouncing Ball Using "Real" Physics].