Repository navigation
Description
Context
A game that wants a crate that falls and tumbles, and that bullets still hit through app.collisions, describes the same box twice today: once as a Collider for Overlap events and once as a physics Shape for the body.
import klyph.collision.Shape as ColliderShape // both packages define a Shape
import klyph.physics.Shape as BodyShape
val collider = Collider(ColliderShape.Box(16f, 16f), layers = CRATE, mask = BULLET)
val body = app.physics.world.rigidBody(BodyShape.box(16f, 16f), x, y)
app.physics.world.rigidBodies.setLayers(body, layers = CRATE, mask = CollisionLayers.ALL)The size and the layers are written in two places and drift apart when one of them changes. The offset is offsetX/offsetY on the collider and x/y on the shape, and the collider scales with the Transform while the body does not. A file that does both has to alias the two Shape imports.
#2 asked for this ("a game can turn a collider into a body without changing its queries"). #55 shipped the physics world without it.
Proposal
An overload next to PhysicsWorld.rigidBody(shape, x, y, …) that takes a Collider and the entity's Transform, with the same density, material and kind defaults. The position, rotation and scale come from the transform:
val collider = Collider(Shape.Box(16f, 16f), layers = CRATE, mask = BULLET)
val transform = Transform(x, y)
val body = app.physics.world.rigidBody(collider, transform)Shape.Box becomes Shape.box, Shape.Circle becomes Shape.circle, the offset becomes the shape's x/y, and the collider's layers become the body's. The game's Overlap handlers stay as they are.
The body's mask is a parameter of its own, defaulting to CollisionLayers.ALL as a new body's does, and is not copied from the collider. The two masks mean different things:
app.collisionsreports a pair when either side's mask holds a layer of the other, and the mask says which overlaps to report.PhysicsLayers.collideneeds both sides to match.
Copying the crate's mask = BULLET would make crates fall through each other. A narrow mask such as mask = CRATE is a trap too: the crate would then fall through a static ground body on the default layer. The level is not part of the mask: since #70 a body stays in it through SolidCollision.COLLIDE, the default, so the overload needs no solids parameter.
The collider still does not turn with the body, since Collider ignores rotation, so hits on a tumbling crate are tested against an upright box unless the game queries the physics world (queryBox, raycast) instead. The KDoc should say so.
Open questions
Settle these before coding:
- Scale.
Colliderscales with the entity'sTransformby default (scalesWithTransform). Applying that scale needs the entity or a scale parameter, not only theCollider. Taking the collider at scale 1 keeps the signature small but gives a different size from whatapp.collisionsuses on a scaled entity. - Scope. A
Commands.spawnRigidBody(x, y, collider)that also spawns the entity with itsTransform,ColliderandPhysicsBodywould save four more lines, but it is a second API to design. Start with the overload and decide this after.
The reserved-layer question is settled: #70 (closing #67) took the level out of the layer bits, and setLayers no longer reserves any layer, so a collider on any layer works and the overload throws only what rigidBody already throws.
Done when
The open questions are answered here. The overload has KDoc with @throws, a sample in samples/, and tests for a box, a circle, an offset collider and the layers, and docs/physics/klyph.md shows it.
Activity
inemtsev commentedon Nov 22, 2026
Depends on #67, which takes the level out of the physics layer bits.
Once #67 lands:
mask = CRATE.PhysicsLayers.SOLIDSgoes away, and a body stays in the level throughSolidCollision.COLLIDE, the default.Build this after #67 so the overload's KDoc and samples don't mention layer 30.
inemtsev commentedon Nov 29, 2026
Decisions, settled 2026-11-29:
PhysicsWorld.rigidBody(collider, transform, density, material, kind, mask = CollisionLayers.ALL), next to the otherrigidBodyoverloads.TransformwhenscalesWithTransformis set, using the ruleCollisionsuses. A box scales byabs(scaleX)andabs(scaleY), a circle byabs(scaleX)alone, and the offset by the signed scale. Later scale changes leave the body as it was. A test checks that the body's bounds match the boxapp.collisionsreports, so the two rules can't drift apart.transform.rotation. Only theTransformof a root entity works, as withPhysicsBody.PhysicsBodybridge used to write a point displaced by the offset, and theColliderthen added the offset again. The bridge now writes the shapes' origin, turned by the body's angle.xOf/yOfstay the centre of mass, and the origin is internal. A single centred shape behaves as before.layersare copied, and themaskis the body's own, as proposed.queryBox/raycast.Commands.spawnRigidBodyfor now.Build a rigid body from a klyph Collider
inemtsev commentedon Nov 29, 2026
Released in v1.4.0.