Skip to content

A rigid body built from a klyph Collider #66

Done

Description

@inemtsev

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.collisions reports a pair when either side's mask holds a layer of the other, and the mask says which overlaps to report.
  • PhysicsLayers.collide needs 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:

  1. Scale. Collider scales with the entity's Transform by default (scalesWithTransform). Applying that scale needs the entity or a scale parameter, not only the Collider. Taking the collider at scale 1 keeps the signature small but gives a different size from what app.collisions uses on a scaled entity.
  2. Scope. A Commands.spawnRigidBody(x, y, collider) that also spawns the entity with its Transform, Collider and PhysicsBody would 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.

1.4 candidate. Unblocked: #67 landed in #70.

Activity

  1. added this to the 1.4 Physics Engine milestone on Nov 21, 2026
  2. added
    enhancement
    on Nov 21, 2026
  3. inemtsev commented on Nov 22, 2026

    @inemtsev
    OwnerAuthor

    Depends on #67, which takes the level out of the physics layer bits.

    Once #67 lands:

    • The example's mask becomes mask = CRATE. PhysicsLayers.SOLIDS goes away, and a body stays in the level through SolidCollision.COLLIDE, the default.
    • Open question 1 (the reserved layers) mostly goes away: a collider on layer 30 no longer makes the overload throw. Layer 31 is freed too, unless Take the level out of the physics collision layers #67 finds a reason to keep it reserved (its open question 1).

    Build this after #67 so the overload's KDoc and samples don't mention layer 30.

  4. inemtsev commented on Nov 29, 2026

    @inemtsev
    OwnerAuthor

    Decisions, settled 2026-11-29:

    • Signature: PhysicsWorld.rigidBody(collider, transform, density, material, kind, mask = CollisionLayers.ALL), next to the other rigidBody overloads.
    • Scale (open question 1): read once from the Transform when scalesWithTransform is set, using the rule Collisions uses. A box scales by abs(scaleX) and abs(scaleY), a circle by abs(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 box app.collisions reports, so the two rules can't drift apart.
    • Placement: the shapes' origin goes at the transform's position, turned by transform.rotation. Only the Transform of a root entity works, as with PhysicsBody.
    • Offset: a body's position is its centre of mass, so with an offset the PhysicsBody bridge used to write a point displaced by the offset, and the Collider then added the offset again. The bridge now writes the shapes' origin, turned by the body's angle. xOf/yOf stay the centre of mass, and the origin is internal. A single centred shape behaves as before.
    • Layers: the collider's layers are copied, and the mask is the body's own, as proposed.
    • Rotation: the collider stays axis-aligned. The KDoc says so and points to queryBox/raycast.
    • Scope (open question 2): no Commands.spawnRigidBody for now.
  5. added a commit that references this issue on Nov 29, 2026
    c3f9190
  6. inemtsev commented on Nov 29, 2026

    @inemtsev
    OwnerAuthor

    Released in v1.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions