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?

I'm not sure I've managed to explain this too well, so I drew something to try and show this better: enter image description here

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?


2 Answers 2


This is usually solved by creating a view matrix, but because you're using libgdx, you can just use your camera's translate and rotate methods.

Camera#translate(float x, float y, floaf z)
Camera#rotate(float angle, float x, float y, float z) // Set the axis you're currently rotating around to 1
Camera#zoom(float n)
  • \$\begingroup\$ This is the correct answer. The problem describes exactly what a camera matrix does. \$\endgroup\$ Apr 4, 2018 at 12:33

Assuming that you mean that when you are rotating the character, the objects will rotate so it looks like you look to another side, then the camera should rotate and not objects.


You must log in to answer this question.

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