It's winter but do you want fireworks? ?? Then implement it

It's winter! It's cold!

Since it's cold, why don't you want to see fireworks to feel warm?

So let's implement fireworks!

How to make fireworks

Preparation

Install Processing!

Processing is a programmable vector drawing tool. The contents are made of Java, and the grammar is also Java.

Create Particle class

That spark of fireworks? As part of, let's create a Particle class. Here is the function you want to implement.

--Sparks? I want you to draw a parabola and move. The laws of physics

For the time being, create a Particle class.

class Particle {
}

Next, we want the laws of physics, so we define the required members as position, velocity, and acceleration.

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;

  Particle (float xpos, float ypos, float xsp, float ysp) {
    this.location = new PVector(xpos, ypos); //Initial position
    this.velocity = new PVector(xsp, ysp); //Initial velocity
    this.acceleration = new PVector(0, 0.001); //Gravitational acceleration guy
  }
}

Now, next is the method to move according to the laws of physics. The contents are simple. I just defined that famous law of physics.

a = \frac{dv}{dt}, v = \frac{dx}{dt}
class Particle {
  //...
  void update () {
    this.velocity.add(this.acceleration);
    this.location.add(this.velocity);
  }
}

And sparks? Write a method for drawing.

class Particle {
  //...
  void display () {
    pushMatrix();
    resetMatrix();
    translate(this.location.x, this.location.y);
    ellipse(0, 0, 10, 10); //Draw a circle
    popMatrix();
  }
}

Create a Firework class

spark? It is a class to scatter. The following functions are required!

--Sparks? Randomly scatter 360 degrees

class Firework {
}

spark? Let's give the members the position of the fireworks.

class Firework {
  PVector location;
  ArrayList<Particle> particles;
  
  Firework (float xpos, float ypos) {
    this.location = new PVector(xpos, ypos);
    this.particles = new ArrayList<Particle>();
  }
}

After that, sparks? Let's write a method to scatter. By the way, even drawing.

class Firework {
  //...

  //Spread
  void explode () {
    for (int i = 0; i < 400; i++) {
      float theta = random(0, TWO_PI);
      particles.add(new Particle(
        this.location.x, this.location.y, //Install sparks at the position of fireworks
        cos(theta), sin(theta) //Set the initial velocity to any of 360 degrees
      ));
    }
  }
  
  //Physical calculation of sparks
  void update () {
    for (int i = 0; i < 400; i++) {
      particles.get(i).update();
    }
  }
  
  //Drawing of sparks
  void display () {
    fill(255); //I tried to make the spark white
    for (int i = 0; i < 400; i++) {
      particles.get(i).display();
    }
  }
}

Well, ready!

Fireworks? Prepare for: When you're done, do it!

Firework firework;

void setup () {
  size(600, 600); //Build a 600x600 campus

  firework = new Firework(width/2, height/2); //Set up fireworks
  
  firework.explode(); //Sprinkle sparks
}

void draw () {
  background(0); //The background is Black
  firework.update();Fireworks calculation
  firework.display(); //Fireworks drawing
}

e? What is shoboi? Isn't it such a firework?

Ugh, fine I will do it~

Finally, when it starts to look like fireworks

Now this!

firework.gif

Finally

This is a program I made when I was in the first year of college. Is it about two years ago? It's super fun to experience programming for the first time since I entered university! It was a time when I was writing the code while thinking about it. Of course I still write it.

I couldn't think of anything, so I tried this.

The source code is dirty, but here it is. https://github.com/RyosukeCla/fireworks

Recommended Posts

It's winter but do you want fireworks? ?? Then implement it
Can you do it? Java EE