Welcome,
Guest
. Please
login
or
register
. Did you miss your
activation email?
French forum
Home
Help
Search
Login
Register
SFML community forums
»
Help
»
Graphics
»
Is there some way of collision different than pixel perfect and getGlobalBounds?
Print
Pages: [
1
]
Author
Topic: Is there some way of collision different than pixel perfect and getGlobalBounds? (Read 2893 times)
0 Members and 1 Guest are viewing this topic.
MDK
Newbie
Posts: 19
Is there some way of collision different than pixel perfect and getGlobalBounds?
«
on:
March 04, 2017, 02:13:41 am »
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.
Logged
dabbertorres
Hero Member
Posts: 505
Re: Is there some way of collision different than pixel perfect and getGlobalBounds?
«
Reply #1 on:
March 04, 2017, 04:35:57 am »
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
Logged
Github
MDK
Newbie
Posts: 19
Re: Is there some way of collision different than pixel perfect and getGlobalBounds?
«
Reply #2 on:
March 04, 2017, 04:12:50 pm »
It looks nice and easy. Thanks!
Logged
Tigrou
Newbie
Posts: 15
Re: Is there some way of collision different than pixel perfect and getGlobalBounds?
«
Reply #3 on:
March 07, 2017, 04:35:21 pm »
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
»
Logged
Mario
SFML Team
Hero Member
Posts: 879
Re: Is there some way of collision different than pixel perfect and getGlobalBounds?
«
Reply #4 on:
March 08, 2017, 03:27:07 pm »
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.
Logged
MDK
Newbie
Posts: 19
Re: Is there some way of collision different than pixel perfect and getGlobalBounds?
«
Reply #5 on:
March 08, 2017, 11:17:44 pm »
Your'e advices are great! Exactly what i need.
Logged
Print
Pages: [
1
]
SFML community forums
»
Help
»
Graphics
»
Is there some way of collision different than pixel perfect and getGlobalBounds?
anything