|
| 1 | +// PROJECT 1 - CALIFORNIA WILDFIRE SIMULATION |
| 2 | +// September 2024 |
| 3 | +//Code for the Wildfire Class |
| 4 | + |
| 5 | +int numForests = (int)random(6, 9); |
| 6 | +int humidity = (int)random(0, 100); |
| 7 | +int temperature = (int)random(40, 100); |
| 8 | + |
| 9 | +// A list to store all the Forests in the simulation |
| 10 | +Forest[] forests = new Forest[numForests]; |
| 11 | + |
| 12 | +void setup() { |
| 13 | + size(1000, 1000); |
| 14 | + frameRate(1); |
| 15 | + background(237, 222, 183); |
| 16 | + |
| 17 | + textSize(50); |
| 18 | + fill(47, 111, 163); |
| 19 | + text("Albert's Wildfire Simulation", 210, 60); |
| 20 | + |
| 21 | + textSize(30); |
| 22 | + fill(0); |
| 23 | + text("Humidity: " + humidity + " %", 300, 130); |
| 24 | + text("Temperature: " + temperature + " °F", 500, 130); |
| 25 | + |
| 26 | + for (int i = 0; i < numForests; i++) { |
| 27 | + forests[i] = new Forest(random(0, width - 175), random(185, height - 175), 175, 175); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void updateSimulation() { |
| 32 | + for (int i = 0; i < numForests; i++) { |
| 33 | + forests[i].update(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void draw() { |
| 38 | + background(237, 222, 183); // Clear background each frame |
| 39 | + |
| 40 | + // Display title and conditions |
| 41 | + textSize(50); |
| 42 | + fill(47, 111, 163); |
| 43 | + text("Albert's Wildfire Simulation", 210, 60); |
| 44 | + |
| 45 | + textSize(30); |
| 46 | + fill(0); |
| 47 | + text("Humidity: " + humidity + " %", 300, 130); |
| 48 | + text("Temperature: " + temperature + " °F", 500, 130); |
| 49 | + |
| 50 | + // Draw and update forests |
| 51 | + for (int i = 0; i < numForests; i++) { |
| 52 | + forests[i].draw(); |
| 53 | + } |
| 54 | + |
| 55 | + updateSimulation(); |
| 56 | +} |
| 57 | + |
| 58 | +//Code for the Forest Class |
| 59 | + |
| 60 | +class Forest { |
| 61 | + float x, y; |
| 62 | + float l, w; |
| 63 | + |
| 64 | + |
| 65 | + int numTrees = (int)random(20,35); |
| 66 | + |
| 67 | + Tree[] trees = new Tree[numTrees]; |
| 68 | + |
| 69 | + Forest(float x, float y, float l, float w) { |
| 70 | + this.x = x; |
| 71 | + this.y = y; |
| 72 | + this.l = l; |
| 73 | + this.w = w; |
| 74 | + |
| 75 | + for (int i=0; i<numTrees; i++) { |
| 76 | + trees[i] = new Tree(random(x,x+w),random(y,y+l),20); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + // draw method where i loop through the array of trees and call the draw method on each tree |
| 83 | + void draw() { |
| 84 | + for (int i=0; i<numTrees; i++) { |
| 85 | + trees[i].draw(); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + double distance(Tree t1, Tree t2) { |
| 92 | + return Math.sqrt(Math.pow(t1.x-t2.x,2)+Math.pow(t1.y-t2.y,2)); |
| 93 | + } |
| 94 | + |
| 95 | + void update() { |
| 96 | + for (int i=0; i<numTrees; i++) { |
| 97 | + trees[i].update(); |
| 98 | + if (trees[i].burning) { |
| 99 | + for (int j=0; j<numTrees; j++) { |
| 100 | + if (distance(trees[i],trees[j])<30) { |
| 101 | + if (humidity<50 && temperature<70) { |
| 102 | + trees[j].catchFire(); |
| 103 | + } |
| 104 | + |
| 105 | + else if (humidity>50 || temperature>70) { |
| 106 | + trees[j].catchFireWhenHumidORHot(); |
| 107 | + } |
| 108 | + |
| 109 | + else if (humidity>50 && temperature>70) { |
| 110 | + trees[j].catchFireWhenHumidANDHot(); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +} |
| 121 | +} |
| 122 | + |
| 123 | +//Code for the Tree Class |
| 124 | + |
| 125 | +//Pretty did all the fire animation in my tree class |
| 126 | + |
| 127 | +class Tree { |
| 128 | + float x, y; // Position |
| 129 | + float r; // Size (radius) |
| 130 | + boolean healthy, burning, dead; // States |
| 131 | + int burning_time; // Time to burn before dying |
| 132 | + int death_time = 13; // Arbitrary burn time |
| 133 | + |
| 134 | + // instance variables that I need for flame animation |
| 135 | + float flameOffset; //this variable is supposed to control the vertical movement, the flickering, ofd the flame |
| 136 | + color flameColor; //just determines the color of the flame |
| 137 | + |
| 138 | + Tree(float tempX, float tempY, float tempR) { |
| 139 | + x = tempX; |
| 140 | + y = tempY; |
| 141 | + r = tempR; |
| 142 | + burning_time = 0; |
| 143 | + |
| 144 | + // 10% chance of being burning initially |
| 145 | + burning = random(0, 10) < 1; |
| 146 | + |
| 147 | + // Initially, no tree is dead |
| 148 | + dead = false; |
| 149 | + |
| 150 | + // Initialize flame properties |
| 151 | + flameOffset = 0; |
| 152 | + flameColor = color(255, 100, 0); // Initial flame color |
| 153 | + } |
| 154 | + |
| 155 | + // Updates the status of this tree |
| 156 | + void update() { |
| 157 | + if (dead) { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + if (burning) { |
| 162 | + burning_time++; |
| 163 | + |
| 164 | + if (burning_time > 7 && burning_time < 10) { |
| 165 | + if (random(2) < 1) { |
| 166 | + burning = false; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (burning_time > 10) { |
| 171 | + if (random(4) < 3) { |
| 172 | + burning = false; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + else { |
| 177 | + if (burning_time > 0) { |
| 178 | + burning_time--; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + if (burning_time > death_time) { |
| 183 | + dead = true; |
| 184 | + } |
| 185 | + |
| 186 | + // ANIMATING THE FLAME |
| 187 | + if (burning) { |
| 188 | + flameOffset = sin(frameCount * 0.1) * 5; //after some googling, I found that you can actually use the math trig function sin to help in processing animation. |
| 189 | + //sin's smoothly changing value was the easiest way I found to simulate the smooth movement of the flame's height. |
| 190 | + flameColor = color( |
| 191 | + 255, |
| 192 | + (int)(random(100, 255)), |
| 193 | + 0 |
| 194 | + ); // Varying the color between yellow and red |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + void catchFire() { |
| 199 | + if (!dead) { |
| 200 | + if (random(2) < 1) { |
| 201 | + burning = true; |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + void catchFireWhenHumidORHot() { |
| 207 | + if (!dead) { |
| 208 | + if (random(3) > 1) { |
| 209 | + burning = true; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + void catchFireWhenHumidANDHot() { |
| 215 | + if (!dead) { |
| 216 | + if (random(5) > 1) { |
| 217 | + burning = true; |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + void draw() { |
| 223 | + noStroke(); |
| 224 | + |
| 225 | + // Draw the tree trunk or base |
| 226 | + fill(34, 139, 34); // Default healthy tree color |
| 227 | + if (dead) { |
| 228 | + fill(105, 105, 105); // Dead tree color |
| 229 | + } |
| 230 | + else if (burning) { |
| 231 | + fill(255, 0, 0); // Initial burning color |
| 232 | + } |
| 233 | + ellipse(x, y, r, r); |
| 234 | + |
| 235 | + // Draw animated flame if burning |
| 236 | + if (burning && !dead) { |
| 237 | + pushMatrix(); //function that I found that basically saves the current drawing on the screen like a bookmark |
| 238 | + translate(x, y - r / 2); //function to position the flame above the tree - I found it was easiest to use this function and draw each flame |
| 239 | + noStroke(); |
| 240 | + fill(flameColor); |
| 241 | + |
| 242 | + // Draw multiple flickering flames |
| 243 | + for (int i = 0; i < 3; i++) { |
| 244 | + float angle = random(TWO_PI); //the angle determines the direction of each flame |
| 245 | + float size = random(5, 10); |
| 246 | + float px = cos(angle) * size; //by multiplying the cos of the angle by the size I can spread the flames in all directions around the tree's center |
| 247 | + float py = sin(angle) * size + flameOffset; |
| 248 | + ellipse(px, py, size, size * 1.5); //drawing each flame as an ellipse |
| 249 | + } |
| 250 | + |
| 251 | + popMatrix(); //function that I found that works with pushMatrix(). when popMatrix is called, it reverts back to the drawing that was on the screen when I called pushMatrix() |
| 252 | + //When animating the fire, one of the biggest troubles that I ran into was not letting the animated fire affect the drawing of the other shapes and texts. |
| 253 | + //Using the pushMatrix and popMatrix functions was one of the easiest ways I found to make the fire animation only affect the burning trees and nothing else |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments