Tyler Ben

Generation

generate functionTue, 01 Aug 2023

Sure, here's the updated code: ```javascript // Define variables let stars = []; let planets = []; let numStars = 10; let numPlanets = 30; // Set up the canvas function setup() { createCanvas(600, 600); // Create stars and add them to the array for (let i = 0; i < numStars; i++) { stars.push(new Star()); } // Create planets and add them to the array for (let i = 0; i < numPlanets; i++) { let starIndex = floor(random(numStars)); planets.push(new Planet(stars[starIndex])); } } // Draw the stars and planets function draw() { background(0); // Update and display each star for (let i = 0; i < numStars; i++) { stars[i].update(); stars[i].display(); } // Update and display each planet for (let i = 0; i < numPlanets; i++) { planets[i].update(); planets[i].display(); } } // Define the Star class class Star { constructor() { this.x = random(width); this.y = random(height); this.size = random(20, 50); this.mass = this.size ** 2; this.color = color(random(255), random(255), random(255)); this.velocity = createVector(random(-1, 1), random(-1, 1)); this.acceleration = createVector(0, 0); } // Update the star's position and velocity update() { this.velocity.add(this.acceleration); this.x += this.velocity.x; this.y += this.velocity.y; if (this.x < -this.size) { this.x = width + this.size; } else if (this.x > width + this.size) { this.x = -this.size; } if (this.y < -this.size) { this.y = height + this.size; } else if (this.y > height + this.size) { this.y = -this.size; } this.acceleration.mult(0); } // Display the star on the canvas display() { fill(this.color); noStroke(); ellipse(this.x, this.y, this.size, this.size); } // Apply force to the star applyForce(force) { let f = force.copy().div(this.mass); this.acceleration.add(f); } // Calculate the gravitational force between this star and another object calculateGravitationalForce(other) { let distance = dist(this.x, this.y, other.x, other.y); let forceMagnitude = (G * this.mass * other.mass) / (distance ** 2); let forceDirection = createVector(other.x - this.x, other.y - this.y).normalize(); let force = forceDirection.mult(forceMagnitude); return force; } } // Define the Planet class class Planet { constructor(star) { this.star = star; this.distance = random(50, 200); this.angle = random(TWO_PI); this.size = random(5, 20); this.mass = this.size ** 2; this.color = color(random(255), random(255), random(255)); this.velocity = createVector(0, 0); this.acceleration = createVector(0, 0); } // Update the planet's position and velocity update() { let force = this.star.calculateGravitationalForce(this); this.applyForce(force); this.velocity.add(this.acceleration); this.distance += this.velocity.mag(); this.angle += this.velocity.mag() / this.distance; this.acceleration.mult(0); } // Display the planet on the canvas display() { let x = this.star.x + this.distance * cos(this.angle); let y = this.star.y + this.distance * sin(this.angle); fill(this.color); noStroke(); ellipse(x, y, this.size, this.size); } // Apply force to the planet applyForce(force) { let f = force.copy().div(this.mass); this.acceleration.add(f); } } // Define constants const G = 0.1;

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant