I'm building a 2D game using LibGDX. I have a collection of Entities each with their own X and Y positions within the 'world'. The player moves around the world and also has its own X and Y world coordinates, and I use the LibGDX OrthographicCamera which is updated each loop to center on the player location. The player's forward direction is always north relative to the viewport.
What I want to be able to "rotate" the view of the player (essentially by reverse-rotating the world objects using the player location/camera centerpoint as the origin).
I'm attempting to do this using Affine transformations. I had tried something like this each time render is called for the world entities.
Affine2 af = new Affine2(); af.translate(player.x, player.y); // Move origin to centerpoint af.rotate(entity.rotation); // For testing this value has 1 added or subtracted to it // when I hold down Q and E to simulate the camera rotation. af.translate(-player.x, -player.y); // Reset origin transform af.translate(entity.x, entity.y); // Move to world location .... // Draw with the af matrix
The thing that I didn't realise at first, is that once the rotation is performed, it also rotates the X and Y axis. So any subsequent translations are done relative to the rotation, and not along the original vertical and horizontally aligned axis, so these objects are not ending up in the correct location at all, and additionally they move around as the player moves which is incorrect behaviour.
What would be the correct way to calculate the transforms so I can rotate these Entities around the player, and then move them into the correct world position based on the rotation?
So when I would want the view to "turn" clockwise, the objects would rotate around the player location counter-clockwise (dotted green box), but if the player moves around the world (i.e. forwards, backwards, left, right) then the entity locations shouldn't be changing.
Edit: After thinking about this more, I'm wondering if I'm going about this entirely the wrong way? I still need the character's X/Y to be "correct" within the world based on the rotation, so actually I should be rotating the player, and then also the view of the world?