improve ecosystem project
This commit is contained in:
parent
8d3c94ea3e
commit
4dbca97293
@ -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, we’re done!
|
|
||||||
if (r2 < probability) {
|
|
||||||
return r1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Walker {
|
class Walker {
|
||||||
constructor(x = 0, y = 0) {
|
constructor(
|
||||||
this.t = 0
|
position,
|
||||||
this.x = x
|
speed,
|
||||||
this.y = y
|
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() {
|
step(dt) {
|
||||||
let dx, dy
|
let dx = this.acceleration.x
|
||||||
if (noise(this.t+20000) < 0.5) {
|
let dy = this.acceleration.y
|
||||||
let step = -1.5
|
if (this.accelerationType == "noise") {
|
||||||
dx = random(-step, step)
|
dx = map(noise(this.t), 0, 1, -1, 1)
|
||||||
dy = random(-step, step)
|
dy = map(noise(this.t + 10000), 0, 1, -1, 1)
|
||||||
} else {
|
} else if (this.accelerationType == "random") {
|
||||||
let step = 0.5;
|
if (random(0, 1) < 0.15) {
|
||||||
dx = map(noise(this.t), 0, 1, -1, 1)*step;
|
dx = random(-1, 1)
|
||||||
dy = map(noise(this.t+10000), 0, 1, -1, 1)*step;
|
dy = random(-1, 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.x = (this.x + dx) % width
|
this.acceleration = createVector(dx, dy).normalize()
|
||||||
this.y = (this.y + dy) % height
|
this.acceleration.setMag(this.speed)
|
||||||
this.t += 0.01
|
|
||||||
|
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() {
|
draw() {
|
||||||
stroke(0)
|
stroke(this.color)
|
||||||
point(this.x, this.y)
|
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() {
|
function setup() {
|
||||||
createCanvas(640, 240)
|
createCanvas(640, 240)
|
||||||
background(255)
|
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() {
|
function draw() {
|
||||||
walker.step();
|
background(255)
|
||||||
walker.draw();
|
|
||||||
|
// 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user