# Simple 3D vector movement math

Started by Jun 28 2001 10:13 AM

,
5 replies to this topic

###
#1
Members - Reputation: **122**

Posted 28 June 2001 - 10:13 AM

Basically, I''d like to know the formula for moving an object in its direction vector, by a movement vector.
This will move an object 20 points forward (X axis)
new_x = old_x+(dir_x*20);
new_y = old_y+(dir_y*20);
new_z = old_z+(dir_z*20);
You can basically swap the direction X and Y vectors to do the Y axis.
How do you do the Z axis?
Is it like this or something?
new_x = old_x+((dir_z*dir_x)*20);
new_y = old_y+((dir_z*dir_y)*20);
new_z = old_z+(dir_z*20);
Thanks.

###
#2
Members - Reputation: **351**

Posted 29 June 2001 - 01:48 AM

quote:

This will move an object 20 points forward (X axis)

new_x = old_x+(dir_x*20);

new_y = old_y+(dir_y*20);

new_z = old_z+(dir_z*20);

Actually these three lines will work for any direction. Just use dir_x, dir_y and dir_z to describe the direction. E.g. with

dir_x = 1; dir_y = 0; dir_z - 0;

it will increase x by 20 but leave y and z unchanged.

with

dir_x = 0; dir_y = 0; dir_z - 1;

it will leave x and y unchanged but add 20 to z, i.e. move 20 units along the z axis.

###
#3
Members - Reputation: **122**

Posted 29 June 2001 - 09:32 AM

This is not what I meant..

This will move the object in the direction relative to the world, not itself.

The equation I am looking for, is, for example....

A plane is facing downward towards the earth 45 degrees... Positive on its Z(Z being height) axis isn''t just straight up, since it is pointing down to the ground, it is positive on the Z axis and will effect another axis, depending on what other axises the plane is pointing towards.

dir_x, etc, would be the direction that the object is facing, and that equation above would always move that object forward no matter what direction the object is pointing.

So I need the math not just to move the object forward, but to move it on other axises, relative to its current direction/angle, not the world direction.

This will move the object in the direction relative to the world, not itself.

The equation I am looking for, is, for example....

A plane is facing downward towards the earth 45 degrees... Positive on its Z(Z being height) axis isn''t just straight up, since it is pointing down to the ground, it is positive on the Z axis and will effect another axis, depending on what other axises the plane is pointing towards.

dir_x, etc, would be the direction that the object is facing, and that equation above would always move that object forward no matter what direction the object is pointing.

So I need the math not just to move the object forward, but to move it on other axises, relative to its current direction/angle, not the world direction.

###
#4
Members - Reputation: **204**

Posted 29 June 2001 - 10:11 PM

If you are using a matrix to position and render the object (it''s hard to imagine any other way ) then you can use the axis vectors within that matrix to move the object through world space, along it''s own axes.

The 4x4 D3DMATRIX is like this;

|Xvector.x, Xvector.y, Xvector.z, 0 | X axis Vector

|Yvector.x, Yvector.y, Yvector.z, 0 | Y axis Vector

|Zvector.x, Zvector.y, Zvector.z, 0 | Z axis Vector

|Xposition, Yposition, Zposition, 1 | Position

So to move a model along it''s own z axis (for example) try this;

Xposition += Zvector.x * Magnitude

Yposition += Zvector.y * Magnitude

Zposition += Zvector.z * Magnitude

You''ll need the magnitude to represent the speed of the object, as the vectors within the matrix are all normalised.

Cheers

Matt

The 4x4 D3DMATRIX is like this;

|Xvector.x, Xvector.y, Xvector.z, 0 | X axis Vector

|Yvector.x, Yvector.y, Yvector.z, 0 | Y axis Vector

|Zvector.x, Zvector.y, Zvector.z, 0 | Z axis Vector

|Xposition, Yposition, Zposition, 1 | Position

So to move a model along it''s own z axis (for example) try this;

Xposition += Zvector.x * Magnitude

Yposition += Zvector.y * Magnitude

Zposition += Zvector.z * Magnitude

You''ll need the magnitude to represent the speed of the object, as the vectors within the matrix are all normalised.

Cheers

Matt

###
#5
Members - Reputation: **187**

Posted 30 June 2001 - 07:45 AM

I think what you are trying to do (if I read your question correctly) is to make the object move in its own space (so if an object is facing NorthWest, moving it forward 20 units will move it 20 units NorthWest from its previous position). For X and Y your code will work, but if you want also do the Z, you need to do it differently.

Basically what you need to accomplish this is take your movement vector and rotate it so it is in World Coordinates (right now it is in the object's local coordinates). Then taking that result and adding it to the object's world position should give you what you're looking for. IMO the best way is to use a matrix.

Doing it the way makes use of the direction vector of the object moot (the information is in the matrix). The object will be oriented along its coordinate system. Now depending on your the coordinate system is used (I usually use Y-forward/back, X-side/side, Z-up/down), just specify your direction vector in this manner. So if you want to move 20 units forward, pass in a vector with x=0, y=20, z=0.

// Rotate the move vector into world coordinates

vx = ((m[0] * dir_x) + (m[4] * dir_y) + (m[8] * dir_z)) + m[12];

vy = ((m[1] * dir_x) + (m[5] * dir_y) + (m[9] * dir_z)) + m[13];

vz = ((m[2] * dir_x) + (m[6] * dir_y) + (m[10] * dir_z)) + m[14];

// Add in the transformation

new_x = old_x + vx;

new_y = old_y + vy;

new_z = old_z + vz;

[Edit: bah message got cut off]

Edited by - Aqualung on June 30, 2001 3:29:28 PM

Basically what you need to accomplish this is take your movement vector and rotate it so it is in World Coordinates (right now it is in the object's local coordinates). Then taking that result and adding it to the object's world position should give you what you're looking for. IMO the best way is to use a matrix.

Doing it the way makes use of the direction vector of the object moot (the information is in the matrix). The object will be oriented along its coordinate system. Now depending on your the coordinate system is used (I usually use Y-forward/back, X-side/side, Z-up/down), just specify your direction vector in this manner. So if you want to move 20 units forward, pass in a vector with x=0, y=20, z=0.

// Rotate the move vector into world coordinates

vx = ((m[0] * dir_x) + (m[4] * dir_y) + (m[8] * dir_z)) + m[12];

vy = ((m[1] * dir_x) + (m[5] * dir_y) + (m[9] * dir_z)) + m[13];

vz = ((m[2] * dir_x) + (m[6] * dir_y) + (m[10] * dir_z)) + m[14];

// Add in the transformation

new_x = old_x + vx;

new_y = old_y + vy;

new_z = old_z + vz;

[Edit: bah message got cut off]

Edited by - Aqualung on June 30, 2001 3:29:28 PM

###
#6
Members - Reputation: **351**

Posted 01 July 2001 - 09:43 PM

I think I see: the vector

(dir_x)

(dir_y)

(dir_z)

is the planes''s "forward" direction. But as you''ve worked out this does not tell you which way it''s up and side directions are.

You will need to keep track of these two directions also. One way is to store them as vectors and update them at the same time as your forward vector, using whatever rules for rotations you have, regularly checking the vectors are at right angles if necessary.

Another way is to store the rotation as a matrix: this is just the three vectors written together, and has the advantage of letting you use matrix rules to manipulate it, and works well with most graphics APIs, e.g. DirectX and OpenGL which both let you use matrices to describe rotations.