Java swing - how to determine side of collision between two rectangles

I'm building a top-down 2D scroller using java swing with very similar gameplay to the classic Bomberman game where the player can move the character in all 4 main directions.

All game objects have instance variables containing the (x, y) coordinates where they should be displayed on the JPanel, they also have instance variables for the previous (x, y) coordinates where they were displayed in the last Frame. My collision detection algorithm essentially checks to see if the player's object is crossing one of the walls present in the grid every time we refresh the screen. This can be seen in the bit of code below:

if (playerRectangle.intersects(wallRectangle)) {
  player.restorePreviousPosition();
}

      

The restorePreviousPosition () method sets the (x, y) coordinates of the player based on what they were before the collision occurred. These methods work perfectly, it prevents the player from going through objects. However, the controls appear to be very jagged because if the player, for example, tries to move left and up while in contact with a wall, the character remains stationary. In other words, the character cannot touch the wall and still move parallel to it. To fix this problem, I created four rectangles drawn on top of the player object, which I use to determine the side that collides with another object, so that instead of restoring the (x, y) coordinates, I can restore x or y depending on which side collides. For example, if I knowthat the vertex collides with something, then I can restore the y position and let x change freely. Below you can see an image of the player object with rectangles drawn on it:

enter image description here

This idea is very unreliable, especially in corners where it is difficult to determine which side the collision is coming from. So my question is, how can I reliably determine which side of a rectangle collides with another rectangle in java using swing? Or do you have a better alternative to my approach to collision detection?

Note that player movement is completely free, my implementation does not bind the character from tile to tile. And that the distance between the tiles is 32x32 pixels and the player is 30x30 pixels.

Thanks for the help. If you have any further questions about my implementation, please let me know.

+3


source to share


2 answers


Most simply, take a collision that occurs in the lower right corner of some rectangle A to B, and you want to know if B collided with A more on the right or more on the bottom. All you have to do is measure how many pixels the top y coordinate above B is above the lowest y coordinate and measure how many pixels B is far away from the x coordinate to the left of the right right coordinate.



If there is a larger difference between the x coordinates than the y coordinates, then it is a bottom collision, otherwise it is a side collision.

0


source


One really simple solution (given what you've already written) is to split the Test-and-Move code into two checks.



//update.player.x.position
if (playerRectangle.intersects(wallRectangle)) {
  player.restorePreviousPosition();
}

//update.player.y.position
if (playerRectangle.intersects(wallRectangle)) {
  player.restorePreviousPosition();
}

      

0


source







All Articles