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
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();
}
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
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.
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)