How to Pre-Calculate to a Given frameCount Value During Compile

I'm making an animation and would like to have a way to preview my work without having to start at the very beginning of the animation every single time. The animations don't have any user interaction and are basically just a linear set of instructions. Is there a way to calculate the state of things according to a given frameCount during compile before running?

Answers

  • Use another variable.

    Set it to framecount at top of draw when running properly, set it to required value in setup and increment it at bottom of draw when running in debug mode.

  • Adding on to my original post, say I have the following code:

    void setup() {
      background(255);
      size(640, 480);
      strokeWeight(0);
      fill(128);
      frameCount = 35;
    }
    
    
    void draw() {
      if (frameCount == 30) {
        rect(30, 100, 30, 30);
      }
    }
    

    When I run the code, I would want to see a gray rectangle.

  • Well it's 35 in setup but you're checking for 30 in draw....

    I'd think framecount is incremented before draw is called. There's probably nothing stopping you modifying framecount yourself, except common sense... 8)

  • Thanks koogs, but the animation isn't entirely driven by the frameCount value. My example was probably a bit oversimplified. The animation includes processes set in motion in the draw() function, so the following code would be more accurate:

    float lineAAngle = 0;
    float lineBAngle = 0;
    
    void setup() {
      size(640, 480);
      strokeWeight(1);
      frameCount = 80;
    }
    
    
    void draw() {
      background(255);
      if (frameCount > 30) {
        line(width/2, height/2, (width/2) + (100 *sin(lineAAngle)), (height/2) - (100 *cos(lineAAngle)));
        lineAAngle = lineAAngle + radians(1);
      }
      if (frameCount > 70) {
        line(width/2, height/2, (width/2) + (100 *sin(lineBAngle)), (height/2) - (100 *cos(lineBAngle)));
        lineBAngle = lineBAngle + radians(1);
      }
    }
    

    So starting with frameCount at 80 shows the lines in the same position, but if the sketch had started with a frameCount of 0, they would show in different positions. I'd like a way to compile the sketch as if it had been started from frameCount = 0, but start playback at frameCount = 80.

  • edited September 2016 Answer ✓

    I would try to become independent of the frameCount-variable. One way would be to move all the animation-code into a seperate function and call it once inside of draw. Here is an example:

    int progress =0;
    int xPos = 10;
    
    void setup() {
      size(200, 200);
      // animation(150);
    }
    
    void draw() {
      animation(1);
    }
    
    
    void animation(int steps) {
    
      // repeat this block as often as you like
      for (int i =0; i<steps; i++) {
    
        background(0);
        if (progress > 30) {
          ellipse(xPos, 30, 20, 20);
          xPos++;
        }
    
        progress++;
      }
    }
    

    As you will notice, i used the variable "progress" instead of frameCount here. The function "animation" contains everything that you would normally do inside of draw. To skip a part of the animation you can call it with the number of steps to be skipped as an argument inside of setup();

  • Thanks, benja! That's exactly what I was looking for.

  • I don't see how that helps when you've got cumulative global variables like lineAAngle

  • edited September 2016 Answer ✓

    There is a simpler option: just use a for loop to call the draw() function a bunch of times at the end of the setup() function.

    Consider this little program that draws a moving circle that resets to a random position when it goes off the window:

    float x = 0;
    float y = 0;
    
    void setup() {
      size(500, 500);
    }
    
    void draw() {
      ellipse(x, y, 10, 10);
      x+=5;
      y+=5;
    
      if (x > width) {
        x = 0;
        y = random(height);
      }
    
      if (y > height) {
        x = random(width);
        y = 0;
      }
    }
    

    The result of all this logic isn't directly dependent on the frameCount variable, but it is dependent on running the draw() function over and over again. So if I want to skip over the first 1000 frames, I can just do that directly:

    float x = 0;
    float y = 0;
    
    int START_FRAME = 1000;
    
    void setup() {
      size(500, 500);
    
      for (int i = 0; i < START_FRAME; i++) {
        draw();
        frameCount++;
      }
    }
    
    void draw() {
      ellipse(x, y, 10, 10);
      x+=5;
      y+=5;
    
      if (x > width) {
        x = 0;
        y = random(height);
      }
    
      if (y > height) {
        x = random(width);
        y = 0;
      }
    }
    

    Note that I'm incrementing the frameCount variable, but that's more a "just in case" thing. Now if I just want to start at the beginning, I can just change START_FRAME to 0 and it'll skip my "fast forward" for loop.

    You could simplify the loop a bit and use the frameCount variable directly:

      for (frameCount = 0; frameCount < START_FRAME; frameCount++) {
        draw();
      }
    
Sign In or Register to comment.