quaternion rotations

Dec 16, 2010 at 11:35 AM

hi,

I was trying to animate a node using quaternions but it becomes impossible because it's Rotation property is of Coordinate type (degree rotations along x,y,z), and there are singularities when converting from Quaternion to Coordinate such that when reaching 180 degree it will jump back to zero degree.

I tried changing the World matrix directly  from the outside of the Node class but then the Rotation property would not be updated and I would also have to replicate the work done in Node::PrepareActualWork to have it work properly.

I ended up changing the Coordinate class to support another property "W", so that the Node now can have the Rotation property be a proper quaternion, and changed the PrepareActualWorld method to rotate using the given quaternion, which works perfectly.

 

if (_isForcePrepareMatrices || Rotation.X != 0f || Rotation.Y != 0f || Rotation.Z != 0f || Rotation.W != 0f)
{
  var quat = new Quaternion((float)Rotation.X, (float)Rotation.Y, (float)Rotation.Z, (float)Rotation.W);
  var rotationMatrix = (Matrix)quat;
  //var rotationMatrix = Matrix.CreateRotation((float)Rotation.X, (float)Rotation.Y, (float)Rotation.Z);
  matrix = matrix * rotationMatrix;
}

Another alternative includes creating another Quaternion like class which is based on the Coordinate class and changing the Node::Rotation property to be an instance of it.

For ease of use I'm using a special type converter on the Node::Rotation property so that users are still able to specify the rotation property in Xaml in a "x,y,z degree rotation way" and have it converted to quaternion automatically.

Quaternions are essential for complex animations so I believe this is the way to go. What are your thoughts?

thanks.

rui silvestre

Coordinator
Dec 17, 2010 at 4:52 PM

Hi,

excellent input. 

Would introducing a class called Rotation that had all the X,Y,Z,W elements and used quaternions behind the scene be Ok? 

The reason I'd like to go that way is that I'd like to keep Coordinate without the W, and have something else. Love your input if you'd think that would be OK, or if you have a better  name for it.

 

Dec 17, 2010 at 5:35 PM

Hi,

I've done just that:

  1. created a new class (I'll name it Rotation as you suggested) so we don't mess Coordinate up
  2. changed the Node.Rotation property to be of Rotation type
  3. changed InstancingNodes.Rotation field to be of Quaternion type
  4. added a FromEulerAngles static method to the Quaternion class to facilitate quaternion creation
  5. changed the PrepareActualWorld to use the matrix from the rotation quaternion
  6. added a new RotationTypeConverter class to convert to and from Rotation type

If you want to include these changes let me know, I'll send them to you.

OT: I'm also interested in doing animations using balder, do you have any plans for that? can you give me any pointers so I can try and do it?

thank you very much

 

 

Coordinator
Dec 17, 2010 at 9:17 PM
Edited Dec 17, 2010 at 9:18 PM

Hi,

I'd love your changes. Sounds like you've done everything as I'd like to do it. 

Animation is something I've been wanting to do for quite a while, in fact it was one of the first things I did in Balder - but then only for 2D stuff. So I have a few ideas there. 

I'll contact you with a direct message.

 

 

And thank you for taking the time to do this!