# Circular Collision Detection

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

## Recommended Posts

I've got a 2d rectangle that you can move about the screen. There is also a circular area in the middle of the screen. When you move the rectangle it orients itself so that it is always at a tangeant with the circle, even though it may be some distance away from the circle. Now currently, my collision detection works as follows: Find the length of a vector from the center of the rectangle (minus half the width) to the center of the circle, and check if this is greater than the radius of the circle. If it is then you can move the rectangle. My Problem is this: When the rectangle comes into contact with the circumference of the cirle I want moving the rectangle to kind of wrap it around the edge of the circle, rather than just not being able to move the rectangle at all. Is there a simple way of doing this? Does my post make sense?

##### Share on other sites
I think I know what you mean. What you want to do is, if you determine that the rectangle is colliding with the circle, use trigonometry to place it so that it is at the edge. Use this pseudocode:

a = atan2(circley-squarey, circlex-squarex)

I think that should work.

##### Share on other sites
Quote:
 Original post by UltimateWalrusI think I know what you mean. What you want to do is, if you determine that the rectangle is colliding with the circle, use trigonometry to place it so that it is at the edge. Use this pseudocode:a = atan2(circley-squarey, circlex-squarex)circlex = radiuses_added_together*cos(a)circley = radiuses_added_together*sin(a)I think that should work.

ah yes of course, that's quite simple really. my brain didn't work liek that for some reason.

should be this though right?:

centerofsquarex = distancefromcenterofsquare*cos(a)
centerofsquarey = distancefromcenterofsquare*sin(a)

##### Share on other sites
either way,

a = atan2(circley-squarey, circlex-squarex)
[...] cos(a)
[...] sin(a)
is suboptimal because you just need to normalize circley-squarey, circlex-squarex
sin_a = circley-squarey;
cos_a = circlex-squarex;
n = 1.0 / sqrt(sin_a*sin_a+cos_a*cos_a)
sin_a *= n;
cos_a *= n;
should be faster and more precise (fewer operations)

Edit: and it just happens not to be any questionable optimization, it's just basic geometry/trigonometry

1. 1
2. 2
Rutin
13
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633261
• Total Posts
3011127
• ### Who's Online (See full list)

There are no registered users currently online

×