# Thread: Distance from circle to rectangle's corner (from outside)

1. ## Distance from circle to rectangle's corner (from outside)

Hello!

Does anyone know how to calculate the distance between a circle and a corner of a rectangle as I have illustrated in this animation.

Thanks!

René Vaessen.

2. Originally Posted by Rene_vaessen
Hello!

Does anyone know how to calculate the distance between a circle and a corner of a rectangle as I have illustrated in this animation.
Nice animation!

The answer to the question is easy (assuming you know the position of the point p2). The points p1, p2 and m (at time of collision) form a right-angled triangle whose hypotenuse is the radius of the circle. So you can find the distance from p2 to m by Pythagoras' theorem.

3. ## Not really what i'm looking for

..then I have the distance between p2 and m, but I don't know the distance between unknown point m2 (which I called m@collisiontime) and point p2. That's the tricky one.

I thought later, there is more wronge, because p1 is the corner in this situation, but it is calculated as by getting the closestpoint on the rectangle to the center of the circle. This could be wronge because the circle's movement path could bring the circle very close to a corner it's never going to hit.

So I know how to do it now, IF i find a nice way of calculating the corner he is going to hit, Hence how do I now it's even going to hit a corner..

I'm in trouble! :-)

4. Originally Posted by Rene_vaessen
..then I have the distance between p2 and m, but I don't know the distance between unknown point m2 (which I called m@collisiontime) and point p2. That's the tricky one.
No! The distance d(m2,p2) between m2 and p2 is exactly what Pythagoras tells you. I'm assuming that you know the location of p2 (because it is the point where the line L1 meets the line along which m moves). So you know the distance d(p1,p2) from p1 to p2. You also know the distance d(m2,p1), because that is the radius of the circle (it is the distance from the centre of the circle to the point on the circumference where the impact takes place). Pythagoras says that $\displaystyle d(m_2,p_2) = \sqrt{d(m_2,p_1)^2 - d(p_1,p_2)^2}$.

Originally Posted by Rene_vaessen
how do I now it's even going to hit a corner..
Look at it this way. If the circle hits the edge of the rectangle (not the corner), then at the moment of impact the vertical distance from the centre of the circle to the edge of the rectangle will be equal to the radius of the circle, and the point of impact will be vertically above m2 (the centre of the circle at the moment of impact). So the condition for the circle to hit the corner p1 of the rectangle is that the vertical distance from p1 to the line along which m moves must be less than the radius of the circle. That will ensure that the circle hits the corner before it has travelled far enough to bounce off the edge.

5. ## Re: I agree for 100% :-)

Hi again,

First I like to thank you for your help, because it gives me new perspective, something I was really looking for.

Don't ask me why I missed that that I could calculate it so easily, but my head was all messed up.. :-)

That's why I made the animation, to relax a little..

>the point of impact will be vertically above m2 (the centre of the circle at the moment of impact).

Did you mean below? or maybe the left edge of the rectangle?

I don't quit get you method of detecting if a collision will occur to a corner..

PS: Bare in mind that the angle of the circle's ray can be any angle, and the circle's position can be in any orientation to the rectangle)

But what I have come up with now is that I calculate the distance between point a and m2 just as like I would do if I knew it would hit an edge for sure.

And then calculate m2 with that, and see if the the tangent point of the circle (in direction towards the impactpoint) is in or outside the rectangle. If it is outside we have hit a corner. I hope it's right though. :-)