xxxxxxxxxx
155
// Constants
const G = 0.5; // Gravitational constant
// Array to store the planets
let planets = [];
function setup() {
createCanvas(windowWidth, windowHeight);
// Create some planets
planets.push(new Planet(width / 2, height / 2, 1000, createVector(0, 0))); // Sun
planets.push(new Planet(width / 2 + 200, height / 2, 20, createVector(0, -4))); // Planet 1
planets.push(new Planet(width / 2 + 300, height / 2, 30, createVector(0, -5))); // Planet 2
}
function draw() {
background(0);
// Apply gravitational forces between planets and the sun
for (let i = 0; i < planets.length; i++) {
for (let j = 0; j < planets.length; j++) {
if (i !== j) {
let force = calculateGravitationalForce(planets[i], planets[j]);
planets[i].applyForce(force);
}
}
let sunForce = calculateGravitationalForce(planets[i], planets[0]); // Attract to the sun
planets[i].applyForce(sunForce);
}
// Update and display planets
for (let i = 0; i < planets.length; i++) {
planets[i].update();
planets[i].display();
// Check for collisions with other planets
for (let j = i + 1; j < planets.length; j++) {
if (planets[i].collidesWith(planets[j])) {
// Create an explosion at the collision position
let explosion = new Explosion(planets[i].position.x, planets[i].position.y, 50);
explosion.start();
// Remove the collided planets
planets.splice(j, 1);
planets.splice(i, 1);
break;
}
}
}
}
function calculateGravitationalForce(planetA, planetB) {
let distance = p5.Vector.dist(planetA.position, planetB.position);
distance = constrain(distance, 5, 25); // Constrain the distance to avoid extremely high forces
let magnitude = (G * planetA.mass * planetB.mass) / (distance * distance);
let direction = p5.Vector.sub(planetB.position, planetA.position);
direction.normalize();
direction.mult(magnitude);
return direction;
}
class Planet {
constructor(x, y, mass, velocity) {
this.position = createVector(x, y);
this.velocity = velocity;
this.acceleration = createVector(0, 0);
this.mass = mass;
this.radius = mass / 10;
}
applyForce(force) {
let f = p5.Vector.div(force, this.mass);
this.acceleration.add(f);
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
display() {
fill(255);
ellipse(this.position.x, this.position.y, this.radius * 2, this.radius * 2);
}
collidesWith(other) {
let distance = p5.Vector.dist(this.position, other.position);
return distance < (this.radius + other.radius);
}
}
class Explosion {
constructor(x, y, size) {
this.position = createVector(x, y);
this.size = size;
this.particles = [];
}
start() {
for (let i = 0; i < 100; i++) {
let angle = random(TWO_PI);
let magnitude = random(2, 8);
let velocity = p5.Vector.fromAngle(angle);
velocity.mult(magnitude);
let particle = new Particle(this.position.x, this.position.y, velocity);
this.particles.push(particle);
}
}
update() {
for (let i = this.particles.length - 1; i >= 0; i--) {
this.particles[i].update();
if (this.particles[i].finished()) {
this.particles.splice(i, 1);
}
}
}
display() {
for (let i = 0; i < this.particles.length; i++) {
this.particles[i].display();
}
}
}
class Particle {
constructor(x, y, velocity) {
this.position = createVector(x, y);
this.velocity = velocity;
this.acceleration = createVector(0, 0.1);
this.lifespan = 255;
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.lifespan -= 5;
}
display() {
noStroke();
fill(255, this.lifespan);
ellipse(this.position.x, this.position.y, 8, 8);
}
finished() {
return this.lifespan < 0;
}
}