I made a small game engine with a hierarchy, but have a with problem with applying transform down the hierarchy. I have an object with a parent, and I want the child to rotate around the parent with an offset (the childs localPosition, relative to the parent, and a parent rotation).

For example, I have a parent "Earth", and a child "Moon". The "Earth" rotates, and the "Moon" rotates with a 100, 50 offset.

How do I rotate an object around another object with an angle and an offset?

Each object has:

  • x / y localPosition
  • private x / y globalPosition
  • localRotation
  • private globalRotation

This is what I have, so far:

float atr = Deg2Rad(parent->GetGlobalRotation());
globalPosition.x = parent->GetGlobalPosition().x + cos(atr) * localPosition.x;
globalPosition.y = parent->GetGlobalPosition().y + sin(atr) * localPosition.y;

but it only only on the x-axis if localPositon is 50, 0


The solution is to use a rotation matrix:

globalPosition.x = parent.GetGlobalPosition().x 
    + (cos(atr) * localPosition.x) + (-sin(atr) * localPosition.y);

globalPosition.y = parent.GetGlobalPosition().y 
    + (sin(atr) * localPosition.x) + (cos(atr) * localPosition.y);
  • \$\begingroup\$ I think this answer would greatly improve if it didn't rely on a link for the explanation. Maybe you can mention a few things about rotation matrices and why they are the right solution, and then provide the link for people that want to read more about it. \$\endgroup\$
    – TomTsagk
    Sep 7 '18 at 8:11

You must log in to answer this question.

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