I was working on my collision detection for my DBZ clone of Liero today, and I was presented with a problem. For any of you who know Liero (which is much like Worms), you will know that the map is displayed like this:
"Dirt" or deformable terrain bitmap
"Rocks" or impassibles
The background is a single bitmap blitted onto the screen. The "dirt" is also a single bitmap blitted onto the screen, but with several spots where it is transparent, so you can see the background, and the dirt can be deformed, so more of the background shows.
The rocks cannot be changed. That is also the order in which things are blitted onto the screen.
Now, keeping in mind how the map is displayed, here is how it is done in my DBZ clone:
My game does not have a deformable foreground as of yet like Liero does, but basically everything in the background is the background, and everything in the foreground can be collided against.
Now how would you do collision detection? Lets say you have a 20x25 character moving along who bumps into a wall while moving right along the screen.
You cannot just check the coordinate (20, 0) for a collision, because the collision could occur at (20, 24)...at the bottom of the guy....but you cant check just (20, 25), because the collision could occur at (20, 0), and you cannot just check (20, 13) (the middle right) because the collision could occur above or below that....
That means that you must check every coordinate of (20, x)...where "x" is greater than 0 and less than 25.....
That means you must make 25 comparisons every time the guy moves right or left.....which could be happening very often....doesnt that seem a bit taxing on the computer to make 25 comparisons every time the guy moves right or left.
Lets say you move to the right for 1 second...which is very likely and very possible....and lets say you are getting 50 fps...that means you must do 50x25 comparisons....which is 1250 comparisons in just 1 second of moving to the right....not to mention having to do all the other stuff like blitting graphics, etc...
How much do you think that would tax the game? Is there a better way to do the collision detection then that? I'd like any input I can get....thanx...
i think you could just use a simple rectangle test.. read this article
Since you are using (I assume) 2d bitmaps, then another simple solution could be for your to determine the center co-ords of your character that is being moved. Then have a struct (or perhaps just ints if that will suffice for you) that holds the fartherest right (how many pixels towards the character's right) it's own body goes out. This isn't including the entire bitmap size, just the body. So say your using the 20x25 character, but the character itself could be boxed within 18x20 pixels. And say the middle of bitmap could be at the 10th-x-pixel and the 12th-y-pixel. This means that you could perhaps have a 9 left-x value and a 9 right-x value (or something similar... these are just quick values for the example). And the left-y value could be 10 and the right-y value could be 10 as well.
So if the character is moving right, check the middle x co-ord of your character + 9 versus objects in your character's path. If an object is less then this value (the objects x co-ord) and higher then your character's middle x co-ord minus 9 then stop the character from going any further towards the right. Or you could go a little further and do some more checks now that you know that the object is within this area and check to see if the object is touching your character YET, cause in reality it might not be. Or you could use the first method and just stop the character where it is.
And you could use this type of checking for if they move up or down. Its a simple method, and a lot less checking has to be done.