Yesterday, I was following this tutorial about Rotating Objects with Mouse. (This:
https://developer.playcanvas.com/en/tutorials/rotating-objects-with-mouse/)
The thing is that it isn’t smooth at all. It doesn’t ease-in and ease-out. So I wanted to try out making it smooth. What came into my mind was to add Lerp to the script of it. But I had no idea how to make it work. After some hours of searching on Lerp and Quat mathematics, I came up with this code: (FYI, surprisingly it doesn’t work either )
Rotate.horizontalQuat = new pc.Quat();
Rotate.verticalQuat = new pc.Quat();
Rotate.resultQuat = new pc.Quat();
Rotate.resultQuatAfterLerp = new pc.Quat(); //for storing the data after the Lerp
Rotate.prototype.rotate = function (dx, dy) {
var horzQuat = Rotate.horizontalQuat;
var vertQuat = Rotate.verticalQuat;
var resultQuat = Rotate.resultQuat;
// Create a rotation around the camera's orientation in order for them to be in
// screen space
horzQuat.setFromAxisAngle(this.cameraEntity.up, dx * this.orbitSensitivity);
vertQuat.setFromAxisAngle(this.cameraEntity.right, dy * this.orbitSensitivity);
// Apply both the rotations to the existing entity rotation
resultQuatAfterLerp.mul2 = pc.math.lerp((resultQuat.mul2()),(resultQuat.mul2(horzQuat, vertQuat)));
resultQuatAfterLerp.mul = pc.math.lerp((resultQuat.mul(this.entity.getRotation())), (resultQuat.mul(this.entity.getRotation())), dt / 0.2);
this.entity.setRotation(resultQuatAfterLerp);
};
What is this issue?
Oh, and what does resultQuat.mul2()
mean? I took it as,
mul2
is kinda a sub variable of reasultQuat
and that piece of code creates it. Another question I have in my mind is that what are horizontalQuat
, verticalQuat
, and resultQuat
? Do they work like sub-variables too?