Game Development Reference
In-Depth Information
The preceding function,
initializePhysics
, takes a Boolean parameter sphere.
If we pass sphere as true, then a
JSphere
collider is initialized; otherwise, a
JBox
collider is initialized. We set the mass of the object equivalent to its volume (radius
for
JSphere
and
width * depth * height
for
JBox
). We then move the rigid body to
the location of the
StageObject
class and also orient it in the
StageObject
class's
direction. Note that we have declared all objects as static bodies (
this.rigidBody.
set_movable(false);
).
The last change is added to our
updateMatrix
function. The
updateMatrix
function
now needs to set the orientation and position of the
StageObject
class to the
position and orientation of the
rigidBody
object. The following code only considers
the
position
and
orientation
values of the
rigidBody
object when the rigid
body is dynamic; otherwise, it sets the values from the
position
and
quaternion
parameters of the class. Our
updateMatrix
function is as follows:
StageObject.prototype.updateMatrix=function () {
if(this.rigidBody&&this.rigidBody.get_movable()){
var pos=this.rigidBody.get_currentState().position;
this.position=vec3.fromValues(pos[0],pos[1],pos[2]);
mat4.copy(this.modelMatrix,this.rigidBody.
get_currentState()._orientation.glmatrix);
}else{
mat4.identity(this.modelMatrix);
mat4.fromQuat(this.modelMatrix,this.quaternion);
}
mat4.scale(this.modelMatrix,this.modelMatrix,this.scale);
this.modelMatrix[12]=this.position[0];
this.modelMatrix[13]=this.position[1];
this.modelMatrix[14]=this.position[2];
this.matrixWorldNeedsUpdate = true;
}
Now, we need to initialize our
rigidBody
object. We initialize it when the object is
added to the
Stage
class. Open the
Stage.js
file from the
primitive
folder and go
to the
addModel
function:
addModel:function(stageObject){
if(!(this.gl===undefined)) {
stageObject.createBuffers(this.gl);
}
var len= this.stageObjects.length;
this.stageObjects.push(stageObject);
if(stageObject.rigidBody){
stageObject.rigidBody._id=len;
}else{