custodiet ipsos custodes 00357 95141123

mail@thrasos.net

A furry planet with anti-collision drones or the Sfera app for Mac/Win/Lin and Android.

sea_urchin

A giant furry planet somewhere in universe and a flock of thousands of drones that don’t collide with each other.

The app uses openGL for the graphics, it is java based and uses the processing libs. This is quite fascinating you take  a break and 20′ later you have a fun doodle running on all platforms that support java.

This “art app” as I like to call these things is a combination of the implementation that  Daniel Shiffman did based on Craig Reynold’s Boids program to simulate the flocking behavior of birds and the “esfera” by David Pena. Each Drone steers itself based on rules of avoidance, alignment, and coherence.

What I would like to do is to remove the mouse functionality and instead use the webcam to identify hand gestures and specifically the finger tips. Imagine this on a large HD projector with millions of Drones around the furry planet using your hands, fun right?

The app runs on Mac, Win, Lin and Android. I haven’t tested it yet on Windows but it should run fine. But seriously don’t use windows.

 

Click your mouse to produce more Drones.
Move your mouse up and down to rotate the sphere.
Windoes 32-bit edition - Download here
Windoes 64-bit edition - Download here
Linux 32-bit edition - Download here
Linux 64-bit edition - Download here
MAC OS X   - Download here
ANDROID   - Download here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
</code>/**author =Thrasos *URL= http://www.thrasos.net</pre>
<pre>* Made for the android originally.
*This "art app" as I like to call these things is
*a combination of the implementation that  Daniel Shiffman
* did based on Craig Reynold's Boids program
* to simulate the flocking behavior of birds
*and the esfera by David Pena. Each Drone steers itself
*based on rules of avoidance, alignment, and coherence.
*
*/

package processing.android.test.sfera;

import processing.core.*;
import processing.xml.*;

import android.view.MotionEvent;
import android.view.KeyEvent;
import android.graphics.Bitmap;
import java.io.*;
import java.util.*;

public class sfera extends PApplet {

int cuantos = 8000;
pelo[] lista ;
float[] z = new float[cuantos];
float[] phi = new float[cuantos];
float[] largos = new float[cuantos];
float radio = 200;
float rx = 0;
float ry =0;

Flock flock;

public void setup() {

  radio = height/3.5f;

  lista = new pelo[cuantos];
  for (int i=0; i&lt;cuantos; i++){
    lista[i] = new pelo();
  }
  noiseDetail(3);
  flock = new Flock();
  // Add an initial set of boids into the system
  for (int i = 0; i &lt; 150; i++) {
    flock.addBoid(new Boid(new PVector(width/2,height/2), 3.0f, 0.05f));
  }
  smooth();

}

public void draw() {
  background(0);
    flock.run();

  translate(width/2,height/2);

  float rxp = ((mouseX-(width/2))*0.005f);
  float ryp = ((mouseY-(height/2))*0.005f);
  rx = (rx*0.9f)+(rxp*0.1f);
  ry = (ry*0.9f)+(ryp*0.1f);
  rotateY(rx);
  rotateX(ry);
  fill(0);
  noStroke();
  sphere(radio);

  for (int i=0;i&lt;cuantos;i++){
    lista[i].dibujar();

  }
}

// Add a new boid into the System
public void mousePressed() {
  flock.addBoid(new Boid(new PVector(mouseX,mouseY),2.0f,0.05f));
}
class pelo
{
  float z = random(-radio,radio);
  float phi = random(TWO_PI);
  float largo = random(1.15f,1.2f);
  float theta = asin(z/radio);

    public void dibujar(){

    float off = (noise(millis() * 0.0005f,sin(phi))-0.5f) * 0.3f;
    float offb = (noise(millis() * 0.0007f,sin(z) * 0.01f)-0.5f) * 0.3f;

    float thetaff = theta+off;
    float phff = phi+offb;
    float x = radio * cos(theta) * cos(phi);
    float y = radio * cos(theta) * sin(phi);
    float z = radio * sin(theta);
    float msx= screenX(x,y,z);
    float msy= screenY(x,y,z);

    float xo = radio * cos(thetaff) * cos(phff);
    float yo = radio * cos(thetaff) * sin(phff);
    float zo = radio * sin(thetaff);

    float xb = xo * largo;
    float yb = yo * largo;
    float zb = zo * largo;

    beginShape(LINES);
    stroke(0);
    vertex(x,y,z);
    stroke(200,150);
    vertex(xb,yb,zb);
    endShape();
  }
}
// The Boid class

class Boid {

  PVector loc;
  PVector vel;
  PVector acc;
  float r;
  float maxforce;    // Maximum steering force
  float maxspeed;    // Maximum speed

    Boid(PVector l, float ms, float mf) {
    acc = new PVector(0,0);
    vel = new PVector(random(-1,1),random(-1,1));
    loc = l.get();
    r = 2.0f;
    maxspeed = ms;
    maxforce = mf;
  }

  public void run(ArrayList boids) {
    flock(boids);
    update();
    borders();
    render();
  }

  // We accumulate a new acceleration each time based on three rules
  public void flock(ArrayList boids) {
    PVector sep = separate(boids);   // Separation
    PVector ali = align(boids);      // Alignment
    PVector coh = cohesion(boids);   // Cohesion
    // Arbitrarily weight these forces
    sep.mult(1.5f);
    ali.mult(1.0f);
    coh.mult(1.0f);
    // Add the force vectors to acceleration
    acc.add(sep);
    acc.add(ali);
    acc.add(coh);
  }

  // Method to update location
  public void update() {
    // Update velocity
    vel.add(acc);
    // Limit speed
    vel.limit(maxspeed);
    loc.add(vel);
    // Reset accelertion to 0 each cycle
    acc.mult(0);
  }

  public void seek(PVector target) {
    acc.add(steer(target,false));
  }

  public void arrive(PVector target) {
    acc.add(steer(target,true));
  }

  // A method that calculates a steering vector towards a target
  // Takes a second argument, if true, it slows down as it approaches the target
  public PVector steer(PVector target, boolean slowdown) {
    PVector steer;  // The steering vector
    PVector desired = target.sub(target,loc);  // A vector pointing from the location to the target
    float d = desired.mag(); // Distance from the target is the magnitude of the vector
    // If the distance is greater than 0, calc steering (otherwise return zero vector)
    if (d &gt; 0) {
      // Normalize desired
      desired.normalize();
      // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
      if ((slowdown) &amp;&amp; (d &lt; 100.0f)) desired.mult(maxspeed*(d/100.0f)); // This damping is somewhat arbitrary
      else desired.mult(maxspeed);
      // Steering = Desired minus Velocity
      steer = target.sub(desired,vel);
      steer.limit(maxforce);  // Limit to maximum steering force
    }
    else {
      steer = new PVector(0,0);
    }
    return steer;
  }

  public void render() {
    // Draw a triangle rotated in the direction of velocity
    float theta = vel.heading2D() + PI/2;
    fill(200,100);
    stroke(255);
    pushMatrix();
    translate(loc.x,loc.y);
    rotate(theta);
    beginShape(TRIANGLES);
    vertex(0, -r*2);
    vertex(-r, r*2);
    vertex(r, r*2);
    endShape();
    popMatrix();
  }

  // Wraparound
  public void borders() {
    if (loc.x &lt; -r) loc.x = width+r;
    if (loc.y &lt; -r) loc.y = height+r;
    if (loc.x &gt; width+r) loc.x = -r;
    if (loc.y &gt; height+r) loc.y = -r;
  }

  // Separation
  // Method checks for nearby boids and steers away
  public PVector separate (ArrayList boids) {
    float desiredseparation = 20.0f;
    PVector steer = new PVector(0,0,0);
    int count = 0;
    // For every boid in the system, check if it's too close
    for (int i = 0 ; i &lt; boids.size(); i++) {
      Boid other = (Boid) boids.get(i);
      float d = PVector.dist(loc,other.loc);
      // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
      if ((d &gt; 0) &amp;&amp; (d &lt; desiredseparation)) {
        // Calculate vector pointing away from neighbor
        PVector diff = PVector.sub(loc,other.loc);
        diff.normalize();
        diff.div(d);        // Weight by distance
        steer.add(diff);
        count++;            // Keep track of how many
      }
    }
    // Average -- divide by how many
    if (count &gt; 0) {
      steer.div((float)count);
    }

    // As long as the vector is greater than 0
    if (steer.mag() &gt; 0) {
      // Implement Reynolds: Steering = Desired - Velocity
      steer.normalize();
      steer.mult(maxspeed);
      steer.sub(vel);
      steer.limit(maxforce);
    }
    return steer;
  }

  // Alignment
  // For every nearby boid in the system, calculate the average velocity
  public PVector align (ArrayList boids) {
    float neighbordist = 25.0f;
    PVector steer = new PVector(0,0,0);
    int count = 0;
    for (int i = 0 ; i &lt; boids.size(); i++) {
      Boid other = (Boid) boids.get(i);
      float d = PVector.dist(loc,other.loc);
      if ((d &gt; 0) &amp;&amp; (d &lt; neighbordist)) {
        steer.add(other.vel);
        count++;
      }
    }
    if (count &gt; 0) {
      steer.div((float)count);
    }

    // As long as the vector is greater than 0
    if (steer.mag() &gt; 0) {
      // Implement Reynolds: Steering = Desired - Velocity
      steer.normalize();
      steer.mult(maxspeed);
      steer.sub(vel);
      steer.limit(maxforce);
    }
    return steer;
  }

  // Cohesion
  // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
  public PVector cohesion (ArrayList boids) {
    float neighbordist = 25.0f;
    PVector sum = new PVector(0,0);   // Start with empty vector to accumulate all locations
    int count = 0;
    for (int i = 0 ; i &lt; boids.size(); i++) {
      Boid other = (Boid) boids.get(i);
      float d = loc.dist(other.loc);
      if ((d &gt; 0) &amp;&amp; (d &lt; neighbordist)) {
        sum.add(other.loc); // Add location
        count++;
      }
    }
    if (count &gt; 0) {
      sum.div((float)count);
      return steer(sum,false);  // Steer towards the location
    }
    return sum;
  }
}

// The Flock (a list of Boid objects)

class Flock {
  ArrayList boids; // An arraylist for all the boids

  Flock() {
    boids = new ArrayList(); // Initialize the arraylist
  }

  public void run() {
    for (int i = 0; i &lt; boids.size(); i++) {
      Boid b = (Boid) boids.get(i);  
      b.run(boids);  // Passing the entire list of boids to each boid individually
    }
  }

  public void addBoid(Boid b) {
    boids.add(b);
  }

}

  public int sketchWidth() { return 1024; }
  public int sketchHeight() { return 768; }
  public String sketchRenderer() { return OPENGL; }
}<span class="Apple-style-span" style="font-family: monospace;">