问题描述
我在 p5js 中的基因 3D boid 不断逃离它们的边界框。我一定做得不正确,可以使用一些帮助。这是一个 live sketch。
这是边界框代码:
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.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.veLocity.z);
} else if (this.position.z > depth - d) {
desired = createVector(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.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.maxspeed);
} else if (this.position.z > depth - d) {
desired = createVector(this.velocity.x,-this.maxspeed); //-++
}
基本上,这只会改变对象超出边界的维度中的所需速度。