3

I have 2 mesh objects. Anchor and Rod. The Anchor rotates around z-axis, as illustrated by the image. The rod is supposed to move only backward and forwards. Here is the image: http://imageshack.us/photo/my-images/841/7a83.png/.

But i am stuck with trying to figure out the matrix calculation. For example if the anchor rotates 45degrees, so its facing the x-axis, how can i make the rod still move backwards and forwards?

1
  • Do you use gl-matrix.js? Commented Nov 1, 2013 at 15:59

2 Answers 2

4

Just as scooby37 noticed, combination example provided by Troopers is not valid. You should NOT:

new THREE.Matrix4().makeTranslation(0, 0, z).makeRotationZ(Math.PI/2); 

Instead you can try something like:

let rotation = new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(0, 1, 0), Math.PI / 6.0); let transformation = new THREE.Matrix4().makeTranslation(0, this.height, 0); this.mesh.applyMatrix(rotation.multiply(transformation)); 

which introduce matrix multiplication - usual way transformations should be combined.

Sign up to request clarification or add additional context in comments.

Comments

3

For the rotation around z axis :

var rotation = new THREE.Matrix4().makeRotationZ(Math.PI/2); 

For the translation, where z is your delta :

var translation = new THREE.Matrix4().makeTranslation(0, 0, z); 

You can combine the two transformations in beginning with the translation :

var transformation = new THREE.Matrix4().makeTranslation(0, 0, z).makeRotationZ(Math.PI/2);

var transformation = rotation.multiply(translation); 

Then apply transformation to your geometry :

geometry.applyMatrix(transformation); 

4 Comments

I'm quite new to Three.js so i don't really know what i'm missing here. I got the rotation around z-axis to work, but still can't figure out how to make the "rod" move backwards and forwards when the direction changes. I can't do it with makerotationX because it does not allways rotate around x-axis, if you know what i mean.
Your transformation is a rotation around z with a rotation around X, so combine the two by multiplying the matrixes : var rotationZ = new THREE.Matrix4().makeRotationZ(angleZ); var rotationX = new THREE.Matrix4().makeRotationX(angleX); var transformation = rotationZ.multiply(rotationX);
That works, but the problem is that the object needs to move, and applying a matrix rotates it around the worlds origon. so when it moves the rotation gets larger, and larger. I am not sure how to bind the rotation to a single blocks own origon.
This Answer should NOT be accepted or at least corrected you can't combine transformations by: var transformation = new THREE.Matrix4().makeTranslation(0, 0, z).makeRotationZ(Math.PI/2) - it overwrites them!!! Use something like: var rotation = new THREE.Matrix4().makeRotationZ(this.toolMeshes[index].rotation.z); var translation = new THREE.Matrix4().makeTranslation(this.toolMeshes[index].position.x, this.toolMeshes[index].position.y, 0); var matrix = rotation.multiplyMatrices(translation, rotation);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.