tags:

views:

151

answers:

1

I'm working with a 3D that has a property of type Vector3D called FrontDirection. This object is rotated as follows:

var rotate = new AxisAngleRotation3D(new Vector3D(0, 1, 0), deltaAngleInDegrees);
var transform = new RotateTransform3D(rotate);

my3DObject.FrontDirection = transform.Transform(my3DObject.FrontDirection);

After some arbitrary amount of rotation, I'd like to determine the angle of the object as viewed from above. I'd expect the value to vary between [0, 360). The closest I can get is the following:

var angle = Vector3D.AngleBetween(new Vector3D(1, 0, 1), my3DObject.FrontDirection);

However, while rotating the object in a complete circle the angle varies 0 to 180, then falls back down to 0. It seems like AngleBetween is giving the distance between the vectors without considering direction. What's a good way to calculate the angle?

A: 

Here's the solution I came up with:

var axisZ = new Vector3D(0, 0, 1);
var angleZ = Vector3D.AngleBetween(axisZ, my3DObject.FrontDirection);
double currentRotation = my3DObject.FrontDirection.X >= 0 ? angleZ : 360 - angleZ;
James Cadd