Difficult collision detection

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 :slight_smile:

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 :slight_smile:

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

You can use the java.awt.Polygon class to create a box with the specified points.
Then you can use it’s intersects() methods :smiley:

Note that you have to give it parallel arrays, one for x points and one for y points.

The link below is to a very similar question, I suspect.

http://www.java-gaming.org/index.php/topic,23694.0.html

I’m not at all sure what is most efficient, but I think you need to have a way of deriving the four corners of your car, which can be done with trig. Then you can test the four corner Points via a contains() on the wall structures?

I’m not sure you can assume there is no need to check rear collisions when moving forward. Consider this scenario: driving very close to the wall, in parallel, and turning away from it. Will the rear end rotate into the wall? I guess that depends on how you program the turns, if they occur as if only the front wheels change direction, or if both the front and back wheels change (as if rotating via a central point).