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:
http://img573.imageshack.us/img573/9898/peoplerunover.png
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
public void checkCollisions() {
Rectangle r3 = car.getBounds();
collide = false;
//WALLS
for (int i = 0; i < walls.size(); i++) {
Wall b = (Wall)walls.get(i); //walls objects in an ArrayList
Rectangle r2 = b.getBounds(); //return rectangle from the wall obj
if (r3.intersects(r2)) { //ctest
car.collision();
collide = true;
}
}
if (!collide) {
car.resetCollision();
}
}
Car.java
[code=java]
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
(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
if (collision) {
rotation -= 90; //face the rotation towards the sprite
if (accel >0) { accel = accel/accel; }
if (accel < 0) { accel = -accel/accel; }
//bounce car back away from the wall
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)
if (key == KeyEvent.VK_LEFT) {
if (accel != 0) {
rotation -= 6;
makeTire();
}
}
car.java (car's loop)
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)
g2d.rotate(Math.toRadians(car.getRotation()), car.getX() + (car.getWidth()/2), car.getY() + (car.getHeight()/2)); //rotate the from its origin
g2d.drawImage(car.getImage(), car.getX(), car.getY(), this); //draw the car at its position
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
http://img19.imageshack.us/img19/4772/boundingbox.png
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