I am working on a simple 2D game with Java, Swing and no framework. I have a rectangular player that the user can move around. On the map are few obstacles which the player should not be able to go through. I did this by making a new Rectangle Object for the player and each obstacle with their bounds. But I’m not really sure if this is the right way to do it. It works but the movements of the player are not really user friendly. If the player wants to pass two obstacles they must be on the perfect coordinates to pass.

Is it even a good idea to check for intersections between the player and the obstacle with a Rectangle object or should I do it another way?

Now for my 2nd question:
I’d like to replace the rectangular hitboxes with the same hitbox but with rounded corners so the player could pass more easily.

My game with hitboxes enabled
This is what the game looks like with hitboxes enabled.

The code that checks if the player and the obstacles have yet intersected:

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

The function that returns the hitbox of the player / obstacle:

public Rectangle obstBounds() {
    return new Rectangle(this.getX(), 
    this.getY(), image.getImage().getWidth(null), 
  • \$\begingroup\$ Your second "question" is not really a question, it's unclear what you ask there. However, you should only ask one thing in one question. \$\endgroup\$
    – Katu
    Commented May 12, 2015 at 18:51
  • \$\begingroup\$ @KatuSorry but I thought that it would not matter since it is very similar to my main question. What did you not understand? \$\endgroup\$
    – Aruloci
    Commented May 12, 2015 at 19:05
  • \$\begingroup\$ "I’d like to replace the rectangular hitboxes with the same hitbox but with rounded corners so the player could pass more easily." You state that you would like to do something, but there is no question there. What is stopping you from doing this? What have you tried? \$\endgroup\$
    – Katu
    Commented May 13, 2015 at 3:58
  • \$\begingroup\$ Since you visualized it in tiles, why not create a tile table and mark the tiles with obstacle? If your next step is on a tile that has no obstacle on it, then move there. \$\endgroup\$
    – Xeon
    Commented Oct 12, 2015 at 5:54
  • \$\begingroup\$ Similar question about Pacman cornering \$\endgroup\$
    – Anko
    Commented Dec 11, 2015 at 12:37

3 Answers 3


Here's why you probably don't want to use a rounded rectangle to try to fix your problem:


Now, getting stuck on the edge like that is probably just as frustrating, if not more, than not being able to move right at all.

I think you probably want to employ some error correction when you move. What I mean is, when your character moves, let's say, right, if there's a collision don't move back right away! Instead, move the rectangle up and down about 5 pixels (1 pixel at a time) to see if it can avoid a wall. Same for the other arrows. When moving up, if there's a collision, check if you can avoid it by moving left and right.

I've posted an answer similar to this before which you might find helpful:

How can I detect a perfect passageway for collision detection? // see method 1

The second thing you can do here which is common in a lot of tile games like yours is to move the character exactly 1 tile every time you move. So if you tap the right arrow and then release it after the character only moves a few pixels, the character will keep moving until it is in the center of the next tile.

Hope this helps! :)


As it is said, you should make one post per question.

You can use a circle collider instead of a rectangle collider. Set the circle radius by experimentation.

You can also implement a combination of both, if you care about extreme precision, but a circle should be enough for your purposes.

  • \$\begingroup\$ See my answer for why a circular (or rounded rectangle) collider will likely just cause more frustration. ;) \$\endgroup\$
    – Superdoggy
    Commented Jul 13, 2015 at 13:51

Set your player bounds or object bounds to a couple pixels less than the actual object is. I usually do about 5 each way. That way you have a little leeway going in between objects.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .