Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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), 
    image.getImage().getHeight(null));
}
share|improve this question
    
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. –  Katu May 12 at 18:51
    
@KatuSorry but I thought that it would not matter since it is very similar to my main question. What did you not understand? –  Aruloci May 12 at 19:05
    
"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? –  Katu May 13 at 3:58

2 Answers 2

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

Hmm

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! :)

share|improve this answer

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.

share|improve this answer
    
See my answer for why a circular (or rounded rectangle) collider will likely just cause more frustration. ;) –  Superdoggy Jul 13 at 13:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.