Game Development Reference
In-Depth Information
The
initializePhysics
function initializes a physics shape, a sphere, by creating
a
jigLib.JSphere
object. The object takes two parameters: the skin object and
the radius of the sphere. The skin object is a reference to the object that holds the
orientation of the geometric shape, which in our case is a sphere. We are not using
a skin parameter in our code. We just pass the radius (
20
) to set up collision points.
Then, we set the mass of our object so that gravity can accelerate the falling mass.
Then, we set the location of the object in reference to the world space using the
rigidBody.moveTo
function. The location of the object in the physics world in
JigLib is defined by an array of [
x
,
y
,
z
] values, and the
jigLib.Vector3DUtil.
create(0,100,120)
function returns an array [0,100,120]. We then add the object to
the physics system (
this.system.addBody(this.rigidBody);
). Note that unless
the object is added to the physics system, the objects properties will not change. The
class variables,
rigidBody
and
system
, are inherited from the
StageObject
class. The
system variable holds the reference to
jigLib.PhysicsSystem
initialized in the main
control code.
The following code connects to the physics system with the geometry—the last
missing piece:
Sphere.prototype.update=function() {
if(this.rigidBody){
var pos = this.rigidBody.get_currentState().position;
this.location=vec3.fromValues(pos[0], pos[1], pos[2]);
mat4.identity(this.modelMatrix);
mat4.translate(this.modelMatrix,this.modelMatrix,
this.location);
mat4.scale(this.modelMatrix,this.modelMatrix,vec3.
fromValues(10,10,10));
} else{
mat4.identity(this.modelMatrix);
mat4.translate(this.modelMatrix,this.modelMatrix,
this.location);
mat4.rotateX(this.modelMatrix,this.modelMatrix,
this.rotationX);
mat4.rotateY(this.modelMatrix,this.modelMatrix,
this.rotationY);
mat4.rotateZ(this.modelMatrix,this.modelMatrix,
this.rotationZ);
mat4.scale(this.modelMatrix,this.modelMatrix,
vec3.fromValues(10,10,10));
}
}