简体   繁体   中英

How to find the rotation of an object relative to the camera

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.

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 ); var rotationOffset = actor.quaternion.clone().inverse(); var rotation = camera.quaternion.clone().multiply( rotationOffset );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM