Original Post
I'm having a problem identical to the one in this thread. I have to move the player to the edge of the object it collided with, but I don't know how to find out which side it collided with, since the player can be moving in different x and y velocities at the same time. I'm using rectangles as bounding boxes and checking for collisions with the intersects() method. I've gotten it so that it works fine if you collide diagonally on the left or right side of an object, but I don't know how to correct the collision response for the top and bottom sides. Here's what I've come up with so far: Thanks [Edited by - nataku92 on August 27, 2005 2:43:34 PM]
private void updatePlayer(long elapsedTime)
{
float dx = player.getVelocityX();
if (player.getBounds().intersects(player2.getBounds())) {
if (dx > 0) {
player.setX(player2.getX() - player.getWidth());
}
else if (dx < 0) {
player.setX(player2.getX() + player2.getWidth());
}
}
player.updateBounds();
float dy = player.getVelocityY();
if (player.getBounds().intersects(player2.getBounds())) {
if (dy > 0) {
player.setY(player2.getY() - player.getHeight());
}
else if (dy < 0) {
player.setY(player2.getY() + player2.getHeight());
}
}
}