# calculating relative position in 3d space

#### S33K3R

First please go easy on me I have never taken a formal Trigonometry class and most of what I know is self-taught.

For a given set of Cartesian coordinates (x1,y1,z1) in degrees I have found I can find a 2-dimensional (i.e. looking from the top view down) set of coordinates D distance away (x2,y2,z2) by doing the following...

Code:
Given Distance D = 10:

x2 = x1 + (D * Cosine( DegreesToRadians(x1) )  * Cosine( DegreesToRadians( y1 ) ) )
y2 = y1 + (D * Cosine( DegreesToRadians(x1) ) *   Sine( DegreesToRadians( y1) ) )
z2 = z1 + (D * Sine( DegreesToRadians(x1) )
... however I need to find the distance in a three-dimensional environment taking the current angles (a,b,c) aka. pitch,yaw,roll into account and find a new set of coordinates from the given coordinates along the line of sight for the given angle. Below is what I remember from a cannon formula I had in physics; I was playing around with simple Trajectory calculations (ignoring wind resistance, simple gravity of 1, etc..)..

Code:
Given:

LengthCannon = 10 (i.e. cannon shaft length in units).
Angles (a1,b1,c1) in degrees
Cannon butt origin (x1,y1,z1) in degrees

Calculate by...

a2 = (a1 / 360) * 2 * pi
b2 = (b1 / 360) * 2 * pi
(c1/c2 doesn't really matter in this case since I do not care about spin/orientation of the object which will be assumed as self-righted)
tip of barrel = LengthCannon * sin(b2)
projection = LengthCannon * cos(b2)

x = projection * cos(a2)
z = projection * sin(a2)

The x component of the cannon shaft in space:

x2 = (x1 - 0) / LengthCannon
y2 = (y1 - 0) / LengthCannon
z2 = (z1 - 0) / LengthCannon
Although that sort of ballistics mathematics is probably overkill.. the new coordinates would then be in theory (x2,y2,z2), but the numbers don't seem to be coming out right. I am basically trying to use this formula to leverage the new point (tip of the barrel) given the butt of the barrel and its relative angle.

Am I on the right track? Can someone help correct this formula or perhaps give me a better one?

#### skipjack

Forum Staff
Cartesian coordinates are (signed) distances from an origin, not angles.

#### S33K3R

Re:

skipjack said:
Cartesian coordinates are (signed) distances from an origin, not angles.
It should be obvious I already know that.

As I stated before I want to find a point in space (x2,y2,z2) that is a fixed distance away (say 10 units to keep math simple) from an originating point in space (x1,y1,z1) following an imaginary line determined by the angles (pitch,yaw,roll) the imaginary barrel / camera is pointing/facing.

Could you provide something that could help solve the problem?

#### skipjack

Forum Staff
It wasn't obvious, as you explicitly referred to Cartesian coordinates as being "in degrees". Surely pitch, yaw, etc., make sense only when applied to some initial direction of the barrel (unrelated to the initial position, which might as well be the point (0, 0, 0)).

#### S33K3R

go grief and flame posts from someone else.

I am here to look for a formula that works, obviously you rather waste peoples times getting caught up in semantics.. probably because you don't know the answer.

It is clear that I have spent some time looking into different related material and it is clear about what I am looking for.

Get lost I don't have time for people like you that waste peoples time.

For anyone else interested I will post the formula when I find it so no one else has to deal with this moron.

#### jickso

It would help a lot if you would post a drawing of what you're trying to accomplish. #### skipjack

Forum Staff
Since the space is 3-D, only 3 values are needed, such as a distance and two angles. It's unclear how the angles would be applied, as an angle needs to apply to a direction (which wasn't given) rather than a point.

#### MarkFL

jickso said:
It would help a lot if you would post a drawing of what you're trying to accomplish. Something else that would help a great deal is civility. :wink:

To the OP, you'll find the moderators here really know what they're talking about, but may need to ask questions for clarification, not for the purpose of criticism.

#### S33K3R

Here is a snapshot that shows the problem visually. Just to throw out some practical numbers for the math (although I will end up converting to variables for this app):
Code:
Angles pitch,yaw,roll (a1,b1,c1): -5.0, -8.0, 0.0
Start Position (x1,y1,z1): -100.0, 200.0, 300.0
Distance: 10.0
End position (x2,y2,z2): what I am trying to find the formula for
Basically the Roll above can just be left at a zero value for the sake of simplicity.

My guess is I am going to end up with something along the lines of the pythagorean theorem to calculate the distance if I have a pitch that is not 0 which then uses the offset of the current location. I am not sure how this would really be calculated through if the camera was pointed in some odd diagonal.

although my previous formula would give me accurate information on the direction of the destination cordinates, it would not tell me the elevation (z):

Code:
Given Distance D = 10:
x2 = x1 + (D * Cosine( DegreesToRadians(x1) )  * Cosine( DegreesToRadians( y1 ) ) )
y2 = y1 + (D * Cosine( DegreesToRadians(x1) ) *   Sine( DegreesToRadians( y1) ) )
z2 = z1 + (D * Sine( DegreesToRadians(x1) )

#### skipjack

Forum Staff
Why don't you give the start position of the tip of the cannon's barrel as well as the start position of its butt?