We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone. I have been playing with the Attraction 2D example as part of the verletphysics library. I currently have a situation where a number of particles arrange themselves at a random point on the screen. I would like to start colouring these agents . The agents closes to the centre of the arrangement should be red whilst the outer agents should be blue to give the impression of temperature. I have tried to use the map function to achieve this but I'm having trouble. Here's what I've got:
import toxi.geom.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
int countNeighbours=0;
int NUM_PARTICLES = 50;
VerletPhysics2D physics;
AttractionBehavior mouseAttractor;
Vec2D mousePos;
Vec2D target;
Vec2D randomTarget;
void setup() {
size(600, 600, P3D);
// setup physics with 10% drag
physics = new VerletPhysics2D();
physics.setDrag(0.05f);
physics.setWorldBounds(new Rect(0, 0, width, height));
// the NEW way to add gravity to the simulation, using behaviors
//physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.15f)));
randomTarget = new Vec2D(random(0, width), random(0, height));
// create a new positive attraction force field around the mouse position (radius=250px)
mouseAttractor = new AttractionBehavior(randomTarget, width, .9f);
physics.addBehavior(mouseAttractor);
}
void addParticle() {
VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(5).addSelf(width / 2, height/2));
physics.addParticle(p);
// add a negative attraction force field around the new particle
physics.addBehavior(new AttractionBehavior(p, 20, -1.2f, 0.01f));
}
void draw() {
background(255);
noStroke();
println(countNeighbours);
fill(0);
if (physics.particles.size() < NUM_PARTICLES) {
addParticle();
}
physics.update();
for (VerletParticle2D p : physics.particles) {
ellipse(p.x, p.y, 5, 5);
}
}
void mousePressed() {
mousePos = new Vec2D(mouseX, mouseY);
target = new Vec2D(300, 300);
randomTarget = new Vec2D(random(0, width), random(0, height));
// create a new positive attraction force field around the mouse position (radius=250px)
mouseAttractor = new AttractionBehavior(randomTarget, width, .9f);
physics.addBehavior(mouseAttractor);
}
//void mouseDragged() {
// // update mouse attraction focal point
// mousePos.set(mouseX, mouseY);
//}
//
void mouseReleased() {
// remove the mouse attraction when button has been released
mouseAttractor = new AttractionBehavior(mousePos, width, 0f);
physics.addBehavior(mouseAttractor);
}