What you are trying is not trivial and the use of the internal Physics system will be much less problematic.
That said, if you really want to look for it by hand, you must first detect the intersections between the relevant balls, then you must solve them and then update the speeds.
- To detect intersections between the balls.
This is simply a sphere / sphere intersection test. Iterate through all the pairs and check the distance. Distance = (sphere1.transform.position – sphere2.transform.position) .Length ()
If Length <sphere1.radius + sphere2.radius, then it has an intersection and must resolve them
- Intersection resolution
First you calculate the normal collision. This is simply
NormalOfCollision = sphere1.transform.position - sphere2.transform.position; NormalOfCollision.Normalise ();
He calculated the distance of the sphere in (1), so the resolutionDistance is totalRadio – sphereDistance. Then you apply this to the bodies.
sphere1.transform.position + = NormalOfCollision * resolutionDistance sphere2.transform.position - = NormalOfCollision * resolutionDistance
If you are going to have spheres of different masses colliding, you must scale the impulse so that the smaller mass moves more than the larger mass. That is to say.
sphere1Impulse = sphere2Mass / (Sphere1Mass + Sphere2Mass); sphere2Impulse = sphere1Mass / (sphere1Mass + Sphere2Mass); sphere1.transform.position + = NormalOfCollision * resolutionDistance * sphere1Impulse sphere2.transform.position - = NormalOfCollision * resolutionDistance * sphere2Impulse
- Calculation of speed after impact.
You need to use the law of conservation of the linear moment. The wikipedia page gives a worked example.
- The above is a pseudo code, I have not executed it, so I can have some of the signs + ve / -ve in the wrong direction, but it's the right address
- This only handles the non-angular linear effects (so it will not simulate spinning shots, etc.)
- If you have larger time steps, or several interpenetration balls you must solve until you have no more contacts (ObjectA can push ObjectB in ObjectC, so you need to solve it)
- There is no optimization in this (island / broadband collision, etc.)
- There is no restitution in this with different objects that are more bulging than others
- Using PhysX will be much easier than doing everything by yourself.