imported from 3dPhysics and cleaned up.