We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello Processing Community
I am new to the forum and I am also a complete rookie when it comes to coding but have been studying some processing books and tutorials in my spare time and am loving every second of it. That being said, I apologize for the confusing explanation/question that follows.
I am attempting to make a separation steering behavior that not only reacts to particles in the system but also reacts to trails left by the particles themselves. Even though I am not entirely satisfied with the behavior yet, I am working on improving it. Yet, my question is not regarding the behavior itself, but instead it is about how the particles are detecting the edges. It seems like the particles are detecting black pixels beyond the canvas and therefore are steering away from the edges which is not what I intend them to do. I would like the particles to ignore the edges entirely and go beyond the extents of the canvas. Any input on how to fix this or why this is happening would be greatly appreciated.
int num =50;
int d = 0;
ArrayList<Particle> plist = new ArrayList<Particle>();
void setup(){
size(1400,900);
background(240);
strokeWeight(1);
//fill(0);
//ellipse(width/2,height/2,200,200);
for (int i= 0; i<num; i++){
plist.add(new Particle(random(width),random(height)));
}
}
void draw(){
for (Particle p: plist){
p.run();
}
}
class Particle{
PVector location;
PVector velocity;
PVector acceleration;
PVector pastloc;
float maxspeed = 2;
float maxforce =.1;
int range = 50;
float ran = .1;
Particle(float x, float y){
location = new PVector (x,y);
velocity = new PVector (0,0);
acceleration = new PVector (0,0);
pastloc = new PVector(location.x,location.y);
}
void separate(){
loadPixels();
PVector black;
PVector sum = new PVector();
int count = 0;
for(int i=-range;i<range;i++){
for(int j=-range;j<range;j++){
int x = round(location.x)+j;
int y = round(location.y)+i;
color c = get(x,y);
black = new PVector (x,y);
float d = PVector.dist(location,black);
if ((brightness(c)<10)&&(d>10)){
count++;
PVector diff = PVector.sub(location,black);
diff.normalize();
diff.div(d);
sum.add(diff);
}
}
}
println(count);
if (count > 0){
sum.setMag(maxspeed);
PVector steer = PVector.sub(sum,velocity);
steer.limit(maxforce);
applyForce(steer);
}
}
void applyForce(PVector steer){
acceleration.add(steer);
}
void run(){
separate();
update();
display();
}
void display(){
line(location.x,location.y,pastloc.x,pastloc.y);
pastloc.x = location.x;
pastloc.y = location.y;
}
void update(){
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
}