Java game Hitbox detection and fillet
I am studying a simple 2D game, including Java, swing and no framework I have a rectangular player where users can move around There are several obstacles that players should not experience on the map I do this by creating a new rectangular object and each obstacle for the player But I'm not sure if this is the right way It works, but the player's actions are not really user - friendly If players want to pass two obstacles, they must pass in perfect coordinates
Is it a good idea to use a rectangle object to check the intersection between players and obstacles, or should it be done in another way?
Now my second question: I want to use the same hit@R_145_2419 @Replace rectangular hit@R_145_2419 @However, there are rounded corners so that players can pass more easily
This is when the game is enabled hit@R_145_2419 @What it looks like in a situation
Code to check whether players and obstacles intersect:
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 } } }
Return to player / obstacle hit@R_145_2419 @Functions of:
public Rectangle obstBounds() { return new Rectangle(this.getX(),this.getY(),image.getImage().getWidth(null),image.getImage().getHeight(null)); }
Solution
Many years ago, I wrote a Khepera simulator as part of my undergraduate final year project I started to do the collision detection you are doing, which is the intersection area... My supervisor made me notice that since the shape of my robot is a circle, I can only check whether the center of the robot is in another shape. If it is a collision
Your situation is even simpler because you moved the tiles... So you either (as suggested in the comments) keep a set of "move / don't move" tiles and check it, or you just check that your player's position is an internal or non internal "no" rectangle, i.e. an obstacle If so, you must reset the character's position to the "outside" of the obstacle (obviously minus the character's bounding box)
I strongly recommend doing it in tile mode: only allow up / down / left / right movement and check the "no" movement group for a given tile position If you really want a "free" action instead of a circle (bounding box / circle), because they are easy to reason, easy to reset the position (if there is a collision) and very suitable for your situation (each tile can contain a circle, whether it is an obstacle or a player.)