Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

3D Vector Math Equations

Posted on 2010-11-12
Medium Priority
1,096 Views
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.

-Javin

0
Question by:Javin007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 4
• 2
• +1

LVL 37

Expert Comment

ID: 34125986
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>
0

LVL 37

Accepted Solution

TommySzalapski earned 1000 total points
ID: 34125998
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.
0

LVL 4

Author Comment

ID: 34126043
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
0

LVL 37

Expert Comment

ID: 34126060
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)
0

LVL 4

Author Comment

ID: 34126086
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.
0

LVL 4

Author Comment

ID: 34126091
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.
0

LVL 37

Expert Comment

ID: 34126111
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
coordinates.
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.
0

LVL 37

Expert Comment

ID: 34126113
If you know ABC you can get DEF and vice-versa.
0

LVL 37

Expert Comment

ID: 34126132
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)
0

LVL 84

Expert Comment

ID: 34126302
How are you storing your (A,B,C) direction?  Are they direction cosines?
0

LVL 37

Expert Comment

ID: 34126317
No, it's a Cartesian unit vector. Or are you asking Javin?
0

LVL 84

Expert Comment

ID: 34126431
A cartesian vector is how I would keep it, but how is that  "kept in degrees/radians"?
0

LVL 37

Expert Comment

ID: 34128403
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 ) )
0

LVL 2

Assisted Solution

nickalh earned 1000 total points
ID: 34210355
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
"I have a "direction" for that vector kept in degrees"
and
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

http://en.wikipedia.org/wiki/Direction_cosine
"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
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
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.
0

LVL 2

Expert Comment

ID: 34210392
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.
0

LVL 4

Author Closing Comment

ID: 34630543
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.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article seeks to propel the full implementation of geothermal power plants in Mexico as a renewable energy source.
This article provides a brief introduction to tissue engineering, the process by which organs can be grown artificially. It covers the problems with organ transplants, the tissue engineering process, and the current successes and problems of the tecâ€¦
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaacâ€¦
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201â€¦
Suggested Courses
Course of the Month5 days, 21 hours left to enroll