How to rotate a object on axis world three.js?

后端 未结 5 1419
深忆病人
深忆病人 2020-12-03 05:35

Is it possible to do rotations taking axis of the world and not of the object?

I need to do some rotations of an object, but after the first rotation, I can\'t do ot

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 05:57

    Here's a small variation. Tested with r56.

    THREE.Object3D._matrixAux = new THREE.Matrix4(); // global auxiliar variable
    // Warnings: 1) axis is assumed to be normalized. 
    //  2) matrix must be updated. If not, call object.updateMatrix() first  
    //  3) this assumes we are not using quaternions
    THREE.Object3D.prototype.rotateAroundWorldAxis = function(axis, radians) { 
        THREE.Object3D._matrixAux.makeRotationAxis(axis, radians);
        this.matrix.multiplyMatrices(THREE.Object3D._matrixAux,this.matrix); // r56
        THREE.Object3D._matrixAux.extractRotation(this.matrix);
        this.rotation.setEulerFromRotationMatrix(THREE.Object3D._matrixAux, this.eulerOrder ); 
        this.position.getPositionFromMatrix( this.matrix );
    }
    THREE.Object3D.prototype.rotateAroundWorldAxisX = function(radians) { 
        this._vector.set(1,0,0);
        this.rotateAroundWorldAxis(this._vector,radians);
    }
    THREE.Object3D.prototype.rotateAroundWorldAxisY = function(radians) { 
        this._vector.set(0,1,0);
        this.rotateAroundWorldAxis(this._vector,radians);
    }
    THREE.Object3D.prototype. rotateAroundWorldAxisZ = function(degrees){ 
        this._vector.set(0,0,1);
        this.rotateAroundWorldAxis(this._vector,degrees);
    }
    

    The three last lines are just to resync the params (position,rotation) from the matrix... I wonder if there is a more efficient way to do that...

提交回复
热议问题