Collision Jump 'n Run

Hi folks,

i am still working on my 2D Jump and Run game. But I have now a problem with the collision detection. My idea was since it is a tile based game to go through my array and check the X,Y coordinates with the player and the map. But somehow it is not working proper. (So far I am just trying to realize it for the Y coordinat because its easyer for me).

I would appriciate your help.

That is my idea so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//Player collision map
void player_collision_map( playerHandle* pPlayer )
{
	//Init of graph coordinats
	int x;
	int y;

	//Rect
	SDL_Rect set[6];

	//Map array
	int map_offset [MAP_HEIGHT][MAP_WIDTH]=
							{
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,1,2,2,3,0,0,0,0,0,0,0,0,0},
								{0,0,0,1,2,3,0,4,5,5,6,0,0,0,0,1,2,3,0,0},
								{2,3,0,4,5,6,0,4,5,5,6,0,0,0,0,4,5,6,0,0}
							};
	
	for( y = 0; y < MAP_HEIGHT; y++ )
	{
		
		for( x = 0; x < MAP_WIDTH; x++ )
		{
			
			switch( map_offset[y][x] )
			{
				case 1  : if( pPlayer->y == (y*32) )
						  {
							pPlayer->isOnGround = true;
						  }; 
							break;

				case 2  : if( pPlayer->y == (y*32) )
						  {
							pPlayer->isOnGround = true;
						  }; 
							break;

				case 3  : if( pPlayer->y == (y*32) )
						  {
							pPlayer->isOnGround = true;
						  }; 
							break;

				case 4  :  break;
				case 5  :  break;
				case 6  :  break;
			}
		
		}
	}

}
What do you hope to accomplish with that strange loop? And what is "not working proper" supposed to mean?
If your tiles are 32x32, then you just need to divide your player coordinates by the tile size and check whether that particular tile is solid or not. No need for loops, in any case.
First of all, I highly suggest you switch from SDL to SFML since it is newer, faster and in C++ rather than C. Secondly, this is not the right way to check collisions - I suggest Googling SDL collision detection. Want to set up your own collision detection? I've never used SDL but in SFML I just use a class that has different bool functions (CircleTest, BoxTest, PixelTest, etc.)

These functions take the two sprites as arguments and does some simple math to to test if the two objects are going to collide. I am actually going to download SDL right now and work on a collision class for fun and practice that I'll try to remember to post here when I am done. PM me if you want the SFML collision class I use.

Helpful Link: http://www.sdltutorials.com/sdl-collision/
Last edited on
Hi,
I see what you mean Athar my way was around the problem.
I changed the code, but now it does nothing.

@ Noobie, I would have liked to use SMGL with C++, but we are not allowed in school to use object oriented "languages" so I have to stick with SDL, which works also fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Player collision map
void player_collision_map( playerHandle* pPlayer )
{
	//Init of graph coordinats
	int x;
	int y;

	//Rect
	SDL_Rect set[6];

	//Map array
	int map_offset [MAP_HEIGHT][MAP_WIDTH]=
							{
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0,1,2,2,3,0,0,0,0,0,0,0,0,0},
								{0,0,0,1,2,3,0,4,5,5,6,0,0,0,0,1,2,3,0,0},
								{2,3,0,4,5,6,0,4,5,5,6,0,0,0,0,4,5,6,0,0}
							};
	
	if( map_offset[ ( (int)pPlayer->y / 32 ) ] [ ( (int)pPlayer->x / 32 ) ] != 0)
	{
		pPlayer->isOnGround = true;
	}
}


Never mind I solved it.
Last edited on
Topic archived. No new replies allowed.