We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, how is it going?
I`m trying to do the exercise 2.8 from the book "Nature of Code":
"In the example above, we have a system (i.e. array) of Mover objects and one Attractor object. Build an example that has systems of both movers and attractors. What if you make the attractors invisible? Can you create a pattern/design from the trails of objects moving around attractors? See the Metropop Denim project by Clayton Cubitt and Tom Carden for an example."
Still not a system of attractors, it only has 1, invisible. About creating a pattern/design fromt he trails, thats the problem. Using the example gived by TFGuy44, posted here https://forum.processing.org/two/discussion/15386/random-walkers-with-tails/p1 I could adapt both codes to make the circles from the exercise have a trail, the problem is the trail is the same color, same size of the circle its related to. How can I make it, for example, fade and get smaller with time before it gets removed from the list?
This is as far as I could get
MOVER CLASS (where the trail is implemented)
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
int cor, tam_Trail;
ArrayList<PVector> trail;
Mover(float m, float x, float y) {
mass = m;
location = new PVector(random(width), random(height));
velocity = new PVector(1, 0);
acceleration = new PVector(0, 0);
cor = 140;
trail = new ArrayList();
tam_Trail = 30;
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update() {
if (trail.size() > tam_Trail) trail.remove(0);
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
trail.add(new PVector(location.x, location.y));
}
void display() {
noStroke();
fill(cor, 160);
ellipse(location.x, location.y, mass*25, mass*25);
for (int i = 0; i<trail.size ()-1; i++) {
ellipse(trail.get(i).x, trail.get(i).y, mass*25, mass*25);
}
}
float retornaTamanho(){
return mass * 25;
}
}
** MAIN TAB**
int numero = 10;
Mover[] movers = new Mover[numero];
Attractor a;
void setup() {
size(800, 800);
smooth();
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(0.1, 2), random(width), random(height));
}
a = new Attractor();
}
void draw() {
background(255);
a.display();
a.drag();
a.hover(mouseX, mouseY);
for (int i = 0; i < movers.length; i++) {
PVector force = a.attract(movers[i]);
movers[i].applyForce(force);
movers[i].update();
movers[i].display();
}
}
void mousePressed() {
a.clicked(mouseX, mouseY);
}
void mouseReleased() {
a.stopDragging();
}
ATTRACTOR CLASS
// Attraction
// Daniel Shiffman <http://www.shiffman.net>
// A class for a draggable attractive body in our world
class Attractor {
float mass; // Mass, tied to size
float G; // Gravitational Constant
PVector location; // Location
boolean dragging = false; // Is the object being dragged?
boolean rollover = false; // Is the mouse over the ellipse?
PVector dragOffset; // holds the offset for when object is clicked on
Attractor() {
location = new PVector(width/2,height/2);
mass = 20;
G = 1;
dragOffset = new PVector(0.0,0.0);
}
PVector attract(Mover m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float d = force.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
// Method to display
void display() {
ellipseMode(CENTER);
//deixar invisivel
noStroke();
noFill();
ellipse(location.x,location.y,mass*2,mass*2);
}
// The methods below are for mouse interaction
void clicked(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
dragging = true;
dragOffset.x = location.x-mx;
dragOffset.y = location.y-my;
}
}
void hover(int mx, int my) {
float d = dist(mx,my,location.x,location.y);
if (d < mass) {
rollover = true;
}
else {
rollover = false;
}
}
void stopDragging() {
dragging = false;
}
void drag() {
if (dragging) {
location.x = mouseX + dragOffset.x;
location.y = mouseY + dragOffset.y;
}
}
}