0
votes

So I am working on an Android game and I have two images on the screen. I currently have the pixel-perfect collision detection working great. My problem is when I rotate one of the images and I check the pixels for a collision. The pixels are still oriented in the original way the image was loaded, so it is not as perfect as it was... I can get all of the pixels in an array, or a 2d array. But I am currently just accessing them by the getPixel(x, y) method in the Bitmap class.

Does anyone know of an algorithm to rotate the values in an array based on an arbitrary number of degrees? Or any other way of solving this problem?

2
Are you sure you are accessing pixels of the rotated image not the original one? - GETah
Well I was thinking about that, but when I load the bitmap at the beginning I am pretty sure that it stores the pixels once, but doesn't update them on rotation. I even tried to update them manually using the getPixels() and setPixels() methods on rotation. - DRiFTy

2 Answers

1
votes

Have you looked at AffineTransform?

It's what I have used to rotate sprites and images in the past.

1
votes

I used this code to rotate and get a pixel value from an image:

Image rotatedImage = new BufferedImage(imageToRotate.getHeight(null), imageToRotate.getWidth(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
// Set rotation here
g2d.rotate(Math.toRadians(90.0));
g2d.drawImage(imageToRotate, 0, -rotatedImage.getWidth(null), null);
g2d.dispose();
int pixelColor = ((BufferedImage)rotatedImage).getRGB(x, y);