xxxxxxxxxx
139
//Jasmine Porter
// 220369500
// FA/DATT 2040 A - Math, Art, Code
// Sketch
let vehicles = []; // Vehicles array
let agents = []; // Agents array
let flowfield; // Flowfield object
let sphereRadius = 200; // Sphere radius
let globeTexture; // Globe texture image
let hurricaneLayer; // Hurricane graphics layer
let song, fft; // Song and FFT object
let speedMultiplier = 5; // Speed multiplier
let volumeSlider; // Volume slider
let colorPicker, agentColorPicker, hurricaneColorPicker; // Color pickers
let centerLat = -10; // Center latitude
let centerLon = -30; // Center longitude
let driftLatSpeed = 0.2; // Latitude drift speed
let driftLonSpeed = 0.2; // Longitude drift speed
function preload() {
globeTexture = loadImage("map.jpg"); // Load globe texture
song = loadSound("hurricane.mp3"); // Load song
}
function setup() {
createCanvas(600, 530, WEBGL); // Create canvas with WEBGL renderer
camera(200, 5, 690); // Set camera position
fft = new p5.FFT(0.9, 64); // Initialize FFT for audio analysis
fft.setInput(song); // Set song input for FFT
flowfield = new FlowField(4); // Create flowfield with resolution 4
let numVehicles = 900; // Number of vehicles
for (let i = 0; i < numVehicles; i++) {
let lat = random(-HALF_PI, HALF_PI); // Random latitude
let lon = random(0, TWO_PI); // Random longitude
vehicles.push(new Vehicle(lat, lon, random(0.1, 2), random(0.1, 0.5))); // Add new vehicle
}
let numAgents = 500; // Number of agents
for (let i = 0; i < numAgents; i++) {
let lat = random(-HALF_PI, HALF_PI); // Random latitude
let lon = random(0, TWO_PI); // Random longitude
agents.push(new Agent(lat, lon, random(0.1, 2), random(0.1, 0.5))); // Add new agent
}
playButton = createButton("Play"); // Play button label
playButton.position(500, height + 25); // Position play button
playButton.mousePressed(() => {
song.loop();
});
let volumeLabel = createDiv("Volume:"); // Volume label
volumeLabel.position(10, height + 5); // Position Volume
volumeSlider = createSlider(0, 1, 0.5, 0.01); // Volume
volumeSlider.position(20, height + 30); // Position slider
colorLabel = createDiv("Wind1"); // Wind1 label
colorLabel.position(200, height + 5); // Position Wind1
colorPicker = createColorPicker("#20CCF0"); // Colour
colorPicker.position(200, height + 25); // Position
colorLabel2 = createDiv("Wind2"); // Wind2 label
colorLabel2.position(300, height + 5); // Position Wind2
agentColorPicker = createColorPicker("#FBF8F8"); // Colour
agentColorPicker.position(300, height + 25); // Position picker
colorLabel3 = createDiv("Hurricane"); // Hurricane label
colorLabel3.position(390, height + 5); // Position Hurricane
hurricaneColorPicker = createColorPicker("#B5F5F0"); // Colour
hurricaneColorPicker.position(400, height + 25); // Position
hurricaneLayer = createGraphics(600, 600); // Create graphics layer for hurricane
let instuction = createDiv(
"Press the play button to initiate sound. The volume adjusts the speed of the wind, the louder"
);
let instuction2 = createDiv(
"it is the faster it will move. You can also rotate the earth to see diffrent points on the globe."
); // Instructions
let instuction3 = createDiv(
"Press s to save a screenshot of your creation."
); // Instructions
instuction.position(10, height + 55); // Position instructions
instuction2.position(10, height + 75); // Position instructions
instuction3.position(10, height + 95); // Position instructions
}
function draw() {
background(0); // Clear background
orbitControl(1, 1, 0); // Allow mouse control of the canvas view
noStroke(); // Disable strokes
song.setVolume(volumeSlider.value()); // Set song volume
ambientLight(200); // Set ambient light
let spectrum = fft.analyze(); // Analyze frequency spectrum
let bass = fft.getEnergy("bass"); // Get bass energy
let treble = fft.getEnergy("treble"); // Get treble energy
let energy = bass + fft.getEnergy("mid") + treble; // Total energy
speedMultiplier = map(energy, 0, 255 * 3, 0.5, 3); // Map energy to speed multiplier
hurricaneLayer.clear(); // Clear hurricane layer
renderHurricane(hurricaneLayer); // Render hurricane on layer
push();
texture(globeTexture); // Apply globe texture
sphere(sphereRadius); // Draw globe sphere
pop();
push();
texture(hurricaneLayer); // Apply hurricane texture
sphere(sphereRadius + 1); // Draw sphere with hurricane texture
pop();
flowfield.update(); // Update flowfield
let vehicleColor = colorPicker.color(); // Get vehicle color from picker
let agentColor = agentColorPicker.color(); // Get agent color from picker
for (let v of vehicles) {
v.follow(flowfield); // Vehicle follows flowfield
v.run(speedMultiplier, vehicleColor); // Run vehicle
}
for (let a of agents) {
a.follow(flowfield); // Agent follows flowfield
a.run(speedMultiplier, agentColor); // Run agent
}
}
function keyPressed() {
if (key === "s" || key === "S") {
saveCanvas("hurricane_simulation", "png"); // Save the canvas as a PNG file
}
}