My problem is that, when I move
LEFT and im not colliding with anything(except the tile that is underneath), my player slows down. That is cause this code detects collision when moving on left side whether im colliding or not:
void HandleCollision(Entity& e, std::vector<std::vector<Entity>>& entities)
{
int sx = (e.position.x + (entitySize.x / 2)) / 16,
sy = (e.position.y + (entitySize.y / 2)) / 16;
int sizeY = entities.size();
int sizeX = entities[0].size();
int minY = getMin((sy - 6), 0);
int minX = getMin((sx - 6), 0);
int maxY = getMax((sy + 6), sizeY);
int maxX = getMax((sx + 6), sizeX);
int collision_x = 0,
collision_y = 0;
for (int y = minY; y < maxY; y++)
{
for (int x = minX; x < maxX; x++)
{
Entity gotEntity = entities[y][x];
if (gotEntity.type != Category::wall)
continue;
sf::FloatRect intersection;
if (GetBoundingRect(e).intersects(GetBoundingRect(gotEntity), intersection))
{
if (e.position == gotEntity.position)
e.position.x += entitySize.x;
sf::Vector2f direction = gotEntity.position - e.position;
sf::Vector2f offset;
// X collision
if (abs(direction.x) > abs(direction.y)) {
if (direction.x < 0)
{
offset.x = -1 * intersection.width;
collision_x = -1;
}
else
{
offset.x = 1 * intersection.width;
collision_x = 1;
}
}
// Y collision
if (abs(direction.x) < abs(direction.y))
{
if (direction.y < 0)
{
offset.y = -1 * intersection.height;
collision_y = -1;
}
else
{
offset.y = 1 * intersection.height;
collision_y = 1;
}
}
if(e.type == Category::player && gotEntity.type == Category::wall) {
e.velocity -= offset;
}
}
}
}
}
I use it like HandleCollision(Player, Tiles)
:/