Just to make sure that we are both talking about the same things, let me rephrase the parameters of the problem:
The observer is located at eyeX, eyeY, eyeZ and the observed object is at targetX, targetY, targetZ. This is assuming a coordinate system wherein the X and Y axes describe a horizontal plane and X and Z axes describe a vertical plane. The direction from the observer to the target will be described using 2 angles:
- XYdeg describes the degrees from the X axis towards the Y axis. Anything at 0 or 180 degrees lies along the X axis, and 90 or 270, along the Y axis.
- XZdeg describes the degrees from the XY plane towards the Z axis. Anything at 0 or 180 degrees lies along the XY plane (on the "horizon"), and 90 or 270, along the Y axis (straight "up" or "down").
Oh, and all trig functions need to operate in radians.
In this case, given the observer's position and the target's position:
Code: Select all
XYdistance = [(targetX - eyeX)^2 + (targetY - eyeY)^2]^.5
XZdistance = [(targetX - eyeX)^2 + (targetZ - eyeZ)^2]^.5
distance = [(targetX - eyeX)^2 + (targetY - eyeY)^2 + (targetZ - eyeZ)^2]^.5
or
distance = (XYdistance^2 + XZdistance^2)^.5
if targetY >= eyeY then
XYdeg = Arccos[(targetX - eyeX) / XYdistance)] * 180 / PI
else
XYdeg = 360 - Arccos[(targetX - eyeX) / XYdistance)] * 180 / PI
end
if targetZ >= eyeZ then
XZdeg = Arccos[(targetZ - eyeZ) / XZdistance)] * 180 / PI
else
XZdeg = 360 - Arccos[(targetZ - eyeZ) / XZdistance)] * 180 / PI
end
Or, given the observer's position and the direction AND distance to the target
Code: Select all
targetX = eyeX + Cos(XYdeg * PI / 180) * distance
targetY = eyeY + Sin(XYdeg * PI / 180) * distance
targetZ = eyeZ + Cos(XZdeg * PI / 180) * distance
Note: I try to avoid using Tan and Arctan functions because they have problems dealing with objects along the axes (tan(90) = infinite)
I hope that's right... someone want to check my math?