Rotation by two vectors. (Complicated)

Started by
1 comment, last by Cuchulainn 21 years, 5 months ago
okay. I have a spaceship mesh. Its facing toward 0,0,1 all the time. I have 2 directional vectors that i have been using for the spaceship. Direction, and up. Direction is the direction the spaceship is pointing in, and up is the upvector - this is so that i can do a z rotation. I have the wholes system set up apart from the rotation. i have vectors vector startingdirection; // The initial direction of the mesh - unrotated. vector startingup; // The initial upvector of the mesh - unrotated. startingdirection = (0,0,1); startingup = (0,1,0); Then i have my two current directional vectors. Spaceship.direction and spaceship.up. i rotate everypoint in the axis so that its now pointing and turned in the same direction as the current direction and up vector. The spaceships vertices for every frame are the unrotated ones so i want a way to rotate a mesh based on the angles between. 0,0,1 -> spaceship.direction 0,1,0 -> spaceship.up i hope you understand. I''ve been working on this 4 a while now. i came up with a way using dot product but it didnt work. I made a vector dir. I set this to the spaceship.direction but i flattened its y value to 0. I then normalised the vector and got the acos(dotproduct of this and 0,0,1). i used this as the radians for my first axis rotation - the axis being 0,1,0. i got the crossproduct of spaceship.direction and spaceship.up to get spaceship.crossways - effectively. i then the acos(dot product of 0,1,0 and spaceship.up). using this i rotated around the axis spaceship.crossways using the angle i got from the acos(dot product of 0,1,0 and spaceship.up). I >-o--- I = upvector of spaceship. --- = directional vector of spaceship. > = thruster (ass of spaceship); I dont think this is an easy question by any standards. I hope you geniuses can answer it. Thanks a million in advance
You'll Never Beat The Irish!
Advertisement
Just looking for clarification....

My understanding is that you are always wanting to rotate your spaceship vertices to spaceship.direction and spaceship.up? Given that the vertices are always represented in a coordinate system for which up = (0,1,0) and direction = (0,0,1)?

Actually, the solution is not difficult. But it involves transformation matrices. I'll refer you to the gamedev.net articles and resources for details on the theory of these matrices. But here is the solution.

First, you have to calculate a third direction, lets call it "right" since it'll point to the right side. Let startingright = (1,0,0). And the thing you have to calculate each frame is spaceship.right. Let that be:

spaceship.right = crossproduct(spaceship.up, spaceship.direction)

Then, you can make a rotation matrix:

    [spaceship.right.x spaceship.up.x spaceship.dir.x]R = |spaceship.right.y spaceship.up.y spaceship.dir.y|    [spaceship.right.z spaceship.up.z spaceship.dir.z] 


I'm assuming that the right direction corresponds to the local x direction, up to the local y, and direction to the local z.

Then, just apply that rotation to all the points of the ship:

point_rotated = R * point_original

And, finally, add the position of the spaceship to get the final
position of the vertices in space:

point_final = point_rotated + spaceship_position.

Look in the articles & resources to find out how to multiply the R matrix times the original points, if you don't already know how to do that.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

[edited by - grhodes_at_work on October 29, 2002 7:29:25 PM]
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Another way to look at that matrix is x*right+y*up+z*dir. If you haven''t had linear algebra then it may not be obvious that those are the same.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement