SDL Hit Detection S.O.S Please

Hi guys, having some some issues with hit detections and I need a 2D gaming expert. Here's the check collision function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool check_collision(SDL_Rect recA, SDL_Rect recB){

	if(recA.x+recA.w<=recB.x)
		return false;
	if(recA.x>=recB.x+recB.w)
		return false;
	if(recA.y+recA.h<=recB.y)
		return false;
	if(recA.y>=recB.y+recB.h)
		return false;
	return true;
}




In the main program, I create a vector of ledges which move up and down, and I check collision with the functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Game::check_ledge_player(Player* player,std::vector<Ledge*> &led){

	for(int i=0;i<led.size();i++){

		if(check_collision(player->get_box(),led[i]->get_box()) && (player->get_box().y+player->get_box().h>=led[i]->get_box().y )  ){//if player lands on top of ledge	

			player->set_ground(true);
			player->set_jump(false);
			player->set_location(player->get_box().x,led[i]->get_box().y-player->get_box().h);
					
			
		}
		if(check_collision(player->get_box(),led[i]->get_box()) && (player->get_box().y<=led[i]->get_box().y+led[i]->get_box().h)){//if player's head hits bottom of ledge
			player->set_location(player->get_box().x,led[i]->get_box().y+led[i]->get_box().h);
				
		}
	}		
}


The idea is simple. I have a player which is able to jump. If the player hits the top of the ledge, he stands on top, if the player's head hits the bottom of the ledge, he falls back down.

Each "if" statement above works if I comment out the other "if". But for some reason, they don't want to work together.

If anyone has an idea what I am doing wrong help would be greatly appreciated as I am kind of stuck and have been all weekend unable to cross this hurdle.

Thanks,

Mike
What do you mean by "work"? What behaviour are you seeing that is unexpected? The more precise you are in the information you give us, the more able we are to help you.

This
player->get_box().y+player->get_box().h>=led[i]->get_box().y
is almost the opposite of what you check in check_collision
1
2
if(recA.y+recA.h<=recB.y)
	return false;
.

The whole if condition can only be true if
player->get_box().y+player->get_box().h == led[i]->get_box().y.
Last edited on
@MikeyBoy

What I mean is that each if statement in the function

void Game::check_ledge_player(Player* player,std::vector<Ledge*> &led

works independently. So for example if I do this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Game::check_ledge_player(Player* player,std::vector<Ledge*> &led){

	for(int i=0;i<led.size();i++){

		if(check_collision(player->get_box(),led[i]->get_box()) && (player->get_box().y+player->get_box().h>=led[i]->get_box().y )  ){//if player lands on top of ledge	

			player->set_ground(true);
			player->set_jump(false);
			player->set_location(player->get_box().x,led[i]->get_box().y-player->get_box().h);
					
			
		
			
		}
	}		
}



it works when the player lands on top of the ledge

and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Game::check_ledge_player(Player* player,std::vector<Ledge*> &led){

	for(int i=0;i<led.size();i++){


					
			
	
		if(check_collision(player->get_box(),led[i]->get_box()) && (player->get_box().y<=led[i]->get_box().y+led[i]->get_box().h)){//if player's head hits bottom of ledge
			player->set_location(player->get_box().x,led[i]->get_box().y+led[i]->get_box().h);
				
		}
	}		
}


works if I hit the bottom of the ledge with the player's head

But when combined, they don't work together as in the original post. So that's my issue.


@Peter I see your point. But right now I have to run to work. I will try changing it tonight and see if your suggestion makes a difference. Thanks for the help guys!
Topic archived. No new replies allowed.