In my 2D physics engine, I can detect AABB vs AABB collisions, and resolve them by finding the shortest penetration vector and adding it to the AABB's position.
Doing this "pushes" the first AABB outside of the second AABB, but doesn't deal with velocity/acceleration changes at all.
If I add gravity acceleration to my simulation, the velocity of the first dynamic AABB keeps growing even when it is resting on top of the second static AABB. Eventually, the velocity will become too big and the collision won't be detected (the dynamic AABB will fall through the static one).
I tried setting the velocity to zero after resolution, but it obviously didn't work well, and created unrealistic simulations.
I read online that resolving collisions by manually working on the position or the velocity is not correct. I tried implementing forces (mass is an "hardcoded" 1 for now):
void Body::applyForce(sf::Vector2f mForce) { acceleration += mForce; }
void Body::integrate(float mFrameTime)
{
velocity += acceleration * mFrameTime;
position += velocity * mFrameTime;
acceleration = {0, 0};
}
If I apply the shortest penetration vector as a force during collision resolution, the dynamic AABB will get "pushed out" from the static one, but its velocity will never decrease in a simulation without gravity and it will keep moving forever.
Is there a way to apply a "temporary" force? A force that deals with pushing the first AABB out of the second AABB, then stops when the AABB doesn't collide anymore?
Entire source code available here: https://github.com/SuperV1234/SSVSCollision