PaulHax / spin-controls

Arcball style controls for three.js Object3Ds and Cameras. Featuring pointer to trackball accuracy and unlimited rotation.
https://paulhax.github.io/spin-controls/
MIT License
78 stars 12 forks source link

how to limit the object rotation #18

Closed Yty202020 closed 3 years ago

Yty202020 commented 3 years ago

hi brother i want to limit the obj-3d spin like the OrbitControls.max(min)AzimuthAngle、max(min)PolarAngle i think may change this part to add limit ` return function applyVelocity() {

        timeStamp = performance.now();
        deltaTime = ( timeStamp - _lastVelTime ) / 1000.0;
        _lastVelTime = timeStamp;

        if( _this.spinAxisConstraint ) {

            normalizedAxis.copy( _this.spinAxisConstraint );
    deltaAngle = normalizedAxis.dot( _angularVelocity ) ;

  } else {

            normalizedAxis.copy( _angularVelocity );
            deltaAngle = _angularVelocity.length();

        }

        if ( deltaAngle && deltaTime ) {

            normalizedAxis.normalize();
    console.log(deltaAngle);
    quat.setFromAxisAngle( normalizedAxis, deltaAngle * deltaTime * _this.rotateSensitivity );

            _this.object.quaternion.normalize();
            _this.object.quaternion.premultiply(quat);

            // using small-angle approximation cos(x/2) = 1 - x^2 / 8

            if ( 8 * ( 1 - _lastQuaternion.dot( _this.object.quaternion ) ) > _EPS) {

                _this.dispatchEvent( changeEvent );

                _lastQuaternion.copy( _this.object.quaternion );

            }

        }

    };

` But im just a ui-d . I try to learn "quaternion" . emmm i want to die

PaulHax commented 3 years ago

Hehe, I don't understand quaternions either. But convert to a rotation representation you understand and apply the constraint at the application code level after the update() call.

spinControl.update();
const objectOri = new THREE.Euler( 0, 0, 0, 'YXZ' );;
objectOri.setFromQuaternion(spinControl.object.quaternion);
if (objectOri.y > 1) {
    objectOri.y = 1; // a silly constraint
}
spinControl.object.quaternion.setFromEuler(objectOri);
Yty202020 commented 3 years ago

thank a lot