Sign in to follow this  
Sivak

Circle to rectangle collisions? 2-D game

Recommended Posts

Sivak    140
Hey all. I have this situation of wanting lasers to hit the player in my game. The player's hitbox is a circle. Lasers would be best represented as a rectangle. They can be thick lines, hence the rectangle approach. Lasers could also be going out of something diagonally on-screen, but always in a straight line. So... is there an easy way to see if said circle overlaps a rectangle of this nature? Also out of curiosity, maybe a parallelogram too? I'm wondering if I should have that shape for lasers instead... Thanks.

Share this post


Link to post
Share on other sites
Mussi    4409
You could do 4 circle line intersection tests that way you could do parallelograms as well. To detect a circle line intersection you only have to check the closest point from the circles origin to the line and check if that lies within the circle radius.

[Edit] If your rectangles are actually big enough to contain the circle you need to check if the circle origin lies on the 'inside' of each line as well.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this