feat(physics): 16 - a rock's own shape: phys_convex (Jolt's convex hull of a point cloud) and phys_scaled (one hull, every rock of that shape at its own size); the libraries rebuilt on the Mac and the PC (still KERNEL32 alone); hull_shape_test holds the scale and that a yaw turns a body the way a renderer turns an instance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-27 15:22:30 +03:00
parent bb2a4c10af
commit dd86dce171
9 changed files with 80 additions and 7 deletions

View file

@ -29,7 +29,7 @@ import "ludic.physics"
| | |
| --- | --- |
| `phys_open(st, max_bodies, threads) -> bool`, `phys_close(st)`, `phys_is_open(st)` | a world, and everything in it let go |
| `phys_box`, `phys_sphere`, `phys_capsule`, `phys_cylinder`, `phys_dome(r, h)`, `phys_offset(shape, x, y, z, yaw)` | shapes, each an int (`-1` refused); a dome is a boulder standing on its origin |
| `phys_box`, `phys_sphere`, `phys_capsule`, `phys_cylinder`, `phys_dome(r, h)`, `phys_convex(points, n)`, `phys_scaled(shape, k)`, `phys_offset(shape, x, y, z, yaw)` | shapes, each an int (`-1` refused); a dome is a boulder standing on its origin; a convex hull of n points (x, y, z) is a rock's own shape, made once and scaled for every rock of that shape; a body's yaw turns it as a renderer turns an instance (x' = cos x + sin z, z' = -sin x + cos z) |
| `phys_heightfield(st, h, n, ox, oz, cell)`, `phys_mesh(st, v, nv, tri, nt)` | the ground (`PHYS_HOLE` is none) and a dock or cabin |
| `phys_ground_add`, `phys_static_add`, `phys_kinematic_add`, `phys_body_add(..., mass)` | bodies: the ground, still things, what the game moves (the player), what falls and floats |
| `phys_settle(st)` | after adding many still things, before the first query |

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -14,6 +14,8 @@ extern function jph_shape_capsule(half_h: float, r: float) -> pointer = "jph_sha
extern function jph_shape_cylinder(half_h: float, r: float) -> pointer = "jph_shape_cylinder"
extern function jph_shape_dome(r: float, h: float) -> pointer = "jph_shape_dome"
extern function jph_shape_offset(s: pointer, x: float, y: float, z: float, yaw: float) -> pointer = "jph_shape_offset"
extern function jph_shape_convex(v: pointer, n: int, radius: float) -> pointer = "jph_shape_convex"
extern function jph_shape_scaled(s: pointer, k: float) -> pointer = "jph_shape_scaled"
extern function jph_shape_heightfield(h: pointer, n: int, ox: float, oz: float, cell: float) -> pointer = "jph_shape_heightfield"
extern function jph_shape_mesh(v: pointer, nv: int, tri: pointer, nt: int) -> pointer = "jph_shape_mesh"
extern function jph_shape_free(s: pointer) -> void = "jph_shape_free"

View file

@ -17,6 +17,19 @@ JPH_SHIM void *jph_shape_dome(float r, float h) {
return keep(RotatedTranslatedShapeSettings(Vec3(0.0f, h - big, 0.0f), Quat::sIdentity(), s).Create());
}
// a rock: the convex hull of n points (x, y, z) about its origin, its edges rounded by radius
JPH_SHIM void *jph_shape_convex(const float *v, int n, float radius) {
Array<Vec3> pts;
pts.reserve(n);
for (int i = 0; i < n; ++i) pts.push_back(Vec3(v[3 * i], v[3 * i + 1], v[3 * i + 2]));
return keep(ConvexHullShapeSettings(pts, radius).Create());
}
// another shape, k times its size on every axis (one hull, every boulder of that shape)
JPH_SHIM void *jph_shape_scaled(void *s, float k) {
return keep(ScaledShapeSettings(static_cast<Shape *>(s), Vec3::sReplicate(k)).Create());
}
// a shape moved and turned about y, inside its body
JPH_SHIM void *jph_shape_offset(void *s, float x, float y, float z, float yaw) {
return keep(RotatedTranslatedShapeSettings(Vec3(x, y, z), yaw_q(yaw), static_cast<Shape *>(s)).Create());

View file

@ -16,6 +16,8 @@
#include <Jolt/Physics/Collision/Shape/HeightFieldShape.h>
#include <Jolt/Physics/Collision/Shape/MeshShape.h>
#include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
#include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
#include <Jolt/Physics/Collision/Shape/ScaledShape.h>
#include <Jolt/Physics/Collision/RayCast.h>
#include <Jolt/Physics/Collision/CastResult.h>
#include <Jolt/Physics/Collision/ShapeCast.h>

View file

@ -59,6 +59,18 @@ export function phys_cylinder(physics_st: mut PhysicsState, half_h: float, r: fl
# a boulder: a dome r across and h tall, standing on its origin
export function phys_dome(physics_st: mut PhysicsState, r: float, h: float) -> int { return ph_keep(physics_st, jph_shape_dome(r, h)) }
# a rock: the convex hull of n points (x, y, z) about its origin, its edges rounded by 2 cm
export function phys_convex(physics_st: mut PhysicsState, v: []float, n: int) -> int {
if n < 4 or len(v) < n * 3 { return -1 }
return ph_keep(physics_st, jph_shape_convex(v, n, 0.02))
}
# another shape k times its size, so one hull serves every boulder of its shape
export function phys_scaled(physics_st: mut PhysicsState, shape: int, k: float) -> int {
let s = ph_shape(physics_st, shape)
if s == null { return -1 }
return ph_keep(physics_st, jph_shape_scaled(s, k))
}
# another shape moved and turned about y inside its body (a post whose foot is its origin)
export function phys_offset(physics_st: mut PhysicsState, shape: int, x: float, y: float, z: float, yaw: float) -> int {
let s = ph_shape(physics_st, shape)

View file

@ -0,0 +1,44 @@
# hull_shape_test.ludic - a rock's shape: a convex hull of points, scaled for one boulder and
# turned by its yaw the way a renderer turns an instance (x' = cos x + sin z, z' = -sin x + cos z)
import "ludic.physics"
import "ludic.base"
program HullShapeTest {
numbers float
# a cube two metres across, its centre two metres along x from its origin
function cube(physics_st: mut PhysicsState) -> int {
let v = floats(8 * 3)
for i in 0 .. 8 {
v[i * 3] = 2.0 + float(i % 2) * 2.0 - 1.0
v[i * 3 + 1] = float((i / 2) % 2) * 2.0 - 1.0
v[i * 3 + 2] = float(i / 4) * 2.0 - 1.0
}
return phys_convex(physics_st, v, 8)
}
function down(physics_st: mut PhysicsState, x: float, z: float) -> PhysHit { return phys_ray(physics_st, x, 10.0, z, 0.0, -20.0, 0.0, PHYS_M_ALL) }
test "a hull stands where its points are, and too few points are no shape" (physics_st: mut PhysicsState) {
expect(phys_open(physics_st, 16, 1))
phys_static_add(physics_st, cube(physics_st), 0.0, 0.0, 0.0, 0.0)
phys_settle(physics_st)
let h = down(physics_st, 2.0, 0.0)
expect(h.body >= 0)
expect(Math.abs(h.y - 1.0) < 0.01)
expect(down(physics_st, 0.0, 0.0).body < 0)
expect_eq(phys_convex(physics_st, floats(9), 3), -1)
phys_close(physics_st)
}
test "scaled twice as big, and turned a quarter the renderer's way" (physics_st: mut PhysicsState) {
expect(phys_open(physics_st, 16, 1))
phys_static_add(physics_st, phys_scaled(physics_st, cube(physics_st), 2.0), 0.0, 0.0, 0.0, 1.5707963)
phys_settle(physics_st)
# (4, 0, 0) turned by +90 degrees about y is (0, 0, -4): x' = cos x + sin z, z' = -sin x + cos z
let h = down(physics_st, 0.0, -4.0)
expect(h.body >= 0)
expect(Math.abs(h.y - 2.0) < 0.01)
expect(down(physics_st, 4.0, 0.0).body < 0)
expect(down(physics_st, 0.0, 4.0).body < 0)
phys_close(physics_st)
}
}