To determine if two spheres are colliding, we take the sum of the radiuses and compare it with the length from the centers of the spheres. If the length is smaller than the sum of the radiuses, we have a collision.
The difference vector (the length is the distance between those two spheres):
Then the length is computed:
Sum of the radiuses:
If distance < sumradius the we have a collision to take care of. This is an example of an elastic collision detection. More advanced algorithm includes calculations of collision time and direction.
Related:
Simple AABB vs AABB collision detection
Sphere vs AABB collision detection
This is a little bit trickier, but with some basics explained, it should be pretty straight forward.
First, find the vector which will serve as a basis vector (x-axis), in an arbitrary direction. It has to be normalized to get realistic results.
Then we calculate the x-direction velocity vector and the perpendicular y-vector.
Same procedure for the other sphere.
Then we mix and play around with some of Newton’s laws to obtain a formula for the speed (in vector format) after the collision.
And it actually works! 🙂
Source code in subversion repository.
Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.