1
\$\begingroup\$

I have two objects in 3 dimensions, one of them (Child) is attached to the other one (Parent) using the offset between them (XYZ - position and rotation). Using the Vector3 library, how can I find that Child offsets from the Parent?

Further explanation: let's say we have the parent at position X:0 Y:0 Z:0 with rotations RX:0 RY:0 RZ:0, and there is an attached Child to it with the offset X:10 Y:10 Z:10, RX:90 RY:45 RZ:90 (which is what I want to know). Supposedly, I had the Parent with a different position and rotation, how can I get the offset between it and the Child (which in this case, it should be X:10 Y:10 Z:10, RX:90 RY:45 RZ:90) (in another word, I want the Child position and rotation as the Parent is the center of the map)

I've tried simple math and some other ways on the internet but with no success, since I'm pretty bad at calculating rotations (since vector3 libraries are pretty identical, so as Math libraries, any language should be fine).

\$\endgroup\$
3
  • \$\begingroup\$ What to derive position and rotation from? \$\endgroup\$
    – Ocelot
    Sep 25, 2019 at 3:16
  • \$\begingroup\$ @Ocelot It is a Matrix that has self.position and rotation, also it has self.up, forward and right (increment by one), also it has self.transformPosition(vector3 position_offset), self.transformRotation(vector3 rotation_offset), returns the position-rotation in the 3D world, which I want the reverse of it \$\endgroup\$ Sep 25, 2019 at 13:18
  • \$\begingroup\$ Ok. if I understand correctly, you know in local space the rotation and orientation of the child to the parent correct? If that is the case, then you should use the world matrix (which you use to place your parent in the world) against the position of the child in local space (this gives you the offset vector orientated to your model in world space). For orientation, this should be a similar methodology. \$\endgroup\$
    – ErnieDingo
    Sep 25, 2019 at 22:38

2 Answers 2

0
\$\begingroup\$

To get the local position, rotation you can decompose the local matrix of the child object. You can calculate the local matrix by multiplying the inverse global matrix of the parent with the global matrix of the child. Like this (example with glm)

Matrix4 invParentMatrix = glm::inverse(GetParent()->GetGlobalMatrix()); LocalChildMatrix = invParentMatrix * GlobalChildMatrix;

You can also calculate the local position seperatly by using the same inverse parent matrix. Calculating the local rotation is a bit more work and also depends on your implementation.

Matrix4 invParentMatrix = glm::inverse(GetParent()->GetGlobalMatrix()); LocalChildPosition = invParentMatrix * glm::vec4(GlobalChildPosition, 1.f);

\$\endgroup\$
-2
\$\begingroup\$
Transform  A , B;

Vector3 avg_transform_up = (A.up + B.up)/2f;
Vector3 avg_position = (A.position + B.position)/2f;

transform.position = avg_position ;
transform.up = avg_transform_up;

\$\endgroup\$
1
  • 2
    \$\begingroup\$ This does not answer the question. It also lacks details about what's going on: chunks of code could fix the OPs issue, but it's not really helping them what's going on. \$\endgroup\$
    – Vaillancourt
    Oct 28, 2019 at 12:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .