xxxxxxxxxx
103
let groundDetail = 10; // Number of points along the ground
let earthParticles = [];
let generateGround = true; // Flag to control ground generation
let ground = [];
function setup() {
createCanvas(400, 400);
}
function createGround() {
for (let i = 0; i < groundDetail; i++) {
ground[i] = height - 50 + random(-10, 10); // Randomize ground heights
}
return ground;
}
function draw() {
background(255);
if (generateGround) {
let ground = createGround(); // Call createGround only once when the flag is true
generateGround = false; // Set the flag to false to prevent further ground generation
}
// Display and update earth particles
for (let i = earthParticles.length - 1; i >= 0; i--) {
earthParticles[i].update();
earthParticles[i].display();
// Remove particles that are off-screen
if (earthParticles[i].isOffScreen()) {
earthParticles.splice(i, 1);
}
}
// Display irregular ground
fill(139, 69, 19); // Brown color
beginShape();
vertex(0, height);
for (let i = 0; i < groundDetail; i++) {
let x = (i / (groundDetail - 1)) * width;
let y = ground[i];
vertex(x, y);
}
vertex(width, height);
endShape(CLOSE);
}
function mousePressed() {
// Create a new earth particle at the mouse position when mouse is pressed
let earthParticle = new EarthParticle(mouseX, height - 50);
earthParticles.push(earthParticle);
}
class EarthParticle {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector(0, random(-5, -2)); // Move upwards
this.size = random(10, 30);
this.shapeDetail = floor(random(5, 10)); // Number of vertices in the rock shape
this.angles = [];
this.lengths = [];
this.lifetime = 255; // Particle fade out effect
// Generate random angles and lengths for the rock shape
for (let i = 0; i < this.shapeDetail; i++) {
this.angles[i] = map(i, 0, this.shapeDetail, 0, TWO_PI) + random(-PI / 4, PI / 4);
this.lengths[i] = this.size + random(-5, 5);
}
}
update() {
// Update position based on velocity
this.position.add(this.velocity);
// Apply some gravity
this.velocity.y += 0.1;
// Fade out effect
this.lifetime -= 2;
}
display() {
// Draw earth particle as a custom rock shape with transparency
fill(139, 69, 19, this.lifetime);
noStroke();
beginShape();
for (let i = 0; i < this.shapeDetail; i++) {
let x = this.position.x + this.lengths[i] * cos(this.angles[i]);
let y = this.position.y + this.lengths[i] * sin(this.angles[i]);
vertex(x, y);
}
endShape(CLOSE);
}
isOffScreen() {
// Check if the particle is off-screen
return (
this.position.y > height ||
this.lifetime <= 0
);
}
}