Christopherkriens Boids Save

Implementation of boids flocking simulation using SpriteKit and Swift 4 šŸ 

Project README

Two dimensional implementation of Boids using SpriteKit and Swift 4.

Now with more emotes! šŸŸ šŸ” šŸ¦„

Overview

Boids is an algorithm for simulating natural group flocking behavior. Flocking is an emergent behavior accomplished by applying a set of simple rules on autonomous agents, called "boids". Each boid operates independently, assessing its surrounding flock and adjusting its heading based on an evaluation of a set of basic rules.

Rules

The standard flocking rules:

1. Cohesion: Steer toward the average position of nearby agents

2. Alignment: Maintain a heading similar to the average heading of nearby agents

3. Separation: Steer away from agents that are close to avoid crowding

In addition to the standard flocking rules, I've added:

4. Bound: Steer away from the bounds of the device screen to keep agents in view

Interaction

A touch adds a temporary rule:

6. Seek: Move toward the current touch position

A force touch adds a temporary rule:

7. Evade: Move away from the current touch position

Preview

Usage

// Initialize and add a Boid SpriteNode to the Scene
let boid = Boid(withCharacter: "šŸ”", andSize: 40)
addChild(boid)
// Configure the boid's behaviors.. or don't; whatever, it's your life
self.behaviors = [Cohesion(intensity: 0.1), Separation(intensity: 0.1), Alignment(intensity: 1.0)]

Performance

Because each boid needs to iterate over the entire group, this runs in quadratic time O(nĀ²). As a benchmark, the iPhone X can maintain 60fps when simulating up to 150 boids.

Source Versioning

  • Xcode 10.1
  • iOS SDK 12
  • Swift 4.2

Credit

Original Paper: Craig W. Reynolds (1987). Flocks, Herds, and Schools: A Distributed Behavioral Model

Pseudocode: Conrad Parker (2007). Boids Pseudocode

Open Source Agenda is not affiliated with "Christopherkriens Boids" Project. README Source: christopherkriens/boids
Stars
39
Open Issues
0
Last Commit
5 years ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating