Shooting stars along a random point ellipse

edited December 2015 in Questions about Code

Hi guys I'm new to processing so this is may be a silly questions for the mayority here : ).

I would like to be able to draw a series of shooting star that starts and ends to a random point of a stroke circle (for example an ellipse of 200 radius), not inside of it. I started by using a code of Charlie McDowell that I find on open processing, in wich creates exactly the same effect that I wanted to create, but the stars are randomly drawned on the size of the screen. How can I exactly set the start and end point of the star on random point of the ellipse perimeter?

Here is the code:

/* OpenProcessing Tweak of @http://www.openprocessing.org/sketch/41149@ / / !do not delete the line above, required for linking your tweak if you upload again / / This program draws a starry sky with stars that twinkle. It also includes randomly occuring shooting stars. Author: Charlie McDowell */

// // the tail of the shooting star int[] shootX = new int[30]; int[] shootY = new int[30]; int METEOR_SIZE = 10; // initial size when it first appears float meteorSize = METEOR_SIZE; // size as it fades

// distance a shooting star moves each frame - varies with each new shooting star float ssDeltaX, ssDeltaY; // -1 indicates no shooting star, this is used to fade out the star int ssTimer = -1; // starting point of a new shooting star, picked randomly int startX,startY;

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

void draw() { background(0); // dark blue night sky
//
// draw the shooting star (if any) for (int i = 0; i < shootX.length-1; i++) { int shooterSize = max(0,int(meteorSizei/shootX.length)); // to get the tail to disappear need to switch to noStroke when it gets to 0 if (shooterSize > 0) { strokeWeight(shooterSize); stroke(255); } else noStroke(); //line(shootX[i], shootY[i], shootX[i+1], shootY[i+1]); ellipse(shootX[i], shootY[i], meteorSizei/shootX.length,meteorSizei/shootX.length); } meteorSize=0.9; // shrink the shooting star as it fades
// move the shooting star along it's path for (int i = 0; i < shootX.length-1; i++) { shootX[i] = shootX[i+1]; shootY[i] = shootY[i+1]; }
// add the new points into the shooting star as long as it hasn't burnt out if (ssTimer >= 0 && ssTimer < shootX.length) { shootX[shootX.length-1] = int(startX + ssDeltaX(ssTimer)); shootY[shootY.length-1] = int(startY + ssDeltaY(ssTimer)); ssTimer++; if (ssTimer >= shootX.length) { ssTimer = -1; // end the shooting star

} }

// create a new shooting star with some random probability if (random(5) < 1 && ssTimer == -1) { newShootingStar();
} }

/* Starts a new shooting star by randomly picking start and end point. */ void newShootingStar() { int endX, endY; startX = (int)random(width); startY = (int)random(height); endX = (int)random(width); endY = (int)random(height); ssDeltaX = (endX - startX)/(float)(shootX.length); ssDeltaY = (endY - startY)/(float)(shootY.length); ssTimer = 0; // starts the timer which ends when it reaches shootX.length meteorSize = METEOR_SIZE; // by filling the array with the start point all lines will essentially form a point initialy for (int i = 0; i < shootX.length; i++) { shootX[i] = startX; shootY[i] = startY; } }

Thank you for your patience.

Tagged:

Answers

  • please format the code.

    select it, hit ctrl-o.

  • Hi koogs! Sorry about that, here is the code :

    // the tail of the shooting star
    int[] shootX = new int[50];
    int[] shootY = new int[50];
    int METEOR_SIZE = 10; // initial size when it first appears
    float meteorSize = METEOR_SIZE; // size as it fades
    
    // distance a shooting star moves each frame - varies with each new shooting star
    float ssDeltaX, ssDeltaY; 
    // -1 indicates no shooting star, this is used to fade out the star
    int ssTimer = -1;
    // starting point of a new shooting star, picked randomly
    int startX,startY;
    
    
    void setup() {
      size(500,500);
      smooth();
    }
    
    void draw() {
      background(0); // dark blue night sky
    
      //
    
      // draw the shooting star (if any)
      for (int i = 0; i < shootX.length-1; i++) {
        int shooterSize = max(0,int(meteorSize*i/shootX.length));
        // to get the tail to disappear need to switch to noStroke when it gets to 0
        if (shooterSize > 0) {
          strokeWeight(shooterSize);
          stroke(255);
        }
        else
          noStroke();
        line(shootX[i], shootY[i], shootX[i+1], shootY[i+1]);
        //ellipse(shootX[i], shootY[i], meteorSize*i/shootX.length,meteorSize*i/shootX.length);
      }
      meteorSize*=0.9; // shrink the shooting star as it fades
    
      // move the shooting star along it's path
      for (int i = 0; i < shootX.length-1; i++) {
        shootX[i] = shootX[i+1];
        shootY[i] = shootY[i+1];
      }
    
      // add the new points into the shooting star as long as it hasn't burnt out
      if (ssTimer >= 0 && ssTimer < shootX.length) {
        shootX[shootX.length-1] = int(startX + ssDeltaX*(ssTimer));
        shootY[shootY.length-1] = int(startY + ssDeltaY*(ssTimer));
        ssTimer++;
        if (ssTimer >= shootX.length) {
          ssTimer = -1; // end the shooting star
    
    
        }
      }
    
      // create a new shooting star with some random probability
      if (random(5) < 1 && ssTimer == -1) {
        newShootingStar();
    
    
      }
    }
    
    /*
      Starts a new shooting star by randomly picking start and end point.
    */
    void newShootingStar() {
      int endX, endY;
      startX = (int)random(width);
      startY = (int)random(height);
      endX = (int)random(width);
      endY = (int)random(height);
      ssDeltaX = (endX - startX)/(float)(shootX.length);
      ssDeltaY = (endY - startY)/(float)(shootY.length);
      ssTimer = 0; // starts the timer which ends when it reaches shootX.length
      meteorSize = METEOR_SIZE;
      // by filling the array with the start point all lines will essentially form a point initialy
      for (int i = 0; i < shootX.length; i++) {
        shootX[i] = startX;
        shootY[i] = startY;
      }
    }
    
  • You should be able to use this example to accomplish what you want.

    void setup(){
      size(600,600);
      background(0);
      noFill();
    }
    
    void draw(){
      stroke(random(255),random(255),random(255));
      translate(random(width),random(height));
      rotate(random(TWO_PI));
    
      //ellipse(0,0,random(200),random(200));
    
      float start = random(-TWO_PI,0);
      float len = random(TWO_PI);
      //arc(0,0,random(200),random(200),0,TWO_PI);
    
      arc(0,0,random(200),random(200),start,start+len);
    
    }
    
  • Thanks ttguy44! I'm almost getting there. Here is a new code that I rewrite.

    float a = random(0.0, PI*2);
    float speed = random (0.008, 0.02);
    
    int startTime;
    int counter = 1;
    int number1;
    
    
    void setup() {
      size(500, 500);
      background(0);
      smooth();
    }
    
    void draw() {
    
      fill (0, 50);
      noStroke();
      rect (0, 0, width, height); 
      fill(255);
    
      float x =  width /2 + cos (a) * 200;
      float y = height /2 + sin (a) * 200;
    
      fill(255);
      ellipse(x, y, 5, 5);
      a = a + speed;
    
    
      if (counter < 10) {
        if (millis() > startTime + 1000) {
          startTime = millis(); 
          noLoop();
          fill(0);
          rect (0, 0, width, height); 
    
        }
      }
    }
    

    I manage to get a shooting star (ellipse) appear in a random radians value along an ellipse of 200 pixel radius. But I'm quite stuck ... :( I don't know how to simply fade the ellipse after for ex 1 sec, without putting a black rectangle that covered brutaly the shape, and then generating new random star position without using predictable arrays. Any tips? Thank you again!

  • you can use fill with a 2nd parameter for opacity

    over time decrease this value (as a var) from 255 to 0 I think

  • Thanks Chrisir! I find another trick by decrasing the width and height of the ellipse, but now I don't really undestand how can repeate this infinetly times. I know that is the basics, please a little hint?

    here is the code:

    float a = random(0.0, PI*2);
    float speed = random (0.00, 0.03);
    
    float radius = 2;
    float rMove = +1;
    float minRadius = 1, maxRadius = 10;
    
    int startTime;
    int counter = 1;
    
    
    void setup() {
      size(500, 500);
     // background(0);
      smooth();
    }
    
    void draw() {
    
      fill (0, 50);
      noStroke();
     rect (0, 0, width, height); 
    
      int r = 200;
      float x =  width /2 + cos (a) * r;
      float y = height /2 + sin (a) * r;
      a = a + speed;
      fill(255);
    
      ellipse(x, y, radius, radius);
      radius += rMove; 
      if (radius > maxRadius)
        rMove = -1;
      else if (radius < minRadius)
        rMove =0.0;
    }
    

    Thank you :)

  • last line:::::: rMove = 1.0;

  • I'm sorry I do not explained very well : /. I would like to create another ellipse with the same random radians probability of the first one. Having a star shooting one after another one. loooooooping over and over :D

  • do you see only one ellipse at a time or many?

  • when it's only one at a time: how long is it there? 360° ?

  • I see 1 ellipse at the time. The x and y of the ellipse are set to the width and height /2 + cos (a) * r (wich is 200 ); and (a) is a random value of (0.0, PI*2); and also the speed is a random value. So each time I run the sketch is a different type of shooting star wich is exactly what I want. But I would like to have that over and over without pressing play each time. This sound reeeally bad I know sorry, I 'm just new to it so I would like to undestand it better. Thank you Chrisir! :)

  • edited December 2015 Answer ✓

    puuuh

    ok, a is the angle, when a is > TWO_PI we want to start all over

    since you set all vars before setup() when declaring them, I advice you to

    have before setup() only float a; etc. etc.

    then have a new function reset where you saya = random(0.0, PI*2); etc. etc.

    without the float !!!!

    now call reset from setup() and when a is > TWO_PI

  • yeeeeesss! It is working now, thank you so much Chrisir! :)

Sign In or Register to comment.