nape-js API
    Preparing search index...

    Class Space

    The physics world. Add bodies, shapes, and constraints, then call step() each frame to advance the simulation.

    Index

    Constructors

    Accessors

    • get elapsedTime(): number

      Cumulative time simulated (sum of all deltaTime values passed to step()).

      Returns number

    • get sortContacts(): boolean

      If true, contact points are sorted for determinism. Default: true.

      Returns boolean

    • set sortContacts(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get userData(): Record<string, unknown>

      Arbitrary user data attached to this Space.

      Returns Record<string, unknown>

    • get worldAngularDrag(): number

      Global angular drag coefficient applied to all bodies.

      Returns number

      If set to NaN.

    • set worldAngularDrag(value: number): void

      Parameters

      • value: number

      Returns void

    • get worldLinearDrag(): number

      Global linear drag coefficient applied to all bodies.

      Returns number

      If set to NaN.

    • set worldLinearDrag(value: number): void

      Parameters

      • value: number

      Returns void

    Methods

    • Return all bodies that have at least one shape overlapping the given AABB.

      Parameters

      • aabb: AABB

        The axis-aligned bounding box to test against.

      • containment: boolean = false

        If true, only shapes fully contained within the AABB count.

      • strict: boolean = true

        If true, exact shape geometry is tested; otherwise only AABBs are compared.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: BodyList | null

        Optional existing BodyList to accumulate results into.

      Returns BodyList

      A BodyList of matching bodies.

      If aabb is null or degenerate (zero width or height).

    • Return all bodies in the space that overlap with any shape attached to body. Equivalent to calling bodiesInShape for each of body's shapes and merging results.

      Parameters

      • body: Body

        The body whose shapes are used as the query region.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: BodyList | null

        Optional existing BodyList to accumulate results into.

      Returns BodyList

      A BodyList of overlapping bodies.

      If body is null.

    • Return all bodies that have at least one shape overlapping a query circle.

      Parameters

      • position: Vec2

        World-space centre of the query circle.

      • radius: number

        Radius of the query circle; must be strictly positive and not NaN.

      • containment: boolean = false

        If true, only shapes fully contained within the circle count.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: BodyList | null

        Optional existing BodyList to accumulate results into.

      Returns BodyList

      A BodyList of matching bodies.

      If position is null/disposed, or radius is NaN or non-positive.

    • Return all bodies in the space that have at least one shape overlapping shape.

      Parameters

      • shape: Shape

        The query shape; must be attached to a Body.

      • containment: boolean = false

        If true, only shapes fully contained within shape count.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: BodyList | null

        Optional existing BodyList to accumulate results into.

      Returns BodyList

      A BodyList of overlapping bodies.

      If shape is null, not attached to a Body, or is an invalid polygon.

    • Return all bodies that have at least one shape containing the given world-space point.

      Parameters

      • point: Vec2

        The world-space point to test.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: BodyList | null

        Optional existing BodyList to accumulate results into.

      Returns BodyList

      A BodyList of matching bodies.

      If point is null or disposed.

    • Remove all bodies, constraints, and compounds from this space.

      Returns void

      If called during a step().

    • Sweep shape along its current velocity for deltaTime seconds and return the first hit.

      Parameters

      • shape: Shape

        The shape to sweep; must belong to a Body.

      • deltaTime: number

        Duration of the sweep; must be non-negative.

      • liveSweep: boolean = false

        If true, other body velocities are considered during the sweep.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      Returns ConvexResult | null

      The first RayResult hit, or null if nothing was struck.

      If shape is null, not attached to a Body, or deltaTime is negative/NaN.

    • Sweep shape along its current velocity for deltaTime seconds and return all hits.

      Parameters

      • shape: Shape

        The shape to sweep; must belong to a Body.

      • deltaTime: number

        Duration of the sweep; must be non-negative.

      • liveSweep: boolean = false

        If true, other body velocities are considered during the sweep.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: ConvexResultList | null

        Optional existing RayResultList to accumulate results into.

      Returns ConvexResultList

      A RayResultList of all hits encountered during the sweep.

      If shape is null, not attached to a Body, or deltaTime is negative/NaN.

    • Draw the current state of the physics world using a user-supplied renderer.

      Walks all bodies, shapes, constraints, contacts, AABBs, and velocity vectors and calls the appropriate draw primitives on drawer. Only the layers selected by flags are rendered.

      Performance note: this method allocates temporary vertex arrays for polygon shapes on every call. It is intended for development/debug use only — do not call it in a performance-critical production loop.

      Parameters

      • drawer: DebugDraw

        The renderer to use. Must not be null.

      • flags: number = DebugDrawFlags.ALL

        Bitmask of DebugDrawFlags layers to render. Defaults to DebugDrawFlags.ALL.

      Returns void

      space.debugDraw(myDrawer, DebugDrawFlags.SHAPES | DebugDrawFlags.JOINTS);
      
    • Determine the type of interaction between two shapes (COLLISION, FLUID, SENSOR, or null if they don't interact).

      Parameters

      • shape1: Shape

        The first shape; must belong to a Body.

      • shape2: Shape

        The second shape; must belong to a Body.

      Returns InteractionType | null

      The InteractionType, or null if the shapes would not interact.

      If either shape is null or not attached to a Body.

    • Cast a ray into the space and return the closest hit.

      Parameters

      • ray: Ray

        The ray to cast.

      • inner: boolean = false

        If true, shapes are tested from the inside as well (useful for concave queries).

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      Returns RayResult | null

      The closest RayResult, or null if nothing was hit.

      If ray is null.

    • Cast a ray into the space and return all hits.

      Parameters

      • ray: Ray

        The ray to cast.

      • inner: boolean = false

        If true, shapes are tested from the inside as well.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: RayResultList | null

        Optional existing RayResultList to accumulate results into.

      Returns RayResultList

      A RayResultList of all shapes the ray intersected.

      If ray is null.

    • Return all shapes that overlap with the given AABB.

      Parameters

      • aabb: AABB

        The axis-aligned bounding box to test against.

      • containment: boolean = false

        If true, only shapes fully contained within the AABB are returned.

      • strict: boolean = true

        If true, exact shape geometry is tested; otherwise only AABBs are compared.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: ShapeList | null

        Optional existing ShapeList to accumulate results into.

      Returns ShapeList

      A ShapeList of matching shapes.

      If aabb is null or degenerate (zero width or height).

    • Return all shapes in the space that overlap with any shape attached to body. Equivalent to calling shapesInShape for each of body's shapes and merging results.

      Parameters

      • body: Body

        The body whose shapes are used as the query region.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: ShapeList | null

        Optional existing ShapeList to accumulate results into.

      Returns ShapeList

      A ShapeList of overlapping shapes.

      If body is null.

    • Return all shapes that overlap with a circle defined by position and radius.

      Parameters

      • position: Vec2

        World-space centre of the query circle.

      • radius: number

        Radius of the query circle; must be strictly positive and not NaN.

      • containment: boolean = false

        If true, only shapes fully contained within the circle are returned.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: ShapeList | null

        Optional existing ShapeList to accumulate results into.

      Returns ShapeList

      A ShapeList of matching shapes.

      If position is null/disposed, or radius is NaN or non-positive.

    • Return all shapes in the space that overlap with shape.

      Parameters

      • shape: Shape

        The query shape; must be attached to a Body.

      • containment: boolean = false

        If true, only shapes fully contained within shape are returned.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: ShapeList | null

        Optional existing ShapeList to accumulate results into.

      Returns ShapeList

      A ShapeList of overlapping shapes.

      If shape is null, not attached to a Body, or is an invalid polygon.

    • Return all shapes whose geometry contains the given world-space point.

      Parameters

      • point: Vec2

        The world-space point to test.

      • Optionalfilter: InteractionFilter | null

        Optional interaction filter to restrict results.

      • Optionaloutput: ShapeList | null

        Optional existing ShapeList to accumulate results into.

      Returns ShapeList

      A ShapeList of matching shapes.

      If point is null or disposed.

    • Advance the simulation by deltaTime seconds. velocityIterations and positionIterations control solver accuracy (default 10 each).

      Parameters

      • deltaTime: number

        Time step in seconds; must be strictly positive and not NaN.

      • velocityIterations: number = 10

        Number of velocity solver iterations (minimum 1).

      • positionIterations: number = 10

        Number of position solver iterations (minimum 1).

      Returns void

      If deltaTime is NaN, non-positive, or any iteration count is less than 1.

    • Returns a brief string summary of this space.

      Returns string

      A string in the form Space(bodies=N).

    • Call lambda for every body in the space, including those inside compounds.

      Parameters

      • lambda: (body: Body) => void

        Callback invoked with each Body.

      Returns void

      If lambda is null.

    • Call lambda for every compound in the space (recursively).

      Parameters

      • lambda: (compound: Compound) => void

        Callback invoked with each Compound.

      Returns void

      If lambda is null.

    • Call lambda for every constraint in the space, including those inside compounds.

      Parameters

      • lambda: (constraint: Constraint) => void

        Callback invoked with each Constraint.

      Returns void

      If lambda is null.