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

Author Topic: Is there some way of collision different than pixel perfect and getGlobalBounds?  (Read 2876 times)

0 Members and 1 Guest are viewing this topic.

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
I mean, i dont need pixel perfect detection but this rectangle collision is a bit to little. I think very good option could be if i could exclude some rectangles from the main rectangle, and then check for collision. Like on my drawing below.



dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
If you're doing just circle-circle collision, the simplest method is:
  • Get the difference vector between the centers of both circles
  • Subtract the length (or length squared) of the difference vector by the radius (or squared radius) of both circles.
  • Negative result is collision, positive result is no collision

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
It looks nice and easy. Thanks!

Tigrou

  • Newbie
  • *
  • Posts: 15
    • View Profile
If both circles have same radius (as in your example) this is even simpler :

vector2f d = spriteA.position - spriteB.position;
float distance = sqrt(d.x * d.x + d.y * d.y);
bool collide = distance < (radius * 2.0f);
« Last Edit: March 07, 2017, 09:25:42 pm by Tigrou »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Drop the sqrt() and instead multiply your distance with itself. You'll have to do a multiplication, but that's far cheaper than getting the square root. :)

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Your'e advices are great! Exactly what i need.