yes, the arraylist ex & ey is filled at runtime, size is too big, but it gets clear after each run.
here's the complete code for your ref.
import geomerative.*;
import processing.video.*;
RShape grp;
RPoint pnts[];
boolean ignoringStyles = false;
Capture video;
ArrayList ex,ey;
Tracker tObj;
ArrayList particles;
ArrayList forces;
float G = 1;
boolean clear = true;
boolean renderForces = false;
Force mouseForce[];
int num;
boolean mouseAttract = false;
void setup()
{
size(500, 500, P3D);
frameRate(999);
num=width*height;
video = new Capture(this, width, height, 30);
prevFrame = createImage(video.width,video.height,RGB);
RG.init(this);
RG.ignoreStyles(ignoringStyles);
grp = RG.loadShape("for_processing4.svg");
grp.centerIn(g);
initz();
}
void initz()
{
ex=new ArrayList();
ey=new ArrayList();
particles = new ArrayList();
forces = new ArrayList();
pnts = grp.getPoints();
for (int i = 0; i <pnts.length; i++)
{
pnts[i].translate(width/2,height/2);
}
for(int i = 0; i<pnts.length; i++)
{
Particle p = new Particle(floor(pnts[i].x),floor(pnts[i].y));
particles.add(p);
}
mouseForce = new Force[num];
for (int i = 0; i <num; i++)
{
mouseForce[i] = new Force(new PVector(0, 0), 300, false, false);
}
forces.add(new Force(new PVector(0, 0), 200.0, false, true));
forces.add(new Force(new PVector(500, 0), 200.0, false, true));
forces.add(new Force(new PVector(500, 500), 200.0, false, true));
forces.add(new Force(new PVector(0, 500), 200.0, false, true));
}
void draw()
{
background(0);
if (video.available())
{
prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height);
prevFrame.updatePixels();
video.read();
}
tObj=new Tracker();
tObj.mnt();
if(c<10)
{
ex.clear();
ey.clear();
}
for(int i = particles.size()-1; i>=0; i--)
{
Particle p = (Particle) particles.get(i);
p.run();
}
call();
ex.clear();
ey.clear();
}
void call()
{
if(mousePressed)
{
Object oex[] = ex.toArray();
Object oey[] = ey.toArray();
for(int i=0;i<oex.length;i++)
{
int nex=((Integer)oex[i]).intValue();
int ney=((Integer)oey[i]).intValue();
mouseForce[i].pos.set(nex,ney,0);
mouseForce[i].forceOn = true;
}
}
}
/////////class Force//////////
class Force {
PVector pos;
float mass, multiplier;
boolean forceOn;
Force(PVector p, float ms, boolean attract, boolean on)
{
pos=p;
mass = ms;
if(attract)
{
multiplier = 1.0;
}
else
{
multiplier = -0.01;
}
forceOn = on;
}
PVector calculateForce(PVector pPos, float pMass)
{
PVector f = PVector.sub(pos, pPos);
float d = f.mag();
d = constrain(d, 20.0, 50000.0);
f.normalize();
float force = (G * mass * pMass) / (d * d);
f.mult(force * multiplier);
return f;
}
}
//////////class Particle/////////
class Particle {
PVector originalPos;
PVector currentPos;
PVector velocity;
PVector acceleration;
float mass;
color particleColor;
Particle(float xPos, float yPos)
{
originalPos = new PVector(xPos, yPos);
currentPos = new PVector(xPos, yPos);
velocity = new PVector(0.0, 0.0);
acceleration = new PVector(0.0, 0.0);
mass = 1;
particleColor = color(0,0,255);
}
void move()
{
acceleration = PVector.sub(originalPos, currentPos);
acceleration.mult(0.01); //0.001
for(int i = 0; i<ex.size(); i++)
{
if(mouseForce[i].forceOn)
{
acceleration.add(mouseForce[i].calculateForce(currentPos, mass));
}
}
for(int i = forces.size()-1; i>=0; i--)
{
Force f = (Force) forces.get(i);
if(f.forceOn)
acceleration.add(f.calculateForce(currentPos, mass));
}
velocity.add(acceleration);
velocity.mult(0.9); //0.99
currentPos.add(velocity);
if(currentPos.x > width-1)
{
currentPos.x = width-1;
velocity.x *= -1;
}
else if(currentPos.x < 0)
{
currentPos.x = 0;
velocity.x *= -1;
}
if(currentPos.y > height-1)
{
currentPos.y = height-1;
velocity.y *= -1;
}
else if(currentPos.y < 0)
{
currentPos.y = 0;
velocity.y *= -1;
}
}
void render()
{
int xPos = constrain(floor(currentPos.x), 0, width-1);
int yPos = constrain(floor(currentPos.y), 0, height-1);
stroke(particleColor);
strokeWeight(1);
point(xPos,yPos);
}
void run()
{
move();
render();
}
}
//////////class Tracker/////////
ArrayList ax,ay;
PImage prevFrame;
int x1=0;
int x2=599;
int y1=214;
int y2=385;
int c=0;
class Tracker
{
float threshold = 50;
Tracker()
{
c++;
ax=new ArrayList();
ay=new ArrayList();
}
void mnt()
{
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
for (int x = 0; x < video.width; x ++ )
{
for (int y = 0; y < video.height; y ++ )
{
int loc = x + y*video.width;
color current = video.pixels[loc];
color previous = prevFrame.pixels[loc];
float r1 = (current>> 16) & 0xFF;
float g1 = (current>> 8) & 0xFF;
float b1 = current & 0xFF;
float r2 = (previous>> 16) & 0xFF;
float g2 = (previous>> 8) & 0xFF;
float b2 = previous & 0xFF;
float diff = dist(r1,g1,b1,r2,g2,b2);
if (diff > threshold)
{
//If motion, fill green
pixels[loc] = color(0,255,0);
ax.add(x);
ay.add(y);
}
else
{
//If not, display actual color
pixels[loc] = current;
}
}
}
updatePixels();
//define fixed area
Object oax[] = ax.toArray();
Object oay[] = ay.toArray();
for(int i=0;i<oax.length;i++)
{
int nax=((Integer)oax[i]).intValue();
int nay=((Integer)oay[i]).intValue();
if((nax>=x1 && nax<=x2) && (nay>=y1 && nay<=y2))
{
ex.add(nax);
ey.add(nay);
}
}
ax.clear();
ay.clear();
}
}