In this article I will show you how to easily calculate the angle of your camera in either degrees or radians for ThreeJS.
First of all you should add the following code to your ThreeJS Animate function:
var cameraAngle = getCameraAngle(yourCamera.getWorldDirection()); console.log('camera angle:' + cameraAngle);
Next you need to declare the following two functions:
/// /// Get ThreeJS Camera Angle /// function getCameraAngle(cameraVector) { return radiansToDegrees(Math.atan2(cameraVector.x, cameraVector.z)); } /// /// Convert Radians to Degrees /// function radiansToDegrees(radians) { return radians * 180 / Math.PI; }
The code above returns the camera angle in degrees, to switch this to Radians just change the getCameraAngle function to the following:
/// /// Get ThreeJS Camera Angle /// function getCameraAngle(cameraVector) { return Math.atan2(cameraVector.x, cameraVector.z); }
Thats how you get the angle your ThreeJS camera is pointing in, in either degrees or radians!
Leave Your Comments...