Falling snowflakes

edited February 2015 in Questions about Code

Hi Forum!

I've been trying to code a program where I have 12 different .svg snowflakes and am trying to get them to "snow" and rotate. The plan is to get them to start above the canvas and fall at a speed depending on the size/scale that will be randomized as well as rotating each flake. So far I've honesty dont have much.

    PShape[] flake_shapes;

    float[] x;
    float[] y;
    float[] size;
    float[] spin;
    float[] spinspeed;

    int n = 100;

    void setup()
    {
      size( 500, 500 );


      x = new float[ n];
      y = new float[ n];
      size = new float[ n];
      spin = new float[ n];
      spinspeed = new float[ n];

      flake_shapes = new PShape[ 12 ];
      for ( int idx = 0; idx < 12; ++idx ) {
        flake_shapes[idx] = 
          loadShape( "snowflake" + (idx+1) + ".svg" );
      }

      frameRate(6);
    }

    void draw()
    {
      background( 200, 200, 255 );

      updateFlakes();
      //drawFlakes();
    }

    void updateFlakes() {

      for ( int i = 0; i < n; i++ ) {
        x[ i] = random( 0, width);
        y[ i] = random( 0, - 100);
        size [ i] = random( 0, 1);
        spin[ i] = random( 0, 15);
        spinspeed[ i] = random( 0, 2);

        // Just a quick test to draw anything at all. 
        for ( int idx = 0; i < 12; i++) {
          shape( flake_shapes[i], 0, 0 );
        }
      }
    }

    void drawFlakes() {

      // Just a quick test to draw anything at all. 
      for ( int i = 0; i < 12; i++) {
        shape( flake_shapes[i], 0, 0 );
      }
    }

and I'm wondering if you guys got any tips/hints?

Thanks

Comments

  • here

    PShape[] flake_shapes;
    
    float[] x;
    float[] y;
    float[] size;
    float[] spin;
    float[] spinspeed;
    
    int n = 100;
    
    void setup()
    {
      size( 500, 500 );
    
    
      x = new float[ n];
      y = new float[ n];
      size = new float[ n];
      spin = new float[ n];
      spinspeed = new float[ n];
    
      flake_shapes = new PShape[ 12 ];
      for ( int idx = 0; idx < 12; ++idx ) {
        //    flake_shapes[idx] = 
        //      loadShape( "snowflake" + (idx+1) + ".svg" );
        x[idx]= random( 0, width);
        y[idx] = random( -10, 0);
        size [ idx] = random( 1, 4);
      }
    
      frameRate(18);
    }
    
    void draw()
    {
      background( 200, 200, 255 );
    
      updateFlakes();
      drawFlakes();
    }
    
    void updateFlakes() {
    
      for ( int i = 0; i < n; i++ ) {
        x[ i] += random( -1, 1);
        y[ i] ++ ; // = random( 0, - 100);
        // size [ i] = random( 0, 1);
        spin[ i] = random( 0, 15);
        spinspeed[ i] = random( 0, 2);
    
        // Just a quick test to draw anything at all. 
        //    for ( int idx = 0; i < 12; i++) {
        //      //  shape( flake_shapes[i], 0, 0 );
        //      rect (x[idx], y[idx], 3, 3);
        //    }
      }
    }
    
    void drawFlakes() {
    
      // Just a quick test to draw anything at all. 
      for ( int i = 0; i < 12; i++) {
        // shape( flake_shapes[i], 0, 0 );
        rect (x[i], y[i], 3, 3);
      }
    }
    
  • you should define your snowflakes in setup()

    then you move them in update

    and draw them in drawFlakes() {

    • don't redefine them in update all the time

    btw

    random must have the smaller number first, so it's random (-100,0) and not random( 0, - 100);

    I couldn't use your shapes because I don't have them. But you how it's done from my sketch.

    ;-)

  • Hi Chris,

    Thanks for the reply, I took what you told me and slapped my image into it and it worked. The only problems Im having is that for scaling each flake I decided to put it in drawFlakes, above the shape but then the flakes stop "snowing".

    For rotating the flakes I believe I should be putting it into the update?

    Thanks

  • you always have to distinguish between setting values (eg. x[3]) and using them

    in general you set them in setup and use it in draw()

    and modify it in your update

    so for scaling: after line 62 pushMatrix();

    scale(size[i]);

    rect.......

    popMatrix();

  • for rotating see reference on rotate

    you rotate in drawflakes and change the rotation in update

    rotating is more complicate....

    do you have to?

    because to rotate you must rotate around the origin

    so it's like

    pushMatrix();

    translate (x[..,y[...);

    rotate(....

    shape(..., 0,0); // !!!!!! 0,0

    popMatrix();

    crazy

    ;-)

  • here

    PShape[] flake_shapes;
    
    float[] x;
    float[] y;
    float[] size;
    float[] spin;
    float[] spinspeed;
    
    int n = 100;
    
    void setup()
    {
      size( 500, 500 );
      // rectMode(CENTER);
    
      x = new float[ n];
      y = new float[ n];
      size = new float[ n];
      spin = new float[ n];
      spinspeed = new float[ n];
    
      flake_shapes = new PShape[ 12 ];
      for ( int idx = 0; idx < 12; ++idx ) {
        //    flake_shapes[idx] = 
        //      loadShape( "snowflake" + (idx+1) + ".svg" );
        x[idx]= random( 0, width);
        y[idx] = random( -30, 0);
        size [ idx] = random( 1, 14);
        spin[ idx] = random( 0, 1);
        spinspeed[ idx] = random( -.1, .1);
      }
    
      frameRate(18);
    }
    
    void draw()
    {
      background( 200, 200, 255 );
    
      updateFlakes();
      drawFlakes();
    }
    
    void updateFlakes() {
      for ( int i = 0; i < n; i++ ) {
        x[ i] += random( -1, 1);
        y[ i] ++ ;
        spin[ i] +=  spinspeed[ i] ;
      }
    }
    
    void drawFlakes() {
    
      // draw  
      for ( int i = 0; i < 12; i++) {
    
        pushMatrix(); // --------------------
        //  scale(size[i]);
        translate (x[i], y[i]);
        rotate(spin[ i]);
    
        //shape(..., 0,0); // !!!!!! 0,0
        // shape( flake_shapes[i], 0, 0 );
        float sizeHalf = size[i] / 2;
        rect (-sizeHalf, -sizeHalf, size[i], size[i]);
    
        popMatrix();
      }
    }
    
  • edited February 2015

    you got now 2 indpendent for loops

    you might as well have one for-loop in draw() and call both functions from there (and the functions don't have a for-loop)

    giving the index as parameter

    ;-)

Sign In or Register to comment.