# Calculating Angle From Two Points in 3d Space

• Apr 3rd 2008, 10:55 PM
Manish Roy
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
• Apr 7th 2008, 12:53 PM
Henderson
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
$\displaystyle \left(\begin{array}{cc}0\\45\end{array}\right) \cdot \left(\begin{array}{cc}4\\45\end{array}\right) = 2025$

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

To find the angle:
$\displaystyle \cos(\Theta) = \frac{2025}{45\sqrt(2041)}$
$\displaystyle \Theta \approx 5.08$
• Apr 9th 2008, 10:28 PM
Manish Roy
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........(Headbang)
any Suggestion....