I am working on a simple 2D game with Java, swing and without a framework. I have a rectangular player that the user can move. There are several obstacles on the map that a player cannot pass. I did this by creating a new Rectangle object for the player and each obstacle with their borders. But I'm not sure if this is the right way to do this. It works, but the player’s movements are not very user friendly. If a player wants to pass two obstacles, they must go through ideal coordinates.
Is it even good to check the intersections between the player and the obstacle with the Rectangle object, or should I do it differently?
Now for my second question:
I'd like to replace rectangular hitboxes with the same hitbox, but with rounded corners so the player can get through easier.

This is what the game looks like with hitbox enabled.
Code that checks if a player has crossed obstacles:
for (Player p : this.getPlayerArray()) { Rectangle recPlayer = p.playerBounds(); for (Obstacle kiste : obstacleArray) { Rectangle recKiste = kiste.obstBounds(); if (recPlayer.intersects(recKiste)) { p.setX(100);
Function that returns the hitbox of a player / obstacle:
public Rectangle obstBounds() { return new Rectangle(this.getX(), this.getY(), image.getImage().getWidth(null), image.getImage().getHeight(null)); }
java swing intersection rectangles
Aruloci
source share