Skip to content

Commit

Permalink
Math fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Sep 8, 2024
1 parent 3ccd38c commit a1f89a2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
57 changes: 34 additions & 23 deletions sources/ts/mat4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type mat4_t = {
m?: f32_array_t;
};

type mat4_decomposed_t = {
loc: vec4_t;
rot: quat_t;
scl: vec4_t;
}

let _mat4_vec: vec4_t = vec4_create();
let _mat4_mat: mat4_t = mat4_identity();

Expand Down Expand Up @@ -110,30 +116,35 @@ function mat4_compose(self: mat4_t, loc: vec4_t, quat: quat_t, sc: vec4_t): mat4
return self;
}

function mat4_decompose(self: mat4_t, loc: vec4_t, quat: quat_t, scale: vec4_t): mat4_t {
loc.x = self.m[12];
loc.y = self.m[13];
loc.z = self.m[14];
scale.x = vec4_len(vec4_new(self.m[0], self.m[1], self.m[2]));
scale.y = vec4_len(vec4_new(self.m[4], self.m[5], self.m[6]));
scale.z = vec4_len(vec4_new(self.m[8], self.m[9], self.m[10]));
if (mat4_determinant(self) < 0.0) {
scale.x = -scale.x;
function mat4_decompose(m: mat4_t): mat4_decomposed_t {
let loc: vec4_t;
let rot: quat_t = quat_create();
let scl: vec4_t;
loc.x = m.m[12];
loc.y = m.m[13];
loc.z = m.m[14];
scl.x = vec4_len(vec4_new(m.m[0], m.m[1], m.m[2]));
scl.y = vec4_len(vec4_new(m.m[4], m.m[5], m.m[6]));
scl.z = vec4_len(vec4_new(m.m[8], m.m[9], m.m[10]));
if (mat4_determinant(m) < 0.0) {
scl.x = -scl.x;
}
let invs: f32 = 1.0 / scale.x; // Scale the rotation part
_mat4_mat.m[0] = self.m[0] * invs;
_mat4_mat.m[1] = self.m[1] * invs;
_mat4_mat.m[2] = self.m[2] * invs;
invs = 1.0 / scale.y;
_mat4_mat.m[4] = self.m[4] * invs;
_mat4_mat.m[5] = self.m[5] * invs;
_mat4_mat.m[6] = self.m[6] * invs;
invs = 1.0 / scale.z;
_mat4_mat.m[8] = self.m[8] * invs;
_mat4_mat.m[9] = self.m[9] * invs;
_mat4_mat.m[10] = self.m[10] * invs;
quat_from_rot_mat(quat, _mat4_mat);
return self;
let invs: f32 = 1.0 / scl.x; // Scale the rotation part
_mat4_mat.m[0] = m.m[0] * invs;
_mat4_mat.m[1] = m.m[1] * invs;
_mat4_mat.m[2] = m.m[2] * invs;
invs = 1.0 / scl.y;
_mat4_mat.m[4] = m.m[4] * invs;
_mat4_mat.m[5] = m.m[5] * invs;
_mat4_mat.m[6] = m.m[6] * invs;
invs = 1.0 / scl.z;
_mat4_mat.m[8] = m.m[8] * invs;
_mat4_mat.m[9] = m.m[9] * invs;
_mat4_mat.m[10] = m.m[10] * invs;
quat_from_rot_mat(rot, _mat4_mat);

let dec: mat4_decomposed_t = { loc: loc, rot: rot, scl: scl };
return dec;
}

function mat4_set_loc(self: mat4_t, v: vec4_t): mat4_t {
Expand Down
5 changes: 4 additions & 1 deletion sources/ts/particle_sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ function particle_sys_update(raw: particle_sys_t, object: mesh_object_t, owner:
}

// Copy owner world transform but discard scale
mat4_decompose(owner.base.transform.world, raw.owner_loc, raw.owner_rot, raw.owner_scale);
let dec: mat4_decomposed_t = mat4_decompose(owner.base.transform.world);
raw.owner_loc = dec.loc;
raw.owner_rot = dec.rot;
raw.owner_scale = dec.scl;
object.base.transform.loc = raw.owner_loc;
object.base.transform.rot = raw.owner_rot;

Expand Down
5 changes: 4 additions & 1 deletion sources/ts/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ function scene_return_object_loaded(object: object_t, o: obj_t, oactions: scene_

function scene_gen_transform(object: obj_t, transform: transform_t) {
transform.world = object.transform != null ? mat4_from_f32_array(object.transform) : mat4_identity();
mat4_decompose(transform.world, transform.loc, transform.rot, transform.scale);
let dec: mat4_decomposed_t = mat4_decompose(transform.world);
transform.loc = dec.loc;
transform.rot = dec.rot;
transform.scale = dec.scl;
if (transform.object.parent != null) {
transform_update(transform);
}
Expand Down
10 changes: 8 additions & 2 deletions sources/ts/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ type transform_t = {
dim?: vec4_t;
radius?: f32;

///if arm_skin
bone_parent?: mat4_t;
last_world?: mat4_t;
///end

///if arm_anim
// Wrong order returned from get_euler(), store last state for animation
Expand Down Expand Up @@ -81,9 +82,11 @@ function transform_build_matrix(raw: transform_t) {
// transform_compose_delta(raw);
// }

///if arm_skin
if (raw.bone_parent != null) {
mat4_mult_mats(raw.local, raw.bone_parent, raw.local);
}
///end

if (raw.object.parent != null && !raw.local_only) {
mat4_mult_mats3x4(raw.world, raw.local, raw.object.parent.transform.world);
Expand Down Expand Up @@ -139,7 +142,10 @@ function transform_mult_matrix(raw: transform_t, mat: mat4_t) {
}

function transform_decompose(raw: transform_t) {
mat4_decompose(raw.local, raw.loc, raw.rot, raw.scale);
let dec: mat4_decomposed_t = mat4_decompose(raw.local);
raw.loc = dec.loc;
raw.rot = dec.rot;
raw.scale = dec.scl;
}

function transform_rotate(raw: transform_t, axis: vec4_t, f: f32) {
Expand Down
4 changes: 4 additions & 0 deletions sources/ts/vec4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ declare function vec4_y_axis(): vec4_t;
declare function vec4_z_axis(): vec4_t;
declare function vec4_nan(): vec4_t;
declare function vec4_isnan(a: vec4_t): bool;

type vec4_box_t = {
v: vec4_t;
};

0 comments on commit a1f89a2

Please sign in to comment.