BB.FlowField2D
A 2D flow field object based off of Daniel Shiffman's Flow Field example in Nature of Code.
Index
Properties
Methods
Constructor
BB.FlowField2D
(
-
resolution
-
width
-
height
)
-
resolution
-
width
-
height
Parameters:
-
resolution
NumberCorresponds directly to the size of each flow field cell. A larger number will result in fewer cells.
-
width
NumberWidth in pixels
-
height
NumberHeight 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.
Methods
drawDebug
(
-
context
-
x
-
y
-
width
-
height
-
[cache=true]
)
-
context
-
x
-
y
-
width
-
height
-
[cache=true]
Draws a debug view of the flow field to context.
Parameters:
-
context
CanvasRenderingContext2DThe 2D HTML5 Canvas context to draw to.
-
x
Numberx position of the debug rectangle
-
y
Numbery position of the debug rectangle
-
width
Numberwidth of the debug rectangle
-
height
Numberposition of the debug rectangle
-
[cache=true]
Boolean optionalDrawing 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]
)
-
[seed=0]
-
[noiseStep=0.1]
Populate field with values using 2D perlin noise.
Parameters:
-
[seed=0]
Number optionalA seed to use when generating noise (e.x. Date.now() * 0.005)
-
[noiseStep=0.1]
Number optionalThe 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
-
x
-
y
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
NumberPixel/screen x coordinate
-
y
NumberPixel/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);