Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
strarch
strarch's Profile
1
Posts
1
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Swarm _ random size of particles
[3 Replies]
01-Mar-2013 08:30 AM
Forum:
Contributed Library Questions
Hello everyone
I am new here at this forum and quite new user of processing..
I've managed to make the code following to work but trying to get
randomly sized agents of the swarm, I got this weird constantly changing result..
Tryed to define it as a global variable but obviously didnt work..
You can find the crucial part of the code in the class tab (particle) on the void display();!
any ideas?
import toxi.geom.*;
import peasy.*;
import controlP5.*;
import processing.opengl.*;
ControlP5 cp5;
ControlWindow controlWindow;
float numParticles = 100;
float cohesionDistance = 10;
float cohesionForce = 0.00005;
float separationDistance = 20;
float separationForce = 0.001;
float lineDistance = 1;
int resX = 100;
int resY = 100;
int resZ = 5;
PeasyCam cam;
float maxSpeed = 0.5;
ArrayList arrayOfParticles = new ArrayList();
//===================================================================================
void setup (){
size (800,800, P3D);
cam = new PeasyCam(this,100);
cp5 = new ControlP5(this);
controlWindow = cp5.addControlWindow("controlP5window",100,100,400,400).hideCoordinates();
Slider s01 = cp5.addSlider("numParticles", 0,200, 20,20, 200,29);
s01.setWindow(controlWindow);
Slider s02 = cp5.addSlider("cohesionForce",0.01,1, 20,100, 200,29);
s02.setWindow(controlWindow);
Slider s03 = cp5.addSlider("cohesionDistance",0,100, 20,140, 200,29);
s03.setWindow(controlWindow);
Slider s04 = cp5.addSlider("separationDistance", 0,100, 20,180, 200,29);
s04.setWindow(controlWindow);
Slider s05 = cp5.addSlider("separationForce", 0.01,1, 20,220, 200,29);
s05.setWindow(controlWindow);
Slider s06 = cp5.addSlider("lineDistance", 1,100, 20,260, 200,29);
s06.setWindow(controlWindow);
createParticles();
}
//===================================================================================
void draw () {
smooth ();
background(0);
noFill ();
stroke (30,30,30);
box (resX, resY , resZ);
strokeWeight (4);
runParticles();
}
//===================================================================================
void createParticles(){
for(int i=0; i<numParticles; i++){
//the initial positions of the particles
Vec3D pos = new Vec3D(random(-resX/2,resX/2), random(-resY/2,resY/2), random(-resZ/2,resZ/2));
Particle p = new Particle(pos);
arrayOfParticles.add(p);
}
}
//===================================================================================
void runParticles(){
for(int i=0; i<arrayOfParticles.size(); i++){
Particle p = (Particle) arrayOfParticles.get(i);
p.run();
}
}
//===================================================================================
//the class!!!!
class Particle {
Vec3D loc = new Vec3D();
Vec3D vel = new Vec3D();
Vec3D acc = new Vec3D();
Particle (Vec3D _loc){
vel = new Vec3D(random(-1,1), random(-1,1), random(-1,1));
loc = _loc;
}
void run(){
display();
update();
boundary();
lineBetweenParticles();
cohesion();
separation();
//functions
}
//===================================================================================
//===================================================================================
void display(){
stroke (255);
strokeWeight (random(0.1,4));
point(loc.x, loc.y, loc.z);
}
//===================================================================================
void update(){
vel.addSelf(acc);
vel.limit(maxSpeed);
loc.addSelf(vel);
acc.clear();
}
//===================================================================================
void cohesion(){
Vec3D forces = new Vec3D();
int countNeighbours = 0;
for (int i=0; i<arrayOfParticles.size(); i++){
Particle p = (Particle) arrayOfParticles.get(i);
float distance = p.loc.distanceTo(loc);
if (distance<cohesionDistance){
forces.addSelf(p.loc);
countNeighbours++;
}
}
forces.scaleSelf(1f/countNeighbours);
forces = forces.sub(loc);
forces.normalize();
forces.scaleSelf(cohesionForce);
acc.addSelf(forces);
}
//===================================================================================
void separation(){
Vec3D forces = new Vec3D();
int countNeighbours = 0;
for(int i=0; i<arrayOfParticles.size(); i++){
Particle p = (Particle) arrayOfParticles.get(i);
float distance = p.loc.distanceTo(loc);
if (distance<separationDistance){
forces.addSelf(p.loc);
countNeighbours++;
}
}
forces.scaleSelf(1f/countNeighbours);
forces = forces.sub(loc);
forces.normalize();
forces.scaleSelf(-separationForce);
acc.addSelf(forces);
}
void boundary() {
if (loc.x>resX/2){
vel.x = -vel.x;
loc.x = resX/2;
}
if (loc.y>resY/2){
vel.y = -vel.y;
loc.y = resY/2;
}
if (loc.z>resZ/2){
vel.z = -vel.z;
loc.z = resZ/2;
}
if (loc.x<-resX/2){
vel.x = -vel.x;
loc.x = -resX/2;
}
if (loc.y<-resY/2){
vel.y = -vel.y;
loc.y = -resY/2;
}
if (loc.z<-resZ/2){
vel.z = -vel.z;
loc.z = -resZ/2;
}
}
void lineBetweenParticles(){
for(int i=0; i<arrayOfParticles.size(); i++){
Particle p = (Particle) arrayOfParticles.get(i);
float distance = p.loc.distanceTo(loc);
if (distance<lineDistance){
stroke(0,0,255,60);
strokeWeight(2);
line (loc.x, loc.y, loc.z, p.loc.x, p.loc.y, p.loc.z);
}
}
}
}
«Prev
Next »
Moderate user : strarch
Forum