Page 1 of 1

Math Question re-visited

Posted: 27.09.2004, 18:23
by Rassilon
Ok this might be obvious but I need help on my geometry/trig...

Lets say I have variables A,B and C used in the following formulas:

A = B + sin(C*PI/180.0);
A = B + tan(C*PI/180.0);
A = B + cos(C*PI/180.0);

now I have A and B already in this instance and wish to compute the value of C...How can I reverse the formulas to find the value of C?

Re: Math Question re-visited

Posted: 27.09.2004, 20:40
by cpotting
Rassilon wrote:
A = B + sin(C*PI/180.0);
A = B + tan(C*PI/180.0);
A = B + cos(C*PI/180.0);

now I have A and B already in this instance and wish to compute the value of C...How can I reverse the formulas to find the value of C?


C = Arcsin(A - B) * 180.0 / PI
C = Arctan(A - B) * 180.0 / PI
C = Arccos(A - B) * 180.0 / PI

Posted: 27.09.2004, 21:48
by Rassilon
Thanks! I am one step closer to plotting my camera centrer command...

Posted: 28.09.2004, 03:24
by Rassilon
Well it didnt work out as I expected, and I think its because I should have been a bit more descriptive...

What I have is 2 sets of coordinates the first set is the current location say eyeX, eyeY and eyeZ...Now the second set is what the first set is pointing to say targetX,targetY and targetZ.

OK now i would have the angle already to find the target

targetX = eyeX + sin(facing*PI/180.0);
targetY = eyeY + tan(facing2*PI/180.0);
targetZ = eyeZ + cos(facing*PI/180.0);

but what I need to do is when I have the target and the eye I need the angle...there is only two angles...horizontal and vertical SO I have no clue what to do to find the correct angle...How would i reverse these functions using all three X,Y and Z to find only two angles?

Posted: 28.09.2004, 15:30
by cpotting
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?

Posted: 28.09.2004, 17:01
by cpotting
cpotting wrote: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

oops. Forgot to handle objects on the axes again...
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 XYdistance = 0 then
    XYdeg = 0
else if targetY >= eyeY then
    XYdeg = Arccos[(targetX - eyeX) / XYdistance)] * 180 / PI
else
    XYdeg = 360 - Arccos[(targetX - eyeX) / XYdistance)] * 180 / PI
end
if XZdistance = 0 then
    XZdeg = 0
else if targetZ >= eyeZ then
    XZdeg = Arccos[(targetZ - eyeZ) / XZdistance)] * 180 / PI
else
    XZdeg = 360 - Arccos[(targetZ - eyeZ) / XZdistance)] * 180 / PI
end

Posted: 29.09.2004, 20:44
by cpotting
cpotting wrote:

Code: Select all

or
distance = (XYdistance^2 + XZdistance^2)^.5


Ignore this - it doesn't work (well, it does work, but it is wrong). I'm still trying to figure out where I came up with that calculation - the value it returns isn't even remotely useful.

I've got to stop licking toads while doing math.

Posted: 29.09.2004, 23:05
by Rassilon
Well done in my book...Its far better than anything Ive seen so far...

I ended up figuring it out using only the XZ plane and set the observer to level with the object on the XY plane...This involved far less math...BUT I will be inserting a HUD that draws an arrow pointing to the object that the observer is tracking I will need the above math to compute this and I am gratified you cleared this up for me...Thanks!

Code: Select all

   facing = 360+atan2((targetZ - eyeZ),(targetX - eyeX))* 180 / PI;
   facing2 = 180;