how to calculate the angle

Started by
2 comments, last by Gorax 18 years, 8 months ago
can anybody tell me how to calculate the angle between two points (x1,y1,z1) and (x2,y2,z2) in 3D space.
Advertisement
angle along which axis?
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
Quote:Original post by Necr0Potenc3
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


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.

This topic is closed to new replies.

Advertisement