Advertisement

Translating vectors and that...

Started by August 26, 2001 12:10 PM
12 comments, last by Ryanza 23 years, 5 months ago
Hey, been going through the tutorials and got a little stuck on something... As an example, if you had a vector or a bearing, and you want to move forward on that heading (both x and y), how would you move that object along on the heading..? Totorial 10 has a similar thing, but its only for rotating on the Y axis..
let me get this right, you have a current vector
and a destination then all you have to do:
(destination / current) / time = incremtent
then you have the additions per intervall.
i.e. every time you move the thing you call
current += incremtent;

i hope someone will correct me if im wrong.
Advertisement
Ryanza, are you talking about a double rotation (as in, moving in 3-space rather than just a 2-D plane)?

~ Dragonus
yeah, 3d space... such as having a xheading a yheading .. 20 degrees right and 20 degrees up as an example. Well basically Im trying to get a model to point in the right direction (lets say a spaceship) and then moving forward along in that direction(3d) at a set speed....
Well, it all depends on your perspective on the axes I''m giving you, but I''m going to assume you''re using first-person. (To me, It makes the most sense out of it stylistically at least.)

To rotate left/right, you''re going to be rotating around the Y-axis (the axes goes through the ship''s top and bottom when not rotated), just as normal.

To rotate up/down, it''ll be the X-axis as you mentioned. NeHe does this, only it''s called "lookupdown" instead of "scenerotx" or something.

Now the tricky part: movement. In tut#10, the additions based on movement were as follows (for all intensive purposes):

xpos += sin(yrot*piOver180) * .05
zpos += cos(yrot*piOver180) * .05
ypos += 0

However, in 3-D, we have that second angle, xrot. You just add sin()''s and cos()''s as follows:

xpos += sin(xrot*piOver180) * sin(yrot*piOver180) * .05
zpos += sin(xrot*piOver180) * cos(yrot*piOver180) * .05
ypos += cos(xrot*piOver180) * .05

I hope this helps (and works!)

~ Dragonus
Thanks Dragonus, just what I needed
I had my x-axis heading and y-axis heading the wrong way round.. and my calc was a bit off
And nope.. not 1st person, its for lots of flying around ships, each with its own bearing and that. Works quite nice now

Just need to get in a way for the ship to turn towards a point and Im set

Actually.. heres another question, hopefully someone can answer.
If you have 2 points(x,y,z) and want to calculate the bearing you need to face to point from the one point straight at the other...how? Looks like some pretty tricky math..



Edited by - Ryanza on August 27, 2001 1:53:42 PM
Advertisement
Okay, I can help you with that too... ^_~

Assuming that the X-axis goes left/right, Y-axis goes down/up, and Z-axis goes in/out (negative/positive for each), let''s say you''re at the point {x, y, z} and you want to face the point {a, b, c}.

Well first things first. Find the component-per-component difference between the two points...

double xDiff = a - x;
double yDiff = b - y;
double zDiff = c - z;


Now, we''ll find the angle of rotation about the Y-axis you need to be. You''ll be using the X and Z differences we calculated above. We''re going to do this using trigonometry of right angles.

Since 0° of rotation about the Y-axis is on the Z-axis (hence why Z has the cos(yrot) instead of the sin(yrot) in the equations I gave you last post), it''ll be the "x-parameter" in the atan2() function, while the X-axis will be the "y-parameter".

To find the angle of rotation about the y-axis, use the following:

yrot = atan2(xDiff, zDiff);

(Notice how the X-coordinates is the y-parameter (the 1st parameter) and the Z''s are the x-parameter (the second one).)

Now we have to find the angle up and down. To do this, we''ll need the length of the hypotenuse of the last triangle. That''ll be the "x-parameter" of the triangle. Then the up/down distance, which is the difference of points along the Y-axis, will be the "y-parameter". So the call will be...

xrot = atan2(yDiff, sqrt(xDiff * xDiff + zDiff * zDiff));

Hope that helps.

~ Dragonus
erk.. not sure if Im doing anything wrong (following your example pretty closely) but its just not returning the (right?) angles.. its giving answers from -1 to 1.. not degrees. maybe missing a function?
Oops... ummm... multiply the results of the atan2() function by 180overPi.

~ Dragonus
nope.. still returns a small number, I think its radians? I tried converting to degrees... but the answer in the end is normally facing in a completely diff direction.. damn.

This topic is closed to new replies.

Advertisement