如何使一个类粒子跟随另一个类粒子 [英] How can I Make that One Class Particle follows Another Class Particle
问题描述
我在处理"中将其编码.我想做的是食者遵循食物粒子,但是我不知道为什么它总是到达(0,0)坐标.
I coded it in Processing. What I want to do is that The Eater follows a food particle, but I don't know why it always goes to the (0,0) coordinate.
我正在尝试做一个生态系统模拟器",在处理中,因此该代码的作用是模拟一个生态系统,但规模很小.我想做的是食者遵循食物颗粒,但是我不知道为什么它总是到达(0,0)坐标.
I'm trying to do an "Ecosystem Simulator" in processing, so what this code does is simulates an ecosystem but a very small scale. What I want to do is that the Eater follows a food particle, but I dont know why it always goes to the (0,0) coordinate.
这是我的主标签:
int cantidadcom = 20;
int Pob_b1 = 1;
float targx;
float targy;
Comida[] comi = new Comida[cantidadcom];
Bio[] Bio = new Bio[cantidadcom];
int RandFood = int(random(0,comi.length));
void setup(){
size(800,800);
frameRate(60);
for(int i = 0; i<cantidadcom; i++){
comi[i] = new Comida();
}
for(int i = 0; i<Pob_b1; i++){
Bio[i] = new Bio();
}
}
void draw(){
PVector TargPos = new PVector(comi[RandFood].loc.x,comi[RandFood].loc.y);
float targx = TargPos.x;
float targy = TargPos.y;
background(255);
for(int i = 0; i<cantidadcom; i++){
comi[i].draw();
}
for(int i = 0; i<Pob_b1; i++){
Bio[i].draw();
}
println(TargPos.x,TargPos.y);
}
这是我的食物"标签:
class Comida{
PVector loc;
PVector vel;
PVector acc;
PVector target;
float x;
float y;
float diam;
float colour;
int topspeed =5;
Comida(){
x = random(0,width);
y = random(0,height);
loc = new PVector(x,y);
vel = new PVector(0,0);
diam = 10;
colour = random(0,200);
}
void update(){
target = new PVector(random(0,width),random(0,height));
acc = PVector.sub(target,loc);
acc.normalize();
float factor = random(1,3);
acc.mult(factor);
vel.add(acc);
//vel.add(random(-4,4),random(-4,4));
vel.limit(topspeed);
loc.add(vel);
}
void check(){
if ((loc.x<diam)||(loc.x>width-diam)){
vel.x = vel.x*-1;
}
if ((loc.y<diam)||(loc.y>height-diam)){
vel.y = vel.y*-1;
}
}
void display(){
fill(colour);
ellipse(loc.x,loc.y,diam,diam);
}
void draw(){
display();
update();
check();
}
}
class Comidatotal{
Comida[] c=new Comida[cantidadcom];
Comidatotal(){
for (int i = 0; i<c.length;i++){
c[i]= new Comida();
}
}
void display(){
for(int i = 0; i<c.length;i++){
c[i].draw();
}
}
}
这是我的食客"标签:
class Bio{
PVector loc;
PVector vel;
PVector acc;
PVector target;
float x;
float y;
float diam;
int colour;
int topspeed =5;
float tempx = comi[RandFood].loc.x;
float tempy = comi[RandFood].loc.y;
Bio(){
x = random(0,width);
y = random(0,height);
loc = new PVector(x,y);
vel = new PVector(0,0);
diam = 32;
}
void update(){
target=new PVector(targx,targy);
acc= PVector.sub(target,loc);
acc.normalize();
float factor=random(1,3);
acc.mult(factor);
//vel.add(random(-2,2),random(-2,2));
vel.add(acc);
vel.limit(topspeed);
loc.add(vel);
}
void check(){
if ((loc.x<diam)||(loc.x>width-diam)){
vel.x = vel.x*-1;
}
if ((loc.y<diam)||(loc.y>height-diam)){
vel.y = vel.y*-1;
}
}
void display(){
//fill(0,125,0);
noFill();
ellipse(loc.x,loc.y,diam,diam);
}
void draw(){
display();
update();
check();
}
}
我想要的是生物"物体遵循随机的"Comida/food".对象,但我不知道为什么它总是指向(0,0)坐标.
What I want is that the "Bio" object follows a random "Comida/food" object, but I don't know why it always goes to the (0,0) coordinate.
推荐答案
我认为问题可能出在主"选项卡的draw函数中:
I think the issue could be in your draw function in your Main tab:
float targx = TargPos.x;
float targy = TargPos.y;
以上几行应为:
targx = TargPos.x;
targy = TargPos.y;
在将新的目标值分配给局部变量之前,为了更新targx和targy全局变量.
In order to update your targx and targy global variables, before you were assigning the new target values to local variables.
这篇关于如何使一个类粒子跟随另一个类粒子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!