I want to prevent the camera from clipping into walls and ground. My idea was to attach a small sphere to a camera (I have an octree implemented from the FPS example), test that sphere against the collider, and if there is a collision, move a sphere along the normals and towards the orbit target, like in popular MMOs.
code:
const sphereCollider = new THREE.Sphere( cameraStartingPosition, 0.2 );
// animation loop
sphereCollider.center.copy(camera.position);
const result = collider.sphereIntersect(sphereCollider);
if ( result ) {
const normalVector: THREE.Vector3 = result.normal.multiplyScalar( result.depth );
camera.position.addScaledVector(normalVector, 1);
camera.position.lerp(orbitControls.target, 0.01);
}
Issues:
#1 It works only when I rotate the camera slowly, if I do it fast it clips. (see the video)
#2 When a camera gets closer to the orbit target due to collision I have to manually re-position it, how to do that automatically?
#3 When a camera is next to the wall, I can see inside, how to prevent that?