0
\$\begingroup\$

I have a triangle in the 3D world. I have the:

  • positions for the three points
  • the normal vectors for the three points (all the same, because vertices are not shared)

This triangle faces to a direction, so it has a XYZ rotation (but I dont know the rotation itself).

If I put a cube into the same scene, how I can achieve the same rotation for the cube? So if the triangle "looks" upwards, the cube should do the same. If it has a 10f x rotation, how can I get what is the x?

\$\endgroup\$
  • \$\begingroup\$ I will have a go at this one, to see if it gives you ideas. The first thing, you need a reference direction to measure the angle between what the direction of the normal is from the reference direction. This can be a vector or 1, 0, 0 for example. You then need to calculate the angle by executing a dot product and a cross product to produce a rotation axis. You can also execute a dot/cross to work out the winding of your angle as well. You do this each frame. Once you have angle and the rotation axis, you can generate a matrix to apply on the cube each frame. \$\endgroup\$ – ErnieDingo Mar 5 '18 at 20:59
1
\$\begingroup\$

The easiest way I can think to do this would be with a rotation matrix. It would be a 3x3 matrix with each row representing each of the basis vectors of the local space of the triangle in question.

Let's consider the normal of the triangle to be the j basis vector. You'd get the i basis vector with cross(normalize(p1 - p0), triNorm). You'd get the k basis vector by taking the cross product of i and j.

You'd put those three resulting basis vectors in the matrix in the order of i, j, k. To produce the XYZ Euler angles for the triangle, you'd simply convert the rotation matrix to that form.

Here is some code that can help you do that. (inspired by code from the book: 3D math primer for graphics and game development):

float sx = -m32;

if(abs(sx) > 1e5f) {
    x = _PI * sx;
    y = atan2(-m23, m11);
    z = 0;
} else {
    y = atan2(m31, m33);
    x = asin(sx);
    z = atan2(m12, m22);
}

Something to keep in mind is that if you're trying to use a certain axis from the resulting Euler angle on it's own, you might get undesired results. You could fix this problem however, but it'll take a few more steps.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.