3

I have some understanding problem concerning quaternions.

In order to have my world object rotate in the correct way, I need to invert their quaternion rotation while refreshing the object world matrix.

I create the object rotation with this code:

Rotation = Quaternion.RotationMatrix(Matrix.LookAtRH(Position, Position + new Vector3(_moveDirection.X, 0, _moveDirection.Y), Vector3.Up));

and refresh the object World matrix like this:

Object.World = Matrix.RotationQuaternion(Rotation) * Matrix.Translation(Position);

This is not working; it makes the object rotate in the opposite way compared to what it should!

The is the way that makes my object rotate correctly:

Object.World = Matrix.RotationQuaternion(Quaternion.invert(Rotation)) * Matrix.Translation(Position);

Why do I have to invert the object rotation?

Fabian
  • 81
  • 1
  • 5
  • We sure need to know more of you transformation framework. Do you use matrix * column-vector convention or some strange row-vector * matrix convention? Maybe your Lookat function is intended for cameras and therefore does the inverse transformation to what you actually want. Also in a matrix*vector convention your combined transformation first translates the object and then rotates it. Is this even intended? – Christian Rau Sep 19 '11 at 21:43
  • 2
    I'm not sure what quaternions are gaining you here. You seem to be generating a matrix, converting it to a quaternion, and then converting it straight back to a matrix? Does the inversion issue still occur if you forget the quaternion and simply keep it as a matrix the whole time? BTW, @ChristianRau, row-vector convention is standard for some people, it's not "strange". :) – Nathan Reed Sep 19 '11 at 22:12
  • @Christian I guess Microsofts DX dev team is quirky then :P. – hiddensunset4 Sep 19 '11 at 22:38
  • 1
    @Nathan I was educated with column-vectors. I have never seen vectors treated as 1-row matrices in any mathematical context. The fact that some frameworks use row-vectors and that there isn't that much difference in practice, doesn't give them a mathematical justification. But maybe the latter is not that important for you. – Christian Rau Sep 19 '11 at 22:40
  • @Daniel obviously! – Christian Rau Sep 19 '11 at 22:41
  • Tbh, Im learning row-vector matrices in my uni course, so I guess that makes your comment subjective and without proof. But theres a thousand discussions on this, no result. Its like OpenGL vs DirectX, its not what you have, but how you use it. – hiddensunset4 Sep 19 '11 at 22:45
  • 2
    @ChristianRau, there's no "mathematical justification" either way. It's just a convention. It would be nice if it was universally one way or the other, but unfortunately both conventions are in widespread use. Column vectors are dominant in pure math, but row vectors are used quite often in applied contexts...but maybe the latter is not that important for you. :) – Nathan Reed Sep 20 '11 at 00:11
  • Hello, For my tansformation framework, it is DirectX API, so I think it is linked to row-vector matrix convention.

    I have quaternion mainly because its a more compact way of sharing a rotation than a matrix. (you have a network layer exchanging the quaternion data).

    So you thing that it could come from the Matrix.LookAtRH ? I will check it.

    – Fabian Sep 20 '11 at 06:30
  • If its the case (Matrix.lookAt is creating a view matrix) is there a straight forward way to create a quaternion from Position + Target vector ? – Fabian Sep 20 '11 at 06:41
  • @Fabian, yes, you will also need the up-vector though – Maik Semder Sep 20 '11 at 10:15

2 Answers2

3
Is there a straight forward way to create a quaternion from Position + Target vector

Yes, you will also need the up-vector though. look at my answer here

Maik Semder
  • 4,718
  • 1
  • 26
  • 24
2

take a look to my answer about use of quaternions to rotate objects. You can find a way to compute the quaternion based rotation matrix after the [edit] section

FxIII
  • 3,996
  • 20
  • 20