Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A fast JavaScript implementation of the boid's algorithm obeying coherence, alignment, and separation

License

NotificationsYou must be signed in to change notification settings

danielmuthama/Boids-algorithm

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A lightweight JavaScript implementation ofboids. Its "API" is a little limited,but it's reasonably performant - my MacBook ran the demo with 1,000 boids at60 frames per second.

check out the demo

I used an earlier, hastier version for the flocks ingrow.

Installation

For use withbrowserify:

npm install boids

Usage

varboids=require('boids'),raf=require('raf')varflock=boids({boids:50,// The amount of boids to usespeedLimit:0,// Max steps to take per tickaccelerationLimit:1,// Max acceleration per tickseparationDistance:60,// Radius at which boids avoid othersalignmentDistance:180,// Radius at which boids align with otherschoesionDistance:180,// Radius at which boids approach othersseparationForce:0.15,// Speed to avoid atalignmentForce:0.25,// Speed to align with other boidschoesionForce:0.1,// Speed to move towards other boidsattractors:[]})raf(window).on('data',function(){ctx.fillStyle='black'ctx.fillRect(0,0,canvas.width,canvas.height)ctx.fillStyle='white'ctx.save()ctx.translate(-canvas.width/2,-canvas.height/2)flock.tick()flock.boids.forEach(function(boid){ctx.fillRect(boid[0],boid[1],1,1)})ctx.restore()})

flock = boids([options])

flock.tick()

Moves the boid simulation forward one tick - if you're running an animation,you should be calling this on each frame.

flock.boids

All of your boids are stored as an array of arrays, with eacharray containing the following variables for a single boid:

[xPosition,yPosition,xSpeed,ySpeed,xAcceleration,yAcceleration]

Because the flock is just an array, it should be entirely safe for youto add and remove elements without any unintended side effects, provided allof the arrays are at least 6 elements long and contain numerical values. Forexample, you can add a new boid moving at a random speed to the origin like so:

flock.boids.push([0,0,Math.random()*10-5,Math.random()*10-5,0,0])

flock.attractors

You can use attractors to control the flow of the boids - essentially,providing them with goals and obstacles. Each attractor contains:

[xPosition,yPosition,radius,force]

Note that you can use a negative value forforce to repel boids instead ofattracting them. Again, it should be safe to modify, add and remove thesearrays without any surprises.

Benchmark

Runningbenchmark.js yielded the following results in Node:

50 boids: 34013 ticks/sec100 boids: 10000 ticks/sec150 boids: 4537 ticks/sec200 boids: 2583 ticks/sec250 boids: 1653 ticks/sec300 boids: 1159 ticks/sec350 boids: 835 ticks/sec400 boids: 654 ticks/sec450 boids: 518 ticks/sec500 boids: 419 ticks/sec550 boids: 347 ticks/sec600 boids: 292 ticks/sec650 boids: 249 ticks/sec700 boids: 215 ticks/sec750 boids: 187 ticks/sec800 boids: 160 ticks/sec850 boids: 130 ticks/sec900 boids: 119 ticks/sec950 boids: 107 ticks/sec1000 boids: 95 ticks/sec

I'm very much open to pull requests that can help improve performance :)

About

A fast JavaScript implementation of the boid's algorithm obeying coherence, alignment, and separation

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript76.1%
  • HTML23.9%

[8]ページ先頭

©2009-2025 Movatter.jp