Game Development Reference
In-Depth Information
}
}
StageObject.prototype.update=function(steps) {
this.updateMatrixWorld();
}
Another interesting function is the
updateMatrixWorld
function. It first invokes
updateMatrix
; if
matrixAutoUpdate
is
true
, the function then checks for
the value of
parent
. If
parent
is not defined, then
modelMatrix
is copied to
matrixWorld
; otherwise,
matrixWorld
for that object is computed by concatenating
the parent's
matrixWorld
matrix and the object's
modelMatrix
(
mat4.mul(this.
matrixWorld,this.parent.matrixWorld,this.modelMatrix)
). Then, we iterate
over all the children of the object to compute their new world matrix. We have also
updated our
update
function. It invokes
updateMatrixWorld
when it is invoked
from our main control code.
Implementing the bone class
Though the bones are never rendered, we will treat them as stage objects, as all the
transformations applied to stage objects are applied to the bones too. We do not add
them to our stage but we surely inherit the
StageObject
class to achieve the desired
functionality. Open
primitive/Bone.js
in your editor and examine the following
code snippet:
Bone= inherit(StageObject, function (belongsToSkin ) {
superc(this);
var d = new Date();
this.id ="id-"+d.getTime();
this.skin = belongsToSkin;
this.skinMatrix = mat4.create();
});
The
Bone
class inherits
StageObject
and has all its properties. We have added
two more variables to the class,
skin
and
skinMatrix
. The
skin
variable holds the
reference to the
Geometry
class, where the bone belongs to. The
skinMatrix
variable
is very similar to
matrixWorld
, but it holds the world space transformation of the
bone. These variables are updated using the following function:
Bone.prototype.update=function ( parentSkinMatrix, forceUpdate ) {
// update local
if ( this.matrixAutoUpdate ) {
forceUpdate |= this.updateMatrix();
}
// update skin matrix
if ( forceUpdate || this.matrixWorldNeedsUpdate ) {