Fix arc camera controls
Amp-Thread-ID: https://ampcode.com/threads/T-01a02421-6802-705a-bc0d-f0745c5902b9 Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
@@ -144,8 +144,16 @@ export class ArcRotateCamera extends Camera {
|
|||||||
if (this.#pointer.button === 2) {
|
if (this.#pointer.button === 2) {
|
||||||
const row = this.cameraRow();
|
const row = this.cameraRow();
|
||||||
this.#updateTransform(() => {
|
this.#updateTransform(() => {
|
||||||
row[16] -= x * (this.#controls?.panSpeed ?? 0.005);
|
const speed = this.#controls?.panSpeed ?? 0.005;
|
||||||
row[17] += y * (this.#controls?.panSpeed ?? 0.005);
|
const horizontal = -x * speed;
|
||||||
|
const vertical = y * speed;
|
||||||
|
const sinAlpha = Math.sin(row[13]);
|
||||||
|
const cosAlpha = Math.cos(row[13]);
|
||||||
|
const sinBeta = Math.sin(row[14]);
|
||||||
|
const cosBeta = Math.cos(row[14]);
|
||||||
|
row[16] += horizontal * cosAlpha - vertical * cosBeta * sinAlpha;
|
||||||
|
row[17] += vertical * sinBeta;
|
||||||
|
row[18] -= horizontal * sinAlpha + vertical * cosBeta * cosAlpha;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const speed = this.#controls.orbitSpeed ?? 0.005;
|
const speed = this.#controls.orbitSpeed ?? 0.005;
|
||||||
|
|||||||
@@ -100,12 +100,14 @@ export class Camera extends Node {
|
|||||||
const z = target[2] - position.z;
|
const z = target[2] - position.z;
|
||||||
const length = Math.hypot(x, y, z) || 1;
|
const length = Math.hypot(x, y, z) || 1;
|
||||||
const direction = [x / length, y / length, z / length];
|
const direction = [x / length, y / length, z / length];
|
||||||
if (direction[2] > 0.999999) this.setRotor([0, 1, 0, 0]);
|
const horizontal = Math.hypot(direction[0], direction[2]);
|
||||||
else {
|
const yaw = horizontal > 0.000001
|
||||||
const rotor = [direction[1], -direction[0], 0, 1 - direction[2]];
|
? Math.atan2(-direction[0], -direction[2])
|
||||||
const rotorLength = Math.hypot(...rotor);
|
: 0;
|
||||||
this.setRotor(rotor.map((lane) => lane / rotorLength));
|
const pitch = Math.atan2(direction[1], horizontal);
|
||||||
}
|
const sy = Math.sin(yaw / 2), cy = Math.cos(yaw / 2);
|
||||||
|
const sx = Math.sin(pitch / 2), cx = Math.cos(pitch / 2);
|
||||||
|
this.setRotor([sx * cy, cx * sy, -sx * sy, cx * cy]);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user