1

How can I draw an image to Pixmap, but rotated by some angle, like I can do when drawing on Batch? There is no Pixmap method that has an angle as parameter, like batch drawing methods have.

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • im not up for coding it now but you could use double for loops to loop through your original pixmap and get the pixels and move them to a new location in the new pixmap. seems easier to create a sprite and rotate that if u can – William Reed Mar 16 '14 at 19:52
  • Well, I'm using one huge pixmap as a map in my game. Sometime I'm changing it (and them moving it to texture, to speed up drawing) and sometime I'm reading pixels for detecting collisions with some moving objects, so sprites/textures are not good for me. Unfortunately, drawing rotated image is not supported, I guess.. – MilanG Mar 17 '14 at 09:54
  • http://stackoverflow.com/questions/12548532/how-to-flip-a-pixmap-to-draw-to-a-texture-in-libgdx?rq=1 try something like this. not exactly what u want but that will lead to your answer. this will be a somewhat intensive event – William Reed Mar 17 '14 at 20:50
  • Hmm...thanks. I didn't consider this kind of solution seriously, because I was afraid of performances I could get. But again, I don't do this kind of drawing very often, so I'll give it a try. – MilanG Mar 18 '14 at 15:55
  • you only have to do this to generate it not to draw it every frame – William Reed Mar 18 '14 at 20:58
  • Well theoretically yes, but in my case every drawing will have different angle so it means new calculation. It would be good if feature like this can be implemented into libGDX. Even software rendering would mean a lot. – MilanG Mar 19 '14 at 07:47
  • try explaining what you actually need this for... it seems like it can definately be avoided – William Reed Mar 20 '14 at 00:05
  • I'm holding game map in Pixmap but also in a Texture. I'm drawing only a texture to batch, because of speed. But time to time I have to change that map so I do the change in Pixmap and then I update Texture. I'm also using Pixmap for reading pixels. So far I was changing Pixmap by drawing simple forms, like circles. Now I need to update Pixmap by drawing rotated image. That's it. – MilanG Mar 20 '14 at 10:58
  • why not just draw everything individually instead of sending it to a pixmap – William Reed Mar 20 '14 at 21:29
  • Because I'm using pixmap also to read pixels from it. Not sure is it possible to read pixels from texture. And even it is, during the game number of those object that need to be drawn rotated would increase so I want to avoid impact on game performance. With pixmap, after every change I upload it to the texture and I always draw just one texture. That should be the fastest solution I think. – MilanG Apr 03 '14 at 06:45

2 Answers2

3

Ok, here is the code I managed to make (actually to borrow and adjust from here: Rotate Bitmap pixels ):

public 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), cos = Math.cos(radians), sin = Math.sin(radians);     


    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            final int
            centerx = width/2, centery = height / 2,
            m = x - centerx,
            n = y - centery,
            j = ((int) (m * cos + n * sin)) + centerx,
            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;

}

It creates and returns rotated Pixmap out of passed source Pixmap. It rotates around the center and it's actually pretty fast, on PC and on my HTC Sensation.

And don't forget to dispose Pixmap you get after the usage.

It would be nice to have system solution for this which would be more optimized, have rotation point coordinates, but this does the job for me, hopefully will be helpful for somebody else.

MilanG
  • 6,994
  • 2
  • 35
  • 64
1

Your code has the k and j reversed. As-is, an angle of 0 degrees should draw the same thing, but it doesn't. It rotates it 90 degrees. So the one line should be

rotated.drawPixel(x, y, src.getPixel(j, k));

instead of

rotated.drawPixel(x, y, src.getPixel(k, j));

Otherwise, works like a champ.

Dennis D
  • 11
  • 2
  • P.S. Are you sure that it's not flipped diagonally that way? – MilanG Sep 21 '18 at 08:09
  • I don't think so. I'm using it to create inventory icons for weapons in an RPG, and for an image of a sword that is normally straight up and down, an angle of 315 degrees is 45 degrees counter-clockwise (pointing North-West). – Dennis D Sep 22 '18 at 10:38