nape-js API
    Preparing search index...

    Class CharacterController

    Velocity-based character controller for 2D platformers.

    The character uses a dynamic body whose velocity is set each frame. Collision response (including one-way platforms) is handled entirely by the physics engine via space.step() and PreListener callbacks — matching the original nape Haxe engine pattern.

    The controller provides:

    • Velocity application (setVelocity)
    • Ground/slope detection (raycast queries)
    • Wall detection (raycast queries)
    • One-way platform support (auto-configured PreListener)
    • Moving platform tracking
    • Coyote time helper (timeSinceGrounded)
    const body = new Body(BodyType.DYNAMIC, new Vec2(100, 100));
    body.shapes.add(new Circle(14));
    body.allowRotation = false;
    body.isBullet = true;
    body.space = space;

    const cc = new CharacterController(space, body, {
    maxSlopeAngle: Math.PI / 4,
    oneWayPlatformTag: platformCbType,
    characterTag: playerCbType,
    });

    // Each frame (before space.step):
    cc.setVelocity(moveX, velY);
    space.step(1/60);
    const result = cc.update();
    if (result.grounded) velY = 0;
    Index

    Constructors

    Properties

    body: Body

    The character body being controlled.

    space: Space

    The physics space.

    Accessors

    Methods

    • Set the character body's velocity. Call this each frame before space.step().

      Parameters

      • vx: number

        Horizontal velocity (px/s). Set to 0 for no horizontal input.

      • vy: number

        Vertical velocity (px/s). Typically includes gravity accumulation.

      Returns void