c ++ – Is there an algorithm to check if two spheres are colliding and if they are colliding on the x-axis or on the x-axis?


I was wondering if there was a way to find out if a sphere is intersecting with another sphere on its x axis or on its z axis. For example, Planet A moves along the Z Axis towards Planet B, a stationary object, and when a collision occurs, I would like to get an answer that tells me that they collided on the z axis. I can find out if a collision occurred using the code below, however I'm not sure how to imply the directional aspect of this. Any help would be appreciated.

    enum ECol { x_Axis, z_Axis, none };

    // Collision detection between two spheres

    distanceX = sphere2XPosition - sphere1XPosition;
    distanceZ = sphere2ZPosition - sphere1ZPosition;
    distance = sqrt(distanceX * distanceX + distanceZ * distanceZ );

    if (distance < (sphere1Radius + sphere2Radius))
    { // Collision occurred }