Update slider value
in
Contributed Library Questions
•
4 months ago
Hi,
Im trying to add a slider to change the number of fish (Slider called nFish and int nFish).
I think i might have done something wrong and something is not being drawn?
From JavaScript i know event listeners but what to do in Processing?
- /* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/40632*@* */
- /* !do not delete the line above, required for linking your tweak if you re-upload */
- // code by Claus Rytter Bruun de Neergaard, March 2011
- // (use of PVector learned from Daniel Shiffman tutorial)
- // Kinect Tracker by Daniel Shiffman
- import controlP5.*;
- import org.openkinect.*;
- import org.openkinect.processing.*;
- // GLOBAL VARIABLES ///////////////////////////////
- KinectTracker tracker; // kinect tracker
- ControlP5 cp5; // control library
- Kinect kinect; // kinect
- int nFish = 4000; // number of agents/fish
- float s = 7.0; // maximum agent velocity
- int count = 0;
- float deg = 15; // start at 15 degrees
- int bgCol = 0; // background color for canvas for slider
- int fishCol = 255; // fish color for slider
- Agent[] agents = new Agent[nFish];
- // SETUP //////////////////////////////////////////
- void setup() {
- size(640,520);
- smooth();
- kinect = new Kinect(this);
- tracker = new KinectTracker();
- kinect.tilt(deg);
- noFill();
- stroke(fishCol);
- for (int i = 0; i < agents.length; i++) {
- agents[i] = new Agent();
- }
- // SLIDER FISH
- cp5 = new ControlP5(this);
- cp5.addSlider("nFish")
- .setPosition(20,10)
- .setRange(0,5000)
- ;
- // SLIDER BG COLOR
- cp5 = new ControlP5(this);
- cp5.addSlider("bgCol")
- .setPosition(20,20)
- .setRange(0,255)
- ;
- // SLIDER FISH COLOR
- cp5 = new ControlP5(this);
- cp5.addSlider("fishCol")
- .setPosition(20,30)
- .setRange(255,0)
- ;
- }
- // DRAW ///////////////////////////////////////////
- void draw() {
- // SLIDER DRAW! IMPORTANT!
- background(bgCol);
- stroke(fishCol);
- // Run the tracking analysis
- tracker.track();
- // Show image
- // tracker.display();
- // Avr track position
- PVector v2 = tracker.getLerpedPos(); // Get position
- fill(100,250,50,200); // Fill ellipse
- ellipse(v2.x,v2.y,30,30); // Get ellipse
- // calling functions of all of the objects in the array
- for (int i = 0; i < agents.length; i++) {
- agents[i].update();
- agents[i].edges();
- agents[i].display();
- }
- }
- // Kinect up and down. Arrow control.
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP) {
- deg++;
- }
- else if (keyCode == DOWN) {
- deg--;
- }
- deg = constrain(deg,0,30);
- kinect.tilt(deg);
- }
- }
- // CLASS //////////////////////////////////////////
- class Agent {
- PVector loc;
- PVector vel;
- PVector acc;
- float speed;
- Agent() {
- loc = new PVector(random(0, width), random(0, height));
- vel = new PVector(0, 0);
- speed = s;
- }
- void update() {
- PVector v2 = tracker.getLerpedPos();
- PVector track = new PVector(v2.x, v2.y);
- PVector dir = PVector.sub(track, loc);
- dir.normalize();
- dir.mult(0.15);
- acc = dir;
- vel.add(acc);
- vel.limit(speed);
- loc.add(vel);
- }
- void edges() {
- if (loc.x > width) {
- loc.x = 0;
- } else if (loc.x < 0) {
- loc.x = width;
- }
- if (loc.y > height) {
- loc.y = 0;
- } else if (loc.y < 0) {
- loc.y = height;
- }
- }
- void display() {
- point(loc.x, loc.y);
- }
- }
- void stop() {
- tracker.quit();
- super.stop();
- }
1