I am going to assume you fully understand vector and the inner product between two vectors.

If there are vector [b]A[/b] and another vector [b]B[/b]. (use [b]bold[/b] face to represent vector)

Let the unit vector of A is [b]a[/b]=[b]A[/b]/|A|, and [b]b[/b]=[b]B[/b]/|B|, where |A|, |B| are the length of A,B

You can calculate the projection of vector A in the direction of B with [b]A[/b]• [b]B[/b]/|B| =[b]A[/b]• [b]b[/b] : where • mean inner product
In 2D, ax=dx/distance, ay=dy/distance so (ax,ay) is the unit vector in the direction between center of those two balls.
So the projection of velocity(vx,vy)  in the direction of (ax,ay) is  vx*ax+vy*ay
For the code:
ouble va1=(vx1*ax+vy1*ay), vb1=(-vx1*ay+vy1*ax);
double va2=(vx2*ax+vy2*ay), vb2=(-vx2*ay+vy2*ax);

Now, you know why va1,va2 can be calculated with the above formula.
vb1,vb2 are the normal components -- perpendicular to the (vx,vy)
So the unit vector is (-ay, ax)  -- (ax,ay)•(-ay,ax)=0 so those two vectors are perpendicular to each other.

For the 3D case, you just need to add another component (z) and calculate it in the same way.

I hope you can understand it. If not, let me know and I will try to explain it in more detail.
However,  I want you to understand it, so you will be able to solve similar problem next time. That is why I do not want to give you the exact solution. And you will be more confident when you can solve it by yourself.  You will enjoy the fun of learning this way,too!  ;)