Question

I'm having trouble figuring out the angle of an object relative to the camera, I'm trying to code a spaceship with a camera following it. I have the camera following the ship but the rotation of the camera sometimes is a little off, here's my 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;

Now I need to limit the rotation of the spaceship (actor) so it doesn't start flying towards the camera, and to fix the camera flipping over problem. So I need to figure out how to find the rotation of the actor relative to the camera, I have absolutely no idea where to start calculating or even how.

Était-ce utile?

La solution

found the answer, by inversing the target's rotation, then multiplying like so: var rotationOffset = actor.quaternion.clone().inverse(); var rotation = camera.quaternion.clone().multiply( rotationOffset );

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top