Please check out [url=http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.0]2D Collision[/url] for related discussion.

You need to understand the physics before you can understand the code.
Please write down in detail your understanding about collision process,
and the code you do not understand, otherwise, I will not be able to help.