node-ctypes - v2.17.1
    Preparing search index...

    Box2D v3 physics

    A cross-platform C11 FFI binding for Box2D v3 (3.x) built with node-ctypes, showing by-value C structs (b2WorldId, b2BodyId, b2ShapeId, b2Transform, b2Vec2, b2Rot, b2RayResult, b2Polygon), default struct constructors (b2DefaultWorldDef, b2DefaultBodyDef, b2DefaultShapeDef), physics step event buffers (b2World_GetContactEvents), raycast & AABB overlap CFUNCTYPE callbacks receiving structs by value, and high-frequency transform reads.

    Platforms: Prebuilt portable binaries included (darwin-arm64, linux-x64, linux-arm64, win32-x64, win32-arm64 — zero system installs required): darwin-arm64/BUILD.md, linux-x64/BUILD.md, linux-arm64/BUILD.md, win32-x64/BUILD.md, win32-arm64/BUILD.md · Libraries: box2d.js, raylib.js · Overrides: set BOX2D_LIB / RAYLIB_LIB or place custom builds under vendor/<platform>-<arch>/

    node examples/cross-platform/box2d/demo_box2d.js
    

    Stream live 60Hz frames to an HTML5 Canvas in your browser via node:http + Server-Sent Events (web/index.html, web/app.js, zero extra npm or C dependencies):

    node examples/cross-platform/box2d/demo_box2d_web.js
    # then open http://127.0.0.1:3456
    • Driven natively by Box2D's b2World_Draw() using 10 CFUNCTYPE callbacks wired into b2DebugDraw (DrawSolidPolygonFcn, DrawSolidCircleFcn, DrawSolidCapsuleFcn, DrawBoundsFcn, DrawPointFcn, DrawLineFcn, DrawTransformFcn, DrawStringFcn, ...).
    • Click to spawn boxes/balls/capsules, trigger b2World_Explode() shockwaves (or right-click anywhere), aim a live b2World_CastRay() beam, switch presets (Pyramid, Dominoes, Mixed Tower), change gravity (b2World_SetGravity), or toggle b2DebugDraw overlays (AABBs, Contacts, Normals, Mass/COM, Islands, Graph Colors).
    • Automated smoke check: node examples/cross-platform/box2d/demo_box2d_web.js --self-test

    Dual-library FFI showcase: Box2D's b2World_Draw() C callbacks call directly into raylib (libraylib) C drawing functions (DrawTriangle, DrawCircleV, DrawLineEx, DrawRectangleLinesEx, DrawText) at 60fps in one Node.js process:

    # Runs out of the box with bundled vendor/<platform>-<arch>/ binaries
    node examples/cross-platform/box2d/demo_box2d_raylib.js
    • Controls: 1..5 tool (Box, Ball, Capsule, Explode, Raycast), Left-click apply tool, Right-click explode, Mouse wheel zoom, Shift+drag or middle-drag pan, Q/W/E presets, G cycle gravity, Space pause, S step, R reset, F1..F6 toggle b2DebugDraw overlays.
    • Automated smoke check (hidden window): node examples/cross-platform/box2d/demo_box2d_raylib.js --self-test

    Zero third-party graphics dependencies — Box2D drives each operating system's built-in windowing and 2D graphics APIs directly via scene.js and platform backends:

    • macOS (native/macos.js): AppKit (NSApplication, NSWindow, runtime NSView subclass via libobjc.A.dylib) + CoreGraphics (CGContext) — verified live on Apple Silicon.
    • Windows (native/windows.js): double-buffered Win32 + GDI+ (user32.dll, gdi32.dll, gdiplus.dll, anti-aliased ARGB rendering).
    • Linux (native/linux.js): double-buffered X11 Pixmap (libX11.so.6).
    node examples/cross-platform/box2d/demo_box2d_native.js
    # Automated smoke check (hidden OS window):
    node examples/cross-platform/box2d/demo_box2d_native.js --self-test
    import {
    b2CastResultCallbackFcn,
    b2Vec2,
    b2_dynamicBody,
    b2_staticBody,
    byref,
    loadBox2D,
    } from "./box2d.js";

    const b2 = loadBox2D(); // or loadBox2D("/path/to/libbox2d.dylib")

    const worldDef = b2.b2DefaultWorldDef();
    worldDef.gravity.x = 0;
    worldDef.gravity.y = -10;
    const worldId = b2.b2CreateWorld(byref(worldDef));

    try {
    // Static ground box at y = -1 (top surface at y = 0)
    const groundDef = b2.b2DefaultBodyDef();
    groundDef.type = b2_staticBody;
    groundDef.position.y = -1;
    const groundId = b2.b2CreateBody(worldId, byref(groundDef));
    const groundShapeDef = b2.b2DefaultShapeDef();
    const groundBox = b2.b2MakeBox(50, 1);
    b2.b2CreatePolygonShape(groundId, byref(groundShapeDef), byref(groundBox));

    // Dynamic 2m x 2m falling crate
    const bodyDef = b2.b2DefaultBodyDef();
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.y = 5;
    const bodyId = b2.b2CreateBody(worldId, byref(bodyDef));
    const shapeDef = b2.b2DefaultShapeDef();
    shapeDef.density = 1.0;
    const box = b2.b2MakeBox(1, 1);
    b2.b2CreatePolygonShape(bodyId, byref(shapeDef), byref(box));

    for (let frame = 0; frame < 120; frame++) {
    b2.b2World_Step(worldId, 1 / 60, 4);
    }

    const tf = b2.b2Body_GetTransform(bodyId);
    console.log(`Settled crate at y=${tf.p.y.toFixed(3)}m`);

    // Raycast callback receiving b2ShapeId, b2Vec2 point, b2Vec2 normal by value
    const hits = [];
    const cb = b2CastResultCallbackFcn((shapeId, point, normal, fraction) => {
    hits.push({ shape: shapeId.index1, y: point.y, ny: normal.y, fraction });
    return 1.0;
    });
    try {
    b2.b2World_CastRay(
    worldId,
    new b2Vec2(0, 10),
    new b2Vec2(0, -15),
    b2.b2DefaultQueryFilter(),
    cb,
    null,
    );
    } finally {
    cb.release();
    }
    } finally {
    b2.b2DestroyWorld(worldId);
    b2.close();
    }

    CDLL / WinDLL + CDLL.func() loading libbox2d alongside libraylib or OS native frameworks (libobjc / AppKit / CoreGraphics, user32 / gdi32, libX11) · Objective-C runtime class creation (objc_allocateClassPair, class_addMethod, typed objc_msgSend prototypes) & WNDPROC callbacks (WINFUNCTYPE) · Structure subclasses passed and returned by value (b2WorldId, b2BodyId, b2ShapeId, b2Transform, b2Polygon, b2RayResult, b2WorldDef, b2BodyDef, b2ShapeDef, b2DebugDraw, raylib Vector2 / Color / Rectangle, CGRect / CGPoint) including HFA (float/double) aggregates on ARM64/x86_64 · Fixed-size nested arrays (array(b2Vec2, 8), array(c_uint8, 46), array(c_uint64, 2)) · CFUNCTYPE callbacks stored as struct fields (b2DebugDraw.Draw*Fcn) and passed to raycast/overlap queries (b2CastResultCallbackFcn, b2OverlapResultCallbackFcn), calling back into native C rendering functions and explicitly released via .release() · POINTER(T) array indexing (verts[i], b2ContactEvents.beginEvents[i], hitEvents[i]) and byref() out/config parameters