View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Parent child relatioship using quaternions

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Rickard_Hermansson  Members

Posted 05 January 2013 - 03:41 PM

Hello.

I have two game objects.

The final world transform of each object is calculated by concatenating the objects local transform with that parents transform, like so:
world =  Matrix.Scaling(scale) * Matrix.RotationQuaternion(orientation) * Matrix.Translation(position)
world *= parent.world;
Now, to my problem.

the scenario is:

1. I pitch objectA 90 deg around the world x-axis (position is (0,0,0))
2. I move objectB a few units along the x-axis (position is (3,0,0))
3. I set the objectA as the parent of objectB

Now I decide to yaw objectB in world space, the operation would be (using quaternions):
objectB.orientation = qYaw * objectB.orientation;
BbjectB will rotate around the world z-axis since it is parallel to the parents y-axis.

I need to account for the parent's oerientaton when performing rotations in world space, and I have no idéa how
to do that. I have tried to use the conjugate of the parents orientation, but without success.

Edited by Rickard_Hermansson, 05 January 2013 - 05:00 PM.

Posted 05 January 2013 - 03:55 PM

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3Rickard_Hermansson  Members

Posted 05 January 2013 - 04:59 PM

Do you mean when creating my world matrix or when performing the rotaion?

currently the object position,orientation, and scale is relative to the parent.

premultiplying it would mean that the orientation is stored relative to the world rather than the parent?

Posted 05 January 2013 - 05:04 PM

Perform the rotation in local space but then premultiply the resulting quaternion by the parents orientation quaternion, then convert to a matrix... that should work as long as there is no non-uniform scale. q = q1 * q2 is the rotation quaternion for q1 followed by q2.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #5Rickard_Hermansson  Members

Posted 05 January 2013 - 06:16 PM

I asume this mean I should no longer use tha parent world matrix.
The code for creating the world matrix now looks like:
How ever, the result is the same.
if (parent != null)            {                Quaternion q = orientation * parent.orientation;                Vector3 p = parent.Position + position;                p = Vector3.Transform(p, parent.orientation);                world = Matrix.Scaling(scale) * Matrix.RotationQuaternion(q) * Matrix.Translation(p);                               // world *= parent.world;            }            else            {                world = Matrix.Scaling(scale) * Matrix.RotationQuaternion(orientation) * Matrix.Translation(position);            }

if i do:
childMesh.Transform.Roll(deltaTime * 5f, TransformSpace.World);
The child will rotate around the y-axis because of the parent orientation and I wan't it tor rotate around the z-axis.

Edited by Rickard_Hermansson, 05 January 2013 - 06:26 PM.

### #6Rickard_Hermansson  Members

Posted 05 January 2013 - 06:25 PM

The code for making th rotations

 /* The rotation we wan't too add to this orientation*/
Quaternion q = Quaternion.RotationAxis(axis, angle);
q.Normalize();
switch (space)
{
case TransformSpace.World:
orientation = q * orientation;
break;

case TransformSpace.Local:

orientation = orientation * q;
break;
}

### #7Rickard_Hermansson  Members

Posted 06 January 2013 - 11:18 AM

Since the parent is rotated forward the inital rotation of the child will also be rotated forward.

I used, being a bit stupid, a unit cube to visualize the child, so I did not notice that it had the same inital orientation of the parent. (ofcoruse it did!)

### #8Rickard_Hermansson  Members

Posted 08 January 2013 - 12:11 PM

I asked around in tge Ogre 3D forum how it's done there. I seems that the quaternion multiplication order differs from ogre and sharp dx (atleast is seems that way), I have asked on their forum. In ogre the multplication order is right to left.

in the XNA documentation is say the product of a quaternion multiplication = Q2 * Q1, e.g right to left.

any ways, my code for rotating in the desired transform space :

   case TransformSpace.Parent:

relativeOrientation =  relativeOrientation * q;
break;

case TransformSpace.World:

relativeOrientation = WorldOrientation * q * worldOrientationInverse * relativeOrientation;
break;

case TransformSpace.Local:

relativeOrientation = q * relativeOrientation;
break;


Now, the order of multiplication here is the reverse of what ogre is using.
When I send the worldmatrix to the shader I have to transpose it, Sharp DX use row major and the HLSL shader expect matrices as column major. So I transpose the world matrix before sending it to the shader.

Is this why I must reverse the order?

Edited by Rickard_Hermansson, 08 January 2013 - 02:12 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.