BB.Particle2D
A 2D Particle class for all your physics needs
Constructor
BB.Particle2D
(
-
[config]
)
-
[config]
Parameters:
-
[config]
Object optionalAn optional config object to initialize Particle2D properties, including: position ( object with x and y ), mass ( defaults to 1 ), radius ( defaults to 0 ) and friction ( defaults to 1 ).
an initial velocity or acceleration can also be set by passing a BB.Vector2 to either of those properties ( ie. velocity or acceleration ). Or an alternative approach is to initialize with a heading property (radians) and speed property ( number ). If no velocity or acceleration or heading/speed is set, the default velocity is BB.Vector2(0,0).
Example:
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
var star = newBB.Particle2D({
position: new BB.Vector2(WIDTH/2, HEIGHT/2 ),
mass: 20000
});
var planet = new BB.Particle2D({
position: new BB.Vector2( WIDTH/2+200, HEIGHT/2),
heading: -Math.PI / 2,
speed: 10
});
var comet = new BB.Particle2D({
position: new BB.Vector2(
BB.MathUtils.randomInt(WIDTH),
BB.MathUtils.randomInt(HEIGHT) ),
velocity: new BB.Vector2(
BB.MathUtils.randomInt(10),
BB.MathUtils.randomInt(10))
});
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 )
Methods
applyForce
(
-
vector
)
-
vector
takes a force, divides it by particle's mass, and applies it to acceleration ( which is added to velocity each frame )
Parameters:
-
vector
BB.Vector2force to be applied
collide
(
-
config
)
-
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
Objectobject 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
});
gravitate
(
-
particle
-
[mass]
)
-
particle
-
[mass]
identifies something to gravitate towards. the object of gravitation needs to have a position ( x, y ) and mass
Parameters:
-
particle
Objectif 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 optionalwhen 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 );
spring
(
-
config
)
-
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
Objectobject 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.