Class Spring

A spring, connecting two bodies.

Example

const spring = new Spring(boxBody, sphereBody, {
restLength: 0,
stiffness: 50,
damping: 1,
})

// Compute the force after each step
world.addEventListener('postStep', (event) => {
spring.applyForce()
})

Constructors

  • Parameters

    • bodyA: Body
    • bodyB: Body
    • Optional options: {
          damping?: number;
          localAnchorA?: Vec3;
          localAnchorB?: Vec3;
          restLength?: number;
          stiffness?: number;
          worldAnchorA?: Vec3;
          worldAnchorB?: Vec3;
      }
      • Optional damping?: number

        Damping of the spring. A number >= 0.

        Default

        1
        
      • Optional localAnchorA?: Vec3

        Anchor for bodyA in local bodyA coordinates. Where to hook the spring to body A, in local body coordinates.

        Default

        new Vec3()
        
      • Optional localAnchorB?: Vec3

        Anchor for bodyB in local bodyB coordinates. Where to hook the spring to body B, in local body coordinates.

        Default

        new Vec3()
        
      • Optional restLength?: number

        Rest length of the spring. A number > 0.

        Default

        1
        
      • Optional stiffness?: number

        Stiffness of the spring. A number >= 0.

        Default

        100
        
      • Optional worldAnchorA?: Vec3

        Where to hook the spring to body A, in world coordinates.

      • Optional worldAnchorB?: Vec3

        Where to hook the spring to body B, in world coordinates.

    Returns Spring

Properties

bodyA: Body

First connected body.

bodyB: Body

Second connected body.

damping: number

Damping of the spring. A number >= 0.

Default

1
localAnchorA: Vec3

Anchor for bodyA in local bodyA coordinates. Where to hook the spring to body A, in local body coordinates.

Default

new Vec3()
localAnchorB: Vec3

Anchor for bodyB in local bodyB coordinates. Where to hook the spring to body B, in local body coordinates.

Default

new Vec3()
restLength: number

Rest length of the spring. A number > 0.

Default

1
stiffness: number

Stiffness of the spring. A number >= 0.

Default

100

Methods

  • Apply the spring force to the connected bodies.

    Returns void

  • Get the anchor point on body A, in world coordinates.

    Parameters

    • result: Vec3

      The vector to store the result in.

    Returns void

  • Get the anchor point on body B, in world coordinates.

    Parameters

    • result: Vec3

      The vector to store the result in.

    Returns void

  • Set the anchor point on body A, using world coordinates.

    Parameters

    Returns void

  • Set the anchor point on body B, using world coordinates.

    Parameters

    Returns void