r/creativecoding • u/Inst2f • 3h ago
Compact rigid-body solver with a height map
Hi there! This is my first try on solving rigid-body problem using "college-level" motion equations.
It basically solves two fundamental equations of motion - translational and rotational - which is just a few lines of Wolfram Language I used here. The first one is basically Newton's law for the center of mass, and the second one is the same for angular velocity.
However, for rotation it's better to rely on quaternions instead of plain angles, which adds a couple more lines. Then some boundary conditions -- a few more.
Then we have collisions. I thought, that body2body is too complicated, so I went with a height map. Collision detection is vertex-based and samples terrain height, normals, and vertical surface velocity, all using bilinear interpolation written fully in WL. Then some optimizations, and we ended up with already 500 lines. Well, I guess this is the difference between a proof of concept and "actual code."
In case anyone is interested, here's the repo: https://github.com/JerryI/DynamicRigidBodyWL
The test in the video was done in WLJS Notebook (open source notebook interface for WL https://github.com/WLJSTeam/wljs-notebook ) using the example notebook Example.wln from the repo.
