I'm encountering a challenge in determining the angle of an object in relation to the camera. My goal is to write code for a spaceship with a camera that follows it. While I have successfully made the camera track the ship, there are instances where the rotation of the camera is slightly off. Here's my current camera code:
var focalpoint = new THREE.Vector3(
actor.position.x,
actor.position.y,
actor.position.z + 14
);
//move camera closer to the object if it gets too far away
var calculatedDistance = distance(camera.position, actor.position);
var cameraTolerance = calculatedDistance - this.cameradistance.min;
var closingvelocity = cameraTolerance * 0.02;
if(calculatedDistance > this.cameradistance.max)cameravelocity.z = -closingvelocity;
if(calculatedDistance < this.cameradistance.min)cameravelocity.z = closingvelocity;
//slow down the camera
if(calculatedDistance < this.cameradistance.max && calculatedDistance > this.cameradistance.min){
cameravelocity.z = 0;
}
camera.translateX( cameravelocity.x );
camera.translateY( cameravelocity.y );
camera.translateZ( cameravelocity.z );
camera.lookAt(focalpoint);
camera.rotation.z = 0;
Currently, I am faced with the task of limiting the spaceship's (actor) rotation so that it doesn't veer towards the camera, and also resolving the issue of camera flipping over. To address this, I need to determine how to calculate the rotation of the actor in relation to the camera, but I'm unsure of where to begin.