Sign in to follow this  
Phoresis

Circular Collision Detection

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 this post


Link to post
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)
circlex = radiuses_added_together*cos(a)
circley = radiuses_added_together*sin(a)

I think that should work.

Share this post


Link to post
Share on other sites
Quote:
Original post by UltimateWalrus
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)
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.

circlex = radiuses_added_together*cos(a)
circley = radiuses_added_together*sin(a)

should be this though right?:

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

Share this post


Link to post
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

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