Options
All
  • Public
  • Public/Protected
  • All
Menu

Class World

A World stores and manages Body and Particle for 2D physics simulation

Hierarchy

  • World

Index

Constructors

constructor

  • Create a World for 2D physics simulation

    Parameters

    • bound: Group

      a rectangular bounding box defined by a Group

    • Default value friction: number = 1

      a value between 0 to 1 where 1 means no friction. Default is 1

    • Default value gravity: PtLike | number = 0

      a number of a Pt to define gravitational force. Using a number is a shorthand to set new Pt(0, n). Default is 0.

    Returns World

Properties

Protected _bodies

_bodies: Body[] = []

Protected _bound

_bound: Bound

Protected _damping

_damping: number = 0.75

Protected _drawBodies

_drawBodies: function

Type declaration

    • (p: Body, i: number): void
    • Parameters

      Returns void

Protected _drawParticles

_drawParticles: function

Type declaration

Protected _friction

_friction: number = 1

Protected _gravity

_gravity: Pt = new Pt()

Private _lastTime

_lastTime: number = null

Protected _particles

_particles: Particle[] = []

Accessors

bodyCount

  • get bodyCount(): number
  • Get the number of bodies

    Returns number

damping

  • get damping(): number
  • set damping(f: number): void

friction

  • get friction(): number
  • set friction(f: number): void

gravity

  • get gravity(): Pt
  • set gravity(g: Pt): void

particleCount

  • get particleCount(): number
  • Get the number of particles

    Returns number

Methods

Protected _updateBodies

  • _updateBodies(dt: number): void
  • Internal function to update bodies

    Parameters

    • dt: number

    Returns void

Protected _updateParticles

  • _updateParticles(dt: number): void
  • Internal function to update particles

    Parameters

    • dt: number

    Returns void

add

  • Add a particle or body to this world.

    Parameters

    • p: Particle | Body

      Particle or Body instance

    • Optional name: string

      optional name, which can be referenced in body() or particle() function to retrieve this back.

    Returns this

body

  • body(id: number | string): Body
  • Get a body in this world by index or string id

    Parameters

    • id: number | string

      numeric index of the body, or a string id that associates with it.

    Returns Body

drawBodies

  • drawBodies(fn: function): void
  • Draw bodies using the provided function

    Parameters

    • fn: function

      a function that draws bodies passed in the parameters (bodies, index).

        • (p: Body, i: number): void
        • Parameters

          Returns void

    Returns void

drawParticles

  • drawParticles(fn: function): void
  • Draw particles using the provided function

    Parameters

    • fn: function

      a function that draws particles passed in the parameters (particles, index).

    Returns void

Protected integrate

  • Internal integrate function

    Parameters

    • p: Particle

      particle

    • dt: number

      time changed

    • Optional prevDt: number

      previous change in time, optional

    Returns Particle

particle

  • particle(id: number | string): Particle
  • Get a particle in this world by index or string id

    Parameters

    • id: number | string

      numeric index of the particle, or a string id that associates with it.

    Returns Particle

remove

  • remove(which: "body" | "particle", index: number, count?: number): this
  • Remove either body or particle from this world. Support removing a range and negative index.

    Parameters

    • which: "body" | "particle"

      Either "body" or "particle"

    • index: number

      Start index, which can be negative (where -1 is at index 0, -2 at index 1, etc)

    • Default value count: number = 1

      Number of items to remove. Default is 1.

    Returns this

update

  • update(ms: number): void
  • Update this world one time step

    Parameters

    • ms: number

      change in time in milliseconds

    Returns void

Static boundConstraint

  • boundConstraint(p: Particle, rect: Group, damping?: number): void
  • Static function to calculate bounding box constraints.

    Parameters

    • p: Particle

      particle

    • rect: Group

      bounding box defined by a Group

    • Default value damping: number = 0.75

      damping between 0 to 1, where 1 means no damping. Default is 0.75.

    Returns void

Static edgeConstraint

  • Static function to calculate edge constraints between 2 particles.

    Parameters

    • p1: Particle

      particle 1

    • p2: Particle

      particle 1

    • dist: number

      distance between particles

    • Default value stiff: number = 1

      stiffness between 0 to 1.

    • Default value precise: boolean = false

      use precise distance calculation. Default is false.

    Returns Particle

Object literals

Protected _names

_names: object

b

b: object

Type declaration

p

p: object

Type declaration

Generated using TypeDoc