Calculating position & angle relative to a frame of reference

In a game I’m working on, the player controls the main gun turret of a space ship.

The turret’s sprite is a child of the spaceship’s sprite. As the ship moves and rotates, so does the turret. Quite often, the game needs to know/set what the “absolute” position & angle of the turret is. For example, the turret will rotate to point toward the player’s mouse when it’s within the viewport (the viewport being the part of the game representing the action).

The only way I’ve been able to get this to work is:

  1. Use localToGlobal to find out where the Sprite is relative to root.
  2. Use localToGlobal to find out where the viewport is relative to root.
  3. Calculate the difference. This gives me the turret’s position in the viewport.

To turn the turret toward the mouse:

  1. Get the mouse position in the viewport the normal way.
  2. Calculate the difference from the turret’s position.
  3. Use atan2 to get the “real” angle.
  4. Add the ship’s current rotation to the real angle to figure out what the turret’s sprite’s rotation should be.

Has anyone else found a better way to do this?