Physics
These sketches get their motion from simulation. Physics lives in a separate library, so add import OllinPhysics. The library is built around one World with two sides. The soft side is a Verlet simulation of particles and the springs between them. The rigid side holds bodies, colliders, and joints. Both sides share the world's gravity, container, bounce, and drag. Build the world in setup(), step it each frame, and draw from its particles and bodies. See the Physics reference. These examples use the 2D world. For rigid bodies inside the 3D scene, see the 3D physics examples. The 3D group keeps the same scenarios on the 3D engine under their own names: 3D/Physics/Stack, Tumble, and Chain. Here the first two are folded into the one RigidBodies sketch below.
| Example | What it shows |
|---|---|
| Packing | 256 discs of mixed sizes drift with no gravity and bounce off the walls and each other. Each disc is drawn as a slowly spinning crosshair token, and the spin reacts to the disc's own speed. This is the main example for World.particlesCollide, a pairwise disk-collision pass. Gravity is off and drag = 0, so the motion never slows down (World, Particle, collisions). The scene composition follows @eaviles's sketch 2025.037. The physics is Ollin's own. |
| Blobs | Nine lumpy blobs in a box, each one addSoftBody(from:) call on a circle bent by looping noise. Gravity swings slowly from side to side, so the pile sloshes across the floor: the blobs squash against the walls and each other, keep their area, and spring back toward their own shapes. Move the mouse through them to shove them. Each is drawn as a smooth filled drawCurve through its own particles. This is the soft-body example (addSoftBody, SoftBody, shapeMemory). |
| RigidBodies | A pyramid of rigid crates stacks, leans, and topples. Beside it, a rain of assorted shapes (boxes, disks, capsules, and random convex polygons) piles up against the floor. A click fires a heavy ball that knocks the stack into the heap, and space clears and rebuilds the scene. This is the rigid-body example. It shows orientation and resting contact, which the particle side cannot do. Every shape is one addBody call with a different Collider, and each is drawn straight from its position and angle (addBody, Collider, Body). |
| Chain | Hanging chains of capsule links, hinged by free-swinging revolute joints and ending in heavy balls. Click a link to grab and fling it through a cursor-drag mouse joint, click again to let go, and press space to reset. This is the joints example (connect, JointKind.revolute, grab). |
| Joints | All four joint kinds side by side, each as one labeled rig hung from a static anchor: a revolute pendulum, a distance ball on a springy rope, a weld tee that swings as one rigid piece, and a prismatic slider that can only travel along its rail. Click a piece to grab and fling it, and press space to reset (JointKind, Body.kind = .static). |
| Forces | The force API in one windy yard. A gust pushes on every loose shape through applyForce, so the leaves scatter while the crates only shuffle. A click kicks the nearest shape with applyImpulse. A paddle spun by applyTorque hits whatever drifts through it, and a kinematic sweeper pushes along the floor, which nothing can stop. A pinned lattice colors its springs by Spring.strain, and World.unitsPerMeter is bound to a parameter (applyForce, applyImpulse, applyTorque, Spring.pin()). |
| Burst | Fruit thrown up through the frame, each one breaking at the top of its arc into pieces that tumble back down. Nobody cuts them: a shape lets go on its own, and what falls is exactly what went up. A click breaks the nearest one early, and space clears the frame. Shows Shape.fractured around the point where it gave way, each piece's centroid becoming a body of its own (fractured, centroid, addBody, World.remove). |
Run one with swift run Example-Physics-<Name>, for example swift run Example-Physics-Packing.






