• Advertisement
Sign in to follow this  

Collision detection for bomberman type game

This topic is 2105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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++;

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement