0
\$\begingroup\$

I'm writing my very first 2D game engine in C++ and SDL2. I wrote a very simplistic RigidBody class that computes the resultant force, applies forces and simulates gravity. I also wrote a BoxCollider class and a QuadTree class which as any game dev would know manages collisions. Every thing seems to be working fine, however I'm facing a pit stop I can't wrap my head around. How do I stop a GameObject when it collides with a static wall or ground. I tried setting the velocity to zero but that literally freezes the object and I can't move it using my controls. Also setting the gravity to zero doesn't stop the object since it still has a non zero velocity and it sort of digs it's way through the ground. How can I implement this and if there are any decent tutorials regarding my question please do share them. Thanks!

\$\endgroup\$
1
  • \$\begingroup\$ The term you want to search for here is collision-resolution. Usually you'll do something like computing the time of impact or penetration depth of one collider into another, and generate impulses to apply to dynamic bodies in the collision to separate them. \$\endgroup\$ – DMGregory Dec 30 '20 at 14:46
1
\$\begingroup\$

Check out Erin Catto’s (one of the creators of Box2D) GDC slides, he has by far one of the most succinct explanations of collision resolutions i’ve ever read. He also provides an accompanying demo of code which actually implements incredibly straight forward collision resolution in just a few hundred lines of code. He uses what is known as a sequential impulse solver, which is mathematically very similar to gauss-siedel solvers (a very common MLCP solver used in game physics).

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, I will check it out! \$\endgroup\$ – Fudge Racoon Jan 5 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.