1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Can you show the code that you use for drawing a rotated Pixmap? \$\endgroup\$ – Pikalek Jan 5 at 18:22
  • \$\begingroup\$ 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 :-) \$\endgroup\$ – user1344545 Jan 5 at 18:24
  • \$\begingroup\$ 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. \$\endgroup\$ – Pikalek Jan 5 at 19:23
  • \$\begingroup\$ 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 ? \$\endgroup\$ – user1344545 Jan 6 at 11:08
  • \$\begingroup\$ 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. \$\endgroup\$ – Pikalek Jan 6 at 20:18
1
+50
\$\begingroup\$

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());

    final double radians = Math.toRadians(angle);
    final double cos = Math.cos(radians);
    final double sin = Math.sin(radians);    

    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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.