Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Angular direction for circle collisions.  (Read 989 times)

0 Members and 1 Guest are viewing this topic.

VahanDavtyan

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Angular direction for circle collisions.
« 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!


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Angular direction for circle collisions.
« Reply #1 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/
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/