improve ecosystem project

This commit is contained in:
Rokas Puzonas 2024-02-10 13:14:10 +02:00
parent 8d3c94ea3e
commit 4dbca97293

View File

@ -1,60 +1,118 @@
// An algorithm for picking a random number based on monte carlo method
function acceptreject(formula) {
// We do this “forever” until we find a qualifying random value.
while (true) {
// Pick a random value.
let r1 = random(1);
// Assign a probability.
let probability = formula(r1);
// Pick a second random value.
let r2 = random(1);
//{!3} Does it qualify? If so, were done!
if (r2 < probability) {
return r1;
}
}
}
class Walker {
constructor(x = 0, y = 0) {
this.t = 0
this.x = x
this.y = y
constructor(
position,
speed,
topSpeed,
trailSize,
color,
accelerationType,
headSize
) {
this.t = random(1000000)
this.position = position
this.velocity = createVector(0, 0)
this.acceleration = createVector(0, 0)
this.trail = []
this.trailSize = trailSize
this.speed = speed
this.topSpeed = topSpeed
this.color = color
this.accelerationType = accelerationType
this.headSize = headSize
}
step() {
let dx, dy
if (noise(this.t+20000) < 0.5) {
let step = -1.5
dx = random(-step, step)
dy = random(-step, step)
} else {
let step = 0.5;
dx = map(noise(this.t), 0, 1, -1, 1)*step;
dy = map(noise(this.t+10000), 0, 1, -1, 1)*step;
step(dt) {
let dx = this.acceleration.x
let dy = this.acceleration.y
if (this.accelerationType == "noise") {
dx = map(noise(this.t), 0, 1, -1, 1)
dy = map(noise(this.t + 10000), 0, 1, -1, 1)
} else if (this.accelerationType == "random") {
if (random(0, 1) < 0.15) {
dx = random(-1, 1)
dy = random(-1, 1)
}
}
this.x = (this.x + dx) % width
this.y = (this.y + dy) % height
this.t += 0.01
this.acceleration = createVector(dx, dy).normalize()
this.acceleration.setMag(this.speed)
this.velocity.add(p5.Vector.mult(this.acceleration, dt))
this.velocity.limit(this.topSpeed)
this.position.add(p5.Vector.mult(this.velocity, dt))
this.t += dt/2
this.position.x = (this.position.x + width) % width
this.position.y = (this.position.y + height) % height
this.trail.push(this.position.copy())
if (this.trail.length > this.trailSize) {
this.trail.splice(0, 1)
}
}
draw() {
stroke(0)
point(this.x, this.y)
stroke(this.color)
for (var p of this.trail) {
point(p)
}
circle(this.position.x, this.position.y, this.headSize)
// line(this.position.x, this.position.y, this.position.x+this.velocity.x, this.position.y+this.velocity.y)
}
}
let walker
let speedUp = 0
let creatures = []
let speedUpSlider
function setup() {
createCanvas(640, 240)
background(255)
walker = new Walker(320, 120)
for (let i = 0; i < 20; i++) {
creatures.push(new Walker(
createVector(random(0, width), random(0, height)),
10,
10,
200,
color(20, 200, 20),
"noise",
3
))
}
for (let i = 0; i < 50; i++) {
creatures.push(new Walker(
createVector(random(0, width), random(0, height)),
400,
50,
10,
color(20, 20, 20),
"random",
1
))
}
speedUpSlider = createSlider(0, 10, 0)
speedUpSlider.changed(() => {
speedUp = speedUpSlider.value()
})
}
function draw() {
walker.step();
walker.draw();
background(255)
// let fps = frameRate();
// text(Math.round(fps), 50, 50);
let dt = deltaTime / 1000
for (let i = 0; i < speedUp + 1; i++) {
for (var creature of creatures) {
creature.step(dt)
creature.draw()
}
}
}