in discreet time, I have 2 balls whose positions are determined by numerical method.
testing for collision is just (d < r1 + r2), that's the 1st step. the 2nd step is to determine the time of collision impact so I can obtain the normal vector when the collision happened and apply the proper collision response <-- the process is very consuming, is there a simpler way to solver this? like some sort of approximation to simplify the calculations?
it would be easy if it's just 2 dots, but the balls have radius. I have to linear interpolate back in time to find the exact time at which they collided.
I know the position and velocity of the balls in the current time step
if the timestep is small enoguh, I can use linear approximation to get the previous positions and use it to get the collision point so I can give it a proper collision response
the time of when the collision is when (d = r1 + r2) is satisfied, expanding on this equation, we get
(P1 - tV1) - (P2 - tV2) = N (r1 + r2), P's are the center posision of the balls, V's are the velocities, and N is the normal vector pointing from ball 1 to ball 2.
collec the terms, (P1 - P2) - t (V1 - V2) = N(r1 + r2)
we can get rid of N by dot product on both sides, but this will be very hard to isoalte t with all the squares and stuff.
let (Px , Py) = P1 - P2, same thing for V, and then perform the dot product on both sides
we get (Px - tVx)^2 + (Px - tVx)^2 = (r1 + r2)^2, to solve for t, we need to expand it
Px^2 + Py^2 - t(2Vx + 2Vy) + t^2(Vx^2 + Vy^2) = (r1 + r2)^2
t^2(Vx^2 + Vy^2) - t(2Vx + 2Vy) + (Px^2 + Py^2 - (r1 + r2)^2) = 0
which can be solved with quadratic solver for at^2 + bt + c = 0, and we know t is smaller than the step size
but this is very a complicated solution, is there no simpler way to solve this problem?
O(1)
for every pair. There is no asymptotically faster solution. – davin