Does anyone know how to calculate the distance between a circle and a corner of a rectangle as I have illustrated in this 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.
..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! :-)
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. :-)