@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
45 lines (37 loc) • 1.53 kB
JavaScript
import { BinaryInterpolationAdapter } from "../sim/BinaryInterpolationAdapter.js";
/**
* Shortest-path nlerp for a 4 × Float32 quaternion payload (16 bytes).
* Switch to a slerp variant if you need it for wide per-tick angular
* steps (animation-blended joints, slow snapshot rate).
*
* @author Alex Goldring
* @copyright Company Named Limited (c) 2025
*/
export class QuaternionInterpolationAdapter extends BinaryInterpolationAdapter {
/** @inheritdoc */
interpolate(out_buffer, source, first_offset, second_offset, t) {
source.position = first_offset;
const ax = source.readFloat32();
const ay = source.readFloat32();
const az = source.readFloat32();
const aw = source.readFloat32();
source.position = second_offset;
let bx = source.readFloat32();
let by = source.readFloat32();
let bz = source.readFloat32();
let bw = source.readFloat32();
if (ax * bx + ay * by + az * bz + aw * bw < 0) {
bx = -bx; by = -by; bz = -bz; bw = -bw;
}
let qx = ax + (bx - ax) * t;
let qy = ay + (by - ay) * t;
let qz = az + (bz - az) * t;
let qw = aw + (bw - aw) * t;
const len2 = qx * qx + qy * qy + qz * qz + qw * qw;
const inv_len = len2 > 0 ? 1 / Math.sqrt(len2) : 1;
out_buffer.writeFloat32(qx * inv_len);
out_buffer.writeFloat32(qy * inv_len);
out_buffer.writeFloat32(qz * inv_len);
out_buffer.writeFloat32(qw * inv_len);
}
}