Solved

Posted on 2013-12-28

This is a question regarding 3D rotation using THREE.js and predefined mesh models. All components originate on the <0,0,1> vector as shown on the image. I have no problem getting the cylinders rotated into the correct position. I can also get the fitting rotated to fit the receiving cylinder. The problem is getting the inlet of the fitting rotated around its own Z-axis to meet the incoming cylinder. Note that the cylinders can be pointing in any direction and the angle between them can be anything from 15 to 90 degrees (using the appropriate fitting. Is it possible to have a single method to rotate the Z-axis regardless of angle?

5 Comments

and that the coordinates for the incoming cylinder are reversed,

and that the Fitting mesh points to <0,0,1> and <0-1,0>

Call <0,0,1> F0 and <0,-1,0> F1

Call the Receiving Cylinder pointing coordinates C0

and the Incoming Cylinder pointing coordinates -C1

Let N0 = F0 - C0

Normalize N0 = N0 / |N0|

Let F' = F1 - 2*N0 * (F1 dot N0)

Let N1 = F' - C1

Normalize N1 = N1 / |N1|

Then let A0 be the reflection matrix along N0 = I-2*N0*N0^T (where ^T means transpose)

and A1 be the reflection matrix along N1 = I-2*N1*N1^T

A0*A1 should then be a rotation matrix that aligns the fitting to the receiving and incoming cylinders, assuming that the receiving and incoming cylinders are at right angles to each other, and that F0 and F1 are at right angles to each other

To restate the problem, I assume you want a matrix where the y-axis matches the z-axis of the receiving cylinder, and the z-axis matches the z-axis of the incoming cylinder, is that right? Do you also need the position of mesh?

Copy the z-axis (elements 2, 6 and 10) from the two pipe object transforms into two Vector3 objects. Receiving object will give vy and incoming will give vz. Use Vector3.crossVectors with vy and vz to get vx. Then use those vectors to set the transform of the joint object with Matrix4.applyMatrix. You may have to negate vz or vy, depends on which end of the pipe you attach to.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**17** Experts available now in Live!