Giter Site home page Giter Site logo

ishanmanchanda / boids Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 1.0 72 KB

An implementation of Craig Reynold's Boids in p5.js, with lots of new features.

Home Page: https://ishanmanchanda.github.io/Boids/

License: MIT License

HTML 2.72% JavaScript 96.35% CSS 0.93%
processing js javascript p5js p5 simulation boids boids-simulation ecosystem

boids's Introduction

Boids Ecosystem

A p5.js implementation of Craig Reynold's Boids artificial life simulation.
Live preview available here: https://ishanmanchanda.github.io/Boids/ (Laptop/desktop recommended)

Craig Reynold's Boids

The simulation demonstrates the flocking behavior of bird-like objects called Boids. It serves as an example of emergent behavior, where complexity is generated by a group of individual agents interact according to simple rules.

The 3 rules of the original simulation are:

  1. Separation: Each Boid has a concept of personal space, and moves in a way to prevent crowding. More specifically, each Boid is repelled by all its neighbors within a certain radius.
  2. Alignment: Each Boid wants to move aligned with the group, and so changes its velocity to be in line with the average velocity of its neighbors.
  3. Cohesion: Each Boid tries to move towards the center of its local group.

These are the rules that govern Boid-Boid interactions; the original simulation additionally allowed for other rules such as avoiding obstacles and seeking targets.

My Implementation

I have implemented the above listed rules as well as some others to try and create an ecosystem. The ecosystem contains both Boids and Predators, with the Predators seeking Boids while the Boids flee from them. The Predators have a look-ahead feature which allows them to take into account the current velocity of Boids in additiona to their position. An earlier version of this ecosystem, which I developed on Khan Academy, had even more interactions in the form of static obstacles as well as a water body.

Each Predator has a hunger value which is reset when it eats a Boid. This is done by getting within a certain distance of a Boid, at which point the Boid is eaten. If a Predator's hunger value reaches a certain limit, it dies of starvation.

Additionally, both Predators as well as Boids have certain sight ranges, beyond which they cannot perceive other agents. The group behavior + the wrap-around effect of the edges of the simulation area generally ensure that agents eventually come close enough to interact, rather than continue on linear paths that never intersect.

Using the Simulation

Alignment, Cohesion, and Separation can be toggled on/off by pressing A, C, and S on the keyboard. Boids and Predators can be added by holding down B or P and clicking inside the simulation area with the mouse. Owing to the keyboard requirement, a laptop/desktop is highly recommended for viewing the simulation.

All parameters including initial simulation values, creature sizes, sight ranges, as well as the relative weights used to balance the various forces as easily tweakable in the globals.js file in the source directory.

Things I've learnt

  • Concepts and implementation of OOP with an emphasis on Inheritance and Polymorphism
  • Common optimization techniques for simulations including Bin-Lattice Spatial Subdivision
  • Extensive applications of Vectors and optimizations for operations on vectors

boids's People

Contributors

ishanmanchanda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

biologik303

boids's Issues

Add Boid hunger mechanism

Mechanics

  • Forest
  • Constantly reducing energy level
  • Force attracting towards forest
  • Death if unable to reach in time
  • Change in energy depletion depending on speed
  • Voluntary rest to recharge

Factors

  • Food capacity
  • Rate of eating
  • Rate of utilization
  • Boost when resting
  • Energy / food

Balance Forces

Balance all forces to ensure optimum behavior.

For example, separation force should absolutely prevent boids from overlapping.

Bin-Lattice Spatial Subdivision

  • Divide the area into a grid of cells.
  • Iterate over all creatures in each cell instead of all creatures overall.
  • (For each cell, check the 8 neighboring cells?)

Add thirst mechanism

Mechanics

  • Water body
  • Constantly reducing water level
  • Force attracting towards water body
  • Death if unable to reach in time
  • Change in water utilization depending on speed

Factors

  • Water reservoir capacity
  • Rate of drinking
  • Rate of utilization

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.