HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Z-Angle between 3-d point formula?

11-26-2007, 01:41 AM#1
Dil999
What is the formula to calculate the z-angle between two 3d points?
For example, point 100,100,500 (x,y,z) and point 50,70,90 -- what is the angle from the Z of point 1 to the Z of point 2 (in degrees).
11-26-2007, 01:49 AM#2
PipeDream
An angle is a measure of the distance you have to travel along a unit circle to get from one point to another. You need a third point to define the center of that circle.

I am guessing that by Z angle you're referring to the angle that the line from point 1 to point 2 makes with the x-y plane. In that case, the answer is

Collapse JASS:
function pitch takes real dx, real dy, real dz returns real
    return Atan2(dz,SquareRoot(dx*dx+dy*dy+0.1))
endfunction

This is in radians, you can call Rad2Deg() on the result or multiply by bj_RADTODEG for degrees.
11-26-2007, 01:54 AM#3
Dil999
What are dx, dy, and dz? x1*x2 or something?
11-26-2007, 01:58 AM#4
PipeDream
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
11-26-2007, 01:59 AM#5
Dil999
Thanks, +rep for the fast response.
11-26-2007, 02:43 AM#6
moyack
Quote:
Originally Posted by PipeDream
Collapse JASS:
function pitch takes real dx, real dy, real dz returns real
    return Atan2(dz,SquareRoot(dx*dx+dy*dy+0.1))
endfunction
I'm curious Pipedream.... Why did you put that 0.1??
11-26-2007, 03:04 AM#7
PipeDream
Because I originally wrote Atan(y/x). Then, guessing that Atan2 needs to do that division internally, and having not tested the behavior as x goes to zero I left it in.
11-26-2007, 03:09 AM#8
grim001
Atan2 produces a result from -pi to pi (-180 to 180) whereas Atan produces a result from -pi/2 to pi/2 (-90 to 90)...

It seems that in most applications you'd want the latter, which you'd get by:
Collapse JASS:
Atan(dz / (SquareRoot(dx*dx+dy*dy) + 0.001))
11-26-2007, 06:00 PM#9
Strilanc
In this case the second argument is never negative, so the output from atan2 will be in the correct range.

Atan2 is by far the better function to use, because it deals with all the border cases like x = 0. The extra cost is negligible compared to the cost of atan.
11-26-2007, 07:36 PM#10
grim001
Thanks for the clarification Strilanc