In Three.js, I would like to use THREE.quaternion to make the camera object rotate to the selected object.
I did search the web but found no example/demo or document about how to use this quaternion class.
I try my luck with following code:
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = 10;
camera.position.z = 0;
camera.position.x = radious;
camera.useQuaternion = true;
// I did use the TrackballTrackballControls. Maybe it causes the problem so I put it here
controls = new THREE.TrackballControls( camera, document.getElementById(_canvasElement) );
// function to make the camera rotate to the object
function focusOn3DObject(obj){
obj.useQuaternion = true;
obj.quaternion = new THREE.Quaternion(obj.position.x, obj.position.y, obj.position.z, 1);
var newQuaternion = new THREE.Quaternion();
THREE.Quaternion.slerp(camera.quaternion, obj.quaternion, newQuaternion, 0.07);
camera.quaternion = newQuaternion;
}
But it doesn't work. Did I miss something? Please help. Thanks in advance.
You can use the method applyQuaternion from ThreeJs
const quaternion = new THREE.Quaternion(obj.position.x, obj.position.y, obj.position.z, 1);
camera.applyQuaternion(quaternion); // Apply Quaternion
camera.quaternion.normalize(); // Normalize Quaternion
I think this line wont work:
obj.quaternion = new THREE.Quaternion(obj.position.x, obj.position.y, obj.position.z, 1);
You need to set
obj.useQuaternion = true
After adding object to scene, when you rotate it will be automatically applied to obj.quaternion
.
The second point is you should apply the object to your controls and not the camera. Because you want to control the object and not the camera?
I am creating a popup window that goes to hello.html. I want my original (parent page) to reload when i close the popup window (hello.html). I can't seem to get it to work, but I'm close. Here is ...
I am creating a popup window that goes to hello.html. I want my original (parent page) to reload when i close the popup window (hello.html). I can't seem to get it to work, but I'm close. Here is ...
In the IE developer (F12) console, I've managed to get my pages to run without errors; all but one! SCRIPT1002: Syntax error mypage.php, line 1 character 6 I am using IE9. Whats it's problem? ...
In the IE developer (F12) console, I've managed to get my pages to run without errors; all but one! SCRIPT1002: Syntax error mypage.php, line 1 character 6 I am using IE9. Whats it's problem? ...
I have several images positioned on top of each other using absolute positioning. These images are partially transparent, and have a html area and map to make only the visible parts clickable. In ...
I have several images positioned on top of each other using absolute positioning. These images are partially transparent, and have a html area and map to make only the visible parts clickable. In ...
I'm currently working on a big JavaScript project and I'm struggling with mapping incomming JSON data (from the backend) to my own JavaScript objects. I am using the Knockout JavaScript MVVM ...
I'm currently working on a big JavaScript project and I'm struggling with mapping incomming JSON data (from the backend) to my own JavaScript objects. I am using the Knockout JavaScript MVVM ...