Initial Commit
This commit is contained in:
64 files changed
+13417
No files matched your search
@@ -0,0 +1,116 @@
|
||||
#! Contains `mass_data`: how heavy a shape is of a density, where its mass is
|
||||
#! centered, and how hard it is to turn about each axis, its inertia tensor,
|
||||
#! as a body's colliders sum to it.
|
||||
|
||||
import std::math;
|
||||
import malunal::linear;
|
||||
module malunal::physics3d;
|
||||
|
||||
#! How heavy something is, where its mass is centered, of its body's own
|
||||
#! places, and its inertia tensor about that center, of the body's own axes.
|
||||
pub struct mass_data {
|
||||
pub var mass: f32;
|
||||
pub var center: vec3;
|
||||
pub var inertia: mat3;
|
||||
}
|
||||
|
||||
#! It and another together: their masses summed about their joint center.
|
||||
pub def mass_data::add(&this, other: &mass_data): mass_data {
|
||||
val mass := this.mass + other.mass;
|
||||
return mass_data{ inertia = this.inertia + other.inertia } if mass <= 0;
|
||||
val center := (this.center * this.mass + other.center * other.mass) * (1 / mass);
|
||||
val inertia := this.inertia + shifted(this.mass, this.center - center) + other.inertia + shifted(other.mass, other.center - center);
|
||||
return mass_data{ mass = mass, center = center, inertia = inertia };
|
||||
}
|
||||
|
||||
# What a mass adds to an inertia tensor, turning about a center as far off as
|
||||
# `d`, as the parallel axis theorem finds.
|
||||
def shifted(mass: f32, d: vec3): mat3 {
|
||||
val dd := d.dot(d);
|
||||
return mat3{
|
||||
vec3{ mass * (dd - d.x * d.x), -mass * d.x * d.y, -mass * d.x * d.z },
|
||||
vec3{ -mass * d.y * d.x, mass * (dd - d.y * d.y), -mass * d.y * d.z },
|
||||
vec3{ -mass * d.z * d.x, -mass * d.z * d.y, mass * (dd - d.z * d.z) },
|
||||
};
|
||||
}
|
||||
|
||||
# A tensor of `across` about every axis but `along` about the axis `u`.
|
||||
def about_axis(across: f32, along: f32, u: vec3): mat3 {
|
||||
val k := along - across;
|
||||
return mat3{
|
||||
vec3{ across + k * u.x * u.x, k * u.x * u.y, k * u.x * u.z },
|
||||
vec3{ k * u.y * u.x, across + k * u.y * u.y, k * u.y * u.z },
|
||||
vec3{ k * u.z * u.x, k * u.z * u.y, across + k * u.z * u.z },
|
||||
};
|
||||
}
|
||||
|
||||
#! Its mass of a density, a mass for each volume of 1.
|
||||
pub def shape::mass(&this, density: f32): mass_data {
|
||||
val pi := math::pi as f32;
|
||||
val r := this.radius;
|
||||
if this.kind == shape_kind::sphere {
|
||||
val mass := density * 4f / 3f * pi * r * r * r;
|
||||
val i := 0.4 * mass * r * r;
|
||||
return mass_data{ mass = mass, center = this.a, inertia = about_axis(i, i, vec3::up) };
|
||||
}
|
||||
if this.kind == shape_kind::capsule {
|
||||
# A cylinder between its ends, and a sphere of its two round halves, each
|
||||
# half's own center 3r/8 past the cylinder's end.
|
||||
val length := (this.b - this.a).length;
|
||||
val cylinder := density * pi * r * r * length;
|
||||
val ball := density * 4f / 3f * pi * r * r * r;
|
||||
val along := cylinder * r * r * 0.5 + ball * 0.4 * r * r;
|
||||
val across := cylinder * (length * length / 12 + r * r / 4) + ball * (0.4 * r * r + length * length / 4 + 3 * length * r / 8);
|
||||
return mass_data{ mass = cylinder + ball, center = (this.a + this.b) * 0.5, inertia = about_axis(across, along, (this.b - this.a).normalized()) };
|
||||
}
|
||||
return hull_mass(this.as_hull(), density);
|
||||
}
|
||||
|
||||
# A hull's, as the tetrahedra of each face's triangles and a place within it
|
||||
# sum it.
|
||||
def hull_mass(h: &hull, density: f32): mass_data {
|
||||
val origin := h.center;
|
||||
var volume: f32 = 0;
|
||||
var center := vec3::zero;
|
||||
# The covariance of the hull's places about the origin.
|
||||
var xx: f32 = 0;
|
||||
var yy: f32 = 0;
|
||||
var zz: f32 = 0;
|
||||
var xy: f32 = 0;
|
||||
var xz: f32 = 0;
|
||||
var yz: f32 = 0;
|
||||
for f in 0..h.faces {
|
||||
val a := h.point(h.corner(f, 0)) - origin;
|
||||
for k in 1..h.face_size(f) - 1 {
|
||||
val b := h.point(h.corner(f, k)) - origin;
|
||||
val c := h.point(h.corner(f, k + 1)) - origin;
|
||||
val det := a.dot(b.cross(c));
|
||||
volume += det / 6;
|
||||
center = center + (a + b + c) * (det / 24);
|
||||
val s := a + b + c;
|
||||
val w := det / 120;
|
||||
xx += w * (a.x * a.x + b.x * b.x + c.x * c.x + s.x * s.x);
|
||||
yy += w * (a.y * a.y + b.y * b.y + c.y * c.y + s.y * s.y);
|
||||
zz += w * (a.z * a.z + b.z * b.z + c.z * c.z + s.z * s.z);
|
||||
xy += w * (a.x * a.y + b.x * b.y + c.x * c.y + s.x * s.y);
|
||||
xz += w * (a.x * a.z + b.x * b.z + c.x * c.z + s.x * s.z);
|
||||
yz += w * (a.y * a.z + b.y * b.z + c.y * c.z + s.y * s.z);
|
||||
}
|
||||
}
|
||||
return mass_data{} if volume <= tiny;
|
||||
center = center * (1 / volume);
|
||||
val mass := density * volume;
|
||||
# Of the covariance about the center of mass, the tensor.
|
||||
xx = density * xx - mass * center.x * center.x;
|
||||
yy = density * yy - mass * center.y * center.y;
|
||||
zz = density * zz - mass * center.z * center.z;
|
||||
xy = density * xy - mass * center.x * center.y;
|
||||
xz = density * xz - mass * center.x * center.z;
|
||||
yz = density * yz - mass * center.y * center.z;
|
||||
val inertia := mat3{
|
||||
vec3{ yy + zz, -xy, -xz },
|
||||
vec3{ -xy, xx + zz, -yz },
|
||||
vec3{ -xz, -yz, xx + yy },
|
||||
};
|
||||
return mass_data{ mass = mass, center = origin + center, inertia = inertia };
|
||||
}
|
||||
Reference in new issue
Block a user