3D Vector Math Equations

Should be a simple 500 points for the geometry geeks in here.  

I have a point located at a vector in 3D space.  (X,Y,Z)
I have a "direction" for that vector kept in degrees/radians another vector (A,B,C)
I have a second point located in the distance (D,E,F)

I need the formulas to do the following:

1.) What is the distance between XYZ & DEF?
2.) What is the find the "angle" between XYZ and DEF (and thus define ABC so that XYZ is facing DEF).
3.) What is the formula to "move" the point XYZ "forward" a set amount (N) based on it's current "direction" (ABC)?

These aren't for school.  I'm trying to do a simple 3D game, and sucked at Geometry 15 years ago.  More will no doubt follow.


Who is Participating?
So after dist/5 iterations you have <X + (D-X)*5/dist*dist/5,... = <X + (D-X)*1,... = <X + D - X,... = <D,...
So it has arrived. Of course your program will round or truncate to the nearest integer when it goes to display, but store the vectors as doubles for the calculations so you don't get cumulative rounding error.
Distance uses Euclid's formula dist = sqrt( (X-D)^2 + (Y-E)^2 + (Z-F)^2 )
The 'angle between' can be expressed as the vector <D-X, E-Y, F-Z>
So let's say the speed is 5 units per iteration. Then dist/5 gives the number of iterations that should be required so <X,Y,Z> at iteration 1 becomes <X + (D-X)*5/dist, Y + (E-Y)*5/dist, Z + (F-Z)*5/dist>
Javin007Author Commented:
1.) What is the distance between XYZ & DEF?
   "Distance uses Euclid's formula dist = sqrt( (X-D)^2 + (Y-E)^2 + (Z-F)^2 )"

So the formula is:

D = Sqrt( (X-D)^2 + (Y-E)^2 + (Z-F)^2 )

I assume this is derived from the Pythagorean theorem? (A^2 + B^2 = C^2)

2.) What is the find the "angle" between XYZ and DEF (and thus define ABC so that XYZ is facing DEF).
   "The 'angle between' can be expressed as the vector <D-X, E-Y, F-Z>"

That's not what I need.  I need the angle of ABC to be in Degrees or Radians, as I stated here:
"I have a "direction" for that vector kept in degrees/radians another vector (A,B,C)"

Thus, the formula that follows wouldn't quite work.  Though I'd imagine there's some quick conversion formulas that can take the ABC (in rad/deg, which means C isn't really important, since it's the "roll" angle) and convert it to a vector?

3.) What is the formula to "move" the point XYZ "forward" a set amount (N) based on it's current "direction" (ABC)?
   "So let's say the speed is 5 units per iteration. Then dist/5 gives the number of iterations that should be..."

I'm pretty lost on this one.

Assuming I have my point of origin:  XYZ
Assuming I have my current angel in Rad/Deg: ABC
Assuming I have a Speed: S
Assuming I have a "Time" traveled: T

What is the formula to find my new point: DEF
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Yes, Euclidean distance is derived from the Pythagorean.

I made it easier for you. Trying to express a 3D angle requires two angles, the angle in the xy plane and the angle above the plane. Then you have complex trigonometry to get the angle and to get the movements.

Forget ABC. You have:
Point of origin (X,Y,Z)
Travel vector <D-X, E-Y, F-Z>
Speed S
Time T

Your new point will be (X + (D-X)*S*T/dist, Y + (E-Y)*S*T/dist, Z + (F-Z)*S*T/dist)
Javin007Author Commented:
I can't "forget ABC."

At some point during the travel, the rotation of, say, the X axis may change.  If it changes, I no longer know what my "look at" point is with your method.
Javin007Author Commented:
The problem here is that you're assuming I know DEF, which I do not.  I would only know DEF for the first two questions, but you've skipped an answer for #2.
Yes you will. That's the beauty of it. Travel vector <D-X, E-Y, F-Z> will rotate seamlessly with points (X,Y,Z) and (D,E,F).
Travel vector <D-X, E-Y, F-Z> is simply expressing the angle in Cartesian coordinates instead of spherical  
However, if you want it in spherical coordinates, here goes. Angle in xy plane (theta) is arctan((E-Y)/(D-X)). Angle above xy axis (phi) is arctan( (F-Z)/sqrt( (D-X)^2 + (E-Y)^2 ) )
And you just do than in reverse to get the travel vector back to plug into the equation for movement.
If you know ABC you can get DEF and vice-versa.
I think I see what you are doing. Take the travel vector and scale it to have a magnitude of 1.

So for question 1 we have the same dist as before.

For question 2 we get the unit vector to face XYZ toward DEF.
<A,B,C> = <(D-X)/dist, (E-Y)/dist, (F-Z)/dist>
Now you just rotate <A,B,C> and (X,Y,Z) with your axis.

Question 3 then is that the position at time T with speed S is
(X + A*S*T, Y + B*S*T, Z + C*S*T)
How are you storing your (A,B,C) direction?  Are they direction cosines?
No, it's a Cartesian unit vector. Or are you asking Javin?
A cartesian vector is how I would keep it, but how is that  "kept in degrees/radians"?
Like I said, if he really wants it in angles he can use spherical coordinates with
theta = arctan((E-Y)/(D-X))
phi = arctan( (F-Z)/sqrt( (D-X)^2 + (E-Y)^2 ) )
Javin, Which representation are you lookng for
I'm with OZO,
there are multiple ways of representing directions in 3-d.

We need to find out which representation will be most effective for Javin.
It sounds like this is some sort of flying game or first person shooter game?

We need a broader context to help more effectively.
Can any of the axes rotate?  (i. x and y only like a land base game with shooting up as a possibility even jumping on buildings or ii. the z axis rotate like a flying game)
Will the origin be shifting also?

We're not exactly sure what you're asking
To start to answer your question #2.
"I have a "direction" for that vector kept in degrees"
"radians another vector (A,B,C)"
isn't really clear what you mean.
Apparently, (A, B, C)  is a different kind of object than <D, E, F> or <X, Y, Z>
Considering this we need a different notation for (A, B, C) type objects to prevent confusion with vectors.  I'm using < > for vectors and ( ) for A, B, C type objects.
The question is what kind of object is <A, B, C>?

B. Interpretation #1.
In 3 dimensions, two angles are necessary and sufficient to represent direction.
Typically, one angle represents rotation in the xy plane.  (A for example)
And another angle represnts lift. (B for example)
Then the length of the vector could be C. (or dropped entirely if the length is not important)
This is the spherical coordinates Tommy mentioned.

Or perhaps
Interpretation: Direction Cosines #2

"More generally, direction cosine refers to the cosine of the angle between any two vectors."

This seems more likely what you're looking for based on
"I have a "direction" for that vector kept in degrees
radians another vector (A,B,C)"
In this interpretation, all 3 of A, B, C are angles.

On that page, x, y, z are used to represent i, j, k.
V = <v1, v2, v3> is much more akin to our
Q = <D, E, F> - <X, Y, Z> = < D-X, E - Y, Z - F>
The square root on the bottom is the distance calculated in question 1.

Looking strictly at "I have a "direction" for that vector kept in degrees or
radians another vector (A,B,C)"
makes me wonder if you're using "unitized" direction vectors.
Unitized means the length is 1.
i, j, k are standard unit vectors, but any vector with length 1 can be a direction vector.
Imagine a sphere with radius 1. And vectors which start at the center(not necessarily at the origin, in fact in our case, most likely centered at <X, Y, Z>) and ends somewhere on the sphere.  These "direction vectors" will be direction vectors.  Is this what your talking about?

In this case, C represents the angle of declination from the vertical. k= <0, 0, 1> in a vertical plane containing Q.
B represents angle from <0, 1, 0> to Q in plane containing Q and the y-axis.
A represents angle from <1, 0, 0> to Q in plane containing Q and the x-axis.
Compensating for the fact that Q is not intended to start from the origin needs to be another comment.
Realize that to use direction cosines effectively, you need to keep in mind the planes I just mentioned are based on Q and NOT the xy plane, yz plane or xz plane.
(At least as far as I can remember)

Also, consider an outside resource like
"Calculus and Analytic Geometry" by Stein & Barcellos.
I have fifth edition, which should be inexpensive.  It's got a whole a chapter (plus) on 3 dimensional topics.  I particularly like it because it starts with a section on drawing 3-d spheres, graphs, etc.
For what you're doing projections may be particularly important.

Interpretation 3
To project Q first onto an xy plane or yz plane, then calculate the angles may ultimately be simpler and more useful to work with.

Please, let us know if where on the right track.
P.S. for simplicity interpretation #2 assumes the direction cosines are based on the origin.

Compensating for that is relatively simple and can be explained once we know which interpretation you're wanting.
Javin007Author Commented:
Unfortunately, this is part of a hobby that I won't be able to get around to for some time, so I'm splitting the points to close the question.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.