Hi guys,
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!
Tank t = (Tank) o;
AffineTransform at = AffineTransform.getRotateInstance(t.getFacing(), t.getCenterX(), t.getCenterY());
Shape newshape = at.createTransformedShape(t); //creates a new, rotated version of Tank t
g2d.draw(newshape); //draws the new, rotated version of the Tank. This looks perfect, just like I want it
/*
THE PROBLEM: I need to apply newshape to my tank, but HOW?
*/
g2d.drawRect(t.x, t.y, t.width, t.height); //this draws a rectangle where the Tank object is situated, this one is static, except for it's location, no rotation or such.
g2d.rotate(t.getFacing(), t.getCenterX(), t.getCenterY()); //this rotates and draws the Tank's image, works as it should.
g2d.drawImage(t.getMainImage(), t.x, t.y, null);