Game Development Reference
In-Depth Information
Then, we initialize our first physics rigid body. We add a plane to our physics system.
We instantiate the jigLib.Plane object, and set its location of y to 0 . The important
thing to understand is that the jigLib.Plane object by default is aligned to the XY
plane. However, we want to align it to the XZ plane. Hence, we rotate our plane by 90
degrees around the x axis. The jigLib.Plane rigid body has many applications but
we want to use it as a terrain. Hence, we set it as a static body using the RigidBody
class's function, set_movable(false) , and then we add it to our physics system.
The animate() function is explained in the following code:
function animate(){
...
var updateIterations = Math.floor(elapsedTime /
MINIMUM_FRAME_RATE);
system.integrate(elapsedTime / 1000 );
while(updateIterations> 0){
drawScene();
updateIterations -= 1;
}
...
}
We invoke the system.integrate() function from our animate() function along
with the drawScene() function call. Basically, the system.integrate() function is
our simulation update. It updates our physics system variables with the elapsed time
(time-based update). The loadStageObject function is as follows:
function
loadStageObject(url,location,rotationX,rotationY,rotationZ){
...
else if(nameOfObject=="Sphere"){
stageObject=new Sphere();
stageObject.system=system;
stageObject.initializePhysics();
stageObject.visible=true;
sphere=stageObject;
}
...
}
Finally, we load our sphere's JSON object and instantiate our Sphere class. We assign
the physics system variable to the sphere's class variable before initializing the rigid
body in the initializePhysics call.
When we view the result in the browser, the sphere will fall on the terrain and
bounce on it. The bounce can be controlled by the restitution factor (cor) which is a
property defined in the RigidBody class. The complete visual effect is because we
update the position of our sphere geometry along the position of the rigid body.
 
Search WWH ::




Custom Search