4

I have two squares in 3D space. I want to find the x,y,z angles between them. I started by finding the normal vectors for both of the squares and I am trying to figure out how to get the angle between them.

I am using XNA (C#) Vector3 objects.

I have calculated the normal vectors as follows:

        Vector3 normal1 = (Vector3.Cross(sq1.corners[0] - sq1.corners[1], sq1.corners[0] - sq1.corners[2]));
        Vector3 normal2 = (Vector3.Cross(sq2.corners[0] - sq2.corners[1], sq2.corners[0] - sq2.corners[2]));

I want to find the euler rotation that will get normal1 facing the same way as normal2

AakashM
  • 62,551
  • 17
  • 151
  • 186
Jkh2
  • 1,010
  • 1
  • 13
  • 25

1 Answers1

9

First, you can calculate the axis and amount of rotation (assuming an arbitrary axis):

Vector3 axis = Vector3.Cross(normal1, normal2);
axis.Normalize();
double angle = Math.Acos(Vector3.Dot(normal1, normal2) / normal1.Length() / normal2.Length());

If the normals are normalized, then the calculation of the angle reduces to

double angle = Math.Acos(Vector3.Dot(normal1, normal2));

Then you can transform this to euler angles with the function from here

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70