The code to do this is 100% open-source, you can view it on GitHub here: physics_2d_game_sopwith (the main code is in “game.pas” unit).
Also, we now have a manual page describing how to use physics in Castle Game Engine.
This work was sponsored by Castle Game Engine supporters on Patreon. If you want to help in the engine development, please support me on Patreon! Thank you!