My genetic 3D boids in p5js keep escaping their bounding box. I must not be doing it correctly, and could use some help. Here is a live sketch.
Here is the bounding box code:
if (this.position.x < d) {
desired = createVector(this.maxspeed, this.velocity.y, this.maxspeed);
} else if (this.position.x > widthZone - d) {
desired = createVector(-this.maxspeed, this.velocity.y, this.maxspeed); //-+-
}
if (this.position.y < d) {
desired = createVector(this.velocity.x, this.maxspeed, this.maxspeed);
} else if (this.position.y > heightZone - d) {
desired = createVector(this.velocity.x, -this.maxspeed, this.maxspeed); //+--
}
if (this.position.z < d) {
desired = createVector(this.maxspeed, this.maxspeed, this.velocity.z);
} else if (this.position.z > depth - d) {
desired = createVector(this.maxspeed, this.maxspeed, -this.velocity.z); //-++
}
Any assistance?
This seems like it has better results:
Basically this only alters the desired velocity in the dimension(s) where the object has exceeded the boundary.