Suppose we have two planets of masses $m_1$ and $m_2$ and of same radius $r$ kept a distance $d$.Owing to their gravitational force they both move and collide with each other. The system has undergone a change in potential energy. And this is negative of the work done by gravitational force. Now,according to the books,they assert the work done as $$-\int_{d}^{2r} \frac{Gm_1m_2}{x^2} \mathrm{d}x$$
But I am confused about how the formula is rigorously true since there is the dot product of force and displacement involved in finding work done. It is obvious that they calculated the work done on the system as a whole, but how do they assign the direction here? There are two bodies and the directions of their displacement are different and the forces being applied to the two bodies also differ in direction. So, how can they not account for those directions and simply deduced the formula taking negative of work done?
I tried to do the problem myself and began by finding the work done on each planet separately. Work done by gravitational force on $m_1$ is $$\int_{0}^{d_1} F \mathrm{d}x$$ The integral is positive since force and displacement are in the same direction. Here $d_1$ is the distance travelled by the center of the first planet at the time of collision. The same logic goes for $m_2$. Hence the total work done should be $$\int_{0}^{d_1} F \mathrm{d}x+\int_{d}^{d_1+2r} F \mathrm{d}x$$ But I don't think it matches with the formula in the book when put limits. Where did I go wrong then?