I wanted to implement a collision detection for my little car game to improve my java skills. I have an image of a car that i can rotate with left and right key and drive forward/backward. I can detect if a collision of two cars occurs but I can’t determine on wich side that happens and I would like to calculate the corner points of the given rectangle. The problem is I can’t figure out what’s wrong with my code and I need some help. Bare in mind that my math skills are a bit rusty but I thought I calculated this right with simple sine/cosine but maybe I did it completely wrong?
I tested this by drawing the path (encloses picture nicely and rotates with it) but when I calculate the corner points and draw them with drawLine both points don’t get rotated with the picture. Maybe I am doing it too complicated and help is much appreciated.
g2d.drawImage(colorImage, 0, 0, null);
af = new AffineTransform();
af.translate(player1.getMapPositionX(), player1.getMapPositionY());
af.rotate(player1.theta);
g2d.drawImage(player1.carImage, af, this);
bf = new AffineTransform();
bf.translate(player2.getMapPositionX(), player2.getMapPositionY());
bf.rotate(player2.theta);
g2d.drawImage(player2.carImage, bf, this);
Rectangle r1 = new Rectangle(10, 5,
(int) (player1.carImage.getWidth() * 0.6),
(int) (player1.carImage.getHeight() * 0.55));
Path2D.Double path1 = new Path2D.Double();
path1.append(r1, false);
AffineTransform t1 = new AffineTransform();
t1.translate(player1.getMapPositionX(), player1.getMapPositionY());
t1.rotate(player1.theta);
path1.transform(t1);
//g2d.draw(path1);
Rectangle r2 = new Rectangle(10, 5,
(int) (player2.carImage.getWidth() * 0.6),
(int) (player2.carImage.getHeight() * 0.55));
Path2D.Double path2 = new Path2D.Double();
path2.append(r2, false);
AffineTransform t2 = new AffineTransform();
t2.translate(player2.getMapPositionX(), player2.getMapPositionY());
t2.rotate(player2.theta);
path2.transform(t2);
// g2d.draw(path2);
r1 = path1.getBounds();
r2 = path2.getBounds();
int p1x = (int) r1.getX();
int p1y = (int) r1.getY();
int p2x = (int) (p1x + Math.cos(player1.theta * delta)
* r1.getWidth());
int p2y = (int) (p1y + Math.sin(player1.theta * delta)
* r1.getWidth());
int p3x = (int) (p1x + Math.cos(player1.theta * delta)
* r1.getHeight());
int p3y = (int) (p1y + Math.sin(player1.theta * delta)
* r1.getHeight());
int p4x = (int) (p3x + Math.cos(player1.theta * delta)
* r1.getWidth());
int p4y = (int) (p3y + Math.sin(player1.theta * delta)
* r1.getWidth());
g2d.drawLine(p1x, p1y, p2x, p2y);