3d flock
in
Contributed Library Questions
•
1 year ago
I'm trying to make 3d flock, but in way that i have 2d flock with particles and than move them in the Z axis, but still swarm of my particle should be in the 2d.
this is my sketch.
import peasy.test.*;
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
import toxi.geom.*;
// DECLARE
PeasyCam jCam;
ArrayList ballCollection;
void setup() {
size(600, 600,P3D);
smooth();
jCam = new PeasyCam (this,200);
//INITILAZOTOR
ballCollection = new ArrayList();
for (int i = 0 ; i < 50; i++) {
Vec3D origin = new Vec3D(random(width), random(200),0);
Ball myBall = new Ball(origin);
ballCollection.add(myBall);
}
}
void draw() {
background(0);
rectMode(CORNER);
stroke(255);
strokeWeight(1);
noFill();
rect(0,0,width,height);
pushMatrix();
translate(300,300,300);
noFill();
stroke(0,255,200);
strokeWeight(1);
box(600,600,600);
popMatrix();
//CALL FUNKCTIONALITY
stroke(1);
fill(255);
for (int i = 0; i < ballCollection.size(); i++) {
Ball myBall = (Ball) ballCollection.get(i);
myBall.run();
}
}
class Ball {
// GLOBAL VARIABLES
Vec3D loc= new Vec3D (0,0,300);
Vec3D speed = new Vec3D (random(-2, 2), random(-4, 4), random(-4, 4));
Vec3D acc = new Vec3D ();
Vec3D grav = new Vec3D (0, 0, 2);
// CONSTRUCOTOR
Ball(Vec3D _loc) {
loc = _loc;
}
// FUNKCTIONS
void run () {
display();
move();
bounce();
//gravity();
lineBetween();
flock();
}
void flock() {
separate(5);
cohesion(0.001);
align(0.1);
}
void align (float magnitude) {
Vec3D steer = new Vec3D ();
int count = 0;
for ( int i = 0; i < ballCollection.size(); i ++) {
Ball other = (Ball) ballCollection.get(i);
float distance = loc.distanceTo(other.loc);
if ( distance > 0 && distance < 40) {
steer.addSelf(other.speed);
count++;
}
}
if (count > 0) {
steer.scaleSelf(1.0/count);
}
steer.scaleSelf(magnitude);
acc.addSelf(steer);
}
void cohesion (float magnitude) {
Vec3D sum = new Vec3D ();
int count = 0;
for ( int i = 0; i < ballCollection.size(); i ++) {
Ball other = (Ball) ballCollection.get(i);
float distance = loc.distanceTo(other.loc);
if ( distance > 0 && distance < 20) {
Vec3D diff = loc.sub(other.loc);
diff.normalizeTo(1.0/distance);
sum.addSelf(other.loc);
count++;
}
}
if (count > 0) {
sum.scaleSelf(1.0/count);
}
Vec3D steer = sum.sub(loc);
steer.scaleSelf(magnitude);
acc.addSelf(steer);
}
void separate (float magnitude) {
Vec3D steer = new Vec3D ();
int count = 0;
for ( int i = 0; i < ballCollection.size(); i ++) {
Ball other = (Ball) ballCollection.get(i);
float distance = loc.distanceTo(other.loc);
if ( distance > 0 && distance < 220) {
Vec3D diff = loc.sub(other.loc);
diff.normalizeTo(1.0/distance);
steer.addSelf(diff);
count++;
}
}
if (count > 0) {
steer.scaleSelf(1.0/count);
}
steer.scaleSelf(magnitude);
acc.addSelf(steer);
}
void lineBetween () {
for ( int i = 0; i < ballCollection.size(); i ++) {
Ball other = (Ball) ballCollection.get(i);
float distance = loc.distanceTo(other.loc);
if ( distance > 0 && distance < 200) {
stroke(255, 0, 0);
strokeWeight(0.4);
line(loc.x, loc.y, other.loc.x, other. loc.y);
}
}
}
void bounce() {
if (loc.x > width) {
speed.x = speed.x * -1;
}
if (loc.x < 0) {
speed.x = speed.x * -1;
}
if (loc.y > height) {
speed.y = speed.y * -1;
}
if (loc.y < 0) {
speed.y = speed.y * -1;
}
}
void move() {
speed.addSelf(acc);
speed.limit(2);
loc.addSelf(speed);
acc.clear();
}
void display () {
ellipse (loc.x, loc.y, 20, 20);
stroke(0, 255,200);
}
}
1