Hello. Basically I'm trying to get the angle of a 3D line and getting the result as an X Y and Z angle in order to rotate polygons to be parallel with that angle. In other words, I'm trying to create a "Wormhole Wall" by taking a series of lines and wrapping 8 polygons around each line at a certain distance away from the line. But honestly I don't think the results of getting the angle of a 3d line are correct. If, for example, point A is at <0, 0, 0> and point B is at <1, 0, 1>, you would think the Yaw is at a 45 degree angle, while the Pitch and Roll remains zero. Instead, my Pitch is 45, Yaw is 90, and Roll is 45. I made a simplified version to see if you can help spot anything wrong. Thanks in advance:
#include <iostream>
using namespace std;
int main(void);
struct Vertex3D {
float x;
float y;
float z;
};
struct Vector3D {
float x;
float y;
float z;
};
const double PI = 4.0 * atan(1.0);
Vertex3D a = { 0.0f, 0.0f, 0.0f };
Vertex3D b = { 1.0f, 0.0f, 1.0f };
Vector3D u;
Vector3D vecX = { 1.0f, 0.0f, 0.0f };
Vector3D vecY = { 0.0f, 1.0f, 0.0f };
Vector3D vecZ = { 0.0f, 0.0f, 1.0f };
Vector3D angle;
Vector3D dot;
float mag;
int main() {
//acos domain: -1 to 1 range: 0 < x < pi
//asin domain: -1 to 1 range: -pi/2 < x < pi/2
//atan domain: all real num range: -pi/2 < x < pi/2
u.x = b.x - a.x;
u.y = b.y - a.y;
u.z = b.z - a.z;
mag = sqrtf(u.x * u.x + u.y * u.y + u.z * u.z);
dot.x = u.x * vecX.x + u.y * vecX.y + u.z * vecX.z;
dot.y = u.x * vecY.x + u.y * vecY.y + u.z * vecY.z;
dot.z = u.x * vecZ.x + u.y * vecZ.y + u.z * vecZ.z;
angle.x = acosf(dot.x / mag) * 180.0f / static_cast<float>(PI); //Pitch --
angle.y = acosf(dot.y / mag) * 180.0f / static_cast<float>(PI); //Yaw |
angle.z = acosf(dot.z / mag) * 180.0f / static_cast<float>(PI); //Roll .
cout << "Point a is at " << a.x << ", " << a.y << ", " << a.z << endl;
cout << "Point b is at " << b.x << ", " << b.y << ", " << b.z << endl;
cout << "angle x (Pitch): " << angle.x << endl;
cout << "angle y (Yaw): " << angle.y << endl;
cout << "angle z (Roll): " << angle.z << endl;
system("pause");
return 0;
}