Rotation for cubes on entities

This commit is contained in:
2026-11-13 23:06:37 -03:00
parent c74ddedd6e
commit 9bf6b98953
5 changed files with 58 additions and 15 deletions
+8 -7
View File
@@ -3,7 +3,7 @@
// turning and moving the parts of models: entity models' bones, the player's parts and what's worn on them. no
// drawing here, so tests can use it
import type { EntityBoneJson, EntityModelJson } from "@/common/mod_data.ts";
import type { EntityBoneJson, EntityCubeJson, EntityModelJson } from "@/common/mod_data.ts";
export type Vec3 = [number, number, number];
@@ -17,11 +17,12 @@ export function model_pivot(bone: EntityBoneJson): Vec3 {
return [-bone.pivot[0], bone.pivot[1], bone.pivot[2]];
}
// its own rotation around its pivot. mirroring x turns x and y rotations the other way
export function bone_matrix(bone: EntityBoneJson): Matrix {
if (!bone.rotation) return IDENTITY;
const [rx, ry, rz] = bone.rotation.map((degrees) => degrees * Math.PI / 180);
return rotation_around(model_pivot(bone), -rx, -ry, rz);
// a bone's or a cube's own rotation around its pivot. mirroring x turns x and y rotations the other way
export function own_matrix(part: EntityBoneJson | EntityCubeJson): Matrix {
if (!part.rotation) return IDENTITY;
const [rx, ry, rz] = part.rotation.map((degrees) => degrees * Math.PI / 180);
const [px, py, pz] = part.pivot ?? [0, 0, 0];
return rotation_around([-px, py, pz], -rx, -ry, rz);
}
// every bone's matrix: its own rotation, then pose's for it (like a head looking or a limb swinging), then its
@@ -29,7 +30,7 @@ export function bone_matrix(bone: EntityBoneJson): Matrix {
export function bone_matrices(model: EntityModelJson, pose: (bone: EntityBoneJson) => Matrix): Map<string, Matrix> {
const bones = new Map<string, Matrix>();
for (const bone of model.bones) {
let matrix = multiply(pose(bone), bone_matrix(bone));
let matrix = multiply(pose(bone), own_matrix(bone));
if (bone.parent) matrix = multiply(bones.get(bone.parent) ?? IDENTITY, matrix);
bones.set(bone.name, matrix);
}