【问题标题】:3D boids escape bounding box, p5js3D boids 逃逸边界框,p5js
【发布时间】:2021-09-14 15:20:33
【问题描述】:

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

我们将不胜感激。

【问题讨论】:

  • 在一个物体超过一个边界条件后确定新的期望速度时,为什么要使用 maxspeed 这么多?您能否描述一下当您的 boid 到达边界框边缘时您希望看到的行为?
  • 您知道,console.log 函数会大大减慢您的程序。在您的最终版本中,您必须删除它。不确定您是否知道,但如果您知道,您可以忽略此评论。

标签: 3d p5.js bounding-box boids artificial-life


【解决方案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); //-++
    }

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

【讨论】:

  • 很抱歉没能早点回来;谢谢你,这很清楚,而且完全有道理——不知道为什么我的大脑不只是去那里。这是完美的;谢谢。
  • 欢迎来到 Stack Overflow。如果您的问题得到解决,通常会接受答案,这样其他人就知道您已经有了答案。 @Paul Wheeler 似乎解决了您的问题,所以我建议您接受他的回答。
猜你喜欢
  • 2015-06-10
  • 2012-03-23
  • 1970-01-01
  • 2013-04-01
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多