Final Project: Ocean Clean-up Simulator

sketch
//Anthony Pan
//Section C
//final project proposal final

//requirements:
    //loops, arrays, conditionals (if), user interaction, transformations, 
    //functions (besides setup and draw), 
    //and use of at least one object definition of your own design.

//net object "line" "pushes" trash objects to the right of the canvas 
    // if trash objects are to the right of the line, update all of the ones to the right 
    //within certain number of pixels, x position of the trash objects will update and increase incrementally
    //lin search particle loop
    //if statement with mouseX
        //capture value of mouseX at that instance to avoid bugs
//create seperate function that changes background color
// use series of if statements

//array and object for garbage 1
var garbage1Showing = [];
var garbage1; 

//array and object for garbage 2
var garbage2; 
var garbage2Showing = [];


//array and object for ocean details
var OceanDetail; 
var oceanDetailShowing = [];

//frame counter
var counter = 0;

var fishes = [];
var fish;  

//angle for rotation og garbage2 object
var angle = 0;

function setup() {
    createCanvas(500, 200);

    //create objects for garbage 1
    for(var i = 0; i < 8; i ++) {
        garbage1 = makeGarbage1(random(50,150), color(random(255)));
        garbage1Showing.push(garbage1);

    }

    //create objects for garbage 2
    for(var i = 0; i < 10; i++) {
        garbage2 = makeGarbage2(random(60, 140));
        garbage2Showing.push(garbage2);
    }

    //create objects for ocean details
    for(var i = 0; i < 7; i++) {
        OceanDetail = makeOceanDetail(random(0,height), color(255));
        oceanDetailShowing.push(OceanDetail);
    }

    
    for (var i = 0; i < 10; i++) {
        fish = makeFish();
        fishes.push(fish);
    }
    

    
}

function draw() {
    //change water color based on # of objects in water
    waterChange();



    //ocean detail functions
    OceanDetailDisplay();
    removeOceanDetail();
    addNewOceanDetail();

    noStroke();
    
    //garbage2 functions
    garbage2DisplayandMove();
    removeGarbage2();

    //garbage1 functions
    garbage1DisplayandMove();
    removeGarbage1();

    //add new garbage to ocean based on probability
    addNewGarbage1();
    addNewGarbage2();

    //display fish if water is blue to dark blue
    if(garbage1Showing.length > 0 & garbage1Showing.length <= 40) {
        displayFish();
    }
    

    //draw net
    fill(255);
    noStroke();
    rect(mouseX, 0, 2, 400);

    //check if garbage is ahead of net, if so use net to push them off screen 
    checkGarbage1Ahead();
    checkGarbage2Ahead();
    

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//fish code

//fish constructor
function makeFish() {
    var fish = {x: random(25, 600), y: random(25, 220),
        speed: random(-0.2,0.2),
        c: color(10,10,random(255)),
        draw: drawFish,
        move: moveFish
    }
    return fish;
}

//draw fish shape
function drawFish() {
    fill(this.c);
    push();
    scale(0.8); //transformation 
    ellipse(this.x, this.y, 20, 10);

    //direction of fish indicates which way tail is facing
    if(this.speed < 0) {
        triangle(this.x+10, this.y, this.x+15, this.y-5, this.x+15, this.y+5);
    } else {
        triangle(this.x-10, this.y, this.x-15, this.y-5, this.x-15, this.y+5);
    }
    pop();

}

//move fish 
function moveFish() {
    this.x += this.speed;

    //hits random point within canvas, fish will turn other direction 
    if(this.x > width - random(50) || this.x < random(50)) {
        this.speed = -this.speed; 
    }
}

//one function to display and move fish on canvas 
function displayFish() {
    for(var i = 0; i < fishes.length; i++) {
        fishes[i].move();
        fishes[i].draw();
    }
}




////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//type garbage1 functions

//garbage1 constructor
function makeGarbage1(gy, c) {
    var garbage1 = {x: -10, y: gy,
        speed: random(0.05,0.75),
        Yspeed: random(0,0.2),
        size: random(1,6),
        fill: c,
        move: garbage1Move,
        draw: garbage1Draw }
    return garbage1;
}

//draw garbage1
function garbage1Draw() {
    fill(this.fill);
    ellipse(this.x, this.y, this.size, this.size);


}

//move garbage1
function garbage1Move() {
    this.x += this.speed;
    this.y += this.Yspeed;
    if(this.y >= 150 || this.y <= 50) {
        this.Yspeed = -this.Yspeed;
    }

    //to slow down at the middle and collect
    if(this.x >= width/2) {
        this.speed = 0.001;

    }

}

//1 function to move and draw garbage
function garbage1DisplayandMove() {
    for(var i = 0; i < garbage1Showing.length; i++) {
        garbage1Showing[i].move();
        garbage1Showing[i].draw();
    }

}

//make new garbage based on probability 
function addNewGarbage1() {
    var newGarbage1likelihood = 0.01;
    if(random(0,1) < newGarbage1likelihood) {
         garbage1Showing.push(makeGarbage1(random(50,150), color(random(255))));
    }
}

//remove garbage off screen 
function removeGarbage1() {
    var garbage1ToKeep = [];
    for (var i = 0; i < garbage1Showing.length; i++){
        if (garbage1Showing[i].x < width) {
            garbage1ToKeep.push(garbage1Showing[i]);
        }
    }
    garbage1Showing = garbage1ToKeep; // remember the showing garbage2
}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//garbage2 functions

//garbage 2 constructor
function makeGarbage2(gy) {
    var garbage2 = {x: -10, y: gy,
        speed: random(0.1,0.3),
        Yspeed: random(0,0.2),
        size: random(2,7),
        r: random(255),
        g: random(255),
        b: random(255),
        move: garbage2Move,
        draw: garbage2Draw }
    return garbage2;

}

//draw garbage 2 
function garbage2Draw() {
    rectMode(CENTER);

    //garbage spins
    push();
    translate(this.x,this.y);
    rotate(radians(angle));
    angle += 0.1;

    fill(this.r, this.g, this.b);
    rect(0, 0, this.size, this.size);

    pop();
}

//move garbage 2
function garbage2Move() {
    this.x += this.speed;
    this.y += this.Yspeed;
    if(this.y >= 140 || this.y <= 60) {
        this.Yspeed = -this.Yspeed;
    }

    //to slow down at the middle and collect
    if(this.x >= width/2) {
        this.speed = 0.001;

    }

}

//1 function to draw and move garbage
function garbage2DisplayandMove() {
    for(var i = 0; i < garbage2Showing.length; i++) {
        garbage2Showing[i].move();
        garbage2Showing[i].draw();
    }

}

//remove garbage 2 if off screen 
function removeGarbage2() {
    var garbage2ToKeep = [];
    for (var i = 0; i < garbage2Showing.length; i++){
        if (garbage2Showing[i].x < width) {
            garbage2ToKeep.push(garbage2Showing[i]);
        }
    }
    garbage2Showing = garbage2ToKeep; // remember the showing garbage2
}

//add new garbage periodically
function addNewGarbage2() {
    var newGarbage2likelihood = 0.01;
    if(random(0,1) < newGarbage2likelihood) {
         garbage2Showing.push(makeGarbage2(random(50,150)));
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//make ocean details

//make ocean detail object
function makeOceanDetail(oy, c) {
    OceanDetail = {x: 0, y: oy,
        speed: random(0.1,0.3),
        fill: c,
        move: oceanDetailMove,
        draw: oceanDetailDraw }
    return OceanDetail;
}

//move ocean detail
function oceanDetailMove() {
    this.x += this.speed;
}

//drawocean details
function oceanDetailDraw() {
    noFill();
    strokeWeight(0.2);
    stroke(this.fill);
    curve(this.x, this.y, this.x + 10, this.y - 10, this.x + 10, this.y - 20, this.x, this.y - 30);

}

//1 function to draw and move ocean details
function OceanDetailDisplay() {
    for(var i = 0; i < oceanDetailShowing.length; i++) {
        oceanDetailShowing[i].move();
        oceanDetailShowing[i].draw();
    }

}

//remove ocean details if off screen
function removeOceanDetail() {
    var oceansToKeep = [];
    for (var i = 0; i < oceanDetailShowing.length; i++){
        if (oceanDetailShowing[i].x < width) {
            oceansToKeep.push(oceanDetailShowing[i]);
        }
    }
    oceanDetailShowing = oceansToKeep; // remember the showing ocean details
}

//add new ocean details periodically
function addNewOceanDetail() {
    counter +=1;
    if (counter % 100 == 0){
        oceanDetailShowing.push(makeOceanDetail(random(40,height-40), color(255)));
    }

}





////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//change background color based on number of garbage objects on the screen 

//make this change more gradual 
function waterChange() {
    if(garbage1Showing.length <= 20) {
        background(20,100,255);
    }

    if(garbage1Showing.length > 20 & garbage1Showing.length <= 40) {
        background(51, 89, 163);
    }

    if(garbage1Showing.length > 40 & garbage1Showing.length <= 60) {
        background(59, 81,125);
    }

    if(garbage1Showing.length > 60 & garbage1Showing.length <= 100) {
        background(59, 117,125);
    }

    if(garbage1Showing.length > 100 & garbage1Showing.length <= 120) {
        background(59, 125, 90);
    }

    if(garbage1Showing.length > 120 & garbage1Showing.length <= 140) {
        background(56, 74, 43);
    }

    if(garbage1Showing.length > 140 & garbage1Showing.length <= 160) {
        background(43, 54, 36);
    }

    if(garbage1Showing.length >= 160) {
        background(69, 57, 42);
    }

}




////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//functions that check if particles are ahead of net

function checkGarbage1Ahead() {
    for(var i = 0; i < garbage1Showing.length; i++) {
        if(garbage1Showing[i].x > mouseX & garbage1Showing[i].x < mouseX + 10) {
            garbage1Showing[i].x = mouseX + 6;
        }
    }   

}

function checkGarbage2Ahead() {
    for(var i = 0; i < garbage2Showing.length; i++) {
        if(garbage2Showing[i].x > mouseX & garbage2Showing[i].x < mouseX + 10) {
            garbage2Showing[i].x = mouseX + 6;
        }
    }   

}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////






For my final project, I have created an ocean clean-up interaction program. Ocean plastics contribute to the rapid increase in the rate of climate change because it acts as a carbon sink. Thus, cleaning up ocean plastics is imperative to slow down the rate of climate change in addition to protecting ocean life.

This program aims to create an abstraction of ocean clean-up and the adverse effects on the environment if pollution is left unattended for too long. The water starts off as a nice, blue color with fish swimming around. Waves are added as extra details to keep the canvas somewhat recognizable as the ocean. As the program continues, trash enters from the left side of the canvas. The trash is represented by randomized rectangles and circles of varying colors and sizes. These objects float towards the center of the canvas on randomized paths. They begin to collect in the middle of the screen, clumping together into a massive garbage patch. As the number of trash objects enters the scene, the water begins to dirty, from a clear blue to a muddy brown—if left out for too long. The fish die-off as the trash on the canvas reaches a certain number.

However, the user is equipped with a net that can be used to clean the garbage. The net is represented by a long, white rectangle. The net works by identifying if a garbage object is within a certain threshold in front of the net. If the trash is within a certain distance in front of the net, it will be sucked in by the net. The user can then use mouseX to push the garbage off the screen to clean the ocean! As trash leaves the screen from the right side of the canvas, the water becomes blue once more. Remember that the net cannot move too fast. If the user is too hasty with their movements of the net, the trash may fall out of the net. Slowly moving the trash off the canvas is the most effective method of cleaning the ocean!

What inspired me to create this program is a video on YouTube by The Ocean Cleanup. They were able to develop a method of cleaning plastics and other pollutants in oceans around the world. They are specifically focusing on cleaning up the Great Pacific Garbage Patch.

If I had more time, I would like to add more detail to the ocean to make the entire program more visually appealing. I would also have liked to use particles and strings to create the net instead of a rectangle. However, I couldn’t figure out how to create a net that you could drag all the particles at once.

In order to run this program, all you have to do is open the index file into the browser of your choice!

Leave a Reply