Celso Shigaki
Celso Shigaki

Reputation: 75

Apply an object's rotation to another with Three.js

I'm currently trying to apply an object's rotation to another. I've tried the following methods without success:

//Rotates much faster than the original object's rotation
boxme1.rotateOnAxis(new t.Vector3(0,1,0), cube.rotation.y);

//The vector and the quaternion part is supposed to get the direction the
//object is facing, however when I apply the vector to a Euler to make the
//rotation, the object just disappears from the scene. I'm not even
//sure if this is the right way to use an Euler.
var vector  = new t.Vector3(0,0,-1);
vector.applyQuaternion( cube.quaternion );
var euler = new t.Euler(vector.x, vector.y, vector.z);
boxme1.rotateOnAxis(new t.Vector3(0,1,0),euler);

//If I try to set the object rotation with the Euler I got above 
//instead of using rotateOnAxis the object doesn't move at all.
boxme1.rotation = euler;

What's the correct way to apply the rotation of one object to another? The problem is not the cube rotation because it is rotating as expected. I'm starting to learn Three.js so I'm still pretty noob.

Thanks in advance

Upvotes: 2

Views: 3719

Answers (2)

Boogiechillin
Boogiechillin

Reputation: 287

boxme1.quaternion = cube.quaternion 

won't work because of how Javascript actually handles pointers. Assigning cube's quaternion to boxme1's makes it so that any changes to boxme1 would apply to cube(bad) and vise versa. I would try to use one of the following:

Best solution:

boxme1.quaternion.copy(cube.quaternion);

Alternately:

boxme1.quaterion = cube.quaterion.clone();

It looks like you have a solution - but the above may be a bit more memory/cpu efficient.

Upvotes: 3

Celso Shigaki
Celso Shigaki

Reputation: 75

I found the answer

//First of all we create a matrix and put the rotation of the cube into it
rotObjectMatrix = new THREE.Matrix4();
rotObjectMatrix.makeRotationFromQuaternion(cube.quaternion);
//Next we just have to apply a rotation to the quaternion using the created matrix
boxme1.quaternion.setFromRotationMatrix(rotObjectMatrix);

I'm still not sure why making boxme1.quaternion = cube.quaternion didn't work though.

Upvotes: 1

Related Questions