Java game Hitbox Detection & Rounded Corners

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.

My game with hitboxes enabled
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); //Not actual code here } } } 

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)); } 
+7
java swing intersection rectangles
source share
2 answers

Many years ago, I wrote a Khepera simulator as part of a project of my last project. I started by detecting a collision, when you do, these are intersecting areas ... My supervisor noticed that since my robot’s shape was circular, I could just check if the center of the robot was inside another shape, if that was the case when the collision occurred .

Your case is even simpler as you move around the tiles ... so either you do (as suggested in the comments) maintain the move / no move tile set and check it, or just check that your player’s position is inside or no, the "forbidden" rectangle, i.e. let. If this is the case, then you must reset the position of the character to be “outside” the obstacle (minus the character restriction frame, obviously)

I highly recommend doing this using the tile method: allow only up / down / left / right movements and check the “forbidden” set of movements, given the position of the tile. If you really want “freedom” of movement than going with circles (bounding boxes / circles), because they are easy to reason with, easy to reset (in case of collisions) and ideal for your business (each tile may contain a circle, whether obstacle or player.)

0
source share

There are many ways to check for collisions, but I think a simple approach would be very good for your use case.

First, from a glance at your screenshot, the tile is either an obstacle or passable, but not half of each. In this case, it would be easier to just check on which plate the center (or legs, choose what looks best) of the character is located.

If this is an obstacle, they cannot go there, just like that. Please note that this will allow the player to partially move into an obstacle, but many games do it this way, and players are certainly used to this behavior, especially in 2D games with graphics designed to look like an isometric view (which I would make your class like). An option for this would be to simply reduce the angle of the player’s collision (for example, by half tiling again centered on the shoulder of the arm or leg).

0
source share

All Articles