I'm writing a Python program and need an accurate collision model for $2$ given circles. I've already got the collision check model, but need to know the points of initial collision so I can set the $2$ circles back to the point of collision, otherwise they get stuck within each other. Here is a Desmos link so it can be better understood what I mean.
Let the center-point of the 2 circles be $C_1$ & $C_2$, the speed of these circles $v_1(x)$, $v_1(y)$ & $v_2(x)$, $v_2(y)$, the radius of the circles $r_1$ & $r_2$.
Given:
- $C_1 (4, 4), C_2, (10, 6).$
- $v_1(x), v_1(y) = (1, 1).$
- $v_2(x), v_2(y) = (-2, 1).$
- $r_1, r_2 = 2, 2.$
Find the first point of collision.
It is important to note that the circles aren't necessarily moving directly towards each other.
Using Pythagorous, the distance (S) between the two centres is $S = \sqrt{ {(\Delta x)}^2 + {(\Delta y)}^2}$ where $\Delta(x)$ and $\Delta(y)$ is the difference in the x and y coordinates of the two circles. When S=4 (the sum of the radii of the two circles), that determines the collision event.
At the collision, the contact point is the midpoint of the two circle centres which can be calculated using x,y = $(x(c1)+x(c2))/2$ , $(y(c1)+y(c2))/2$ which is basically the average of their respective coordinates.
If the velocities remain constant the collision point can be predicted. The separation equation becomes
$$\sqrt{((x2+v2(x)T)-(x1+v1(x)T))^2+((y2+v2(y)T)-(y1+v1(y)T))^2}$$
where $(x1,y1)$ and $(x2,y2)$ are the initial coordinates of $c1$ and $c2$ respectively. T = (t-1). I used T because t was 1 initially. Solve for t when the above equation is equal to 4 and that is the predicted collision time.
Edit: solving for T: First put in the standard form $AT^2 + BT + C = 0$ where
$A = (a-b)^2+(c-d)^2$
$B = 2((x2-x1)(a-b)+(y2-y1)(c-d))$
$C = x1^2 + x2^2 +x3^2 +x4^2 -2(x1 x2 + x3 x4) -(r_1+r_2)^2$
where a,b,c,d = $v2(x),v1(x),v2(y),v1(y)$ respectively. In your case (c-d) = 0 which simplifies things a bit.
Substitute into the quadratic formula:
$T = \frac{-B \pm \sqrt{B^2 - 4AC}}{2A}$
I eventually get $T = 2 \pm \sqrt{4/3}$
The two roots are $\approx 0.8452$ and $\approx 3.1547$.
Remember I defined t as T+1, so $ t \approx 1.8452$ and $ \approx 4.1547$.
This seems to agree with your Desmos construction. The first root is when they first collideand the second is when they part ways at $ t \approx 4.1547$ and they are once again separated by a distance of 4.
It is probably best to just check the separation at every iteration, rather than predict the collision time, because some other object may collide with the circles before the predicted collision time, messing things up.