0

我在 p5js 中的基因 3D boid 不断逃离它们的边界框。我一定做得不对,可以使用一些帮助。这是一个现场草图

这是边界框代码:

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); //-++
}

协助将不胜感激。

4

1 回答 1

1

这似乎有更好的结果:


    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); //-++
    }

基本上,这只会改变对象超出边界的维度中的所需速度。

于 2021-07-02T21:40:25.187 回答