# Parent child relatioship using quaternions

This topic is 1841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
Just premultiply your quaternion orientation by your parents quaternion orientation?

##### Share on other sites
Just premultiply your quaternion orientation by your parents quaternion orientation?

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?

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites

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;
}

##### Share on other sites

Well I made a mistake.

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!)

Thank you for your help!

##### Share on other sites

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