Skip to content

Using Matrices & Object3Ds in THREE

renegademaster88 edited this page Jun 26, 2011 · 10 revisions

I have been using THREE for a month or so and its is an incredible library, however my biggest stumbling point has been understanding how to use Matrices. Here is what i have learnt Now the Matrix4 class has all the usual transfrom functions you would expect for 3D computer graphcis matrix class - mulitply , setRotationX, setRotationY , setPosition are just a few. However its really the Object3D class and its updateMatrix and update functions that we need to understand.

The Object3D class has its own matrix:

this.matrix

and contains within itself:

this.position

this.quaternion

this.rotation

this.scale

When updateMatrix is called the Object3Ds matrix is set according to those variables, and the following boolean:

this.useQuaternion

if this is true it will use this.quaternion instead of this.rotation to set the objects orientation - this.rotation will do nothing.

Now if you want to manually configure this.matrix using Matrix4 methods, then you need to make sure this.matrixAutoUpdate is false and that you don't call this.updateMatrix. Otherwise all your settings will be over-ridden and your object will do nothing.

If you change the scale of the object manually be sure to update this.boundRadiusScale with the changes also.

If your object is static i.e. its not going to move in the environment then set this.updateMatrix to false to save the effort of calculating the objects matrix every cycle.

if this.matrixAutoUpdate is **true **then at render time this.updateMatrix is called automatically so you don't need to call it yourself.

There is also the world matrix and the parent-child hierarchy. This is propagates the parents this.matrix to its children, and is dependent on this.matrixWorldNeedsUpdate being true in for the propagation to occur during the this.update call.

Now most of the object types inherit from Object3D, so if your object isn't doing what you expected it probably because this.updateMatrix is over-writing your settings!

Here is an example of two different ways of setting the orientation and position of an object:

Method 1: using the Object3D members

	your_object.position = start_position;
	your_object.useQuaternion  = true;
	your_object.quaternion = quaternion;
	your_object.updateMatrix();

Method 2: Manually setting the matrix

	your_object.matrix.setRotationFromQuaternion(quaternion);
	your_object.matrix.setPosition(start_position);
	your_object.matrixAutoUpdate = false;

I hope this helps. renegademaster88