How can I find the offset rotation and position between two objects in 3D?

Game Development Asked by Mahmoud Sagr on December 27, 2021

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

2 Answers

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;

Answered by Oren on December 27, 2021

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

Answered by Arthur on December 27, 2021

Add your own answers!

Related Questions

Compute shader: Property at kernel index is not set

1  Asked on November 2, 2021 by glen-pierce


How can I cast light onto a sprite in 3D environment?

1  Asked on November 2, 2021 by vern-rowan


How to run skactions randomly

0  Asked on November 2, 2021 by why25


AntiGravity Vehicle?

1  Asked on November 2, 2021 by roswell


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir