How to connect sliders to attractors with Punktiert + ControlP5?

J_DJ_D
edited August 2015 in Library Questions

Hi, I'm working with Punktiert and ControlP5 to create a definition with a bunch of particles and attractors.

The definition below is working fine but I would like to add some sliders to change in real time the behavior of the attractors and their number. At the moment I'm stuck on this two issues:

1// How to connect the slider s01 to change automatically the number of attractors created? Since the attractors are created in the draw() function, I cannot update them again. But if I create the attractors in the draw() function Processing keeps creating new attractors every new frame.

2// How can I change the value of the radius for each attractor with the sliders s02, s03, s04? To change the slider radius I'm using " attr.setRadius(radii.get(i))" in a for loop in the draw() function. How can I relate the sliders to the radius values?

Below the code.

Thank you.

import punktiert.math.Vec;
import punktiert.physics.*;
import controlP5.*;

// World object
VPhysics physics;

int attractorsNum = 3;
int particlesNum = 100;

ArrayList<BAttraction> attractors;
FloatList attractorsX;
FloatList attractorsY;
FloatList radii;

void setup() {
  size(800, 800);
  smooth();
  ControlP5 cp5 = new ControlP5(this);

  // Set up physics
  physics = new VPhysics();
  physics.setfriction(0.4);

  attractors = new ArrayList<BAttraction>();
  attractorsX = new FloatList(); 
  attractorsY = new FloatList();
  radii = new FloatList();

  //Create radius values for attractors
  for (int i=0; i<attractorsNum; i++) {
    float radius = random(100, 500);
    radii.append(radius);
  } 

  //Slider to control attractor numbers and attractors radius
  Slider s01 = cp5.addSlider("attractorsNum", 1, 15, attractorsNum, 10, 10, 100, 10);
  Slider s02 = cp5.addSlider("radius01", 100, 500, radii.get(0), 10, 30, 100, 10);
  Slider s03 = cp5.addSlider("radius02", 100, 500, radii.get(1), 10, 50, 100, 10);
  Slider s04 = cp5.addSlider("radius03", 100, 500, radii.get(2), 10, 70, 100, 10);

  // Create attractor in random positions
  for (int i = 0; i<attractorsNum; i++) { 
    float x = random(0, width);
    float y = random(0, height);
    attractorsX.append(x);
    attractorsY.append(y);
    BAttraction attr = new BAttraction(new Vec(x, y), radii.get(i), 0.1); 
    attractors.add(attr);
    println(attr);
  }

  // Add attractors to world
  for (int i=0; i<attractors.size (); i++) {
    BAttraction attr = attractors.get(i); 
    physics.addBehavior(attr);
  }

  //Create particles
  for (int i = 1; i<particlesNum; i++) {
    // Set particle radius
    float rad = 5; 
    // Set position vector
    Vec pos = new Vec(random(0, width), random(0, height)); 
    // Create particle (position, mass, radius)   
    VParticle particle = new VParticle(pos, 1, rad); 
    // Add Collision Behavior
    particle.addBehavior(new BCollision()); 
    // Add particle to world
    physics.addParticle(particle);
  }
}

void draw() {
  background(0);
  // Update physics
  physics.update();

  // Draw particles
  fill(255);
  for (VParticle p : physics.particles) {    
    ellipse(p.x, p.y, p.getRadius()*2, p.getRadius()*2);
  }

  // Set attractors
  for (int i=0; i<attractors.size (); i++) {
    BAttraction attr = attractors.get(i); 
    float x = attractorsX.get(i); 
    float y = attractorsY.get(i); 
    attr.setAttractor(new Vec(x, y)); 

    //Set Radius for each attractor:
    attr.setRadius(radii.get(i));

    // Show influence radius
    noFill(); 
    stroke(255, 0, 0); 
    ellipse(attr.getAttractor().x, attr.getAttractor().y, attr.getRadius(), attr.getRadius());

    // Show attractor
    noStroke();
    fill(255, 0, 0);
    ellipse(attr.getAttractor().x, attr.getAttractor().y, 10, 10);
  }
}

Answers

Sign In or Register to comment.