title: Distance between Matrix Objects, relative to parents orientation, using XNA?
body: <p>I would like to understand how to measure the distance between two 3D objects, let's call them a parent object and a child object. Think of the parent as the body of a car and the child being a wheel of the car. </p>
  
  <p>I understand how to get the difference based on the objects position in world space but I would like to get the difference as a measurement based on the parents relative object space. </p>
  
  <p>E.g if the parent is facing East and the child is 2X, 3Y from the parent, measured in a relative sense. Such that if the parent rotated 60 degrees, the relative location of the child remains at a distance of 2x, 3y in the object space. Where as in a world space sense the child objects measurement as a Vector3 would be quite different. </p>
  
  <p>Basically I just want a predictable way to get the difference so that a child object which is on the right of the patent can always stay right of the parent object. </p>
  
  <p>This is the parent component, this update is run every frame:</p>
  
  <pre><code>[Serializable]
      public class Component_Parent : BaseComponentAutoSerialization&lt;ISceneEntity&gt;
      {
          public override void OnUpdate(GameTime gameTime)
          {
              PassThrough.ParentMatrix = ParentObject.World;
              PassThrough.ParentTranslation = ParentObject.World.Translation;
  
  
          }
      }
  </code></pre>
  
  <p>This next part is the child component:</p>
  
  <pre><code>[Serializable]
      public class Component_Child : BaseComponentAutoSerialization&lt;ISceneEntity&gt;
      {
          Vector3 _parentOffset;
          Quaternion _parentQuaternionOffset;
  
          public override void OnUpdate(GameTime gameTime)
          {
              // Get a sceneobject from the ParentObject
              SceneObject sceneobject = (SceneObject)ParentObject;
  
              // This relies on the position never being at 0,0,0 for setup, so please don't do that
              // or change it with more look ups so that you don't need to rely on a Zero Vector3 :-)
              if (PassThrough.GroupSetupMode || _parentOffset == Vector3.Zero)
              {
                  if (PassThrough.ParentTranslation != Vector3.Zero)
                  {
                      _parentOffset = sceneobject.World.Translation - PassThrough.ParentTranslation;
  
                      // Decompose World Matrix (Parent)
                      Quaternion parentQ = new Quaternion();
                      Vector3 parentSpot = new Vector3();
                      Vector3 parentScale = new Vector3();
                      PassThrough.ParentMatrix.Decompose(out parentScale, out parentQ, out parentSpot);
  
                      Matrix identity = Matrix.Identity;
  
                      // Decompose Identity Matrix (Parent)
                      Quaternion identityQ = new Quaternion();
                      Vector3 identitySpot = new Vector3();
                      Vector3 identityScale = new Vector3();
                      identity.Decompose(out identityScale, out identityQ, out identitySpot);
  
                      _parentQuaternionOffset = identityQ - parentQ;
                  }
              }
              else
              {
                  if (_parentOffset != Vector3.Zero)
                  {
  
                      // Decompose World Matrix (Child)
                      Quaternion rotationQ = new Quaternion();
                      Vector3 spot = new Vector3();
                      Vector3 scale = new Vector3();
                      sceneobject.World.Decompose(out scale, out rotationQ, out spot);
  
  
                      // Decompose World Matrix (Parent)
                      Quaternion parentQ = new Quaternion();
                      Vector3 parentSpot = new Vector3();
                      Vector3 parentScale = new Vector3();
                      PassThrough.ParentMatrix.Decompose(out parentScale, out parentQ, out parentSpot);
  
                      Matrix location = Matrix.CreateTranslation(PassThrough.ParentTranslation);
                      Matrix rotation = Matrix.CreateFromQuaternion(parentQ);
  
                      Matrix rotation2 = Matrix.CreateFromQuaternion(_parentQuaternionOffset);
  
                      Matrix newWorld = rotation * location;
  
                      Vector3 testTranslation = newWorld.Translation + ((newWorld.Left * _parentOffset.X) + (newWorld.Up * _parentOffset.Y) + (newWorld.Forward * _parentOffset.Z));
                      Matrix scaleM = Matrix.CreateScale(scale);
  
  
  
                      //sceneobject.World = scaleM * (rotation * (Matrix.CreateTranslation(testTranslation)));
                      sceneobject.World = (Matrix.CreateTranslation(testTranslation));
                  }
              }
          }
      }
  </code></pre>
  
  <p>I think it has something to do with keeping track of an offset rotation, from the identity matrix and I have started trying to add some code to that effect but really unsure of what next now.</p>
  
  <p>Additional:</p>
  
  <p>If I have the parent object facing the direction of the world space it all works, if it's facing a different direction then it's an issue and the child seems to rotate by the same amount when they are grouped together.</p>
  
  <p>I've uploaded a demo video to try and explain:</p>
  
  <p><a href="http://www.youtube.com/watch?v=BzAKW4WBWYs" rel="nofollow">http://www.youtube.com/watch?v=BzAKW4WBWYs</a></p>
  
  <p>I've also pasted up the complete code for the components, the static pass through and the scene entity.</p>
  
  <p><a href="http://pastebin.com/5hEmiVx9" rel="nofollow">http://pastebin.com/5hEmiVx9</a></p>
  
  <p>Thanks</p>