Files
physics/physics2d/mass.sakura
T
2026-10-30 23:21:01 -05:00

78 lines
3.1 KiB
Plaintext

#! Contains `mass_data`: how heavy a shape is of a density, where its mass is
#! centered, and how hard it is to turn, as a body's colliders sum to it.
import std::math;
import malunal::linear;
module malunal::physics2d;
#! How heavy something is, where its mass is centered, of its body's own
#! places, and how hard it is to turn about that center: its rotational
#! inertia.
pub struct mass_data {
pub var mass: f32;
pub var center: vec2;
pub var inertia: f32;
}
#! 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);
# Each turns about the joint center as about its own, and as far as it's
# from it, as the parallel axis theorem finds.
val inertia := this.inertia + this.mass * (this.center - center).length_squared
+ other.inertia + other.mass * (other.center - center).length_squared;
return mass_data{ mass = mass, center = center, inertia = inertia };
}
#! Its mass of a density, a mass for each area of 1.
pub def shape::mass(&this, density: f32): mass_data {
if this.kind == shape_kind::circle {
val rr := this.radius * this.radius;
val mass := density * math::pi as f32 * rr;
return mass_data{ mass = mass, center = this.point(0), inertia = mass * rr * 0.5 };
}
if this.kind == shape_kind::capsule {
return capsule_mass(this.point(0), this.point(1), this.radius, density);
}
return polygon_mass(this, density);
}
# A capsule's: a box between its ends, and a circle of its two round halves,
# each half's own center 4r/3π past the box's end, as Box2D finds it.
def capsule_mass(p1: vec2, p2: vec2, radius: f32, density: f32): mass_data {
val rr := radius * radius;
val length := (p2 - p1).length;
val circle := density * math::pi as f32 * rr;
val box := density * 2 * radius * length;
val half := length * 0.5;
val off := 4 * radius / (3 * math::pi as f32);
val inertia := circle * (0.5 * rr + half * half + 2 * half * off) + box * (4 * rr + length * length) / 12;
return mass_data{ mass = circle + box, center = (p1 + p2) * 0.5, inertia = inertia };
}
# A polygon's, as the triangles fanned from its first corner sum it.
def polygon_mass(s: &shape, density: f32): mass_data {
val origin := s.point(0);
var area: f32 = 0;
var center := vec2::zero;
var inertia: f32 = 0;
for i in 1..s.count - 1 {
val e1 := s.point(i) - origin;
val e2 := s.point(i + 1) - origin;
val d := e1.cross(e2);
val triangle := 0.5 * d;
area += triangle;
center = center + (e1 + e2) * (triangle / 3);
val xx := e1.x * e1.x + e2.x * e1.x + e2.x * e2.x;
val yy := e1.y * e1.y + e2.y * e1.y + e2.y * e2.y;
inertia += (0.25 / 3 * d) * (xx + yy);
}
return mass_data{} if area <= tiny;
center = center * (1 / area);
val mass := density * area;
# Of the corner it was summed about, moved to the center.
return mass_data{ mass = mass, center = origin + center, inertia = density * inertia - mass * center.length_squared };
}