Cracking Up is Hard to Do
Sheiva Rezvani and I decided to mimic the cracking of a surface for our Nature of Code midterm project. The appearance of the cracks is determined by a one-dimensional Perlin Noise function. This function is often used to simulate the erratic edges of coastlines and mountain ranges. We designed our program to draw a crack, and then draw a new crack starting from the middle of the first crack once it hit the edge of the display window. There was an issue with the code that caused every crack after the first one to draw itself more than once simultaneously (hence why the newer cracks appear thicker and grow more slowly than the older ones in the video). This caused the program to run very slowly and eventually crash.
Main sketch code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | //Kim Ash & Sheiva Rezvani //builds a system of cracks Crack cStart; Crack b; ArrayList<Crack> cracks; //float crackSize = 1000; PVector middle1; void setup() { size(400, 400); cracks = new ArrayList<Crack>(); cStart = new Crack(width/2, height); //need starting crack, and then build off from there --> may not need this cracks.add(cStart); } void draw() { background(0); smooth(); for(Crack c : cracks) { c.display(); c.stop(); } if (cStart.pos.y < 0 || cStart.pos.x < 0 || cStart.pos.x > width) //when crack continues offscreen { //middle = new PVector(width/2,height/2); b = new Crack(middle.x, middle.y); cracks.add(b); } else { cracks.remove(b); } //PVector middle = crack.get(crack.size()/2); //PVector end = crack.get(crack.size()-1); } |
Crack class code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | PVector middle; class Crack { ArrayList <PVector> segment; PVector pos; float xoff = 0; float yoff = 1000; Crack(float x, float y) { pos = new PVector(x,y); segment = new ArrayList<PVector>(); } void display() { xoff += 0.015; yoff += 0.015; pos.x += map(noise(xoff),0,1,-1,1); pos.y += map(noise(yoff),0,1,0,-0.5); segment.add(pos.get()); beginShape(); stroke(255); noFill(); for (PVector v : segment) { vertex(v.x,v.y); } endShape(); } void stop() //stop the crack from continuing offscreen { if (pos.y < 0 || pos.x < 0 || pos.x > width) //when crack continues offscreen { segment.remove(pos.get()); middle = segment.get(segment.size()/2); } else { } } } |