Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a confusing problem on my hands. I've managed to get tile-based collision working, and I'm using the following code in my update loop to restrict my character's movement.

        for (Block b : grid.getSelectedRoom().map.blocks) {
            if (this.rect.intersects(b.rect)) {
                if (b.type.equals("wall")) {
                    if (this.direction.equals("north")) {
                        this.y = this.x + 1;
                    if (this.direction.equals("south")) {
                        this.y -= 1 * delta;
                    if (this.direction.equals("east")) {
                        this.x -= 1 * delta;
                    if (this.direction.equals("west")) { 
                        this.x += 1 * delta;

My problem is that if multiple keys are pressed at a corner, the character gets all wonky and clips through walls. I've considered checking to see if the rectangle will collide on key press before movement, but I'm not sure how I could do that since I have to deal with looping through every tile with collision. How should I go about limiting my character's movement?

share|improve this question

closed as too localized by Byte56, Anko, Josh Petrie, bummzack, msell Apr 25 '13 at 8:51

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

There are so many collision response questions on this site, probably the reason they didn't help you is they're all too localized. This question is the same. Voting to close as too localized. – Byte56 Apr 19 '13 at 18:14
I don't really get it but shouldn't this forum be answering other people's problems? Just asking though... – Xeon Apr 19 '13 at 19:14
Yes Xeon, it's about answering people's problems, but there's a line. If this site was filled with localized questions like this one, all of the really good questions that would help more people get ignored. People would stop coming to this site because it's just full of people asking how to fix their code. What would be the reason for people to continue answering those questions? When a question is too localized it diminishes the site. As with all of the other collision response questions on the site, this question is unlikely to be of help to anyone in the future. – Byte56 Apr 19 '13 at 20:00
This is NOT a forum! – Doorknob Apr 19 '13 at 20:19
hoho, save your exclamation mark bro, it's just a question. Chill! Ah, I see. Make sense I guess – Xeon Apr 20 '13 at 7:00
up vote 3 down vote accepted

Check horizontal and vertical collision separately. Also you shouldn't need to loop through every tile for collision, you should only have to check the tiles the character is entering.

As a personal preference, I like to check for collision before applying movement. It seems to save a lot of headache.

share|improve this answer
why even try its a bad question they should ask in chat or watever – GameDev-er Apr 19 '13 at 18:28
@G Some bad questions have good answers. – Anko Apr 19 '13 at 18:36
Thanks for the horizontal/vertical info. I'll experiment with that for a bit. How does something like "When W is pressed - check tile one to north - if it is blocked - don't move" sound? – user1500452 Apr 19 '13 at 19:23
Try it out and see what you get. For me, half the fun of programming is getting it close and then working to fix it. – UnderscoreZero Apr 19 '13 at 19:54
+1 indeed bad questions can have good answers. – Byte56 Apr 19 '13 at 20:00

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