mrdoob/three.js — Issue #1269: Offsetting a rotation value in THREE.Matrix4
Author: jayfield1979
Created: 2012-02-08 10:19:35+00:00
State: open

Hey guys,

I have built a little 3D racing car demo combining THREE.js and the JigLibJS2 physics engine (will share a link when I am done) but I have a question that would solve a remaining issue.

Sorry to hijack your Issues board on here as most of my posts have been questions rather than issues but I don't know where else to post them for them to get the quality and speediness of answers I receive on here. I hope you don't mind and if there is a place to ask questions and get answers from this community then let me know and I'll post there instead.

Here goes:

```javascript
var pos = mesh.rigidBody.get_currentState().position;
var dir = mesh.rigidBody.get_currentState().orientation.get_rawData();

var matrix = new THREE.Matrix4();
var rotate = new THREE.Matrix4( dir[0], dir[1], dir[2], dir[3], dir[4], dir[5], dir[6], dir[7], dir[8], dir[9], dir[10], dir[11], dir[12], dir[13], dir[14], dir[15] );
matrix.setTranslation( pos.x, pos.y, pos.z );
matrix.multiplySelf( rotate );
mesh.matrix = matrix;
mesh.updateMatrixWorld( true );

var matrix2 = new THREE.Matrix4();
var rotate2 = new THREE.Matrix4( dir[0], dir[1], dir[2], dir[3], dir[4], dir[5], dir[6], dir[7], dir[8], dir[9], dir[10], dir[11], dir[12], dir[13], dir[14], dir[15] );

//doesn't work
matrix2.setRotationY( degToRad (180 ) );

matrix2.setTranslation( pos.x, pos.y+5, pos.z );
matrix2.multiplySelf( rotate2 );
camera.matrix = matrix2;
camera.updateMatrixWorld( true );
```

Basically I have a car (mesh) and I want the camera (camera) to follow it (3rd person style). By applying the same transform matrix from the physics engine to the car mesh (which works perfectly) I was hoping for the camera to move with the car. It actually works except the camera was too low so I offset the `pos.y` value by 5. That worked. The only problem is that the camera is always looking backwards. So I tried to offset the Y rotation of the camera's matrix by 180 degrees which made the camera look the correct way (forwards) but it is now stationary... it is not longer applying the position transforms.

My question therefore is how do I "offset" the Y rotation of the camera while keeping the rest of the transforms from the matrix or is there a simpler way to do what I want?

Thank you in advance.