Before the collision happens, the total momentum of both boxes is calculated ((mass1 x velocity1)+(mass2 x velocity2)). Then, using the total mass, the velocity of both masses is calculated: (totalMomentum / (mass1 + mass2)).