In one of my projects I have a game area in the shape of a circle. Inside this circle another small circle is moving around. What I want to do is keep the small circle from moving outside the bigger one. Below you can see that in frame 2 the small circle is partly outside, I need a way to move it back to just before it is about to move outside. How can this be done?
Also, I need the collision point along the arc of the big circle so that I can update the small circle's velocity. How would one go about calculating this point?
What I would like to do is before moving the small circle, I predict its next position and if it is outside I find the time of collision between t=0 and t=1 (t=1 full time step). If I have the collision time t then I just move the small circle during t instead of a full time step. But again, the problem is I don't know how to detect at that time the collision occurs when it comes to two circles and one being inside the other.
EDIT:
Example of collision point (green) I want to find. Maybe the picture is a bit off but you get the idea.
B
, andk=0
. Now if it is collision resolution you want, I haven't covered that in my answer because it would require knowledge about the physical properties of the objects. What is supposed to happen? Should the inner circle bounce inside? Or roll? Sweep? – sam hocevar May 26 '12 at 20:31V
, make the inner circle advanceV*t
along the circumference of theR-r
circle. This means a rotation of angleV*t/(R-r)
radians around pointA
. And the velocity vector can be rotated in the same way. No need to know the normal (which is always oriented towards the centre of the circle anyway) or to update the velocity in any other way. – sam hocevar May 27 '12 at 01:37R-r
. Maybe we could move this to chat to see what's wrong? Example numeric values would help find out the problem. – sam hocevar May 29 '12 at 09:09k
. – dbostream May 29 '12 at 13:36k
. It's untested. – sam hocevar May 29 '12 at 14:24V*t/(R-r)
theV*t
I use isdouble arcLength = (destinationPosition - collisionPoint).Length();
i.e. the distance left to travel after the collision, I believe this is the problem since this will always give me a positive angle. How can I solve this? – dbostream May 30 '12 at 19:37AB.x * BC.y - AB.y * BC.x
, if it's positive you need to turn counterclockwise, if it's negative you rotate clockwise. – sam hocevar May 31 '12 at 06:30k
variables like for exampled
andk
sometimes get values like-0.000000000000033750779948604759
and-0.00000000000013500311979441904
etc., to prevent the algorithm from failing I simply set values very close to0
to0
, from what I can tell this is ok to do.Now I just have to find a solution for the special case when the small circle starts outside the big one and moves inside it.
– dbostream May 31 '12 at 16:35