问题描述
当前,我正在尝试使用LWJGL在3d环境中实现boid。在关于this的博伊德论文之后,我编写了一个基本的植绒模拟程序,每个博伊德都能够基于相邻的博伊德进行对齐,内聚和回避。然而,问题在于,在本文中描述了每个投标仅具有有限的视野,这意味着它无法直接检测到其后面的任何东西。我创建的代码只是检查围绕boid半径内的所有周围区域,这意味着甚至直接位于其自身后面的对象也将被检测到,从而影响转向。有什么方法可以限制每个投标的视野。
为提高性能而对我的代码进行的其他更改和更正。
检测代码
for(Boids other : flock){
if(other != this){
Vector3f offset = other.position.subtract(this.position);
float d = offset.length();
if(d < perception){
//Steering calculations
}
}
}
当前的植绒模式(注意:由于感知半径问题,波多斯穿过中心形成漩涡状的模式)
完整的二进制代码
public class Boids extends Entity {
Vector3f forwards;
Vector3f velocity;
Vector3f acceleration = new Vector3f();
float speed = 1f;
float maxforce = 0.1f;
float seperationMultiplier = 1f,alignmentMultiplier = 1f,cohesionMultiplier = 1f;
int perception = 15;
public Boids(TexturedModel model,Vector3f position,float rotX,float rotY,float rotZ,float scale) {
super(model,position,rotX,rotY,rotZ,scale);
Random rand = new Random();
this.velocity = new Vector3f(rand.nextFloat()*BoidHelper.getRandom(rand),rand.nextFloat()*BoidHelper.getRandom(rand),rand.nextFloat()*BoidHelper.getRandom(rand));
}
public void run(List<Boids> flock){
this.acceleration = new Vector3f(0,0);
flock(flock);
this.position = this.position.add(velocity);
this.velocity = this.velocity.add(acceleration);
}
public void flock(List<Boids> flock){
Vector3f alignment = align(flock).scale(alignmentMultiplier);
Vector3f cohesion = cohesion(flock).scale(cohesionMultiplier);
Vector3f seperation = seperation(flock).scale(seperationMultiplier);
this.acceleration = this.acceleration.add(seperation);
this.acceleration = this.acceleration.add(alignment);
this.acceleration = this.acceleration.add(cohesion);
}
public Vector3f align(List<Boids> flock){
int total = 0;
Vector3f steering = new Vector3f();
for(Boids other : flock){
if(other != this){
Vector3f offset = other.position.subtract(this.position);
float d = offset.length();
if(d < perception){
steering = steering.add(other.velocity);
total++;
}
}
}
if(total > 0){
steering = steering.divide(total);
steering = steering.normalize();
steering = steering.scale(speed);
steering = steering.subtract(this.velocity);
steering.limit(maxforce);
}
return steering;
}
public Vector3f cohesion(List<Boids> flock){
int total = 0;
Vector3f steering = new Vector3f();
for(Boids other : flock){
if(other != this){
Vector3f offset = other.position.subtract(this.position);
float d = offset.length();
if(d < perception){
steering = steering.add(other.position);
total++;
}
}
}
if(total > 0){
steering = steering.divide(total);
steering = steering.subtract(this.position);
steering = steering.normalize();
steering = steering.scale(speed);
steering = steering.subtract(this.velocity);
steering.limit(maxforce);
}
return steering;
}
public Vector3f seperation(List<Boids> flock){
int total = 0;
Vector3f steering = new Vector3f();
for(Boids other : flock){
if(other != this){
Vector3f offset = other.position.subtract(this.position);
float d = offset.length();
if(d < perception-2){
Vector3f diff = this.position.subtract(other.position);
diff = diff.normalize();
diff = diff.divide(d*d);
steering = steering.add(diff);
total++;
}
}
}
if(total > 0){
steering = steering.divide(total);
steering = steering.normalize();
steering = steering.scale(speed);
steering = steering.subtract(this.velocity);
steering.limit(maxforce);
}
return steering;
}
public Vector3f getVelocity(){
return this.velocity;
}
public Vector3f getAcceleration(){
return this.acceleration;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)