source here: http://www.mediafire.com/download/8d0osuuabogodb9/NickTest_6-2-2013_1741.zip
I am having a problem with collisions. Now I know the problem, I just cant find anywhere on a way to better optimize it. In player.cpp, you will see the code
Code:
void player::move()
{
//Move the player left or right
box.x += xVel;
//If the player went too far to the left or right or has collided with the wall
for( i = 0; i<= 0; i++)
{
if( ( box.x < 0 ) || ( box.x + player_w > screenx ) || ( check_collision( box, wall[i] ) ) )
{
//Move back
box.x -= xVel;
}
}
//Move the player up or down
box.y += yVel;
//Falling
for( i = 0; i<= 0; i++)
{
if( ( box.y < 0 ) || ( box.y + player_h > screeny ) || ( check_collision( box, wall[i] ) ) )
{
box.y -= yVel;
grounded = true;
//printf("player is on the ground\n");
}
else //if there is nothing underneath the player
{
box.y += yVel;
grounded = false;
//printf("player is in air\n");
}
}
}
I know that it is checking to collide with every instance of wall (ill rename is block later on). Wall[0] is a rectangle that represents the floor, Wall[1] is a rectangle that represents the wall; in the future, I want to make every wall a 16x16 or 32x32 block, and that will mean having a zillion different wall[i]'s.
Is my system on the right track or will it cause problems later? Can any of you show me with my code how to improve this? I have been stuck on this for three days because of this. Thanks------------------------
I am making an awesome SDL platformer