3D boids escape bounding box

155 views Asked by At

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?

1

There are 1 answers

2
Paul Wheeler On

This seems like it has better results:

if (this.position.x < d) {
  desired = createVector(this.maxspeed, this.velocity.y, this.velocity.z);
} else if (this.position.x > widthZone - d) {
  desired = createVector(-this.maxspeed, this.velocity.y, this.velocity.z); //-+-
}

if (this.position.y < d) {
  desired = createVector(this.velocity.x, this.maxspeed, this.velocity.z);
} else if (this.position.y > heightZone - d) {
  desired = createVector(this.velocity.x, -this.maxspeed, this.velocity.z); //+--
}

if (this.position.z < d) {
  desired = createVector(this.velocity.x, this.velocity.y, this.maxspeed);
} else if (this.position.z > depth - d) {
  desired = createVector(this.velocity.x, this.velocity.y, -this.maxspeed); //-++
}

Basically this only alters the desired velocity in the dimension(s) where the object has exceeded the boundary.