Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

3D Vector Math Equations

Posted on 2010-11-12
16
Medium Priority
?
1,097 Views
Last Modified: 2013-12-26
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
Comment
Question by:Javin007
  • 8
  • 4
  • 2
  • +1
16 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
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

by:
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

by:Javin007
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:TommySzalapski
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

by:Javin007
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

by:Javin007
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

by:TommySzalapski
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

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

Expert Comment

by:TommySzalapski
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 85

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

by:TommySzalapski
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

by:nickalh
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
To start to answer your question #2.
"I have a "direction" for that vector kept in degrees"
and
"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

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
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.
0
 
LVL 2

Expert Comment

by:nickalh
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

by:Javin007
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Complex Numbers are funny things.  Many people have a basic understanding of them, some a more advanced.  The confusion usually arises when that pesky i (or j for Electrical Engineers) appears and understanding the meaning of a square root of a nega…
Aerodynamic noise is the cause of the majority of the noise produced by helicopters. The inordinate amount of noise helicopters produce is a major problem in the both a military and civilian setting. To remedy this problem the use of an aerogel coat…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question