Jump to content
  • Advertisement
Sign in to follow this  
soofian

how to calculate the angle

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!