xxxxxxxxxx
83
let particles = [];
let sun;
function setup() {
createCanvas(600, 600, WEBGL);
sun = new Particle(0, 0, 0, 20, true);
// Create planets
for (let i = 0; i < 9; i++) {
let radius = random(5, 15);
// let orbitSpeed = random(0.05, 1);
particles.push(new Particle(random(-500, 500), random(-25, 25), random(-500,500), radius, false, random(0.05, 0.1), random(50,175)));
}
}
function drawAxes() {
strokeWeight(2);
// X-axis (red)
stroke(255, 0, 0);
line(-200, 0, 0, 200, 0, 0);
// Y-axis (green)
stroke(0, 255, 0);
line(0, -200, 0, 0, 200, 0);
// Z-axis (blue)
stroke(0, 0, 255);
line(0, 0, -200, 0, 0, 200);
}
function draw() {
background(0);
rotateY(frameCount * 0.01);
// drawAxes();
sun.update()
sun.display();
// Update and display planets
particles.forEach(particle => {
push();
particle.update();
particle.display();
pop();
}
);
}
class Particle {
constructor(x, y, z, radius, isSun = false, orbitSpeed = 0, orbitRadius = 0) {
this.position = createVector(x, y, z);
this.radius = radius;
this.isSun = isSun;
this.angle = random(TWO_PI);
this.orbitSpeed = orbitSpeed;
this.orbitRadius = orbitRadius;
this.velocity = createVector(0, 0, 0);
}
update() {
if (this.isSun) {
// Move the sun based on mouseX and mouseY
this.position.x = map(mouseX, 0, width, -200, 200);
this.position.y = map(mouseY, 0, height, -200, 200);
// this.position.x = mouseX;
// this.position.y = movedY;
}
if (!this.isSun) {
this.angle += this.orbitSpeed;
this.position.x = this.orbitRadius * cos(this.angle);
this.position.z = this.orbitRadius * sin(this.angle);
}
}
display() {
noStroke();
if (this.isSun) {
fill(255, 200, 0);
} else {
fill(255, 105, 180);
}
translate(this.position.x, this.position.y, this.position.z);
sphere(this.radius);
}
}