# Thread: Calculating Angle From Two Points in 3d Space

1. ## Calculating Angle From Two Points in 3d Space

Hi Gurus,

I am working in a 3d Application where i need to rotate a 3d Object as per Axis Selected (x,y,z). i have two point say (12,0,45) and (12,4,45) i.e i am towards Y Axis .

I need to calculate an angle between this point and pass it to my selected Axis say X Axis in order to rotate my objects.

pls Help me out.

Thanks

2. Hopefully I'm understanding your problem.

From what I'm reading, you want to rotate around an axis to translate a point in space to a new, projected point in space, and find the angle of revolution at the axis.

There are a couple ways, but what I would do is this (using your example):

-Rotating around the x-axis, we can ignore the variables in x (the 12s)
-The cosine of the angle between two vectors is the dot product divided by their magnitudes, so
$\left(\begin{array}{cc}0\\45\end{array}\right) \cdot \left(\begin{array}{cc}4\\45\end{array}\right) = 2025$

To find the Magnitudes:
$\sqrt(0^2 +45^2) = 45, \sqrt(4^2 + 45^2) = \sqrt(2041)$

To find the angle:
$
\cos(\Theta) = \frac{2025}{45\sqrt(2041)}$

$
\Theta \approx 5.08$

3. Thanks,

Code:
 Vector3 s = new Vector3(InputManager.startX, InputManager.startY,
InputManager.startZ);

Vector3 e = new Vector3(InputManager.endX, InputManager.endY, InputManager.endZ);

float t = InputManager.startY * InputManager.endY + InputManager.startZ * InputManager.endZ;

float sRoot = (float)Math.Sqrt((InputManager.startY * InputManager.startY) + (InputManager.startZ * InputManager.endZ));
float eRoot = (InputManager.endY * InputManager.endY) + (InputManager.endZ * InputManager.endZ);

float costheta = (float)((t) / Math.Sqrt(InputManager.endZ * eRoot));

Output i am getting:

Start : Vector3(1, 1, 1)
End : Vector3(-87.91284, -54.91942, 142.8247)
Cos Theata : 0.04806909
Start : Vector3(1, 1, 1)
End : Vector3(-88.92979, -52.88537, 141.8078)
Cos Theata : 0.04933823
Start : Vector3(1, 1, 1)
End : Vector3(-89.94674, -50.85131, 140.7908)
Cos Theata : 0.05063638
Start : Vector3(1, 1, 1)
End : Vector3(-89.94674, -50.85131, 140.7908)
Cos Theata : 0.05063638
Start : Vector3(1, 1, 1)
End : Vector3(-90.96368, -48.81727, 139.7739)
Cos Theata : 0.05196397
Start : Vector3(1, 1, 1)
End : Vector3(-90.96368, -48.81727, 139.7739)
Cos Theata : 0.05196397
Start : Vector3(1, 1, 1)
End : Vector3(-91.98063, -46.78321, 138.7569)
Cos Theata : 0.05332147

oops i m not getting the right output........
any Suggestion....