Giter Club home page Giter Club logo

boids's Introduction

Boids - Flocking Simulation

Boids are an algoirthmic name for the objects used to simulate the flocking of birds. This idea was first created by Craig Reynolds in 1986. More can be read about it here. Here I've recreated the Boids simulation using Swift and Apple's Metal Graphics API.

The final result

The 3 Rules of being a Boid


1. Seperation Steer away from nearby Boids to avoid collisions

func seperation(boid: Boid) -> SIMD2<Float> {
    let minDistance: Float = 0.15
    var nearbyVect = SIMD2<Float>(0,0)
    for other in BOIDS {
        if(other !== boid && distance(a: boid, b: other) <= minDistance){
            nearbyVect += (other.position - boid.position)
        }
    }
    return -1 * nearbyVect
}

2. Alignment Rotate to move in the same direction as other Boids

func alignment(boid: Boid) -> SIMD2<Float> {
    let minDistance: Float = 0.15
    var nearbyVect = SIMD2<Float>(0,0)
    var neighborCount: Float = 0
    for other in BOIDS {
        if(other !== boid && distance(a: boid, b: other) <= minDistance){
            nearbyVect += other.velocity
            neighborCount += 1
        }
    }
    if(neighborCount == 0){
        return nearbyVect
    }
    return simd_normalize(nearbyVect / neighborCount)
}
                                                                     

3. Cohesion Move towards the center of nearby Boids

func cohesion(boid: Boid) -> SIMD2<Float> {
      let minDistance: Float = 0.15
      var nearbyVect = SIMD2<Float>(0,0)
      var neighborCount: Float = 0
      for other in BOIDS {
          if(other !== boid && distance(a: boid, b: other) <= minDistance){
              nearbyVect += other.position
              neighborCount += 1
          }
      }

      if(neighborCount == 0){
          return nearbyVect
      }
      nearbyVect /= neighborCount
      return simd_normalize(nearbyVect  - boid.position)
  }

boids's People

Contributors

kjbrooks1 avatar

Watchers

 avatar

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.