Note that there are some explanatory texts on larger screens.

plurals
  1. POQuaternion rotation around a pivot point in XNA
    primarykey
    data
    text
    <p>Situation: I have a 3d model which I am attempting to rotate and orbit around a pivot point. Let's for now say the model is a pencil, and it's pivot point is a Vector3(0, 0, 0). The pencil nib should always be touching the pivot point, and it should rotate around each axis depending on the users button press. The initial offset of the pencil to the pivot point is a Vector3(0, 10, 0). </p> <p>I have been using quaternions in order to avoid gimbal lock, although I think I may be experiencing it through the incorrect use of quaternions. </p> <p>My current code is: </p> <pre><code>public void UpdateRotation(Vector3 pivotPoint) { KeyboardState keyboardState = Keyboard.GetState(); Vector3 tempPos = pivotPoint - position; if (keyboardState.IsKeyDown(Keys.X)) { // Rotate around X axis. Vector3 temp = position - pivotPoint; temp = Vector3.Transform(temp, Matrix.CreateRotationX(rotateSpeed)); position = pivotPoint + temp; // Update the direction the object is oriented. rotation *= Quaternion.CreateFromAxisAngle( Vector3.Right, rotateSpeed); } if (keyboardState.IsKeyDown(Keys.Y)) { // Update the direction the object is oriented. rotation *= Quaternion.CreateFromAxisAngle( Vector3.Up, rotateSpeed); } if (keyboardState.IsKeyDown(Keys.Z)) { // Rotate around Z axis. Vector3 temp = position - pivotPoint; temp = Vector3.Transform(temp, Matrix.CreateRotationZ(rotateSpeed)); position = pivotPoint + temp; // Update the direction the object is oriented. rotation *= Quaternion.CreateFromAxisAngle( Vector3.Backward, rotateSpeed); } } </code></pre> <p>and this is in the DrawModel() function:</p> <pre><code> effect.World = Matrix.CreateScale(gameobject.scale) * Matrix.CreateFromQuaternion(gameobject.rotation) * Matrix.CreateTranslation(gameobject.position); </code></pre> <p>Basically the model will rotate around a single axis ok, and I can even have it rotate around the x or z and the y axis, but if i attempt rotation around all three, the rotation will become skewed and the pencils nib will leave the pivot point. </p> <p>Any light you can shed on this situation would be greatly appreciated. I have a feeling that I am not using the Matrices correctly, but I can't find an order that works. </p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload