Using local rotations - how to find stop point

I am rotating an entity with this code, where angMult is just a script attribute to vary the size. Because it is using Euler angles, I’m not sure how to stop when the entity rotates to a particular 90 degree point. The y value simple starts reversing, while x and z go to 180. Is there a ‘right way’ to do this? I just want to rotate an item 180 degrees, but stop at that point. The pause is just so I can see each set of rotate values.

click event comes here—>

       if (!this.paused) {
            console.log("dt, angMult: " + dt + " " + this.angMult);
            this.entity.rotate(0, this.angMult * 100 * dt, 0);
            var q = this.entity.getRotation();
            var v = q.getEulerAngles();
            console.log(v.x + "," + v.y + "," + v.z);
            this.paused = true;

Well the problem with Euler angles is that multiple values can show the same rotations, so comparison of the elements isn’t going to work.

If you set up some Quaternions on your desired angles you can measure the difference in the angles between them and stop when it’s suitably small.

    var scratchQ = new pc.Quat();
    function rotationDifference(q1, q2) {
        return scratchQ.mul2(q1,scratchQ);

The above function will return the quaternion that represents the difference between the two. If you want to measure how big this difference is in degrees then you can do:

console.log(rotationDifference(this.entity.getLocalRotation(), someStopPoint).getEulerAngles().length())

Though for performance you should use .lengthSq() to avoid a recursive square root. Also my scratchQ is a global variable to stop there being lots of garbage collection caused by allocating a new Quat each time the function is called - but this means it’s value is overwritten each time the function is called. If for some reason you needed to store it you would do someQuaternionToStoreAnswer.copy(rotationDifference(this.entity.getLocalRotation(), someStopPoint))

someStopPoint might be declared like this: var someStopPoint = new pc.Quat(); someStopPoint.setFromEulerAngles(0,90,0);

Thanks for that. I’ll try putting that into place, although the performance sounds a bit higher than just a subtraction and test for zero!