p5js (no HTML) swarm of 30 colorful boids swimming inside of rotating hexagon. I like…
- User
p5js (no HTML) swarm of 30 colorful boids swimming inside of rotating hexagon. I like supernova nebulae
Assistant
Here's the final code for a p5.js sketch that simulates a swarm of 30 colorful boids swimming inside a rotating hexagon with a supernova nebula theme:
let boids = []; let hexagonVertices = []; let rotationAngle = 0; let hexagonSize = 200; function setup() { createCanvas(800, 800); for (let i = 0; i < 30; i++) { // Nebula-like colors let r = random(100, 255); let g = random(50, 150); let b = random(150, 255); let boidColor = color(r, g, b, 200); boids.push(new Boid(boidColor)); } updateHexagonVertices(); } function draw() { // Nebula-like background background(0); for (let i = 0; i < 10; i++) { let x = random(width); let y = random(height); let size = random(50, 200); let col = color(random(100, 200), random(50, 150), random(150, 255), 50); fill(col); noStroke(); ellipse(x, y, size, size); } // Draw hexagon push(); translate(width/2, height/2); rotate(rotationAngle); drawHexagon(); pop(); // Update and display boids for (let boid of boids) { boid.update(); boid.display(); boid.checkBoundaries(); } rotationAngle += 0.01; updateHexagonVertices(); } function updateHexagonVertices() { hexagonVertices = []; let angle = TWO_PI / 6; for (let i = 0; i < 6; i++) { let x = hexagonSize * cos(i * angle); let y = hexagonSize * sin(i * angle); hexagonVertices.push(createVector(x, y)); } } function drawHexagon() { noFill(); stroke(200, 100, 255); // Purple-ish color strokeWeight(2); beginShape(); for (let vertex of hexagonVertices) { vertex(vertex.x, vertex.y); } endShape(CLOSE); } class Boid { constructor(col) { this.position = createVector(random(width), random(height)); this.velocity = p5.Vector.random2D(); this.velocity.setMag(random(2, 4)); this.acceleration = createVector(); this.maxSpeed = 4; this.maxForce = 0.1; this.color = col; this.size = random(5, 10); } update() { this.flock(boids); this.position.add(this.velocity); this.velocity.add(this.acceleration); this.velocity.limit(this.maxSpeed); this.acceleration.mult(0); } flock(boids) { let alignment = this.align(boids); let cohesion = this.cohesion(boids); let separation = this.separate(boids); alignment.mult(1.0); cohesion.mult(1.0); separation.mult(1.5); this.acceleration.add(alignment); this.acceleration.add(cohesion); this.acceleration.add(separation); } align(boids) { let perceptionRadius = 50; let steering = createVector(); let total = 0; for (let other of boids) { let d = dist(this.position.x, this.position.y, other.position.x, other.position.y); if (other != this && d < perceptionRadius) { steering.add(other.velocity); total++; } } if (total > 0) { steering.div(total); steering.setMag(this.maxSpeed); steering.sub(this.velocity); steering.limit(this.maxForce); } return steering; } cohesion(boids) { let perceptionRadius = 50; let steering = createVector(); let total = 0; for (let other of boids) { let d = dist(this.position.x, this.position.y, other.position.x, other.position.y); if (other != this && d < perceptionRadius) { steering.add(other.position); total++; } } if (total > 0) { steering.div(total); steering.sub(this.position); steering.setMag(this.maxSpeed); steering.sub(this.velocity); steering.limit(this.maxForce); } return steering; } separate(boids) { let perceptionRadius = 50; let steering = createVector(); let total = 0; for (let other of boids) { let d = dist(this.position.x, this.position.y, other.position.x, other.position.y); if (other != this && d < perceptionRadius) { let diff = p5.Vector.sub(this.position, other.position); diff.div(d); steering.add(diff); total++; } } if (total > 0) { steering.div(total); steering.setMag(this.maxSpeed); steering.sub(this.velocity); steering.limit(this.maxForce); } return steering; } checkBoundaries() { let center = createVector(width/2, height/2); let relativePos = p5.Vector.sub(this.position, center); let rotatedPos = createVector(); rotatedPos.x = relativePos.x * cos(-rotationAngle) - relativePos.y * sin(-rotationAngle); rotatedPos.y = relativePos.x * sin(-rotationAngle) + relativePos.y * cos(-rotationAngle); if (!this.isPointInPolygon(rotatedPos, hexagonVertices)) { let target = createVector(width/2, height/2); let steering = p5.Vector.sub(target, this.position); steering.setMag(this.maxSpeed); steering.sub(this.velocity); steering.limit(this.maxForce); this.acceleration.add(steering); } } isPointInPolygon(point, vertices) { let inside = false; for (let i = 0, j = vertices.length - 1; i < vertices.length; j = i, i++) { let xi = vertices[i].x, yi = vertices[i].y; let xj = vertices[j].x, yj = vertices[j].y; let intersect = ((yi > point.y) != (yj > point.y)) && (point.x < (xj - xi) * (point.y - yi) / (yj - yi) + xi); if (intersect) inside = !inside; } return inside; } display() { // Glow effect drawingContext.shadowBlur = 15; drawingContext.shadowColor = color(red(this.color), green(this.color), blue(this.color), 150); fill(this.color); noStroke(); ellipse(this.position.x, this.position.y, this.size, this.size); drawingContext.shadowBlur = 0; } }This code creates a visually appealing simulation of boids inside a rotating hexagon with a nebula-like background. The boids have a glowing effect and are colored in vibrant hues reminiscent of a supernova nebula.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.