### Author Topic: Collision between circle and rect  (Read 6535 times)

0 Members and 1 Guest are viewing this topic.

#### Bizarreofnature

• Newbie
• Posts: 48
##### Collision between circle and rect
« on: October 26, 2017, 08:29:29 pm »
Hello!

Ive searched in the forum but did not find anything useful.

I want my ship to have a shield and if something touches it, it will be destroyed.

So basically all my collision boxes are rectangles. All but this single one.

How do I realize a collision detection between my circle and the enemy rectangles?

Bizarreofnature

#### Laurent

• Hero Member
• Posts: 32504
##### Re: Collision between circle and rect
« Reply #1 on: October 26, 2017, 08:39:55 pm »
Quote
Ive searched in the forum but did not find anything useful.
Well, given that this is the SFML forum, and that SFML doesn't provide anything for collision detection, this is not surprising

I'm sure Google has plenty of relevant links that explain how to do this simple circle/rectangle collision test.
Laurent Gomila - SFML developer

#### Bizarreofnature

• Newbie
• Posts: 48
##### Re: Collision between circle and rect
« Reply #2 on: October 26, 2017, 08:42:49 pm »
Well I have hoped there is something like intersect funtion or so..

#### Bizarreofnature

• Newbie
• Posts: 48
##### Re: Collision between circle and rect
« Reply #3 on: October 26, 2017, 08:56:03 pm »
Maybe I should be lazy and create a lot rectangles to form out a circle. But this is not nice

#### Laurent

• Hero Member
• Posts: 32504
##### Re: Collision between circle and rect
« Reply #4 on: October 26, 2017, 09:09:51 pm »
Quote
Maybe I should be lazy
Yes, you should be lazy. Ask Google and copy a well known algorithm. There's really no point in reinventing shape intersection algorithms.

Quote
create a lot rectangles to form out a circle
The circle is the simplest shape (it's just a radius), so it usually has the simplest collision detection algorithms. Don't make it complicated.
Laurent Gomila - SFML developer

#### barnack

• Jr. Member
• Posts: 56
##### Re: Collision between circle and rect
« Reply #5 on: October 26, 2017, 09:09:54 pm »
https://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection

PRO TIP:
Lazyness is slightly acceptable, crazyness isn't. Don't let your lazyness evolve in crazyness
Your idea together with aiming at actual precise results, would imply having an amount of rectangles 'n' which is at least equal to half the radius of your circle in pixels...
« Last Edit: October 26, 2017, 09:15:00 pm by barnack »

#### mcassimus

• Newbie
• Posts: 3
##### Re: Collision between circle and rect
« Reply #6 on: October 26, 2017, 09:12:50 pm »
Doing multiple rectangles rotated around a common point would not be ideal however, would definitely be the easiest way to get a circular (relatively) boundary to check against a rectangles boundary. you could perhaps figure out a clever way to use pixel checking for collision in comparison for colors.  This also inst a great way to do it, but you would need to put your frame to a sf::Image and use getPixel(x, y); and use some fancy equations to create coordinates to check for a circular boundary. The downfall is that you would have to check collision with color.

#### Bizarreofnature

• Newbie
• Posts: 48
##### Re: Collision between circle and rect
« Reply #7 on: October 28, 2017, 11:49:59 pm »
Thank you very much for your answers. I see what I can do. Got too much bugs in the moment to progress :-/