Skip to content

Implementation of boids flocking simulation using SpriteKit and Swift 4 🐠

License

Notifications You must be signed in to change notification settings

christopherkriens/boids

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Implementation of boids flocking simulation using SpriteKit and Swift 4 🐠

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages