Snow Globe

I felt inspired by some visual artists that used simple animations to create interesting effects in otherwise still art pieces. It’s sort of like the idea of a snow globe, changing around a still scenery. I decided to make an art piece that essentially replicated this.

I created a class that loops 100 times to create snow falling at different speeds down the screen, with an umbrella girl I drew from another drawing program I made. The user’s cursor is a snowflake. It’s like a simple interactive desktop image. I’m hoping to add to this by having snowflakes fall out of the cursor as it moves across the screen.

 

Screen Shot 2015-11-02 at 1.58.45 PM

PImage Umbrella;

class Snow {
  float ySpeed;
  float diam;
  float xsky;
  float yPos;
  float opacity;
  float snowflake;

  Snow() {
    ySpeed = 0;
    diam = 0;
    xsky = 0;
    yPos = 0;
  }
  
  void drawing(){
    fill(35,1);
    strokeJoin(BEVEL);
    rect(600,330,100,200);
    fill(35,2);
    rect(600+10,330+10,100-20,200-20);
    fill(35,3);
    rect(600+20,330+20,100-40,200-40);
    fill(190,180);
  }
  void snowball() {
    fill(210, opacity);
    xsky = random(1, width);
    ellipse(xsky, yPos, diam, diam);
  }
  void falling() {
    ySpeed = random(-1, 12);
    yPos += ySpeed;
    if (yPos >= height) {
      yPos = 0;
    }
  }

  void snowsize() {
    diam = random(1, 25);
  }
  void opacity(){
    opacity = map(yPos,0, height*0.87,210,0);
}
  void cursory(){
    fill(225);
    pushMatrix();
    stroke(225,opacity);
    snowflake = 30;
    line(mouseX,mouseY,mouseX+snowflake,mouseY-snowflake);
    line(mouseX,mouseY-snowflake,mouseX+snowflake,mouseY);
    line(mouseX+snowflake/2,mouseY-snowflake,mouseX+snowflake/2,mouseY);
    line(mouseX,mouseY-snowflake/2,mouseX+snowflake,mouseY-snowflake/2);
    popMatrix();
    
    
    if(mousePressed == true){
      line(pmouseX,pmouseY,pmouseX+snowflake*2,pmouseY-snowflake*2);
      line(pmouseX,pmouseY-snowflake*2,pmouseX+snowflake*2,pmouseY);
      line(pmouseX+snowflake*2/2,pmouseY-snowflake*2,pmouseX+snowflake*2/2,pmouseY);
      line(mouseX,pmouseY-snowflake*2/2,pmouseX+snowflake*2,pmouseY-snowflake*2/2);
    }
    noStroke();
}
}


Snow[] unicorn;

void setup() {
  size(800, 600);
  background(35);
  noStroke();
  fill(250);
  Umbrella = loadImage("Umbrella.png");
  unicorn = new Snow[100];
  for(int q = 0;q<unicorn.length; q++){
    unicorn[q] = new Snow();
    
  }
    
}

void draw() {  
 image(Umbrella,600,330);
 noCursor();
 for(int q = 0;q<unicorn.length; q++){
    unicorn[q].snowball();
    unicorn[q].snowsize();
    unicorn[q].falling();
    unicorn[q].opacity();
    unicorn[q].cursory();
    //unicorn[q].drawing();
 }
    
  fill(35,10);
  rect(0,0,width,height);
}

 

Three body simulation in Processing

Ever since I took Mechanics I’ve been fascinated by solving for the equations of motion of different physical systems. Only recently have I developed the programming confidence to be able to actually solve these equations so I can see the time evolution of some of my favorite systems. One rather fascinating system is the ‘three body’ system. This system has three particles of mass m1, m2, and m3 that experience the gravitational force resulting from every other body’s gravitational field. In this blog post I go through the process of actually solving for the equations of motion of the two body system. It is rather easy to extend this derivation to the three body system. The three body system is an example of a ‘chaotic’ system — small changes in input parameters result in large changes in the time evolution of the system. One can mitigate the instability of the problem by assuming that the mass of one of the particles is much larger than that of the others. While my code will solve this problem regardless of masses, I’ve set it up such that the first mass is about 100 times larger than that of the second mass, which is in turn about 100 times larger than the third mass. This makes it easier to adjust parameters. I’ve added some sliders that will adjust the position and velocity of each of the masses in my simulation. Below is a GIF showing the functionality of my code. Sorry it’s so darn ugly.

three_body_sliders

Notice that the central mass moves in the positive y direction while the simulation runs. I don’t think that this is due to anything physical. In fact, I think that it has to do with the stability of my ODE solver. For those who are interested, I’m using an explicit method called Runge-Kutta. Runge-Kutta does a good job of dealing with unstable ODEs (like the one I’m solving here) but it can’t do as well as an implicit solver. Implicit solvers require solving a system of nonlinear equations at each time step, so I haven’t bothered to implement it yet.

Paintball Game

Several weeks ago I went to a school-organized paintball trip. It was pretty fun, though the paintball gun is heavy, my aiming is the worst, and it actually hurt when you’re hit. So, I made a paintball game with processing that allows you to shoot your enemies WITHOUT being shoot back. Of course, to make it a bit fairer, the enemy have a few barriers to hide behind.

When the game starts, it looks like this:

Screen Shot 2015-11-01 at 8.34.11 PM
The game started with 5 “enemies”, though a few more is shown in this screenshot.

Continue reading “Paintball Game”

Maze Game

For this week’s assignment, I made a simple maze game. The game has two modes: one player and two players. For the one player mode, the player uses keys to move the dot around the maze. The player wins once he reaches the end of the maze. For the two players mode, two players start out on different sides of the maze. Then they move the dot to reach the opposite side of the maze. The one who reaches the other side first wins the game. In both modes, players need to move the dot carefully so as not to touch any edge of the maze. If the dot is in contact with the edge, the player loses the game. (p.s. Don’t try to crash the dots. You will die.)

Screen Shot 2015-11-01 at 1.30.49 PM

Continue reading “Maze Game”