• Create Account

## Rotation Matrix from Vector in DirectX

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1lukesmith123  Members

153
Like
0Likes
Like

Posted 17 March 2012 - 10:04 AM

Hi,

I have a direction vector and I want to create a rotation matrix from it. This is what I am trying:

//direction = 0, 0, 1
//up		   = 0, 1, 0
//right		= 0, 0, 0

D3DXVec3Normalize(&direction, &direction);
//right
D3DXVec3Cross(&right, &up, &direction);
D3DXVec3Normalize(&right, &right);

rot(0,0) = right.x;
rot(1,0) = right.y;
rot(2,0) = right.z;
rot(3,0) = 0;
rot(0,1) = up.x;
rot(1,1) = up.y;
rot(2,1) = up.z;
rot(3,1) = 0;
rot(0,2) = direction.x;
rot(1,2) = direction.y;
rot(2,2) = direction.z;
rot(3,2) = 0;
rot(0,3) = 0.0f;
rot(1,3) = 0.0f;
rot(2,3) = 0.0f;
rot(3,3) = 1.0f;


I had thought that his would work however it gives me an error where the model that uses the matrix is stretched and appears as a giant line across the screen.

Am I doing something wrong?

### #2Álvaro  Members

20244
Like
0Likes
Like

Posted 17 March 2012 - 11:23 AM

Your matrix always maps (0,1,0) to (0,1,0), but that's probably not what you want. What rotation exactly does the vector represent?

### #3lukesmith123  Members

153
Like
0Likes
Like

Posted 17 March 2012 - 11:53 AM

I'm a little confused but the vector is the objects heading. So if the vector was 0,0,1 it would face on the positive z and if it were 0,0,-1 it would face -z.

### #4Álvaro  Members

20244
Like
0Likes
Like

Posted 17 March 2012 - 12:17 PM

A direction in three dimensions is not enough to describe a rotation. Think of a pencil pointing somewhere: You can still rotate it around the axis and it still points to the same place.

Perhaps you are trying to build a lookAt matrix. That looks a lot like your code.

### #5lukesmith123  Members

153
Like
0Likes
Like

Posted 17 March 2012 - 04:14 PM

Ah that would explain why I'm getting so confused. So what would be the next step after determining the objects heading vector to create the rotation matrix?

### #6Álvaro  Members

20244
Like
-1Likes
Like

Posted 17 March 2012 - 04:28 PM

Imagine I asked you that x is if I know that x+y=7. Obviously, you'd tell me that you can't tell me what x is, because there are many solutions. Well, your situation is very similar: Your problem is not completely specified.

### #7lukesmith123  Members

153
Like
0Likes
Like

Posted 17 March 2012 - 06:31 PM

My problem is that I have the direction vector that the object should be facing and I want to create the world matrix with the object rotated in the direction defined by the vector.

I had thought that I could do this simply by finding the right vector from the direction vector and the up vector and filling the rows of the matrix with this information. But that appears to be incorrect so now I am wondering how this can be achieved?

### #8clb  Members

2143
Like
1Likes
Like

Posted 17 March 2012 - 06:32 PM

Another source for a LookAt matrix (code here).
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

### #9lukesmith123  Members

153
Like
0Likes
Like

Posted 17 March 2012 - 07:07 PM

Ok so is it possible to orient an object with a lookat matrix? otherwise how do I go from direction vector to world matrix?

### #10lukesmith123  Members

153
Like
0Likes
Like

Posted 17 March 2012 - 08:07 PM

EDIT: Ah I realised I had made a stupid mistake elsewhere and the code that I originally posted was fine. Sorry!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.