SFML community forums

Help => General => Topic started by: Bizarreofnature on October 26, 2017, 08:29:29 pm

Title: Collision between circle and rect
Post by: Bizarreofnature 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?

Thanks in advance,
Bizarreofnature
Title: Re: Collision between circle and rect
Post by: Laurent 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.
Title: Re: Collision between circle and rect
Post by: Bizarreofnature on October 26, 2017, 08:42:49 pm
Well I have hoped there is something like intersect funtion or so..
Title: Re: Collision between circle and rect
Post by: Bizarreofnature 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  :(
Title: Re: Collision between circle and rect
Post by: Laurent 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.
Title: Re: Collision between circle and rect
Post by: barnack 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...
Title: Re: Collision between circle and rect
Post by: mcassimus 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.
Title: Re: Collision between circle and rect
Post by: Bizarreofnature 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 :-/