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

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

## Related Questions

### Compute shader: Property at kernel index is not set

1  Asked on November 2, 2021 by glen-pierce

### Applying texture maps from Blender to Unity material properties

1  Asked on November 2, 2021

### 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

### How can I allow player-created content to add morphs to Daz Genesis 8 characters(UE4)?

0  Asked on November 2, 2021

### Unity 2018 can’t find android sdk path on mac osx

0  Asked on November 2, 2021

### Deforming a lip mesh to match face-tracking points

0  Asked on November 2, 2021 by anubhav-roy

### Check if 2 objects with multiple colliders overlap using bounds

1  Asked on November 2, 2021 by thrindil

### Position/orient parent to place child at a particular position/orientation

1  Asked on November 2, 2021 by noname123

### Tetris block kinematic rigidbodies pass through one another with Transform.Translate

1  Asked on November 2, 2021

### mesh gets twisted wildly when trying to use Unreal’s mannequin’s skeleton

1  Asked on November 2, 2021 by serhii

### AntiGravity Vehicle?

1  Asked on November 2, 2021 by roswell

### Voxel River Generation at Biome Borders or Biome Blending

0  Asked on November 2, 2021 by computer-craft

### Need help accessing booleans from other script Unity3D

1  Asked on November 2, 2021 by matheson

### LibGDX changing actors parents is resulting in unwanted (x,y) translation

1  Asked on November 2, 2021 by rocketscienceguy

### Unity: How to connect to server with status in dialog

1  Asked on November 2, 2021 by zechy

### How to make object move in exact gametime in Monogame, XNA?

1  Asked on March 11, 2021 by steyrix

### Bring the blender .blend file into unity : replacing materials

1  Asked on March 10, 2021

### My character gets stuck when It’s running on a straight level ground

1  Asked on March 5, 2021 by sleepysleeper