Jump to content
  • Advertisement
Sign in to follow this  
Silver83

Simple circle/AA box collision?

This topic is 3581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know this is such a common topic and it seems so simple, and yet I have spent the past 48 hours googling/reading tutorials/searching forums, and trying everything i can think of, to no avail. was wondering if anyone would be so kind as to help me with some sample C (not C++) code, or just the math equations? to elaborate, here's my situation: im writing a simple breakout style game. the collision detect seems simple and yet i get multiple bugs, like the ball getting stuck in place or bouncing sometimes in the wrong direction. essentially, what im looking for (i think) is circle/axis-aligned rect testing that will tell me which side of the box the ball hit (or hit the most, since it could possibly overlap 2 or even 3 planes of the rectangle) something like (int)sphereIsHittingSideOfRect(struct aRect box, struct aRect circle) { //test if circle hits, and if so which of the four sides it is most overlapping return whichSide; } the main problem is that my ball can touch/overlap several rects, such as if it hits a corner area just right. this seems to cause problems. perhaps i can just ignore all but the most overlapped box in that case? any advice/sample code/equations are appreciated. thank you very much in advance

Share this post


Link to post
Share on other sites
Advertisement
[edit]Sorry is c++[/edit]

Hi,

On IndieLib engine I implemented collision system that let's you create collision areas (triangles, rectangles or circles) and add them to your sprites grouped by names. And you can rotate / scale your sprites or zoom or rotate the camera and the collision areas will fit the sprite.

If you just want to check collisions between a circle and a AA box, you can checkout the IndieLib sourcecode. I think I used triangl to circle collision (using two triangles for the box). If you want something that will tell you which side of the box the ball hit, you can use 4 thin triangles, wrapped to each of the faces. And to check triangle to circle collision.

Share this post


Link to post
Share on other sites
To solve the same problem i simply modeled the bricks as 5 boxes instead of one, the main brick which is tested first and if that test succedes 4 boxes on each side which are tested to work out which side the collision took place on:
________
_|________|_
| | | |
| | | |
|_|________|_|
|________|

Thats what each brick looks like if i was to draw the bounding boxes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!