# Move and rotate one object to another C++ [3D]

I have two object, need attach 'TWO OBJECT' to 'ONE OBJECT'.

I have vectors 'angle' 'origin' of ONE and TWO objects.

I tried do "TWOOBJECT.origin += ONE.origin - ONE.oldorigin", this just attach coordinates, but not rotate with one object....

You need to associate a matrix with each object.

The matrix of the parent object is in world space, so the translation part contains the position of the parent in the world.

The matrix of the child object is in parent's space, so the translation part contains the relative position of the child to the parent.

Note that the same goes for the rotational part of the matrix: the parent's absolute rotation in the world is encoded. The child's relative rotation to the parent is encoded.

To know where the child ends up in the world, you need to multiply these two matrices.

The order of this multiplication depends on your conventions, whether you pre-multiply or post-multiply your vectors with a matrix.

So it could either be Mp X Mc or else Mc X mP

The inventor of homogeneous coordinates used vertices as row vectors multiplied by a matrix.

So, the mesh vertices for child shape would end up being computed as:

      v        *       Mc        *        Mp

|v0 v1 v2 1 |  * | .. .. .. 0 |     | .. .. .. 0 |
| .. .. .. 0 |  *  | .. .. .. 0 |
| .. .. .. 0 |     | .. .. .. 0 |
| ox oy oz 1 |     | tx ty tz 1 |


With o as the child's offset to parent, and t the position of the parent in the world.

Note that every game engine associates a matrix with an object. If the game engine uses Scene Graphs, it is as easy as putting the child object as a child of the parent object in this graph, and you are done.