Avatar of Javin007
Javin007Flag for United States of America asked on

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.

-Javin

Game ProgrammingMath / Science

Avatar of undefined
Last Comment
Javin007

8/22/2022 - Mon
TommySzalapski

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>
ASKER CERTIFIED SOLUTION
TommySzalapski

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Javin007

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
TommySzalapski

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)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
Javin007

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.
ASKER
Javin007

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.
TommySzalapski

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
TommySzalapski

If you know ABC you can get DEF and vice-versa.
TommySzalapski

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)
ozo

How are you storing your (A,B,C) direction?  Are they direction cosines?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
TommySzalapski

No, it's a Cartesian unit vector. Or are you asking Javin?
ozo

A cartesian vector is how I would keep it, but how is that  "kept in degrees/radians"?
TommySzalapski

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 ) )
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
nickalh

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.
ASKER
Javin007

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.