我在 p5js 中的遗传 3D boids 不断逃离它们的边界框。我一定做得不对,需要一些帮助。这是实时草图。
这是边界框代码:
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); //-++
}
有什么帮助吗?
这似乎有更好的结果:
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); //-++
}
基本上,这只会改变物体超出边界的维度中的所需速度。