# how to calculate the angle

This topic is 4902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

can anybody tell me how to calculate the angle between two points (x1,y1,z1) and (x2,y2,z2) in 3D space.

##### Share on other sites
angle along which axis?

##### Share on other sites
are they points or vectors?
you cant calculate angles between points, so I'm assuming you're talking about vectors

to avoid confusion, v1 is v and v2 is w. the formula is:
arccos(t) = (v.w) / (|v|.|w|)

in other words:
arccos(t) = (vx*wx + vy*wy + vz*wz) / (sqrt(vx² + vy² + xz² ) * sqrt(wx² + wy² + wz² ))

sqrt being square root

##### Share on other sites
Quote:
 Original post by Necr0Potenc3are they points or vectors?you cant calculate angles between points, so I'm assuming you're talking about vectorsto avoid confusion, v1 is v and v2 is w. the formula is:arccos(t) = (v.w) / (|v|.|w|)in other words:arccos(t) = (vx*wx + vy*wy + vz*wz) / (sqrt(vx² + vy² + xz² ) * sqrt(wx² + wy² + wz² ))sqrt being square root

For those of you that don't understand that (I'm with you on this one), I offer a slight alternative, only it doesn't result in a single angle, it results in two. I use simple trigonometry to calculate the angles, so it's fairly easy to follow.

To try and explain it a little better, think of a point just infront of you, then think of one a little further away, but down, and to the left.

Step 1: Get the difference between the points.
x3 = x1 - x2
y3 = y1 - y2
z3 = z1 - z2

Now imagine a flat triangle that extends in the directions of the second point. For a better example, hold out your right hand, palm down, then point your thumb out on a 45 degree angle. The space between your thumb and your index finger is what the triangle should look like.

Step 2: Calculate the hypotenuse of that triangle.
hypA = sqrt(x3^2 + z3^2)
NOTE: if x3 is zero, the hypotenuse is z3.

Now imagine a triangle that extends from the first point, along that line, then directly down to the second point, then back up to the first point. We'll need the distance between the two points, so we'll use the new line (the old hypotenuse) to find it. There are other methods to do this, but we don't need to do the extra calculation since we've gone this far.

Step 3: Find the length between the two points.
hypB = sqrt(y3^2 + hypA^2)
NOTE: if y3 is zero, the 2nd hypotenuse is hypA.
NOTE: in either case, if z3 is zero, the 2nd hypotenuse is sqrt(x3^2 + y3^2).

Now we have all the lengths we need to calculate the angles between the two points (x axis (pitch), and y axis (yaw)). Those two triangles I mentioned before were to represent the rotation angles more than anything.

Step 4: Calculate the angles.
rx = arcsin(y3 / hypB)
ry = arcsin(x3 / hypA)

Now before you even consider carrying this out, all of the values (x3, y3 and z3) need to be checked, because if they're all zero, the rotation is zero. Also, it wont work if the second point is directly up or directly down (gimbal lock), but it's not that hard to calculate.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634091
• Total Posts
3015435
×