2024-01-06 13:21:11 -06:00
|
|
|
import { clamp } from 'lodash';
|
2024-03-12 18:00:00 -05:00
|
|
|
import { degToRad } from 'three/src/math/MathUtils';
|
2024-04-20 12:00:48 -05:00
|
|
|
import { PitchLimits, YawLimits } from '../blitzrinth/modelDefinitions';
|
2024-03-09 20:56:22 -06:00
|
|
|
import { normalizeAngleRad } from '../math/normalizeAngleRad';
|
2024-01-06 13:21:11 -06:00
|
|
|
|
|
|
|
|
export function applyPitchYawLimits(
|
|
|
|
|
pitch: number, // rad
|
|
|
|
|
yaw: number, // rad
|
|
|
|
|
pitchLimits: PitchLimits, // deg
|
|
|
|
|
yawLimits?: YawLimits, // deg
|
2024-03-12 18:00:00 -05:00
|
|
|
verticalStabilizer = false,
|
2024-01-06 13:21:11 -06:00
|
|
|
) {
|
|
|
|
|
let newPitch = pitch;
|
|
|
|
|
let newYaw = yaw;
|
|
|
|
|
|
|
|
|
|
if (yawLimits) {
|
2024-03-08 20:43:30 -06:00
|
|
|
newYaw = normalizeAngleRad(
|
2024-03-12 18:00:00 -05:00
|
|
|
clamp(newYaw, -degToRad(yawLimits.max), -degToRad(yawLimits.min)),
|
2024-01-06 13:21:11 -06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 18:00:00 -05:00
|
|
|
const pitchBuff = verticalStabilizer ? degToRad(3) : 0;
|
|
|
|
|
let lowerPitch = -degToRad(pitchLimits.max) - pitchBuff;
|
|
|
|
|
let upperPitch = -degToRad(pitchLimits.min) + pitchBuff;
|
|
|
|
|
const transition = degToRad(pitchLimits.transition ?? 10);
|
2024-01-06 13:21:11 -06:00
|
|
|
|
|
|
|
|
if (pitchLimits.back) {
|
2024-03-12 18:00:00 -05:00
|
|
|
const range = degToRad(pitchLimits.back.range);
|
2024-03-08 20:43:30 -06:00
|
|
|
const yawRotatedAbs = Math.abs(normalizeAngleRad(newYaw - Math.PI));
|
2024-01-06 13:21:11 -06:00
|
|
|
|
|
|
|
|
if (yawRotatedAbs <= range + transition) {
|
|
|
|
|
if (yawRotatedAbs <= range) {
|
|
|
|
|
// inside range
|
2024-03-12 18:00:00 -05:00
|
|
|
lowerPitch = -degToRad(pitchLimits.back.max);
|
|
|
|
|
upperPitch = -degToRad(pitchLimits.back.min);
|
2024-01-06 13:21:11 -06:00
|
|
|
} else {
|
|
|
|
|
// inside transition
|
|
|
|
|
const transitionProgress = (yawRotatedAbs - range) / transition;
|
|
|
|
|
const maxInterpolatedPitch =
|
|
|
|
|
(1 - transitionProgress) * pitchLimits.back.max +
|
|
|
|
|
transitionProgress * pitchLimits.max;
|
|
|
|
|
const minInterpolatedPitch =
|
|
|
|
|
(1 - transitionProgress) * pitchLimits.back.min +
|
|
|
|
|
transitionProgress * pitchLimits.min;
|
|
|
|
|
|
2024-03-12 18:00:00 -05:00
|
|
|
lowerPitch = -degToRad(maxInterpolatedPitch);
|
|
|
|
|
upperPitch = -degToRad(minInterpolatedPitch);
|
2024-01-06 13:21:11 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pitchLimits.front) {
|
2024-03-12 18:00:00 -05:00
|
|
|
const range = degToRad(pitchLimits.front.range);
|
2024-03-08 20:43:30 -06:00
|
|
|
const yawAbs = Math.abs(normalizeAngleRad(newYaw));
|
2024-01-06 13:21:11 -06:00
|
|
|
|
|
|
|
|
if (yawAbs <= range + transition) {
|
|
|
|
|
if (yawAbs <= range) {
|
|
|
|
|
// inside range
|
2024-03-12 18:00:00 -05:00
|
|
|
lowerPitch = -degToRad(pitchLimits.front.max);
|
|
|
|
|
upperPitch = -degToRad(pitchLimits.front.min);
|
2024-01-06 13:21:11 -06:00
|
|
|
} else {
|
|
|
|
|
// inside transition
|
|
|
|
|
const transitionProgress = (yawAbs - range) / transition;
|
|
|
|
|
const maxInterpolatedPitch =
|
|
|
|
|
(1 - transitionProgress) * pitchLimits.front.max +
|
|
|
|
|
transitionProgress * pitchLimits.max;
|
|
|
|
|
const minInterpolatedPitch =
|
|
|
|
|
(1 - transitionProgress) * pitchLimits.front.min +
|
|
|
|
|
transitionProgress * pitchLimits.min;
|
|
|
|
|
|
2024-03-12 18:00:00 -05:00
|
|
|
lowerPitch = -degToRad(maxInterpolatedPitch);
|
|
|
|
|
upperPitch = -degToRad(minInterpolatedPitch);
|
2024-01-06 13:21:11 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newPitch = clamp(newPitch, lowerPitch, upperPitch);
|
|
|
|
|
|
|
|
|
|
return [newPitch, newYaw];
|
|
|
|
|
}
|