3

I have a superclass object which represents a model in a scene. This object contains a 3D vector, position for its location and three variables x, y and z to describe its rotation around those axes. Its transformation matrix is calculated as follows:

Matrix.CreateRotationX(x) *
Matrix.CreateRotationY(y) *
Matrix.CreateRotationZ(z) *
Matrix.CreateTranslation(position)

This is all working fine, except in some circumstances I need to calculate the values for x, y and z based on a normalised directional vector and an up vector. How can I do this?

  • I'm not entirely sure what you're asking, but if you want to know how to normalise a vector you use Vector3.Normalize() – annonymously Nov 28 '11 at 22:20
  • Just to confirm: Are you asking, for a given vector, how do you determine the equivalent rotation around the three axes? – lzcd Nov 28 '11 at 22:48
  • @lzcd Yes, for a given vector, I want the rotation for all three axes in the `x`, `y`, and `z` variables so that I can later calculate rotational matrices using the `CreateRotation...` methods of the `Matrix` class. – Matthew Bird Nov 29 '11 at 13:07

3 Answers3

0

Don't know how to do the math myself but wouldn't Matrix.CreateLookAt satisfy your needs?

Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(x, y, z), yourUpVector) 
* Matrix.CreateTranslation(position);
ClassicThunder
  • 1,896
  • 16
  • 25
  • It would except at the moment the superclass has the `x`, `y` and `z` components stored separately instead of using a `Matrix`. – Matthew Bird Nov 29 '11 at 13:12
0

Convert the matrix to a quaternion:

Quaternion q = Quaternion.CreateFromRotationMatrix(matrix);

then copy and paste the last method found on this page and run your quaternion through it.

Steve H
  • 5,479
  • 4
  • 20
  • 26
0

Same as ClassicThunder's answer (with the same limitation that it gives you the matrix you want, rather than the angles), except it's a bit nicer than messing with CreateLookAt (which is really for making a view matrix).

Matrix.CreateWorld(position, direction, up);

(MSDN)

IMO, you should probably shouldn't be using or storing the Euler rotation of an object anyway. (see also)

Community
  • 1
  • 1
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104