Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6253293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:57:50+00:00 2026-05-24T13:57:50+00:00

I would like to understand how to measure the distance between two 3D objects,

  • 0

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.

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. 

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. 

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. 

This is the parent component, this update is run every frame:

[Serializable]
    public class Component_Parent : BaseComponentAutoSerialization<ISceneEntity>
    {
        public override void OnUpdate(GameTime gameTime)
        {
            PassThrough.ParentMatrix = ParentObject.World;
            PassThrough.ParentTranslation = ParentObject.World.Translation;


        }
    }

This next part is the child component:

[Serializable]
    public class Component_Child : BaseComponentAutoSerialization<ISceneEntity>
    {
        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));
                }
            }
        }
    }

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.

Additional:

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.

I’ve uploaded a demo video to try and explain:

http://www.youtube.com/watch?v=BzAKW4WBWYs

I’ve also pasted up the complete code for the components, the static pass through and the scene entity.

http://pastebin.com/5hEmiVx9

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:57:51+00:00Added an answer on May 24, 2026 at 1:57 pm

    The issue was the way I was trying to use the offset of the world space.
    Thanks to flashed from #XNA on EFnet, this code works perfectly:

     [Serializable]
        public class Component_Child_fromxna : BaseComponentAutoSerialization<ISceneEntity>
        {
            Vector3 _parentOffset;
            Matrix _ParentMatrixOffset;
    
    
            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)
                    {
                        // The old offset - This is just in world space though...
                        _parentOffset = sceneObject.World.Translation - PassThrough.ParentTranslation;
    
                        // Get the distance between the child and the parent which we keep as the offset
                        // Inversing the ParentMatrix and multiplying it by the childs matrix gives an offset
                        // The offset is stored as a relative xyz, based on the parents object space
                        _ParentMatrixOffset = sceneObject.World * Matrix.Invert(PassThrough.ParentMatrix);
                    }
                }
                else
                {
                    if (_parentOffset != Vector3.Zero)
                    {
    
                        //Matrix pLocation = Matrix.CreateTranslation(_parentOffset);
                        //sceneObject.World = Matrix.Multiply(pLocation, PassThrough.ParentMatrix);
    
                        sceneObject.World = Matrix.Multiply(_ParentMatrixOffset, PassThrough.ParentMatrix);
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to understand which is the difference between these two programming concepts.
I would like to understand that what is Mesh Object and its conection with
I would like to understand the difference between the Boolean and boolean types in
I would like to understand what class << self stands for in the next
I am developing a Rails application and would like to understand when to use
I would like to better understand the basic steps needed to a take an
I would like to know and understand the steps involved in fetching mail from
I would like to know this to understand why some games like Mario is
I would like to know about the Invoke(delegate) method. I do not understand why
Conceptually, I would like to accomplish the following but have had trouble understand how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.