I am writing a physics engine / simulator that includes three-dimensional space flight, planetary / stellar gravity, ship propulsion and relativistic effects. So far, everything is going well, but I need help with the math of the collision detection algorithm.
The iterative motion simulation that I use basically looks like this:
(Note. 3D vectors are ALL COPIES.)
For each obj obj.ACC = Sum(all acceleration influences) obj.POS = obj.POS + (obj.VEL * dT) + (obj.ACC * dT^2)/2 (*EQ.2*) obj.VEL = obj.VEL + (obj.ACC * dT) Next
Where to:
obj.ACC is the acceleration vector of the object obj.POS is the position or location vector of the object obj.VEL is the velocity vector of the object obj.Radius is the radius (scalar) of the object dT is the time delta or increment
Basically, I need to find an effective formula that is derived from (EQ.2) above for two objects (obj1, obj2) and say if they ever collide, and if so, at what time. I need the exact time so that I can determine if it is in this particular time increment (because the acceleration will differ with different time increments), and also so that I can determine the exact position (which I know how to do, given time)
For this engine, I model all objects as spheres, all that needs to be done to this formula / algorithm is to find out at what points:
(obj1.POS - obj2.POS).Distance = (obj1.Radius + obj2.Radius)
where .Distance is a positive scalar value. (You can also enclose a square in both directions, if it's easier, to avoid the square root function implicit in the .Distance calculation).
(yes, I am aware of many, many other issues of collision detection, however, it seems that their solutions are very specific to their engine and assumptions, and none of them matches my conditions: 3D, spheres and acceleration applied with the modeling step Give let me know if I'm wrong.)
Some clarifications:
1) It is not enough for me to check the intersection of two spheres before and after an increase in time. In many cases, their speeds and changes in position will far exceed their radii.
2) RE: efficiency, I do not need help (at this stage in any case) with regard to identifying likely candidates for collisions, I think I have covered it.
Another refinement that seems to be a lot:
3) My equation (EQ.2) of incremental motion is a quadratic equation that applies both speed and acceleration:
obj.POS = obj.POS + (obj.VEL * dT) + (obj.ACC * dT^2)/2
In the physical engines I've seen (and, of course, in every game engine I've ever heard of), there are only linear equations of incremental motion that apply only speed:
obj.POS = obj.POS + (obj.VEL * dT)
That's why I can't use the publicly available collision detection solutions found on StackOverflow, Wikipedia, and the entire Internet, such as finding the intersection / closest approach of two segments. My simulation deals with variable accelerations, which are fundamental to the results, so I need the intersection / closest approach of the two parabolic segments.