Determine degree rotation in 3D space

545 Views Asked by At

This is a programming issue. I need to understand the math before I program it though.

This is an issue that has vexed me before and I was not happy many years ago with the result. When I solved it many years ago it was in 2D and I had tight control of starting and ending locations. I used a whole bunch of if, then, else statements to determine what quadrant the final point location was in. I hated the solution.

The problem is pretty simple:

  • I have points in 3D space.
  • The points can be orientated in any orientation.
  • In essence the three points are all on a single plane and the plane is oriented in 3D space.
  • I need to judge between multiple rotated points to determine which point is rotated less than the others so I need some sense of rotation direction and distance.

My three points

I need to determine the angle of rotation in degrees or radians. Rotation can be any value between 0 - 360 degrees.

The start place can be anywhere on the 360 degree circle:

enter image description here

The end can be anywhere on the 360 degree circle:

enter image description here

The direction of rotation can be either "direction" on the circle and the plane the two lines are on can be oriented in any location in 3D space:

enter image description here

My programming tools can give the angle between two lines (A-B) and (A-C) of between 0-180 degrees. It has no sense of direction. If I get two points that are rotated 170 degrees one is rotated 170 degrees and one 190 degrees. I can't tell the difference between the two programatically except (in my limited way of thinking) calculating where a 170 degree rotation and a 190 degree rotation is and compare those results to the trial points to determine rotation over 180 degrees.

When I look at this in the human sense my brain can't help but orient itself perpendicular to the plane and judge rotation in clockwise or counterclockwise. This construct seems to be getting in my way in this case.

Is there a mathematical solution that I am missing here?

1

There are 1 best solutions below

5
On

First you have to define the vectors $AB$ and $AC$ by subtracting head - tail. i.e. $\vec{AB} = B - A$ and $\vec{AC} = C - A$. This should give you an array of three numbers. i.e. $\vec{AB}= [x_{AB}, y_{AB}, z_{AB}]$ for each vector.

You will need the length (i.e. norm) of each vector.
We denote this as $\|\vec{AB}\|$ and $\|\vec{AC}\|$. I assume you know how to do this.

You will need the dot product of the of the vectors which is just the component-wise multiplication of the x, y and z elements together and then summed.

$$\vec{AB}\cdot \vec{AC} = x_{AB}x_{AC} + y_{AB}y_{AC} + z_{AB}z_{AC}$$

You can look that up.

Then the angle that you are looking for is:

$$ \theta = \cos^{-1}\left( \frac{\vec{AB}\cdot \vec{AC}}{\|\vec{AB}\| \|\vec{AC}\|} \right)$$