我在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); //-++
}
如有协助,不胜感激。
这似乎有更好的结果:
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); //-++
}
基本上,这只会改变物体超过边界的维度上的期望速度。