Hey!!!
It’s me again with a new problem. Please tell me it hasn’t been fixed in version 8.16.0, as I’m using version 8.15.1… or please tell me it has
.
For context, I use my own camera, as an extend of the Universal Camera. It behaves like the Google Maps camera. To achieve this, I move it by editing the target and position. That’s why I had issues in the first place, because I use setTarget
a lot.
In a certain context, I position the camera at the perfect vertical angle. From this position, I cannot tilt the camera any more, meaning that I cannot edit the rotation.x
using setTarget
.
After some research, I found that this is caused by:
rotationQuaternion.toEulerAnglesToRef(this.rotation);
. Somehow, my angle triggers this:
const zAxisY = rotationQuaternion._y * rotationQuaternion._z - rotationQuaternion._x * rotationQuaternion._w;
const limit = 0.4999999;
if (zAxisY < -limit) {
We fall into the first if condition, zAxisY < -limit
, and it locks x at Math.PI / 2
. However, this may be a side effect of Quaternion.FromRotationMatrixToRef(TmpMatrix, rotationQuaternion)
. My maths knowledge is a little rusty, but I don’t think so.
For now, the only workaround I have found is to override setTarget
and replace this part with the old one, so it looks something like this:
public override setTarget(target: Vector3): void {
this.upVector.normalize();
this._initialFocalDistance = target.subtract(this.position).length();
if (this.position.z === target.z) {
this.position.z += Epsilon;
}
this._referencePoint.normalize().scaleInPlace(this._initialFocalDistance);
if (this.getScene().useRightHandedSystem) {
Matrix.LookAtRHToRef(this.position, target, Vector3.UpReadOnly, TmpMatrix);
} else {
Matrix.LookAtLHToRef(this.position, target, Vector3.UpReadOnly, TmpMatrix);
}
TmpMatrix.invert();
this.rotation.x = Math.atan(TmpMatrix.m[6] / TmpMatrix.m[10]);
const vDir = target.subtract(this.position);
if (vDir.x >= 0.0) {
this.rotation.y = -Math.atan(vDir.z / vDir.x) + Math.PI / 2.0;
} else {
this.rotation.y = -Math.atan(vDir.z / vDir.x) - Math.PI / 2.0;
}
this.rotation.z = 0;
if (isNaN(this.rotation.x)) {
this.rotation.x = 0;
}
if (isNaN(this.rotation.y)) {
this.rotation.y = 0;
}
if (this.rotationQuaternion) {
Quaternion.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this.rotationQuaternion);
}
}
The first part remains the same; I just replaced the rotation computation with the old one. I’m not sure if this breaks the fix for the RH problem.
However, having read your first message in the Git conversation, and given that I am still using the Matrix.LookAtRHToRef
function, the fix might still be there.
I am yet to reproduce it in a playground…