Skip to content

Commit 07bae04

Browse files
committed
ADD: image and some structure implementation
1 parent 8ff4a0a commit 07bae04

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Flocking simulation of starling murmuration using web graphics library (webGL) a
33

44
> **Checkout the [demo](https://techcentaur.github.io/Starling-Simulation/index.html#64) here**
55
6+
![Flocking Simulation](img/flocking.png)
7+
68
#### Flocking behavior
79
Flocking is a the motion of birds together and flocking behavior is a type of behavior exhibited when a group of birds, called a flock, are in flight.
810

@@ -127,12 +129,52 @@ guiControls = new function(){
127129
spotLight = new THREE.SpotLight(0xffffff);
128130
scene.add(spotLight);
129131
```
132+
133+
### Algorithm of Separation, Cohesion, and Alignment.
134+
135+
**Separation**
136+
- `distSquared` is the square of distance between the current position of canvas texture rendered ( at that time ) and each point on resolution window ( for boids ).
137+
- We shall add a velocity vector away from the velocity.now, with change proportional to the rendering time ( del_change ) and the amount any boid is closer to some other boid on the old texture.
138+
- `zoneRadiusSquared` is a design choice, we set its value to 35.0 ( a hit and trial technique ).
139+
140+
```javascript
141+
percent = distSquared / zoneRadiusSquared;
142+
if ( percent < separationThresh ) {
143+
f = (separationThresh / percent - 1.0) * del_change;
144+
velocity -= normalize(dir) * f;
145+
}
146+
```
147+
148+
**Alignment**
149+
150+
- We deal with alignment by creating a reference direction and then adjusting it using trignometric function with change proportional to the rendering time.
151+
152+
```javascript
153+
else if{
154+
float adjustedPercent = ( percent - separationThresh ) / (alignmentThresh - separationThresh);
155+
birdVelocity = texture2D( VeloctiyTexture, ref ).xyz;
156+
f = ( 0.5 - cos( adjustedPercent * PI_2 ) * 0.5 + 0.5 ) * del_change;
157+
velocity += normalize(birdVelocity) * f;
158+
}
159+
```
160+
161+
**Cohesion**
162+
163+
- Similar to aligment, we normalise the change and add in velocity vector.
164+
```javascript
165+
else {
166+
float adjustedPercent = ( percent - alignmentThresh ) / (1.0 - alignmentThresh);
167+
f = ( 0.5 - ( cos( adjustedPercent * PI_2 ) * -0.5 + 0.5 ) ) * del_change;
168+
velocity += normalize(dir) * f;
169+
}
170+
```
130171
### Importance of `requestAnimationFrame()`
131172
---
132173
- It allows you to execute code on the next available screen repaint, taking the guess work out of **getting in sync** with the user's browser and hardware readiness to make changes to the screen.
133174
134175
- Code running inside background tabs in your browser are either paused or slowed down significantly (to 2 frames per second or less) automatically to further **save user system resources**.
135176
177+
136178
## Folder-Terminology
137179
138180
- **js-libs**: Static javascript library files from three.js and some from webGL-js.

0 commit comments

Comments
 (0)