Some code for precise collision detection