DRiFTy
DRiFTy

Reputation: 11369

Rotational Pixel-Perfect Collision Detection

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?

Upvotes: 0

Views: 667

Answers (2)

GETah
GETah

Reputation: 21409

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

Upvotes: 1

skynet
skynet

Reputation: 9908

Have you looked at AffineTransform?

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

Upvotes: 1

Related Questions