Rotating a joint relative to a parent's orientation
Posted: Sun Oct 07, 2012 3:29 pm
When moving an object attached to a joint I also translate the joint so the anchor point remains relative to its parent object.
When the joint is attached to the body the difference in orientation is calculated.
I would then like to update to joint's orientation so it remains at the same relative orientation to its parent's orientation.
The problem is that the joint will not rotate correctly and will instead spin rapidly.
I have tested the code above with a child entity. Its orientation will correctly rotate relative to its parent's orientation so the orientation values are correct.
However, when transforming the normal and anchor point by the same rotation matrix it will not work as intended. How can I apply the same method to the RevoluteJoint class?
Code: Select all
set
{
entity.Position = value;
// If we are allowing object to be translated when it possesses a joint
if (joint != null)
{
Vector3 jP = joint.AngularJoint.ConnectionA.Position + joint.BallSocketJoint.OffsetA;
Vector3 freeAxis = joint.AngularJoint.WorldFreeAxisA;
game.Space.Remove(joint);
// Anchor must be translated if the entity is moved
// Free axis argument must be in world space
joint = new RevoluteJoint(entity, null, jP, freeAxis);
game.Space.Add(joint);
}
}
Code: Select all
qDiff = Quaternion.Conjugate(entity.Orientation);
joint = value;
Code: Select all
set
{
entity.Orientation = value;
if (joint != null)
{
Vector3 jP = joint.AngularJoint.ConnectionA.Position + joint.BallSocketJoint.OffsetA;
Vector3 freeAxis = joint.AngularJoint.WorldFreeAxisA;
game.Space.Remove(joint);
Quaternion newOrientation = entity.Orientation * qDiff;
// Anchor and free axis must be rotated if the entity is rotated
Matrix m = Matrix.CreateFromQuaternion(newOrientation);
joint = new RevoluteJoint(
entity,
null,
Vector3.Transform(jP, m), // Error
Vector3.TransformNormal(freeAxis, m) // Error
);
game.Space.Add(joint);
}
}
I have tested the code above with a child entity. Its orientation will correctly rotate relative to its parent's orientation so the orientation values are correct.
However, when transforming the normal and anchor point by the same rotation matrix it will not work as intended. How can I apply the same method to the RevoluteJoint class?