![]() |
vkmEngine 1.0.15
A C++ game engine · vkmengine.com
|
Fixed-step rigid-body dynamics over box, capsule and triangle-mesh colliders, solved with soft-constraint sequential impulses and written back to Transform. On top of it, a character controller that turns movement input into velocity.
PhysicsSystem runs in SystemStage::Simulation, after AnimationSystem and before HierarchySystem, all of it in fixedUpdate(). CharacterControllerSystem runs immediately after it, so it reads that tick's fresh contacts.
Plain data; the headers in src/engine/ecs/component/physics/ give every field and default, and ecs.md lists them all.
Rigidbody - mass, velocities, material, collision masks (layer / collidesWith) and a motion: Dynamic, Kinematic or Static. Static and kinematic bodies have infinite mass and are never integrated, so a kinematic body's pose is the project's to write - but its velocities still reach a contact, which is how a kinematic platform pushes what stands on it - so a kinematic body that stops being driven should zero its velocities. freezeRotation stops contacts torquing a dynamic body (the character case). Mass properties are re-derived from mass and Collider every tick, so edits need no "apply".
It also carries per-tick outputs, runtime only: supported, supportNormal (the most upward contact normal), supportBody, and blockNormal (the most horizontal one). Two normals because a character pressed against a wall is held by the floor and blocked by the wall. A body that touched nothing reports world up for both.
Each tick, for each Dynamic controller this end simulates, the controller decides grounded (a support no steeper than maxSlopeAngle - touching a wall is not standing), steers the body's velocity toward moveInput at no more than acceleration, and jumps when asked while grounded. It writes velocity, never position: the solver owns the pose, so a character cannot be pushed through a wall and friction, sleeping and penetration recovery keep working underneath. The steps, and the knockback and air-control rules, are in character_controller_system.cpp.
It logs two silent misconfigurations once each: no enabled capsule collider (it will not climb steps) and freezeRotation off (contacts will topple it). It is a System driving a component rather than a Behavior, so whatever writes moveInput can change without the controller changing.
Climbing. Gameplay sets climbing and climbNormal every tick it holds a ladder or ledge; pushing into the face climbs, a jump leaps off, and the flag clears after each tick so a character whose hold is gone falls. There is no ladder component: what counts as reaching one is the game's.
The controller removes the part of its target that pushes into blockNormal, so a character walking into a wall slides instead of snagging. Left to friction, slide or snag would be decided by material numbers nobody picked for that. Only a surface steeper than maxSlopeAngle deflects (a walkable one is ground), and only horizontally (deflecting upward would climb any wall).
The capsule's round bottom rolls over a step up to radius * (1 - cos(maxSlopeAngle)). A taller edge is a wall; if it is under stepHeight, findStep probes with two spherecasts (clearance, then a walkable landing) and the controller mounts it. The probes use the body's own collision mask, so a character's ragdoll bones never block its own stairs.
Holding crouching shortens the first capsule part to crouchHeight, feet kept in place (fitCapsule). Released, the character stands only when roomToStand finds space overhead; otherwise it stays crouched, which is what a game reads to pose and slow it. Collider is not replicated, so in networked play a replay can mispredict a crouch changed within a round trip; a lag-compensated hit meets the current crouch (networking.md).
Grounded on a body that is not static, the character is carried at groundVelocity - the velocity of that surface at its feet, spin included - and steers relative to it: walking on a moving deck is walking, a released stick stands still on it, a jump keeps the carry. A kinematic platform carries by the velocities it is driven with; one moved by its Transform alone carries nothing. groundSpin reports the deck's turn about up; turning the view with it is gameplay's call.
A body at rest for SLEEP_DELAY sleeps, together with its whole island (every dynamic body it touches or is jointed to) - and only while something outside the island holds it. Anything that could push it - an awake dynamic body, a driven kinematic one - wakes the whole island; a static one never does. Sleeping bodies still generate contacts, so stacks stay supported. Code that wakes a body calls Rigidbody::wake.
"At rest" is one bound on the fastest point of the body (SLEEP_SPEED), not a linear and an angular threshold apart: a tall box tipping slowly about an edge would pass both.
A pair reports Began on its first touching tick, Stayed on each later one, Ended on the first tick it does not. A collision's a is the body with the lower entity slot, so a pair is named - and its normal points - the same way for its whole life. speed on Began is how hard it struck.
Both are enqueued, so listeners run on the next EventBus flush, never mid-solve. Behaviors can use onCollisionEnter / Stay / Exit and onTriggerEnter / Stay / Exit instead (scripting.md).
raycast and spherecast (query/query.h) return the nearest body a ray or a swept sphere strikes, as a RayHit, filtered by a QueryFilter. They read the scene directly, so they answer where bodies are now, walking every body per call. A Collider without a Rigidbody is in no query.
Many rays in one moment go through QueryParts: gather places the parts a filter sees around where the rays will run, once, and its raycast answers each ray as raycast would, testing a part's world bound before its shape. It answers as of gather: no Collider may change before the last cast.
buildRagdoll / clearRagdoll make and remove capsule bodies per bone, on their own collision layer, each joint holding its angle (RagdollSettings::muscle). While a clip drives the rig, RagdollSystem poses the bones as kinematic hitboxes that make no contacts - the character's capsule meets the world - but queries find them. Gone live, the bodies drive the pose (animation.md). Either way, a query ignoring the ragdoll's owner (QueryFilter::ignore) skips its bones, so a character casting from its own eye sees past them.
PhysicsSystem::fixedUpdate works on a per-tick copy of the world, never the components directly:
Soft-constraint projected Gauss-Seidel in the shape of Box2D v3's soft step, taking the whole tick as one step: each pass solves every joint and then every contact, so a surface has the last word over a joint. Penetration is recovered by a stiff damped spring in the contact impulse; poses are integrated with that velocity and then relax passes take it back out, so recovery adds no energy. There is no position solver.
The spring stiffnesses and thresholds are file-local constants in solver.cpp and joint_solver.cpp, not settings: every end of a session must agree about the solve, and a contact stiffness set wrong looks like a physics bug. SolverParams carries only iterations and dt.
A stack needs three things, and loses any one by settling into a lean:
The same world run twice ends bit-identical; replay and the network rely on it. Every order the solve depends on is keyed on entity slot, not storage order, and vkm_core builds with -ffp-contract=off so fused multiply-add does not change rounding. testTheSameWorldRunTwiceEndsBitIdentical and testABusyPileStepsTheSameOnAnyThread hold it.
Pairs run in parallel chunks (NARROWPHASE_CHUNK_PAIRS), each writing only its own NarrowphaseChunk, and are merged in pair order - so the result is the serial one to the bit on any core count.
Every routine returns normal A -> B, positive penetration, at most MAX_CONTACTS_PER_MANIFOLD points, so everything downstream is shape-blind: contactBoxes (SAT, preferring a face over an edge unless the edge clearly wins, so resting boxes keep a four-point manifold), contactCapsuleBox (two points for a capsule lying flat, or it rolls forever), contactCapsuleCapsule. A mesh part is culled to nearby triangles and contactTriangle meets each with the other part's shape, using gjkOverlap. Two meshes never collide - a mesh is level geometry, and a dynamic body wearing one falls through a mesh floor.
A solid box of the collider's extent (inertia.h), except a single capsule, which gets capsuleInertiaLocal - an upright capsule spins about its axis far more freely than its box, and a character grazing a wall shows it.
Under src/engine/system/physics/:
Components: src/engine/ecs/component/physics/ - rigidbody.h, collider.h, character_controller.h, joint.h, ragdoll.h.