If you're looking to find out how a physics engine works you should check out Box2D, it's an open source 2D physics engine. https://github.com/erincatto/Box2D