最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - THREE.js - Rotate the camera just like trackball controls - Stack Overflow

programmeradmin1浏览0评论

I'm using Trackball controls in a scene and I want to implement a function to rotate the camera just like the way dragging the mouse in the canvas does it. How can I acplish something like that? I've been looking the code of the Trackball control module, but I can't find something to start.

EDIT: I've been looking several pages, the THREE documentation and whatnot, but still can't reproduce the Trackball style rotation. I've been using quaternions too but they can't reproduce the behavior(or I'm missing something, most probably). Any help?

EDIT 2 : What I'm looking for is a way to do something like this:

function rotateCam(angle) { // code }

var angle = 0.01; //some value
rotateCam(angle);
$('#button').addEventListener('mousedown', function() { rotateCam(angle); } );

Where button is an HTML element representing a button.

I'm using Trackball controls in a scene and I want to implement a function to rotate the camera just like the way dragging the mouse in the canvas does it. How can I acplish something like that? I've been looking the code of the Trackball control module, but I can't find something to start.

EDIT: I've been looking several pages, the THREE documentation and whatnot, but still can't reproduce the Trackball style rotation. I've been using quaternions too but they can't reproduce the behavior(or I'm missing something, most probably). Any help?

EDIT 2 : What I'm looking for is a way to do something like this:

function rotateCam(angle) { // code }

var angle = 0.01; //some value
rotateCam(angle);
$('#button').addEventListener('mousedown', function() { rotateCam(angle); } );

Where button is an HTML element representing a button.

Share Improve this question edited Aug 13, 2013 at 21:18 Leprosy asked Jul 25, 2013 at 15:31 LeprosyLeprosy 1,1554 gold badges14 silver badges36 bronze badges 2
  • Shouldn't you be able to click the trackball button and then move the trackball around to get the same effect? – Grant Commented Jul 25, 2013 at 21:15
  • I need to do it without the mouse. – Leprosy Commented Jul 25, 2013 at 21:25
Add a ment  | 

3 Answers 3

Reset to default 3

I noticed that the Trackball controls, apart of rotate via quaternion, do a zoom to correct some distances. I tried to read the code, and got this:

function rotate(L) {
    var vector = controls.target.clone();
    var l = (new THREE.Vector3()).subVectors(camera.position, vector).length();
    var up = camera.up.clone();
    var quaternion = new THREE.Quaternion();

    // Zoom correction
    camera.translateZ(L - l);

    quaternion.setFromAxisAngle(up, 0.015);
    camera.position.applyQuaternion(quaternion);
    camera.lookAt(vector);
    renderer.render(scene, camera);
}

Works like a charm...hope someone find this useful too. ;)

function cameraRotate(distance, angle){

            camera.position.x = distance * Math.cos( angle );
            camera.position.z = distance * Math.sin( angle );

}

This would rotate your camera at the specified distance and angle around the scene. If you want a smooth rotation you could call this with a small angle increase from the animate-loop, depending on input for example.

I'm on the right way I think...I did this:

function rotate() {
    if (this.showCase) {
        var vector = controls.target.clone(); // controls is a TrackballControls
        var up = camera.up.clone();
        var quaternion = new THREE.Quaternion();
        quaternion.setFromAxisAngle(up, 0.015);
        camera.position.applyQuaternion(quaternion);
        camera.lookAt(vector);
        renderer.render(this.scene, this.camera);
    }
},

Still, it doesn't look right right like the TrackballControls rotation.

发布评论

评论列表(0)

  1. 暂无评论