You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Kit (src/box2dxt-kit.livecodescript) is a batteries-included,
pure-xTalk toolkit over the box2dxt extension. You work in pixels, screen
coordinates and degrees; the Kit hides the world, the fixed-timestep loop,
coordinate conversion, and the per-frame control updates — and it runs with the
demo's performance practices built in.
Setup: paste the Kit into your card/stack script (or save it as a library
stack and start using it). It requires the box2dxt extension loaded
(put b2Version() should return 3).
on openCard
b2kQuickStart -- world + gravity + card-edge walls + go
get b2kSpawnBall(200, 80, 50) -- create & drop a 50px ball
get b2kSpawnBox(260, 80, 60, 40, "orange")
b2kContactTarget the long id of me -- (optional) collision messages
end openCard
on mouseDown ; get b2kGrab(the mouseH, the mouseV) ; end mouseDown
on mouseUp ; b2kRelease ; end mouseUp
on closeCard ; b2kStop ; end closeCard
Or attach controls you designed in the IDE (graphics rotate; other control
types follow position):
b2kSetup -- world + gravity, auto origin
b2kAddStatic the long id of graphic "Floor"
b2kAddBox the long id of graphic "Crate"
b2kAddBall the long id of graphic "Ball"
b2kContactTarget the long id of me -- on b2kContact pA, pB
b2kStart
World & loop
Handler
Purpose
b2kSetup [gx, gy]
Create the world + gravity, auto-detect the origin.
b2kQuickStart [gy]
One call: world + gravity + card-edge walls + start the loop.
b2kStart / b2kStop
Begin / end the simulation loop.
b2kPause / b2kResume
Freeze / resume stepping without tearing down.
b2kStepOnce
Advance exactly one fixed step (even while paused) — drives a Step button.
b2kIsRunning()
True while the loop is stepping (not stopped or paused).
b2kAddWalls
Static walls around the current card edges.
b2kAddGround [screenY]
A static floor across the card (optionally at a given Y).
b2kWall x1, y1, x2, y2
A static collision segment between two screen points (custom walls, ramps, ledges). Invisible — draw your own graphic to match.
b2kClear
Remove all bodies/controls the Kit created, keep the world.
b2kTeardown
Stop and destroy the world and all Kit state.
b2kVersion()
Native shim ABI version (3) — a load / in-sync check from Kit-only code.
Configuration
Handler
Purpose
b2kSetScale px
Pixels per metre (default 40).
b2kSetOrigin x, y
Screen point that maps to the world origin.
b2kSetGravity gx, gy
Change gravity (pixels-down is positive).
b2kSetSubsteps n
Solver sub-steps per step (≈ 4).
b2kContactTarget obj
Object that receives on b2kContact / on b2kEndContact messages.
b2kFrameTarget obj
Object that receives an on b2kFrame message once per simulated frame.
b2kEnableSleeping flag
Toggle island sleeping (saves CPU when bodies rest).
b2kEnableContinuous flag
Toggle continuous collision (CCD) for the world.
Attach & spawn
Attach physics to existing controls, or spawn new ones. Pass controls by
reference (the long id of … is safest). Add ,true/,false to force
dynamic/static where a handler takes an optional dyn flag.
Any control type works — it falls, collides, and is draggable. How it's
drawn to follow its body depends on the type:
Control
Follows position
Rotates
Graphic (box → polygon, or ball)
✅
✅
Image (dynamic)
✅
✅ via the angle
Button / field / other
✅
❌ rotation locked so the sim matches the upright render
Handler
Purpose
b2kAddBox ctrl [,dyn]
Treat a control as a dynamic (default) box.
b2kAddBall ctrl [,dyn]
Treat a control as a circle.
b2kAddCapsule ctrl [,dyn]
Treat a control as a capsule (pill); long side = axis, short side = diameter.
b2kAddPolygon ctrl [,dyn]
Treat a graphic's points as a convex polygon.
b2kAddStatic ctrl
Immovable body matching the control.
b2kAddGround [screenY]
Static floor (see above).
b2kSpawnBox x, y, w, h [,color] → control
Create a control and its box body.
b2kSpawnBall x, y, diam [,color] → control
Create a control and its ball body.
b2kSpawnCapsule x, y, len, thick [,color] → control
Create a pill-shaped control and its capsule body.
Grab the body under a point (mouse joint). Returns the grabbed control, or empty.
b2kRelease
Release a grabbed body.
on b2kContact pCtrlA, pCtrlB
Sent to your b2kContactTarget when two attached controls begin touching. Long ids are empty for walls/ground.
on b2kEndContact pCtrlA, pCtrlB
Sent when two attached controls stop touching.
on b2kFrame
Sent to your b2kFrameTarget once per simulated frame (after bodies sync) — use it for motors, input, and custom drawing.
Polling contacts — instead of (or alongside) the messages above, read this
frame's touch pairs directly, e.g. from on b2kFrame. Indices are 1-based; each
accessor returns a control (empty for a wall, the ground, or any untracked body).
Handler
Returns
b2kContactCount()
How many pairs began touching this frame.
b2kContactA(i) / b2kContactB(i)
The two controls of the i-th begin-touch pair.
b2kEndContactCount()
How many pairs stopped touching this frame.
b2kEndContactA(i) / b2kEndContactB(i)
The two controls of the i-th end-touch pair.
Sensors (trigger zones)
Non-solid fixtures that report overlaps but never block. The Kit enables sensor
events on every body it creates, so sensors detect them automatically.
Handler
Purpose
b2kAddSensor ctrl [,shape] → body
Attach a static sensor (shape = box/ball/capsule).
on b2kSensorEnter pSensorCtrl, pVisitorCtrl
Sent to your b2kContactTarget when a body enters a sensor.
"totalStep,collide,solve" ms for the last step (a perf HUD).
b2kAwakeBodyCount()
Awake dynamic bodies (native count).
Tips
Keep moving objects a sensible on-screen size (default scale 40 px/m, so
roughly 4–400 px).
Graphic boxes/polygons and dynamic images rotate with their body (images
via the angle). Buttons, fields, and other controls follow position only and
have their rotation locked, so the simulation stays consistent with the
upright render.
When you need something the Kit doesn't expose, drop to the
core b2… API: b2kWorld() returns the world and
b2kBodyOf(control) the body, and b2kToWorldX/Y(px) / b2kToScreenX/Y(m)
convert between screen pixels and Box2D metres — so you can mix both layers.