[java] Images/Collision and AffineTransformations

Started by
8 comments, last by erdirck 14 years, 5 months ago
I am making an asteroids like game and have everything the way I want it to be except for the collision. I looked around the internet and have a few questions and I also just want to clarify a few things. I read http://www.gamedev.net/community/forums/topic.asp?topic_id=329033 about collision detection and noticed that in roles code, he used a buffered image. I am using just an Image asteroid instead of BufferedImage asteroid and was wondering in order to have pixel perfect collision, I have to use bufferedImages? How can I get pixel perfect collision with just using the Image class? This brings me to my next question. I started to use the regular rectangle collision detection method and ran into another problem. My asteroid rotates using an AffineTransformation. However, while the asteroid is in a different position (rotation, the posX and posY coords of the asteroid remain the same. Therefore, the collision goes unnoticed. Graphics2D g2 = (Graphics2D) g.create(); AffineTransform transform = new AffineTransform(); transform.setToTranslation(posX, posY); transform.rotate(Math.toRadians(x)); g2.drawImage(enemyPic, transform, null); How can I get either the X and Y to change with the image rotation, or change the rotation method I am using to get the collision to work? I am fairly new to java and this is the first game I am working on so help would be appreciated. Thanks!
Advertisement
Quote:Original post by erdirck
I am using just an Image asteroid instead of BufferedImage asteroid and was wondering in order to have pixel perfect collision, I have to use bufferedImages?

How can I get pixel perfect collision with just using the Image class?


A BufferedImage provides access to the image's raw pixel data, either in bulk or one-by-one, whereas a Image is a generic interface to an Image, which doesn't provide this level of access.

Check this thread for info on how to go from an Image to a BufferedImage (in the OP), or how to get raw pixel input from a Image or load a BufferedImage directly (probably the best idea), in my post.

Quote:My asteroid rotates using an AffineTransformation.


Just a point that you might be aware of, but just in case - when you use a AffineTransformation in a call to drawImage you are only affecting its representation on the screen, and not the object's actual state.

Quote:How can I get either the X and Y to change with the image rotation, or change the rotation method I am using to get the collision to work?


You don't want to really do either of these - instead you want to modify your collision detection code to take into account the asteroid's rotation.

I'm assuming you're currently using a Shape like a Rectangle2D to hold the bounding rectangles of the asteroid and the ship. In this case, you can use the createTransformedShape method in your AffineTransform to modify the asteroid's rectangle and return a rotated (and translated) version, as a Shape object. You can then call the intersects method on this Shape object to see whether it intersect the ship's bounding rectangle.

If the ship can rotate too, you need a generic area-area intersection test. You can convert the Shapes representing the rotated bounding rectangles to Areas by using this constructor of the Area class. Then call the intersect method of one of the Area objects on the other one, and then check to see whether a collision occured using its isEmpty method.

Let me know if this is unclear, or if you need some example code.
ok, thanks, I'll take a look at it more when I get out of class. The buffered image sounds easy enough. However, the rotation stuff sounds complicated, lol. I'll let you know if I need any help. Thanks
ok, one quick question, how come I have to add an exception here?


try {
enemyPicImg = ImageIO.read(new File("enemyPic.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File(String) throws an IOException if file cannot be opened. I don't know about ImageIO but it may throw such an exception as well if file data is invalid.
Quote:Original post by Beyond_Repair
File(String) throws an IOException if file cannot be opened. I don't know about ImageIO but it may throw such an exception as well if file data is invalid.


Actually that's ImageIO.read throwing the IOException, not File(String).

But yes, the exception is thrown if the file cannot be opened, and so on.
Ah sorry for the misguiding information. :)

Anyway, the OP is in the future better off looking in javadocs directly for getting the quickest answer to the question "what causes this exception".
ah, ok thanks, I guess I only add exceptions when using IO, because I didn't have to use an exception when I loaded just an image (not a buffered image)..
ok, I played around with transformations a lot and still haven't figured out something. Maybe the answer is right in front of me, but how come the transformed image is rotating so far away from the original image? I made rectangles behind both images to test this. Here is my code:

AffineTransform transform = new AffineTransform();


transform.setToTranslation(posX, posY);
transform.rotate(Math.toRadians(x));

Rectangle rect = new Rectangle(posX, posY, enemyPic.getWidth(),
enemyPic.getHeight());

g2.fill(rect);

Shape newrect = transform.createTransformedShape(rect);
g2.fill(newrect);

g2.drawImage(enemyPic, transform, null);
nvm, I got it, i just created a new rectangle with x and y coords equal to 0 so it looks like:

Rectangle rectTwo = new Rectangle(0, 0, enemyPic.getWidth(), enemyPic.getHeight());

Shape newrect = transform.createTransformedShape(rectTwo);

This topic is closed to new replies.

Advertisement