99jonathan

I am currently trying to figure out the best way to represent an object that will be rotated and transformed in 3d. This is what I have so far:

Code Snippet

Vector3 Position;
float CurrentSpeed = 0;
Vector3 Direction;
Vector3 UpDirection;

The goal is to make everything compact and small. Now the problem comes up when I want to rotate the object. All I should have to do is change the Direction vector. I believe I can achieve this by using this matrix:
Matrix.CreateFromAxisAngle(UpDirection,.05f);
However, how do I then multiply my Direction by this rotation matrix

### Re: XNA Game Studio Express 3d rotations on different axis

Shawn Hargreaves - MSFT

That looks like a good way of storing orientation.

Use Vector3.TransformNormal to apply a matrix to one of your orientation vectors.

### Re: XNA Game Studio Express 3d rotations on different axis

99jonathan

So I would be correct in assuming that the following:

v3Direction = Vector3.Transform(v3Direction, mMatrix);
Vector3.Normalize(ref v3Direction, out v3Direction);

is the same as:

v3Direction = Vector3.TranformNormal(v3Direction, mMatrix);

So it takes out the extra step of Normalizing the vector And which is best to use: the void ref/out version or the method that returns a Vector3 (As in which is more efficient)

Thanks!!

### Re: XNA Game Studio Express 3d rotations on different axis

99jonathan

ok I am now having yet another problem. I am using the following in the World Matrix:

...*Matrix.CreateLookAt(Vector3.Zero,v3Direction,v3UpDirection)*...

This is supposed to rotate my model using the normalized Direction vector. Is this the correct way to do this I'm currently using this for my movement:

v3Position += Vector3.Multiply(v3Direction,v3CurrentSpeed);

The result of these two is that the dwarf is running right only when he is directly on the z-axis (or x possibly... i forget) and completely backwards on the x-axis. Which part of the code is the problem

### Re: XNA Game Studio Express 3d rotations on different axis

Shawn Hargreaves - MSFT 99jonathan wrote:
 So I would be correct in assuming that the following:v3Direction = Vector3.Transform(v3Direction, mMatrix);Vector3.Normalize(ref v3Direction, out v3Direction);is the same as:v3Direction = Vector3.TranformNormal(v3Direction, mMatrix);So it takes out the extra step of Normalizing the vector And which is best to use: the void ref/out version or the method that returns a Vector3 (As in which is more efficient)Thanks!!

Transform and TransformNormal are very different. The first includes the translation component of the matrix, while the second does not. This can give very different results, so it is important to use the right one depending on whether your vector holds position data or direction data.

Ref methods are usually faster (that's why they are included) but also make less readable code so I wouldn't bother with them except in places that you know for sure are causing performance issues.

### Re: XNA Game Studio Express 3d rotations on different axis

Shawn Hargreaves - MSFT 99jonathan wrote:
 ok I am now having yet another problem. I am using the following in the World Matrix:...*Matrix.CreateLookAt(Vector3.Zero,v3Direction,v3UpDirection)*...This is supposed to rotate my model using the normalized Direction vector. Is this the correct way to do this I'm currently using this for my movement:v3Position += Vector3.Multiply(v3Direction,v3CurrentSpeed);The result of these two is that the dwarf is running right only when he is directly on the z-axis (or x possibly... i forget) and completely backwards on the x-axis. Which part of the code is the problem

Matrix.CreateLookAt will do the opposite of what you want for a world matrix: it creates a camera matrix for looking at an object, which is exactly backward from what you need to transform the object itself.

Call Transpose on the matrix you get back from CreateLookAt, and all will be well.

### Re: XNA Game Studio Express 3d rotations on different axis

99jonathan

Thank you SO MUCH for your help!! It's people like you that make a difference in the experience of xna!