# How to rotate a pixmap (LIBGDX)

In my game I have a world with randomly generated islands. My islands are based on the pixel information of the LibGDX Pixmap class.

Now I need to rotate the islands. For this I need to rotate the Pixmaps. Not for drawing, but for reading the pixel data of the rotated Pixmap afterwards.

My problem is, that I don't know how to rotate the Pixmap, so I can read the pixel data of the rotated Pixmap.

Drawing a rotated Pixmap works well, but I need the rotated data persisted, so I can read it.

My code I started with, which doesn't work:

public Pixmap rotatePixmap(Pixmap pixmap, int angle){
Sprite sprite = new Sprite(new Texture(pixmap));
sprite.setRotation(angle);
return sprite.getTexture().getTextureData().consumePixmap();
}


How can I rotate the Pixmap, so the pixels are altered to the desired rotation?

• Can you show the code that you use for drawing a rotated Pixmap? – Pikalek Jan 5 '19 at 18:22
• As I wrote. I dont need the pixmap beeing displayed. Imagine I would like to save the rotated Pixmap back to a file. I need the bits of the rotated bitmap to create islands in my own syste,. So what I need is a Pixmap that is really rotated. Drawing a rotated Pixmap works well, but this is not what I need. I hope I explained it well :-) – user1344545 Jan 5 '19 at 18:24
• Right, I get that you want to generate rotated Pixmap data. What I'm asking is to see how you were drawing it rotated - the reason is, there might be a relatively simple way to use your some of your rotated drawing code to generate a new Pixmap that is also rotated. – Pikalek Jan 5 '19 at 19:23
• In my code you see that I am creating a Sprite and then setRotation. drawing the spring shows the Sprite (underlying Pixmap) rotated. I still don't don't understand how this can help ? – user1344545 Jan 6 '19 at 11:08
• Ah, I see, thanks for clarifying. I was hoping it would lead me to something in the API that directly manipulated/rotated data in a buffer that could then be used to initialize a new Pixmap. If there's such a thing in LibGDX, I didn't find it. I did find something else that might work though. – Pikalek Jan 6 '19 at 20:18

As taken from this post on stack overflow, you can manually build a new Pixmap by sampling from rotated position in the original Pixmap data:

public static Pixmap rotatePixmap (Pixmap src, float angle){
final int width = src.getWidth();
final int height = src.getHeight();
Pixmap rotated = new Pixmap(width, height, src.getFormat());

for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
final int centerX = width/2;
final int centerY = height / 2;
final int m = x - centerX;
final int n = y - centerY;
final int j = ((int) (m * cos + n * sin)) + centerX;
final int k = ((int) (n * cos - m * sin)) + centerY;
if (j >= 0 && j < width && k >= 0 && k < height){
rotated.drawPixel(x, y, src.getPixel(j, k));
}
}
}
return rotated;
}


Note: this solution returns a Pixmap with the same dimensions as the target. If you have a rectangular rather than a square Pixmap, this might not be quite what you want. Also, depending on your data, it's possible that some of the image will get clipped. If that's a problem, you'll need to do a little bit of math to adjust the size of the destination Pixmap to make it big enough to accommodate the result.

Calling Sprite.setRotation is realized when calling Sprite.getVertices or Sprite.set. These functions do not transform the underlying texture.

One option is to write your own code to preform the rotation. This website is an excellent resource for learning how to do so and what the pitfalls (and solutions) are.