Collision detection for bomberman type game

Started by
2 comments, last by DanielWilkins 12 years ago
I am trying to get back into SDL and 2d games by writing a simple Bomberman clone. I have gotten map loading, texture loading and a bunch of the logic worked out. Now I am trying to go about and figure out how to get collision detection working and facilitate easy movement throughout the map. Currently, my collision detection code is about 500 lines. It works with a few hiccups but there must be an easier way to do this.

Each block is 32x32 and my player (although his head peaks over) is also 32x32. So for that, I implemented really rigid collision detection. It would simply see if it could move in a specific direction, get the distance to the block if there was one and then move until it hit it. Fair enough. Then I added something I call "nudging" where if the player was at a specific part of the block and tried to move, say, left, and was within a specific threshold, it would move him up or down and then left. Made it a lot better. But still I am getting errors. It seems diagonally, the player can still go through the blocks. I could add more checks against that but I figure I am doing this wrong, or just a way that isn't optimal. There is another error where the collision vector (my data is stored in the vector) will crash the program when the player queries a part that is out of range (when the player travels through the walls.

I only end up checking four directions every frame but still, I think there has to be a better way (especially considering 500 lines of code just for collision detection!).

What kind of collision detection do you recommend for a game like this? I definitely don't need to be checking all of the squares every frame.

Thank you!

I attached the program so you can see what I mean.
Advertisement
Sorry about that. The walkspeed is very slow. Here it is but a bit quicker so you can really test.
I only end up checking four directions every frame but still, I think there has to be a better way (especially considering 500 lines of code just for collision detection!).[/quote]

What directions? When moving up and left is when I can cut through the whole map. All you need to do is check 1 block. If holding up, just check the one block above, if holding up and left, check the upper left block, left block and upper block. Worst case is you are in a corner and hitting 3 blocks max.

Also, when holding up and left, these checks should be separate for instance if I hit up and left, and there is collision with the block above, I should still move left while holding both of those. I see you have the halfway check and he will kind of curve/maneuver which I dont really like, but if u just did each collision check separate like (if left && no block left), move.x--; then separately (if up && no block up), move.y++;

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal


I only end up checking four directions every frame but still, I think there has to be a better way (especially considering 500 lines of code just for collision detection!).


What directions? When moving up and left is when I can cut through the whole map. All you need to do is check 1 block. If holding up, just check the one block above, if holding up and left, check the upper left block, left block and upper block. Worst case is you are in a corner and hitting 3 blocks max.

Also, when holding up and left, these checks should be separate for instance if I hit up and left, and there is collision with the block above, I should still move left while holding both of those. I see you have the halfway check and he will kind of curve/maneuver which I dont really like, but if u just did each collision check separate like (if left && no block left), move.x--; then separately (if up && no block up), move.y++;
[/quote]

Can you elaborate on what would would do instead of the "curve/maneuver". I thought it worked pretty good for allowing players to move through despite the rigid size. What do you not like about it. I can make it much smaller. Right now it is 15 pixels. What if it was half of that instead?

Also, it seems you can only move through when going up and left. Thank you for noticing that. Doesn't make me throw out all the code.

Any more opinions?

This topic is closed to new replies.

Advertisement