I have very basic collision detection requirements, so, instead of a full library, I just tried to write up the most bare-bones algorithm I could. It *almost* works, and I've been staring at it for a while with no progress. Any help is appreciated.
Here's the resolution method:
void PhysicsSystem::resolve_collision(DynamicEntity& entity, int x, int y)
{
Vector2f tile_pos(x, y);
Vector2f nearest(entity.pos);
Vector2f min(x, y), max(x + 1, y + 1);
if (nearest.x() < min.x()) nearest.x() = min.x();
else if (nearest.x() > max.x()) nearest.x() = max.x();
if (nearest.y() < min.y()) nearest.y() = min.y();
else if (nearest.y() > max.y()) nearest.y() = max.y();
Vector2f ray(entity.pos - nearest);
auto length = ray.norm();
auto depth = entity.size - length;
if (length != 0 && depth > 0)
{
ray.normalize();
entity.pos += depth * ray;
}
}
This produces a good response when colliding with a tile below and one to the right, but it collides one tile too soon coming from the other two directions.