# Lin Al question: finding a matrix/quat

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

First time poster in this area. I'd ask a math professor if I knew one.... Given 3 axes i, j, k and new axes i',j', k' can you find a matrix/quaternoion/something that rotates all directoinal vectors in i, j, k to be defined in i', j' k'? Right now a guy at work is asking for all this information from me which I htink is unnecessary, but I am an undergrad questioning the authority of someone with a PhD......

##### Share on other sites
Google for "Change of Basis" matrix.
The matrix will exist if i', j', k' are linearly INdependent.

##### Share on other sites
In this forum I see you get hw problems....

If you fear this is a math/hw problem (which it isnt, its a plane flying around the globe doing barrel roles problem), can you tell me yes it can be done and send me in a general direction of where to look?

##### Share on other sites
okay, thanks. i'll begin looking.

these axes are like x, y, z... all independent. In some cases they will be x, y, z

##### Share on other sites

You have to understand that a triplet i,j,k is equivalent to :

- a frame
- a linear transformation from the canonical frame to i,j,k (*)
- a matrix with columns i,j,k (Say M)

(*) If i,j,k form an (edit cf Paradigm Shifter, thx) : - right handed - orthonormal base, then the transfo is a rotation, and M a rotation matrix which can be transformed into a normalized quaterinon.

Thus what you want is obvious :
(EDIT : wrote too quickly earlier)
R = inverse(M') * M

Well you just have to learn the basics of linear algebra. there is enough on the www. ANY 3D coder knows all this stuff by heart, making it as obvious as adding integers. That's ultimately necessary to be totally fluent with such concepts. Writing 3D video games needs a hell more than what you probably think.

EDIT :

OK I'll be more kind, to show you how it works. If the said vector has local (IJK) coords Vi, Vj, Vk, then :

(Vi',Vj',Vk') = R*(Vi,Vj,Vk)

Raplece R and read left to right :

The first step is from local coords (i,j,k) to world (x,y,z)

(Vx,Vy,Vy) = M*(Vi,Vj,Vk)

Then from world to local (i',j',k') coords.

(Vi',Vj',Vk') = inverse(M') * (Vx,Vy,Vy)

[Edited by - Charles B on September 13, 2004 1:44:15 PM]

##### Share on other sites
Quote:
 Original post by Charles BRead the other threads about rotations here.You have to understand that a triplet i,j,k is equivalent to :- a frame- a linear transformation from the canonical frame to i,j,k (*)- a matrix with columns i,j,k (Say M)(*) If i,j,k form an orthonormal base, then the transfo is a rotation, and M a rotation matrix which can be transformed into a normalized quaterinon.

If and only if det(M) == +1. If it's -1 then it will have a reflection component also (cf change from right handed to left handed system).

##### Share on other sites
Haha, I am a 3d coder (THE 3d coder at my company, go figure) and I dont know this by heart. I took Lin Al before realizing it'd be one of the most important classes and now regret skipping half of it. I was pretty sure that it was possible, I usually remember what can be done but forget how its done.

I am going off of htis paper:
http://www.math.hmc.edu/calculus/tutorials/changebasis/changebasis.pdf

and cant beleive how simple this is. I probably should take a refresher on Lin Al....

What reference books do you recommend? I don't like my college textbook.

##### Share on other sites
You basically have to construct a rotation matrix to do this. Its a lot of work, and you make a lot of assumptions becuase there are an infinite amount of was to rotate one vector into another, which is all you're doing.
Solve the equation :

Rx=x'

You'll have 3 equations, and if you use Euler angles you'll have 3 unknowns.

Also, what do you mean given axis i,j,k and i' j' k', are those all vectors? Like is i=<1,0,0> j = <0,1,0> k = <0,0,1> and you need to go to i' = <i_x,i_y,i_z> ....

Assuming this is what you mean, I just realized its easy.
Your rotation matrix, since how you apply it is a matrix multiplication, is going to just be your axis in row form, so:

assuming the new 3 axis vectors are
i' = <i_x,i_y,i_z>
j' = <j_x,j_y,j_z>
k' = <k_x,k_y,k_z>

your Rotation Matrix would be :

|i_x i_y i_z |
|j_x j_y j_z |
|k_x k_y k_z |

so long as i', j', k' are all normalized.

##### Share on other sites
Okay, to fully show my ignorance here is the context of the problem:

You have a camera flying around the earth, like a plane. It needs to move 50 meters forward (forget if its straight 50 meters or around the greater circle 50) and you need to update the camera preserving altitude and what the camera is looking at.

I, rushing to code texture look ups and bad LOD functions, asked our algorithms person (who developed our representation of the earth), given the up vector, camera direction and view position and that distance moved, could he give me the updated info. And it kind of spiraled down from there....

##### Share on other sites
Quote:
 Haha, I am a 3d coder (THE 3d coder at my company, go figure)

;p I know. I have met many people like you in my earlier game prog jobs. Some were rather worried when I left, I used to be the mecano of math bugs. I don't intend to bash but I just can't understand why people can't get it or remeber it. Well in fact I admit it was hard to see the equivalences I mentionned, and to interpret the formulas the first two weeks I had to learn all this stuff at school.

Quote:
 What reference books do you recommend? I don't like my college textbook.

Frankly no idea. I've learned such stuff decades ago. ;) Sometimes with memory leaks, I try to figure out math stuff on paper. Sometimes with 2D/3D schemes, and I try to visualize things in my head spatially. I think it's always better to learn this way. Once you get it with mental pictures, it's forever printed. Text demonstrations do not last so long.

##### Share on other sites
I am a defense contractor, prefered to be a game programmer but I wasnt good enough to be a runt and learn from someone with experience. So instead I got a job as THE graphics programmer at a non-game company. As a result, there is no one I can ask for advice other than this message board, which is rather frustrating since I am still rather green.

Edit: I felt I was good enough, but I didn't have a friend in the industry to get me in and this job came up first.

##### Share on other sites
Quote:
 Original post by jm_hewittOkay, to fully show my ignorance here is the context of the problem:You have a camera flying around the earth, like a plane. It needs to move 50 meters forward (forget if its straight 50 meters or around the greater circle 50) and you need to update the camera preserving altitude and what the camera is looking at. I, rushing to code texture look ups and bad LOD functions, asked our algorithms person (who developed our representation of the earth), given the up vector, camera direction and view position and that distance moved, could he give me the updated info. And it kind of spiraled down from there....

This is probably the best place to come for this question though. There are a lot of tutorials on exactly this sort of thing. I have never done anything like it, and dont have THAT great of a background in mathematics. But heres what I'd do (probably really slow method and im missing a lot of stuff but anyways...)

We have in the global, a position of the camera
C.x C.y C.z
We have its Vector of travel from its current position
<CV.x CV.y CV.z>

We have the point of the target its looking at
T.x T.y T.z
We have the vector that the camera must face to look at the point
<CT.x CT.y CT.z>

Then we will also need the up, right and forward vectors of the camera. Can the camera Roll, pitch, yaw ? Or just translate? (not the picture of the target, just the motion of the camera)

Once answered ill try to continue. (using Rot-Trans matrices)

##### Share on other sites
We're discussing that in other thread, "Finding angles of rotation between 2x2 vectors"... and maybe there are some other threads i don't know.

Explanations of Charles B 's idea:

1,some basics:
I guess, you used to transform several vectors by matrix using something like:

for(i=0;i<how_many_vectors_do_you_want_to_transform;i++){YourNewVector[i]=M*YourVector[i];}

It's can be written alot more scientifically as

[MatrixWithAllYourNewVectorsAsColumns]=M*[MatrixWithAllYourVectorsAsColumns]
(and if you'll do it by hand, you can see , it's essentially The Same Thing!)
(*)

2,explanation:
Let's your I,J,K (it's vectors,right?) is packed into matrix IJK
Let's your I',J',K' is packed into matrix IJK'
So we have
IJK' = UnknownMatrix * IJK
and if we find that UnknownMatrix, that's will be exactly what you want.

If we multiply both sides of equality from the right with
Inverse(IJK)
we will have
IJK'*Inverse(IJK) = UnknownMatrix * IJK * Inverse(IJK)
But
IJK*Inverse(IJK)= Identity (by definition of inverse)
So
IJK'*Inverse(IJK) = UnknownMatrix
aka
UnknownMatrix = IJK'*Inverse(IJK)
We just found our unknown matrix, congratulations.

Simple,uh?

*******
(*): As someone said, "algorithm is always preferable to hand-written loop". This phrase sounds really stupid, but at least with matrices i totally agree.

[Edited by - Dmytry on September 13, 2004 1:17:17 PM]

##### Share on other sites
The camera can have pitch roll and yah, though for the most part, the camera will be generlaly used to give a SimCity/RailRoadTycoon isometric view of a city. All of this was very easy and simple when dealing with a flat world, the rotations only came up when having to deal with the whole earth.

I follow the math Dmytry did and think I can use it....

The plan of work is as follows:

The old IJK will be defined as
i: the vector from the center of the earth, simply camera positoin normalized.
j: the cross product of i and the view direction
k: the cross product if i and j.

There are circumstances where this wont work, i.e. when view direction is = i, but that is not the case concerned.

The new axes will be

I: vector from the center of the earth to new position, simply
K: tangent vector to the earth (returned by the move across earth function)
J: cross product of the two

Then I can use the math from Dmytry and the position/tangent vector returned from the moveAcrossTheEarth function and preserve the camera angle in respect to the ground, to keep that SimCity feel of movement.

##### Share on other sites
FYI, this question is fine, as it is directly related to math for games. Even if it happened to have been homework, I'd have allowed it, :).

##### Share on other sites
Quote:
 Original post by jm_hewittThe camera can have pitch roll and yah, though for the most part, the camera will be generlaly used to give a SimCity/RailRoadTycoon isometric view of a city. All of this was very easy and simple when dealing with a flat world, the rotations only came up when having to deal with the whole earth.I follow the math Dmytry did and think I can use it....The plan of work is as follows:The old IJK will be defined asi: the vector from the center of the earth, simply camera positoin normalized.j: the cross product of i and the view directionk: the cross product if i and j.There are circumstances where this wont work, i.e. when view direction is = i, but that is not the case concerned.The new axes will beI: vector from the center of the earth to new position, simplyK: tangent vector to the earth (returned by the move across earth function)J: cross product of the twoThen I can use the math from Dmytry and the position/tangent vector returned from the moveAcrossTheEarth function and preserve the camera angle in respect to the ground, to keep that SimCity feel of movement.

i'd do that differently.
simpler:
Calculate vector from camera to center of earth before moving camera. Move camera. Rotate the camera by shortest-arc rotation between current vector to center of earth and previous one.
(if vectors is columns, and your camera rotation matrix is M, use M*ThatShortestArcRotationMatrix)

more mathematically stable:
1:when player rotates camera, calculate vector from camera to earth in camera's system of coordinates, let's name it V.

2:when player moves the camera, move the camera, then compute new vector to center of earth in camera's system, let's name it NewV.
Then rotate camera by shortest ark rotation that turns NewV to V.
(if vectors is columns, and your camera rotation matrix is M, use ThatShortestArcRotationMatrix*M , because V and NewV is in camera's system)
(edit: and there you don't need to set V=NewV (!) so V is always preserved as player moves, and everything is stable. In simpler version, camera orientation "relatively to earth" will move due to calculation errors)

It's simpler to do with quaternions, but you can do it with matrices as well.

Shortest arc rotation in axis&angle form have axis equal to cross-product of vectors and angle equal to angle between vectors. (and good that it's very unlikely you'll have to rotate by 180 degrees)

look there

[Edited by - Dmytry on September 14, 2004 3:14:57 AM]

##### Share on other sites
I do have a quaternion class and after reading the quaternion solution I followed what it was doing (which isnt always the case with quaternion solutions), its reltatively simple and exactly what is needed.

This same quaternion operation can also be used to orientate buildings/planes/objects on the earth so that their models can properly line up on the ground.

##### Share on other sites

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628730
• Total Posts
2984426

• 25
• 11
• 10
• 16
• 14