Hi! A little introduction, my name is Seb and I just started doing Java this past week. I have experience with C++ and Flash/ Actionscript which helps me a lot, so I jumped straight into game developing with Java

So a little background, my friend tried giving me a challenge: make a car that you can drive from top view, and have it get more and more damaged as you hit the walls. So I took it as a good way to learn by trial and error how to do it !
This is what I have so far:

It works great so far ! Except: for the collision detecting on the walls. If you hit the wall in certain ways, sometimes it will float off through the wall into non-existence, or get stuck. But I realize thats because on a rotating car, I just made a rectangle around it and checked if it x.intercepts(y);
Ideally what I would love to do, is to make a front collision bumper on it and a back one for when you're driving forwards or backwards, but I have no idea how to place it at the right spot as the car is turning. This is where I need help

Here's my coding for the collision detection:
Board.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public void checkCollisions() { Rectangle r3 = car.getBounds(); collide = false; for (int i = 0; i < walls.size(); i++) { Wall b = (Wall)walls.get(i); Rectangle r2 = b.getBounds(); if (r3.intersects(r2)) { car.collision(); collide = true; } } if (!collide) { car.resetCollision(); } } |
Car.java
[code=java]
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
[/code]
(walls.class has the same function)
car.collision() just sets 'collision' to be true in car so when its checked in the car's loop it does this:
car.java
1 2 3 4 5 6 7 8 9 10
| if (collision) { rotation -= 90; if (accel >0) { accel = accel/accel; } if (accel < 0) { accel = -accel/accel; }
x += Math.cos(Math.toRadians(rotation))*-(accel*2); y += Math.sin(Math.toRadians(rotation))*-(accel*2); rotation += 90; } |
now, as far as the car control goes, I have:
car.java (keyboard input)
1 2 3 4 5 6
| if (key == KeyEvent.VK_LEFT) { if (accel != 0) { rotation -= 6; makeTire(); } } |
car.java (car's loop)
1 2 3 4 5 6
| if (accel != 0 && !collision) { rotation -= 90; x += Math.cos(Math.toRadians(rotation))*accel; y += Math.sin(Math.toRadians(rotation))*accel; rotation += 90; } |
Board.java (in paint loop)
1 2 3 4
| g2d.rotate(Math.toRadians(car.getRotation()), car.getX() + (car.getWidth()/2), car.getY() + (car.getHeight()/2)); g2d.drawImage(car.getImage(), car.getX(), car.getY(), this); g2d.setTransform(at); |
(I tried to limit the code i paste to just whats necessary to get the point across.. if you'd like to see more just request)
So any help or suggestions/concepts on how to go about getting better collision would be helpful !
EDIT:
So I took an extra step and drew the bounding box used for collision

I think I just need to rotate the rectangle object to the same rotation the car is.. can you do that? All I'm finding by searching is how to draw the rectangle rotated onto the screen