I am using Verletphysics with Vec2D.
I also tried to use math operations in vectors. But I was not succesful.
I added the script below. If you run it, you'll see that the line does not change at all and points only in one direction.
The part that it is drawn is marked in bold.
- import toxi.physics2d.behaviors.*;
import toxi.physics2d.*;
import toxi.geom.*;
import toxi.math.*;
VerletPhysics2D physics;
Cluster cluster;
void setup() {
size(1280, 800);
smooth();
//INITIALIZE PHYSICS
physics=new VerletPhysics2D();
physics.setWorldBounds(new Rect(0,0,width,height));
physics.setDrag(0.1);
//INITIALIZE CLUSTER
cluster = new Cluster(10, 0.000001, 30);
}
void draw() {
//UPDATE PHYSICS
physics.update();
background(255);
cluster.display();
cluster.showConnections();
}
class Attractor extends VerletParticle2D {
float r;
Attractor(Vec2D loc) {
super(loc);
r = 10;
physics.addParticle(this);
physics.addBehavior(new AttractionBehavior(this, 500, 0.2));//RADIUS AND STRENGTH
}
void display () {
fill(100);
ellipse (x, y, r*2, r*2);
}
}
class Particle extends VerletParticle2D {
float r;
Particle(Vec2D loc) {
super(loc);
r=10;
physics.addParticle(this);
physics.addBehavior(new AttractionBehavior(this, r*4, -1));
}
void display() {
fill(0);
ellipse(x, y, r, r);
}
}
class Cluster {
ArrayList <Particle> particles;
ArrayList <Attractor> attractors = new ArrayList <Attractor>();
float diameter;
float springRadius = 40;
float springForce;
int gridDimension;
Cluster(float d, float sf, int gridDim) {
particles = new ArrayList<Particle>();
diameter = d;
springForce = sf;
gridDimension = gridDim;
int w = width/gridDim;
int h = height/gridDim;
//ARRAYLIST OF PARTICLES
for (int y = 0; y<gridDim; y++) {
for (int x = 0; x<gridDim; x++) {
particles.add(new Particle(new Vec2D( (x+0.5)*w, (y+0.5)*h )));
}
}
//INITIALIZE ATTRACTOR CLASS
attractors.add( new Attractor(new Vec2D(200, 200)));
attractors.add( new Attractor(new Vec2D(900, 500)));
//ARRAYLIST OF SPRINGS PHYSICS
for (int i = 0; i<particles.size()-1; i++) {
VerletParticle2D ni = particles.get(i);
for (int j = i+1; j<particles.size(); j++) {
VerletParticle2D nj = particles.get(j);
if (dist(ni.x, ni.y, nj.x, nj.y)<springRadius) {
//physics.addSpring(new VerletSpring2D(ni, nj, d, sf));//FIZIKA LINIJU SPRINGU, VISO SPRINGU YRA 3
}
}
}
}
void display() {
for (Attractor a: attractors) {
a.display();
for (Particle p: particles) {
p.display();
a.lock();
}
}
}
void showConnections() {
stroke(0);
strokeWeight(2);
for (int i = 0; i<particles.size()-1; i++) {
VerletParticle2D pi = (VerletParticle2D) particles.get(i);
for (int j = i+1; j<particles.size();j++) {
VerletParticle2D pj = (VerletParticle2D) particles.get(j);
if (dist(pi.x, pi.y, pj.x, pj.y)<springRadius) {
//line(pi.x, pi.y, pj.x, pj.y);
line(pi.x, pi.y, pi.x+10, pi.y+10);
noFill();
// ellipse(pi.x, pi.y,10,10);
}
}
}
}
}