Time for action – bouncing ball
Open
ch5_BouncingBalls.html
in your HTML5-enabled Internet browser.The orbiting camera is activated by default. Move the camera and you will see how all the objects adjust to the global transform (camera) and yet they keep bouncing according to its local transform (bouncing ball).
Let's explain here a little bit more in detail how we keep track of each ball.
First of all let's define some global variables and constants:
var ball = []; //Each element of this array is a ball var BALL_GRAVITY = 9.8; //Earth acceleration 9.8 m/s2 var NUM_BALLS = 50; //Number of balls in this simulation
Next, we need to initialize the ball array. We use a for loop in the
load
function to achieve it:for (var i=0;i<NUM_BALLS;i++){ ball.push(new BouncingBall()); Scene.loadObject('models/geometry/ball.json','ball'+i); }
The
BouncingBall
function initializes the simulation variables for each ball in the ball array. One of this attributes is the position, which...