Game Development Reference
In-Depth Information
return this.getObjectByName( name, recursive );
}
StageObject.prototype.getDescendants=function ( array ) {
if ( array === undefined ) array = [];
Array.prototype.push.apply( array, this.children );
for ( var i = 0, l = this.children.length; i < l; i ++ ) {
this.children[ i ].getDescendants( array );
}
return array;
}
We have also added traversal functions to locate the child objects either by ID or
by name. The key function is
traverse
; it calls itself recursively followed by the
depth-first search algorithm.
StageObject.prototype.updateMatrix=function () {
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;
}
The preceding function is the most significant change we have done from the
previous code. Earlier, we were using rotational matrices to compute the object's
transformation matrix, but now we are using the quaternion to calculate the model
matrix (
mat4.fromQuat(this.modelMatrix, this.quaternion)
). Then, we apply
shear transformation and scale our object with the provided scale vector. Then we
simply place the
position
vector in
m31
,
m32
, and
m33
of our transformation matrix.
StageObject.prototype.updateMatrixWorld=function ( force ) {
if ( this.matrixAutoUpdate === true ) this.updateMatrix();
if ( this.matrixWorldNeedsUpdate === true || force === true ) {
if ( this.parent === undefined ) {
this.matrixWorld.copy( this.modelMatrix );
} else {
mat4.mul(this.matrixWorld, this.parent.matrixWorld,
this.modelMatrix);
}
this.matrixWorldNeedsUpdate = false;
force = true;
}
// update children
for ( var i = 0, l = this.children.length; i < l; i++ ) {
this.children[ i ].updateMatrixWorld( force );