I wanted to make a simple 2D tank game, just for fun.I'm having 1 major problem however, and I feel it should be easy to solve, but I just can't find the answer anywhere.

I have a Tank class, which extends Rectangle (java.awt.Rectangle)It contains an image, which I am easily able to rotate using Graphics2D's rotate method.

I want to use the Rectangle.intersects method, but while the image moves exactly like I want it to, the object itself will not change (it changes location, but not it's shape), meaning the image can easily go through walls before stopping.

The problem however, is that I can't get a Tank -object-to rotate.Using AffineTransform, I can get a rotated version of the tank Rectangle to appear just as I want it...but I can't get the newshape into my Tank object!

I tried using getBounds, but that doesn't return the actual shape, just a rectangle that encloses the shape.I believe I need to use Tank.setFrame...but I can't get newshape's frame, or x/y location or dimensions for that matter.What am I doing wrong here? Please help!

A Tank is not a Rectangle. A Tank has bounds; it may be suitable for your needs to simplify these bounds as a Rectangle.

You shouldn't store the transformed geometry, as doing so will compound rounding errors when further transformations are applied.Instead, store the untransformed geometry & the transformation matrix seperately. (though in this case, the transformation matrix is just a simple angle.)

I'll just add that in Java2D a Rectangle is always aligned to the axes, so if you rotate a Rectangle the result is no longer a Rectangle. It is a general Shape instead.

The quickest way to fix your code would be to add a Shape member to the Tank class (instead of extending Rectangle) and to initially set that member equal to your original Rectangle (or a Rectangle2D since that class is more up-to-date). Then the Shape member can be transformed the way you expect.

But, as Abuse says, numerical errors will eventually start to mess things up if you keep applying transformations to a Shape, so this isn't great as a long-term solution. Better to store position and angle, and transform the shape from scratch each time.

Thanks a lot!I removed the Rectangle inheritance, and simply put a Shape, plus x,y, width and height values in my GameObject class (which Tank now inherits)And this solved my problem so far. Now my tank will bump into Tiles (which also inherit GameObject).However, these Tiles always have a Rectangle as Shape, so I can simply do Tank.Shape.intersects(Tile.Shape).

But then how would I go about checking collisions with other GameObjects which have had their Shape rotated?Is there a way to do this using features in Java2D, or will I have to write something myself?

But then how would I go about checking collisions with other GameObjects which have had their Shape rotated?Is there a way to do this using features in Java2D, or will I have to write something myself?

For some reason Java2D doesn't have a function for intersecting two Shapes. But it does have one for intersecting two Areas, and an Area can be built from a Shape.

But then how would I go about checking collisions with other GameObjects which have had their Shape rotated?Is there a way to do this using features in Java2D, or will I have to write something myself?

For some reason Java2D doesn't have a function for intersecting two Shapes. But it does have one for intersecting two Areas, and an Area can be built from a Shape.

Areas are quite slow as I recall. Make sure you keep one in memory for each object or you'll find your overhead getting really noticeable. Since you're only using OOBB's, you might want to just look up the math for rect-to-rect collision and give the math a go yourself. At least go that route if you find your game is running slowly.

I've just run into a situation that's really similar to this one. It's really bugging me as to how I can't seem to rotate the rectangle to match the image that the rectangle is based on. The image just uses a transform to rotate. i've tried messing around with Graphics2D's rotate for the rectangle and holy hell that swirls the entire screen into oblivion.

Here's a screenshot as an example of my woes:

So I'm supposed to use a shape object instead of a rectangle to work with collision detection? I'm just wondering how to get the rectangle to match up with the bullet, basically.

Yes you would have to use Area for this. The easiest way would be to put a Polygon into the Area. Then do a cheap bounding rectangle v. rectangle check before you do an Area check since the intersect(Area) method of Area is slooooow.

I've just run into a situation that's really similar to this one. It's really bugging me as to how I can't seem to rotate the rectangle to match the image that the rectangle is based on. The image just uses a transform to rotate. i've tried messing around with Graphics2D's rotate for the rectangle and holy hell that swirls the entire screen into oblivion.

Here's a screenshot as an example of my woes:

So I'm supposed to use a shape object instead of a rectangle to work with collision detection? I'm just wondering how to get the rectangle to match up with the bullet, basically.

Are you restoring the matrix after applying the transformation? Graphics2D and OpenGL transformations are state-based, so you apply it to everything, draw, then revert to a previous matrix.

Looks like you need translate the matrix to the center of your sprite before rotating it.

Yes you would have to use Area for this. The easiest way would be to put a Polygon into the Area. Then do a cheap bounding rectangle v. rectangle check before you do an Area check since the intersect(Area) method of Area is slooooow.

I'm going to try this solution first.

I've never used an area object, but the docs say that every area object has a getBounds() method that returns a rectangle that completely encases the area. So if I'm reading you correctly, I'd create an area around each bullet, then use the area object's getBounds() to cheaply test for a collision. If a collision is found, we test further to see if the actual area is colliding as well?

So here's my progress: I've managed to draw what I want on the screen using a global AffineTransform (i named it identity in the code below) that holds the default transform so that the screen doesn't go all out of whack, but the collisions that occur behind the scenes don't match what I've drawn. For reference, another screenshot here where the red rectangle surrounding the bullet is what I want to check collisions with, while the blue rectangle is what collisions are actually checked with.

Well yeah, the AffineTransform is only applied to graphics, not to your game logic. There may be some way to put a point into the affine transform and get a transformed point out but that's not what you want.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org