BB.Agent2D

Extends BB.Particle2D
Defined in: src/BB.Agent2D.js:13

A 2D Autonomous Agent class for "intelligent" physics behaviors.

Constructor

BB.Agent2D
(
  • config
)

Parameters:

  • config Object

    Agent2D configuration object. Exactly the same configuration object expected in BB.Particle2D.

Example:

 var WIDTH = window.innerWidth;
 var HEIGH = window.innerHeight;
 var agent = new BB.Agent2D({
    maxSpeed: 6,
    position: new BB.Vector2( Math.random() * WIDTH, Math.random() * HEIGHT ),
    velocity: new BB.Vector2(1, 2),
    radius: 50
 });

Properties

acceleration BB.Vector2

Usually used to accumulate forces to be added to velocity each frame

elasticity Number default:0.05

how bouncy it is when it collides with an object

friction Number default:1

the particle's friction ( not environment's friction ) multiplied by velocity each frame

heading Number

the particle's "heading" expressed in radians, essentially: Math.atan2( velocity.y, velocity.x );

mass Number default:1

the particle's mass

radius Number default:0

the particle's radius, used for callculating collistions

speed Number

the particle's "speed", essentially: the square root of velocity.x² + velocity.y²

velocity BB.Vector2

the particle's velocity ( see acceleration also )

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

align
(
  • particles
  • [maxForce=0.1]
  • [neighborDistance=50]
  • [multiplier=1]
)

Applies a force that that is the average velocity of all nearby particles(s).

Parameters:

  • particles Array

    An array of BB.Particle2D objects. May also be a single BB.Particle2D object.

  • [maxForce=0.1] Number optional

    The maximum force used to limit the align behavior. Defaults to 0.1 if parameter is null or undefined.

  • [neighborDistance=50] Number optional

    Threshold distance to apply the align behavior. Defaults to 20 if parameter is null or undefined.

  • [multiplier=1] Number optional

    An optional parameter (usually between 0-1.0) used to scale the align force. This multiplier operation is run right before the align force is applied, after the force may have already been limited by maxForce.

Example:

 // assuming agent is an instance of BB.Agent2D
 // assuming particles is an array of BB.Vector2s
 agent.align(particles, 0.1, 50);
 // or to half the align force, use a multiplier
 agent.align(particles, 0.1, 50, 0.5);

applyForce
(
  • vector
)

takes a force, divides it by particle's mass, and applies it to acceleration ( which is added to velocity each frame )

Parameters:

avoid
(
  • particles
  • [maxForce]
  • [seperationDistance]
  • [multiplier=1]
)

Applies a force that steers the agent to avoid particles(s).

Parameters:

  • particles Array

    An array of BB.Particle2D objects. May also be a single BB.Particle2D object.

  • [maxForce] Number optional

    The maximum force used to limit the avoid behavior. Defaults to 0.1 if parameter is null or undefined.

  • [seperationDistance] Number optional

    Threshold distance to apply the avoid behavior. Defaults to 20 if parameter is null or undefined.

  • [multiplier=1] Number optional

    An optional parameter (usually between 0-1.0) used to scale the avoid force. This multiplier operation is run right before the avoid force is applied, after the force may have already been limited by maxForce.

Example:

 // assuming agent is an instance of BB.Agent2D
 // assuming particles is an array of BB.Particle2Ds
 agent.avoid(particles, 0.1, 100);
 // or to half the avoid force, use a multiplier
 agent.avoid(particles, 0.1, 100, 0.5);

avoidWalls
(
  • config
)

Causes the agent to steer away from a rectangular bounding box. Must be run once per frame.

Parameters:

  • config Object

    The config object.

    • top Number

      The top of the bounding box.

    • bottom Number

      The bottom of the bounding box.

    • left Number

      The left of the bounding box.

    • right Number

      The right of the bounding box.

    • distance Number

      The threshold distance inside of which the avoidWalls force will be applied to the agent.

    • [maxForce=0.1] Number optional

      The maximum force used to limit the avoidWalls behavior. Defaults to 0.1 if parameter is null or undefined.

Example:

 // assuming agent is an instance of BB.Agent2D
 agent.avoidWalls({
    top: 0,
    bottom: window.innerHeight,
    left: 0,
    right: window.innerWidth,
    distance: 100,
    maxForce: 0.1
 });

cohesion
(
  • particles
  • [maxForce=0.1]
  • [neighborDistance=50]
  • [multiplier=1]
)

Applies a steering force that is the average position of all nearby particles(s).

Parameters:

  • particles Array

    An array of BB.Particle2D objects. May also be a single BB.Particle2D object.

  • [maxForce=0.1] Number optional

    The maximum force used to limit the cohesion behavior. Defaults to 0.1 if parameter is null or undefined.

  • [neighborDistance=50] Number optional

    Threshold distance to apply the cohesion behavior. Defaults to 20 if parameter is null or undefined.

  • [multiplier=1] Number optional

    An optional parameter (usually between 0-1.0) used to scale the cohesion force. This multiplier operation is run right before the cohesion force is applied, after the force may have already been limited by maxForce.

Example:

 // assuming agent is an instance of BB.Agent2D
 // assuming particles is an array of BB.Vector2s
 agent.cohesion(particles, 0.1, 50);
 // or to half the cohesion force, use a multiplier
 agent.cohesion(particles, 0.1, 50, 0.5);

collide
(
  • config
)

tracks objects to collide against, this can be other particles ( objects with position vectors and a radius ) and/or a perimeter ( top, left, right, bottom )

Parameters:

  • config Object

    object with properties for top, left, bottom, right ( all numbers ) and particles ( array of other particles or objects with position.x, positon.y and radius properties )

Example:

  // assuming ball is an instance of BB.Particle2D
  // assuming balls is an array of BB.Particle2D objects
  ball.collide({
     top:0,
     right: canvas.width,
     bottom: canvas.height,
     left: 0,
     particles: balls
  });

flee
(
  • targets
  • [maxForce=0.1]
  • [multiplier=1]
)

Applies a force that steers the agent away from particles(s). Opposite of seek.

Parameters:

  • targets Array

    An array of BB.Vector2 objects. May also be a single BB.Vector2 object.

  • [maxForce=0.1] Number optional

    The maximum force used to limit the flee behavior. Defaults to 0.1 if parameter is null or undefined.

  • [multiplier=1] Number optional

    An optional parameter (usually between 0-1.0) used to scale the flee force. This multiplier operation is run right before the flee force is applied, after the force may have already been limited by maxForce.

Example:

 // assuming agent is an instance of BB.Agent2D
 // assuming targets is an array of BB.Vector2s
 agent.flee(targets, 0.1);
 // or to half the flee force, use a multiplier
 agent.flee(targets, 0.1, 0.5);

gravitate
(
  • particle
  • [mass]
)

identifies something to gravitate towards. the object of gravitation needs to have a position ( x, y ) and mass

Parameters:

  • particle Object

    if passed as the only argument it should be an Object with a position.x, position.y and mass ( ie. an instance of BB.Particle2D ). Otherwise the first argument needs to be an Object with an x and y ( ie. instance of BB.Vector2 or at the very least { x: ..., y: ... } )

    alternatively, gravitate could also be passed an array of objects ( each with position and mass properties )

  • [mass] Number optional

    when particle is not an instance of BB.Particle2D and is a Vector an additional argument for mass is required

Example:

  // assuming star and planet are both instances of BB.Particle2D
  planet.gravitate( star );
  // or
  planet.gravitate( star.position, star.mass );
  // or
  planet.gravitate( { x:WIDTH/2, y:HEIGHT/2 }, 20000 );

  // assuming stars is an array of BB.particle2D
  planet.gravitate( stars );

seek
(
  • targets
  • [maxForce=0.1]
  • [arriveDistance]
  • [multiplier=1]
)

Applies a force that steers the agent towards target(s). Opposite of flee.

Parameters:

  • targets Array

    An array of BB.Vector2 objects. May also be a single BB.Vector2 object.

  • [maxForce=0.1] Number optional

    The maximum force used to limit the seek behavior. Defaults to 0.1 if parameter is null or undefined.

  • [arriveDistance] Number optional

    Threshold distance to apply the arrive behavior. If a non-null/undefined value is supplied, the agent will slow its movement porportionate to its distance from a target if it is within this distance from that target.

  • [multiplier=1] Number optional

    An optional parameter (usually between 0-1.0) used to scale the seek force. This multiplier operation is run right before the seek force is applied, after the force may have already been limited by maxForce.

Example:

 // assuming agent is an instance of BB.Agent2D
 // assuming targets is an array of BB.Vector2s
 agent.seek(targets, 0.1, 200);

seperate
(
  • particles
  • [maxForce]
  • [seperationDistance]
  • [multiplier=1]
)

Alias of avoid(). Applies a force that steers the agent to avoid particles(s).

Parameters:

  • particles Array

    An array of BB.Particle2D objects. May also be a single BB.Particle2D object.

  • [maxForce] Number optional

    The maximum force used to limit the avoid behavior. Defaults to 0.1 if parameter is null or undefined.

  • [seperationDistance] Number optional

    Threshold distance to apply the avoid behavior. Defaults to 20 if parameter is null or undefined.

  • [multiplier=1] Number optional

    An optional parameter (usually between 0-1.0) used to scale the avoid force. This multiplier operation is run right before the avoid force is applied, after the force may have already been limited by maxForce.

spring
(
  • config
)

identifies something to spring towards. the target needs to have an x,y position, a k value which is a constant factor characteristic of the spring ( ie. its stiffness, usually some decimal ), and a length.

Parameters:

  • config Object

    object with properties for point ( vector with x,y ), k ( number ) and length ( number ).

    alternatively, spring could also be passed an array of config objects

Example:

  // assuming ball is an instance of BB.Particle2D
  // and center is an object with x,y positions
  ball.spring({
     position: center.position,
     k: 0.1,
    length: 100
  });
 
  // the ball will spring back and forth forever from the center position
  // unless ball has friction value below the default of 1.0

update ()

Update the particle's internals and apply acceleration to veloicty. Called once per animation frame.

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