controlP5 acting up

Here is a simple problem, but I can't seem to figure out why the sliders won't adjust the values of the recursion. Thanks in advance.

//hexagonal fractal
import controlP5.*;
ControlP5 cp5;

int radius = 400;
int num  = 4;

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

  smooth();
  noFill();
  noLoop();
  frameRate(2);

  cp5.addSlider("num")
    .setRange(0, 6)
    .setPosition(100, 100)
    .setSize(10, 100)
    .setNumberOfTickMarks(5)
    ;

  cp5.addSlider("radius")
    .setRange(150,450)
    .setPosition(0, 100)
    .setSize(10, 100)
    .setNumberOfTickMarks(5)
    ;
}

void draw() {
  translate(width/2, height/2);
  drawHexagon(radius, num);
}

void drawHexagon(float radius, int num ) {
  stroke(255);
  float x, y = 0;
  beginShape();
  for (int i = 0; i < 6; i++) {
    x = cos(radians(60*i))*radius;
    y = sin(radians(60*i))*radius;
    vertex(x, y);
  }
  endShape(CLOSE);
  //////////////////////////////////////////////////////////////

  if (num-- > 1) {

    pushMatrix();
    translate(radius/2, 0);
    drawHexagon(radius/2, num);
    popMatrix();

    pushMatrix();
    translate(-radius/4, -radius*cos(radians(30))/2);
    drawHexagon(radius/2, num);
    popMatrix();

    pushMatrix();
    translate(-radius/4, radius*cos(radians(30))/2);
    drawHexagon(radius/2, num);
    popMatrix();
  }
}

Answers

  • edited July 2016

    I've removed setup()'s noLoop() and added resetMatrix() at the end of draw() and it worked! >-)

  • P.S.: Don't forget background() at the top of draw()! L-)

  • Excellent! Thank you. I am not sure why that interfered with the controlP5, but works for me.

  • edited July 2016 Answer ✓

    controlP5 needs draw() in order to update its controls. So noLoop() would halt it.
    Unless we have other means from where to invoke redraw() like mouseMoved() or mouseDragged().

    And it's obvious any coordinate transformations like translate(), rotate(), etc. are messing up controlP5's controls. Either we resetMatrix() or have a separate PGraphics in order to apply those transformations.

  • Ok this makes sense. Thanks!

  • This was really helpful, thank you!

Sign In or Register to comment.