I'm making a game using JigLib and GLGE. The camera follows the player the way I want it to, but when the player is close to a wall, the camera moves to the other side of the wall, which is annoying when there is something that needs quick reflexes.
I tried using picking to move the camera when it can't see the player, but it only moves until half of the wall is in front of it and half is behind it, making an annoying split screen effect where one half of the screen is blank.
camera.setLocY(Math.max(playerMesh.getLocY() - 10, 3));
camera.setRotY(playerHeading);
camera.setLocX(playerMesh.getLocX() + Math.sin(playerHeading) * Math.max(camera.getLocY() - playerMesh.getLocY() + 5, 0));
camera.setLocZ(playerMesh.getLocZ() + Math.cos(playerHeading) * Math.max(camera.getLocY() - playerMesh.getLocY() + 5, 0));
camera.setRotX(Math.atan2(playerMesh.getLocY() - camera.getLocY(), Math.max(camera.getLocY() - playerMesh.getLocY() + 5, 0)));
var playerVec = [playerMesh.getLocX(), playerMesh.getLocY(), playerMesh.getLocZ()],
cameraVec = [camera.getLocX(), camera.getLocY(), camera.getLocZ()];
scene.removeChild(playerMesh);
scene.removeChild(floor);
do {
var ray = scene.pick(canvas.width / 2, canvas.height / 2);
if (ray.distance && ray.distance < GLGE.distanceVec3(playerVec, cameraVec)) {
camera.setLocX(ray.coord[0]);
camera.setLocY(ray.coord[1]);
camera.setLocZ(ray.coord[2]);
cameraVec = ray.coord;
camera.setRotX(Math.atan2(playerMesh.getLocY() - camera.getLocY(), Math.max(camera.getLocY() - playerMesh.getLocY(), 0)));
}
} while (ray.distance && ray.distance < GLGE.distanceVec3(playerVec, cameraVec));
scene.addChild(playerMesh);
scene.addChild(floor);
Is there a resource where I could find information about how to move the camera to avoid walls? Is my code wrong?