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.

Nevermind, I rewrote most of the code and added several better ways of handling movement. Essentially I moved all the collision detection to the end of the movement function. There is where I move the player out of a wall if they have moved into it. Here is the revised code:

``````//collision crap

if (check_collision(player.PlayerBox, block[t].clip) == true)
{
//if theres a block to the left
if ( (player.PlayerBox.y + player.PlayerBox.h) - block[t].clip.y < abs(15) )
{
player.PlayerBox.y -= ( (player.PlayerBox.y+player.PlayerBox.h) - block[t].clip.y);
}

//if theres a block to the right

if ( (player.PlayerBox.x + player.PlayerBox.w) > block[t].clip.x && (player.PlayerBox.y + player.PlayerBox.h) - block[t].clip.y > abs(15) && (player.PlayerBox.x + player.PlayerBox.w) < (block[t].clip.x + block[t].clip.w)  )
{
player.PlayerBox.x -= ( (player.PlayerBox.x + player.PlayerBox.w) - block[t].clip.x);
}

//if thers a block to the left

if (player.PlayerBox.x < (block[t].clip.x + block[t].clip.w) && (player.PlayerBox.y + player.PlayerBox.h) - block[t].clip.y > abs(15) && player.PlayerBox.x > block[t].clip.x )
{
player.PlayerBox.x += ( (block[t].clip.x + block[t].clip.w) - player.PlayerBox.x);
}

}``````

Everything else is the same other than the fact that the gravity function no longer sets the player's vspeed to 0. The vspeed stays the same and the player is perpetually moved out of the floor. It is a very fluid system and it works well. If anyone wants more of the source lemme know!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.