First person camera using quaternion

For my first person camera in 3 .js I need to constrain the pitch to the range [-PI / 2; PI / 2]. This works great with the example code from 3js.org using Euler angles, which I rewrote to suit my needs:

this.pitchObj.rotation.x -= input.check("looky") * settings.mouse.sensitivityY;
this.pitchObj.rotation.x = Math.clamp(this.pitchObj.rotation.x, -HALF_PI, HALF_PI);

      

However, since I changed a few things, I now need to use the quaternion version. Here, the spin works like a charm, but the stride is not limited. So how can I write the above snippet using quaternion rotation?

My attempt:

this.pitchObj.rotateX(-this.input.check("looky") * settings.mouse.sensitivityY);
this.pitchObj.quaternion.x = Math.clamp(this.pitchObj.quaternion.x, -HALF_PI, HALF_PI);

      

+3


source to share





All Articles