SFML community forums

Help => General => Topic started by: VahanDavtyan on March 12, 2023, 10:38:27 pm

Title: Angular direction for circle collisions.
Post by: VahanDavtyan on March 12, 2023, 10:38:27 pm
Hey, I've implemented a simple circle collision detection system.

bool SEMath::circle_collision(const TVector& a, const TVector& b,
                                                  const float& r1,  const float& r2)
{
        // (x2-x1)^2 + (y2-y1)^2 <= (r1+r2)^2
        float center_distance = square(b.x - a.x) + square(b.y - a.y);
        float collision_distance = square(r1 + r2);
        return center_distance <= collision_distance;
}

Upon collision, self circle will get it's opposite velocity (to bounce back) and the collided circle will get the get the velocity it was hit with:

CollidedActor->setVelocity(InActor->getVelocity());
InActor->setVelocity(InActor->getVelocity() * -1.f);

This works fine. But it's just a linear response. What I want is to consider at what angle the circles are colliding as well to be able to simulate a billiard like game by taking into account angular velocity as well but I have no idea how to approach this, any directions would be greatly appreciated. Thanks!

Title: Re: Angular direction for circle collisions.
Post by: eXpl0it3r on March 14, 2023, 11:00:11 pm
I did a quick web search for "reflection angle on circle collision" and ended up with this resource, hope it can help you out :)

https://flatredball.com/documentation/tutorials/math/circle-collision/