nape-js API
    Preparing search index...

    Class Body

    A rigid body in the physics simulation. Add shapes to give it geometry, then add it to a Space to participate in simulation.

    Hierarchy (View Summary)

    Index

    Constructors

    • Parameters

      • Optionaltype: BodyType

        Body type (DYNAMIC by default).

      • Optionalposition: Vec2

        Initial world-space position (defaults to origin).

      Returns Body

    Properties

    debugDraw: boolean = true

    If true, this body is included in debug rendering.

    Accessors

    • get allowMovement(): boolean

      If false, translational motion is frozen (infinite effective mass).

      Returns boolean

    • set allowMovement(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get allowRotation(): boolean

      If false, rotational motion is frozen (infinite effective inertia).

      Returns boolean

    • set allowRotation(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get angularVel(): number

      Angular velocity in radians per second.

      Returns number

    • set angularVel(value: number): void

      Parameters

      • value: number

      Returns void

    • get castBody(): any

      Cast to Body — returns the Body wrapper if this is a Body, else null.

      Returns any

    • get castCompound(): any

      Cast to Compound — returns the Compound wrapper if this is a Compound, else null.

      Returns any

    • get castShape(): any

      Cast to Shape — returns the Shape wrapper if this is a Shape, else null.

      Returns any

    • get cbTypes(): any

      Callback types assigned to this interactor.

      Returns any

    • get constraintInertia(): number

      Effective inertia used by the constraint solver (accounts for allowRotation).

      Returns number

    • get constraintMass(): number

      Effective mass used by the constraint solver (accounts for allowMovement).

      Returns number

    • get disableCCD(): boolean

      If true, CCD is disabled even if isBullet is set.

      Returns boolean

    • set disableCCD(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get gravMass(): number

      Gravitational mass. Defaults to the same as mass.

      Returns number

    • set gravMass(value: number): void

      Parameters

      • value: number

      Returns void

    • get gravMassScale(): number

      Scale factor applied to gravMass relative to the dynamic mass.

      Returns number

    • set gravMassScale(value: number): void

      Parameters

      • value: number

      Returns void

    • get id(): number

      Unique numeric identifier for this interactor.

      Returns number

    • get inertia(): number

      Moment of inertia. Must be finite and > 0. Setting switches inertiaMode to FIXED.

      Returns number

      If the body is the world body, or if no shapes are present in DEFAULT inertia mode.

    • set inertia(value: number): void

      Parameters

      • value: number

      Returns void

    • get isBullet(): boolean

      If true, continuous collision detection (CCD) is enabled for this body.

      Returns boolean

    • set isBullet(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get isSleeping(): boolean

      True if the body is currently sleeping.

      Returns boolean

      If the body is not in a Space.

    • get kinAngVel(): number

      Desired angular velocity for kinematic bodies.

      Returns number

    • set kinAngVel(value: number): void

      Parameters

      • value: number

      Returns void

    • get localCOM(): Vec2

      Local-space centre of mass (read-only, lazy-computed from shapes).

      Returns Vec2

      If this is the world body.

    • get mass(): number

      Mass in kg. Must be finite and > 0. Setting switches massMode to FIXED.

      Returns number

      If the body is the world body, or if no shapes are present in DEFAULT mass mode.

    • set mass(value: number): void

      Parameters

      • value: number

      Returns void

    • get rotation(): number

      Rotation of the body in radians.

      Returns number

      If set on a static body that is already in a space.

    • set rotation(value: number): void

      Parameters

      • value: number

      Returns void

    • get torque(): number

      Accumulated torque applied to this body for the current step (only for DYNAMIC bodies).

      Returns number

    • set torque(value: number): void

      Parameters

      • value: number

      Returns void

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

      User-defined data storage object.

      Returns Record<string, unknown>

    • get worldCOM(): Vec2

      World-space centre of mass (read-only, lazy-computed).

      Returns Vec2

      If this is the world body.

    Methods

    • Translate all shapes and adjust the body position so the local centre of mass coincides with the body origin.

      Returns Body

      this for chaining.

      If the body has no shapes.

    • Apply a direct angular impulse (change in angular velocity × inertia).

      Parameters

      • impulse: number

        The angular impulse magnitude.

      • sleepable: boolean = false

        If true, the body will not be woken if sleeping.

      Returns Body

      this for chaining.

    • Apply a linear (and optionally angular) impulse to the body. If pos is given, it creates a torque about the body's centre. If sleepable is true, sleeping bodies are not woken.

      Parameters

      • impulse: Vec2

        The linear impulse vector (world space).

      • Optionalpos: Vec2

        Optional world-space point of application.

      • sleepable: boolean = false

        If true, the body will not be woken if sleeping.

      Returns Body

      this for chaining.

    • Sum of buoyancy impulses received from fluid arbiters this step.

      Parameters

      • body: Body | null = null

        If provided, only arbiters shared with body are summed.

      Returns Vec3

      A Vec3 where x/y are the linear component and z is the angular component.

    • Return the set of bodies connected to this body via constraints.

      Parameters

      • depth: number = -1

        Maximum traversal depth (-1 means unlimited).

      • output: BodyList | null = null

        Optional existing list to accumulate results into.

      Returns BodyList

      A BodyList of connected bodies.

    • Sum of impulses applied to this body by all attached constraints this step.

      Returns Vec3

      A Vec3 where x/y are the linear component and z is the angular component.

    • Test whether a world-space point lies inside any shape attached to this body.

      Parameters

      • point: Vec2

        The point to test in world space.

      Returns boolean

      True if the point is inside at least one shape.

    • Create a deep copy of this body (shapes, mass properties, etc.).

      Returns Body

      A new Body with identical configuration.

      If this is the world body.

    • Compute a heuristic crush factor indicating how strongly this body is being compressed from multiple directions. A value near zero means balanced forces; larger values indicate compression.

      Returns number

      The crush factor (dimensionless).

      If the body is not in a Space.

    • Sum of fluid drag impulses received from fluid arbiters this step.

      Parameters

      • body: Body | null = null

        If provided, only arbiters shared with body are summed.

      Returns Vec3

      A Vec3 where x/y are the linear component and z is the angular component.

    • Manually integrate the body's position and rotation by deltaTime seconds (outside of Space.step).

      Parameters

      • deltaTime: number

        Time in seconds to integrate over.

      Returns Body

      this for chaining.

      If deltaTime is NaN.

    • Return the set of bodies currently interacting with this body via arbiters.

      Parameters

      • type: InteractionType | null = null

        Filter by interaction type (COLLISION, FLUID, SENSOR), or null for all.

      • _depth: number = -1

        Unused; reserved for future use.

      • output: BodyList | null = null

        Optional existing list to accumulate results into.

      Returns BodyList

      A BodyList of interacting bodies.

    • Return true if this body is dynamic.

      Returns boolean

    • Return true if this body is kinematic.

      Returns boolean

    • Return true if this body is static.

      Returns boolean

    • Transform a point from local body space to world space.

      Parameters

      • point: Vec2

        The point in local space.

      • weak: boolean = false

        If true, the returned Vec2 is a weak (pooled) reference.

      Returns Vec2

      The transformed point in world space.

    • Rotate a vector from local body space to world space (no translation).

      Parameters

      • vector: Vec2

        The vector in local space.

      • weak: boolean = false

        If true, the returned Vec2 is a weak (pooled) reference.

      Returns Vec2

      The rotated vector in world space.

    • Sum of normal (penetration-resolving) impulses received from collision arbiters this step.

      Parameters

      • body: Body | null = null

        If provided, only arbiters shared with body are summed.

      • freshOnly: boolean = false

        If true, only newly created arbiters are considered.

      Returns Vec3

      A Vec3 where x/y are the linear component and z is the angular component.

    • Sum of rolling (angular friction) impulses from collision arbiters this step.

      Parameters

      • body: Body | null = null

        If provided, only arbiters shared with body are summed.

      • freshOnly: boolean = false

        If true, only newly created arbiters are considered.

      Returns number

      The total rolling impulse scalar.

    • Rotate the body about a world-space pivot point by angle radians. Moves the body's position and increments its rotation.

      Parameters

      • centre: Vec2

        World-space pivot point.

      • angle: number

        Rotation in radians.

      Returns Body

      this for chaining.

      If centre is null or angle is NaN.

    • Rotate all shapes attached to this body in local space by angle radians.

      Parameters

      • angle: number

        Rotation in radians.

      Returns Body

      this for chaining.

    • Scale all shapes attached to this body in local space.

      Parameters

      • scaleX: number

        Horizontal scale factor.

      • scaleY: number

        Vertical scale factor.

      Returns Body

      this for chaining.

    • Set linear and angular velocity so the body reaches the given target pose in deltaTime seconds. Useful for kinematic or manually driven bodies.

      Parameters

      • targetPosition: Vec2

        Desired world-space position.

      • targetRotation: number

        Desired rotation in radians.

      • deltaTime: number

        Time in seconds over which to reach the target.

      Returns Body

      this for chaining.

      If targetPosition is null or deltaTime is zero.

    • Sum of tangent (friction) impulses received from collision arbiters this step.

      Parameters

      • body: Body | null = null

        If provided, only arbiters shared with body are summed.

      • freshOnly: boolean = false

        If true, only newly created arbiters are considered.

      Returns Vec3

      A Vec3 where x/y are the linear component and z is the angular component.

    • Sum of total contact impulses (normal + tangent) from collision arbiters this step.

      Parameters

      • body: Body | null = null

        If provided, only arbiters shared with body are summed.

      • freshOnly: boolean = false

        If true, only newly created arbiters are considered.

      Returns Vec3

      A Vec3 where x/y are the linear component and z is the angular component.

    • Sum of total fluid impulses (buoyancy + drag) from fluid arbiters this step.

      Parameters

      • body: Body | null = null

        If provided, only arbiters shared with body are summed.

      Returns Vec3

      A Vec3 where x/y are the linear component and z is the angular component.

    • Sum of all impulses (contacts + constraints) applied to this body this step, excluding sensor arbiters.

      Parameters

      • body: Body | null = null

        If provided, only arbiters shared with body are summed.

      • freshOnly: boolean = false

        If true, only newly created contact arbiters are considered.

      Returns Vec3

      A Vec3 where x/y are the linear component and z is the angular component.

    • Apply an affine transform matrix to all shapes in local space.

      Parameters

      • matrix: Mat23

        The 2D affine transformation matrix.

      Returns Body

      this for chaining.

    • Translate all shapes attached to this body in local space by translation.

      Parameters

      • translation: Vec2

        Offset vector in local space.

      Returns Body

      this for chaining.

    • Transform a point from world space to local body space.

      Parameters

      • point: Vec2

        The point in world space.

      • weak: boolean = false

        If true, the returned Vec2 is a weak (pooled) reference.

      Returns Vec2

      The transformed point in local space.

    • Rotate a vector from world space to local body space (no translation).

      Parameters

      • vector: Vec2

        The vector in world space.

      • weak: boolean = false

        If true, the returned Vec2 is a weak (pooled) reference.

      Returns Vec2

      The rotated vector in local space.