1
\$\begingroup\$

I am implementing a sphere to sphere collision resolution and I am a little confused on where to start. First question, is there a standard way that games/engines do sphere to sphere collision resolution? Is there only like a couple standard ways to do it? Or does the resolution vary very heavily based on whats needed?

I want to implement this in my engine and I wrote a basic one that pushes a sphere and another sphere (so basically the one interacting can push the other) but this was just a super simple concept. How exactly can I improve this to make it more accurate? (Mind you the code isn't optimized since I am still testing)

It seems like there is a lack of solid documentation on collision resolution in general as it's a more niche topic. Most resources I found only concern the detection part.

bool isSphereInsideSphere(glm::vec3 sphere, float sphereRadius, glm::vec3 otherSphere, float otherSphereRadius, Entity* e1, Entity* e2)
{
    float dist = glm::sqrt((sphere.x - otherSphere.x) * (sphere.x - otherSphere.x) + (sphere.y - otherSphere.y) * (sphere.y - otherSphere.y) + (sphere.z - otherSphere.z) * (sphere.z - otherSphere.z));
    if (dist <= (sphereRadius + otherSphereRadius))
    {
        //Push code
        e1->move(-e1->xVelocity / 2, 0, -e1->zVelocity / 2);
        e2->move(e1->xVelocity / 2, 0, e1->zVelocity / 2);
    }
    return dist <= (sphereRadius + otherSphereRadius);
}
\$\endgroup\$
6
  • \$\begingroup\$ This is covered extensively in tutorials and existing Q&A you can find with a quick search. Is there something about these documented approaches you're unsatisfied with, or a particular step you're stuck on that you need help solving? \$\endgroup\$ – DMGregory Sep 27 '20 at 17:40
  • \$\begingroup\$ More or less just a complete and proper explanation from beginning to end. I am looking for a resource that would explain this sort of thing ground up rather than scanning peoples existing and very specific stackoverflow code, and maybe explain a few different ways. Scanning stackoverflow posts has been very unintuitive for me so far. \$\endgroup\$ – user2940320 Sep 27 '20 at 17:57
  • \$\begingroup\$ Okay, check the link above. There are literally dozens of "complete and proper explanations from beginning to end" that have already been written. You don't need to wait for a user here to regurgitate what you can already find online. You can get the full benefits instantly with just a little research. :) \$\endgroup\$ – DMGregory Sep 27 '20 at 17:58
  • \$\begingroup\$ In particular though, I do have a question regarding my code. When I move the spheres by their velocity, there is always a gap inbetween them (as opposed to a proper sphere resolution which would make them just barely touching). How would I achieve this affect that would allow them to touch rather than leave a gap inbetween them? All I'm doing atm is inverting their velocity both so they both get pushed back. Not sure what the proper way to achieve this is. \$\endgroup\$ – user2940320 Sep 27 '20 at 17:59
  • 1
    \$\begingroup\$ Sounds like that should be in the text of your question, not buried in a comment. "How to separate overlapping spheres so they just touch" is a much more tightly-focused question than "explain everything from beginning to end", so it will tend to attract better answers, faster. You can edit your question to refine the focus to help attract answers. \$\endgroup\$ – DMGregory Sep 27 '20 at 18:00

Your Answer

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

Browse other questions tagged or ask your own question.