Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In a 3D environment I have an object with a displacement x that I know I need to traverse in a given time interval. I have the object's heading and elevation and I want to figure out the distance the object needs to travel on the x/y/z axis to move x units in the given direction. How can I calculate this? Are there two distinct ways to calculate it (using trigonometry or linear algebra)? Which way is the best in what scenarios?

EDIT: To clarify, I will make an analogy to a simple 2D scenario that is similar to the 3D scenario I'm working with. Let's say the object has an angle of 35 degrees from the x-axis. Making a right triangle, let's say we know that the hypotenuse (or displacement of the object) is 10 units. Now to figure out the x and y deltas to get from the object's current position to the object's new position 10 units away at a 35 degree angle, you simply do:

x = 10 * cos(35) 
y = 10 * sin(35)

I simply need to solve the same problem except in 3 dimensions.

share|improve this question
    
Can you clarify what you're looking for? If you have the displacement from one object to another, that is the distance between them. –  chaosTechnician Sep 7 '11 at 22:24
    
It would help greatly if you could draw a quick image in bitmap or something. Right now it's really hard to see what you want to calculate. –  TravisG Sep 7 '11 at 22:27
    
object's heading should be a vector, you only have to normalize it and then multiply it by whatever value you want your object to move. the result will be delta x/y/z –  Ali.S Sep 7 '11 at 22:50
    
Because of spam prevention I cannot add a picture to the main post but here is a link to a picture that will hopefully clarify (@chaosTechinician and @heishe) screensnapr.com/e/rDZ95Y.png. @chaosTechnician the displacement is from the object's current position to a new position (which is known), I just need to figure out the distance that needs to be traveled on the x/y/z axis to get to the new position from the current. –  Matt Sep 7 '11 at 23:15
    
Axes are separable in classical mechanics and you can work with the x axis velocity independently of YZ. It's entirely possible I don't understand your entire problem, however. –  Patrick Hughes Sep 7 '11 at 23:25
show 1 more comment

4 Answers

accoring to your changes I can assume you have two angles, one that I call theta which is just like the one you have in 2d plane and has some value between -pi -> +pi. and you have a second angle called (let's call this one phi) and it has some value between -pi/2 -> pi/2. you can see in the picture what are these angles exactly specifying.

enter image description here

using these 2 angles you can compute normal 3d vector using this formula :

 x = cos(theta) * cos(phi);
 y = sin(theta) * cos(phi);
 z = sin(phi);
share|improve this answer
add comment

Ok, I think I get what you're asking now...

If you have the 3D position of the target object (B) and your own position (A), the vector B - A is the 3D displacement to the target.

If you only have a bearing and an elevation to the target, you can get a vector that points at it but you can't determine how far away it is with only those angles. The components for that vector would be: x = cos(bearing) * cos(elevation); y = sin(bearing) * cos(elevation); z = sin(elevation); You may need to convert between degrees and radians and may need to add a constant to your bearing and/or elevation to properly line them up with the world axes.

If you have the bearing, elevation, and distance, multiply the vector components above by that distance.

share|improve this answer
    
This is probably what I was looking for but I something that you said allows me to explain exactly what I'm looking for. I don't actually have the 3D position of the "target object" (in the scenario I presented it's the single object's new position, but the math is the same). I want to be able to find the position (vector) of B and or the 3D displacement by using the known values of the bearing, elevation, and 2D displacement. –  Matt Sep 8 '11 at 1:00
    
What plane is the 2D displacement (distance) on? The XY plane? –  chaosTechnician Sep 8 '11 at 1:01
    
Sorry I messed that up there, it's technically 1D displacement, it's just a distance in units from one position to the target position. So it isn't on any plane. –  Matt Sep 8 '11 at 1:16
    
So you have the bearing, elevation, and distance between them? The last sentence in my answer addresses that. –  chaosTechnician Sep 8 '11 at 1:21
    
Ahhh that's what I assumed, thanks for the help! –  Matt Sep 8 '11 at 1:23
show 1 more comment

Things simplify considerably if you're willing to use quaternions.

In this case you have to simply store a "direction" unit vector(D) and an "upside" unit vector(U). Optionally you can store the "right" unit vector that points to the right direction (R) or you can compute doing the cross product (D x U).

When you want to change your pitch or bearing or orientation you simply have to construct a quaternion using the right vector and multiply the other two for that quaternion.

With this strategy you can store a "direction" vector that even points where you are going and rotate without the problem you may encounter if you touch some nasty rotation pole.

[EDIT]

Quaternions are 4-tuple of real numbers that represent a real part and the coefficients for tree orthogonal imaginary parts i, j, k. A quaterion may be write as (s,v) where s is the real part and v is the imaginary 3D vector.

A point in 3D (p) may be represented as the quaternion (0,p) and the rotation of θ rads about the r unit vector is: q = (s,v) where s = cos(θ/2) and v is r·sin(θ/2).

now p* = q · p · q' is the rotated point where q' is the inverse of q.

The inversion of a unit quaternion equals its conjugate so q' = (s, -v)

The quaternion multiplication shows a little complexity: while the a quaternion can be written as q = w + x i +y j +z k thus p · q is a simple polinomial multiplication, there are the relations so:

complex relations

we can say that a multiplication between quaternion can be written as:

general quaternion multiplication

this lead us to the conclusion that if q1 is the vector Q = [W1, X1, Y1, Z1] then

matrix form

so q1 · q2 = Q · A in term of matrix multiplications.

You can see that a rotation using a quaternion involves 32 multiplications and 24 sums Vs the 9 multiplications and 6 sums for the rotation using the standard matrix rotations.

This difference is due to the fact that a fourth dimension is used to avoid the pole problem that often makes the matrix rotation unviable: the euclideans matrix rotations is not faster, it simply compute less things and you pay this if you pass through a pole.

share|improve this answer
    
Thanks for the response, I was interested in seeing alternatives to the trigonometry method. When it comes down to pros and cons of each method it would seem that using quaternions simplifies the math and avoids the problem with rotation poles that you mentioned. But I also have to consider the overhead that would come with using the abstraction of a Quaternion class as opposed to accessing an array of precomputed sin values. For simulations that may have thousands of moving objects, would you say that the quaternion method may lose out on computational speed? –  Matt Sep 12 '11 at 3:08
    
@matt please see if my edit helps you in this sense –  FxIII Sep 12 '11 at 11:13
add comment

Sounds like what you need is spherical coordinates.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.