[C++/SDL2] Collision Detection is off

Sup once more

Everything starts getting hasty and now I've realised that the vertical collision detection of my platformer is totally messed up. The horizonatal way works just fine, but the vertical one is nearly non-existant.

If there's two solid tiles to the left of the Player he'll be stopped at the second one. If there are two solid tiles to the right of the Player, he won't stop O_O. Of course he should already be stopped at the first one... Full source is up here: https://github.com/HalfNOoBee/Collision-Detection


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
int CPlayer::Collision_Hor(int x, int y, int &TileCoordY, CMap* Map)
{
	int TileXInPixels = x - (x % Tile.GetSize());
	int CheckEnd = x + m_Width;

	TileCoordY = y / Tile.GetSize();

	int TileCoordX = TileXInPixels / Tile.GetSize();

	while(TileXInPixels <= CheckEnd)
    {
        return Map->GetTypeOfTileAtPos(TileCoordX,TileCoordY);

 		TileCoordX++;
		TileXInPixels += Tile.GetSize();
	}
}

int CPlayer::Collision_Ver(int x, int y, int &TileCoordX, CMap* Map)
{
	int TileYInPixels = y - (y % Tile.GetSize());
	int CheckEnd = y + m_Height;

 	TileCoordX = x / Tile.GetSize();
 	std::cout << TileCoordX << "\n";

	int TileCoordY = TileYInPixels / Tile.GetSize();

	while(TileYInPixels <= CheckEnd)
    {
        return Map->GetTypeOfTileAtPos(TileCoordX,TileCoordY);

		TileCoordY++;
		TileYInPixels += Tile.GetSize();
	}
}


1
2
3
4
int CMap::GetTypeOfTileAtPos(int X, int Y)
{
    return TileList[Y*MAP_WIDTH_IN_TILES + X].GetTypeID();
}



Anybody seeing something obviously wrong here?
Last edited on
yeah... what the fuck?
return Map->GetTypeOfTileAtPos(TileCoordX,TileCoordY);
return Map->GetTypeOfTileAtPos(TileCoordX,TileCoordY);

The first thing you do in your while loops is returning a single tile and after that you still do something?
Also, if TileYInPixels is not <= CheckEnd nothing is returned

I didn't look closer at the methods but returning something and then doing something doesn't work in most cases
Okey, and what would you suggest instead?
Does this collision return the type of the tile it collides with or if it collides with anything at all?
Do you need the direction where the collision takes place? (right, left, up, down)
Well it should just return with what type of tile im colliding. There are several types which are saved in an enumeration. There are for example blank tiles (0) or solid tiles (1) or platform tiles(2) which can be moved through if the Player jumps from below. There are other types but basically I just wanna know with what type of tile im colliding... Depending on its type, the game will act accordingly like for ex.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int TilePos = 0;

if(m_bRight == true)
    {

	if(Collision_Ver(m_PosX + m_Width + m_VelX, m_PosY, TilePos, Map) == 1)
        {
		m_PosX = (TilePos * Tile.GetSize()) - m_Width;
		Mix_PlayChannel(-1,cBlockhit,0);
        }
        else if (Collision_Ver(m_PosX + m_Width + m_VelX, m_PosY, TilePos, Map) == 2)
        {
            m_PosX = (TilePos * Tile.GetSize()) - m_Width;
            Mix_PlayChannel(-1,cBlockhit,0);
        } 
// and so on 
Last edited on
If the direction does not matter, why do you have 2 different functions?

I guess your Tile.GetSize() also gives you the size of the player, right?

Kepp in mind that you almost allways collide with 4 different tiles, so you might want to get all tiles returned.
(Correct me if I'm wrong here, I just decided that myself, you might want something else)

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
template<typename T>
struct Vector2
{
    T x, y;
};

struct collision_tiles
{
// [0][0] -> top left
// [1][1] -> bottom right
    int tiles[2][2]; 
};

collision_tiles collision(const Vector2<T>& position, CMap* map)
{
    // get position of tile in top left corner
    Vector2<int> pos0;
    pos0.x = position.x / Tile.GetSize();
    pos0.y = position.y / Tile.GetSize();

    // get position of tile in bottom right corner
    Vector2<int> pos1;
    pos1.x = (position.x + Tile.GetSize()) / Tile.GetSize();
    pos1.y = (position.x + Tile.GetSize()) / Tile.GetSize();

    collision_tiles collisions;
    collisions[0][0] = map[pos0.x][pos0.y];
    collisions[0][0] = map[pos0.x][pos1.y];
    collisions[0][0] = map[pos1.x][pos0.y];
    collisions[1][1] = map[pos1.x][pos1.y];

    return collisions;
}
Last edited on
Topic archived. No new replies allowed.