BB.FlowField2D

A 2D flow field object based off of Daniel Shiffman's Flow Field example in Nature of Code.

Constructor

BB.FlowField2D
(
  • resolution
  • width
  • height
)

Parameters:

  • resolution Number

    Corresponds directly to the size of each flow field cell. A larger number will result in fewer cells.

  • width Number

    Width in pixels

  • height Number

    Height in pixels

Example:

 var flowField = new BB.FlowField2D(40, window.innerWidth, window.innerHeight);

Properties

field Array

A two-deminsional array of BB.Vector2Ds that makes up this flow field. All vectors default to values (0, 0) when first created.

height Number

Height of the flow field in pixels.

resolution Number

The resolution of the flow field. Corresponds directly to the size of each flow field cell. A larger number will result in fewer cells.

width Number

Width of the flow field in pixels.

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

drawDebug
(
  • context
  • x
  • y
  • width
  • height
  • [cache=true]
)

Draws a debug view of the flow field to context.

Parameters:

  • context CanvasRenderingContext2D

    The 2D HTML5 Canvas context to draw to.

  • x Number

    x position of the debug rectangle

  • y Number

    y position of the debug rectangle

  • width Number

    width of the debug rectangle

  • height Number

    position of the debug rectangle

  • [cache=true] Boolean optional

    Drawing the debug view is fairly expensive. For this reason the drawDebug(...) function lazily caches an image of the flow field that it draws to context, only updating when new values are passed for width and height parameters. Set this parameter to false to disable caching and redraw the flow field debug view each time drawDebug(...) is called.

generateNoiseField
(
  • [seed=0]
  • [noiseStep=0.1]
)

Populate field with values using 2D perlin noise.

Parameters:

  • [seed=0] Number optional

    A seed to use when generating noise (e.x. Date.now() * 0.005)

  • [noiseStep=0.1] Number optional

    The value to increase noise by per each field cell.

Example:

 // assuming flowField is an instance of BB.FlowField2D
 function update() {
     // assumes update(...) will be called once per animation frame
     flowField.generateNoiseField(Date.now()*0.005, 0.1);
 };

generateRandomField ()

Populate a flow field with cells containing normalized random vectors

Example:

 // assuming flowField is an instance of BB.FlowField2D
 function update() {
     // assumes update(...) will be called once per animation frame
     flowField.generateRandomField();
 };

lookup
(
  • x
  • y
)
BB.Vector2D

Lookup the corresponding field cell using pixel space coordinates x and y. Note, x and y must be a value between 0 and width and height respectively.

Parameters:

  • x Number

    Pixel/screen x coordinate

  • y Number

    Pixel/screen y coordinate

Returns: BB.Vector2D

The cell "beneath" x and y

Example:

 // assuming flowField is an instance of BB.FlowField2D
 // assuming agent is an instance of BB.Agent2D
 var cell = flowField.lookup(agent.position.x, agent.position.y);
 agent.applyForce(cell);

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index