I'll try to keep this short. I'm writing a 2D platforming engine and I had good collision detection, but I had to add a separate condition for every platform I had. Essentially the collision function compares the sides of 2 rects. Here is a code snippet of the for loop I am now using for the collision detection:
for( int t = 0; t < floors; t++ )
{
if (keystates[SDLK_UP])
{
if (check_collision("down", player.PlayerBox, block[t].clip)==true )
player.playervspeed = -15;
What it does is loop through all my 'floor' objects and tell me if there is a collision "down" and sets the playervspeed accordingly. Before I had to have one collision detection function for every platform, so this is much better!
HOWEVER! The problem is that now the player will sink through all but the last two declared blocks. Here is my gravity code:
if (check_collision(player.PlayerBox, block[t].clip)==false)
{
if (player.playervspeed<7.5)
{
player.playervspeed+= 0.5;
}
}
else if (player.playervspeed != -15)
{
player.playervspeed = 0;
}
I already double checked the bracket placement, and tried using a separate for loop for all the keyboard events. The problem persists.
Here is my declaration of the boxes:
platform block[4] = { platform(0,280,100,100,0,100,100,100), platform(150,300,100,100,100,100,100,100), platform(300,395,100,100,0,100,100,100), platform(500,380,100,100,100,100,100,100) };
They call this constructor function in the platform class:
platform::platform(int x1, int y1, int w1, int h1,int x2,int y2, int w2, int h2 )
{
//sprite clip
snip.x = x2;
snip.y = y2;
snip.w = w2;
snip.h = h2;
//collision box
clip.x = x1;
clip.y = y1;
clip.w = w1;
clip.h = h1;
}
I have tried moving around the boxes in the array, so different boxes would be the last two, and the result is the two boxes that are now the last two are correctly collided with and the others do not.
Any help would be appreciated, I would be glad to send someone the entire source file or post more or the rest of the code but it is really long and these are the only portions that pertain to the problem.