This part of code taken from "Game Programming Algorithms and Techniques" book