Rotating about a point in world space causes object to fly off into space

Hello,

I’m writing a function called rotateQuaternionAroundPoint which is supposed to apply an absolute rotation with respect to the world about a world point. In other words, I expect that if the function is called twice with the same parameters, it should yield the same result.

In order to test it out, I’ve written an animation loop which rotates the object 180degrees, and then tries to snap it back to the original position and rotation before rotating 180 degrees again.

Unfortunately, after the initial 180degree rotation, the rotating object goes off the rails in an inexplicable manner. I have a feeling this is some issue related to pointers or transforms persisting when they really shouldn’t.

Would appreciate a second pair of eyes on it.

Here’s the fiddle.

https://jsfiddle.net/7c5qx14L/