首页 > 解决方案 > 我怎样才能使一个类粒子跟随另一个类粒子

问题描述

我在处理中对其进行了编码。我想做的是 The Eater 跟随一个食物粒子,但我不知道为什么它总是去 (0,0) 坐标。

我正在尝试在处理中做一个“生态系统模拟器”,所以这段代码所做的是模拟一个生态系统,但规模很小。我想做的是Eater跟随一个食物粒子,但我不知道为什么它总是去(0,0)坐标。

这是我的主标签:

    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();
        
      }
      }

我想要的是“Bio”对象跟随一个随机的“Comida/food”对象,但我不知道为什么它总是去(0,0)坐标。

标签: javaclassobjectattributesprocessing

解决方案


我认为问题可能出在主选项卡中的绘图功能中:

      float targx = TargPos.x;
      float targy = TargPos.y;

上面的行应该是:

      targx = TargPos.x;
      targy = TargPos.y;

为了更新您的 targx 和 targy 全局变量,在您将新目标值分配给局部变量之前。


推荐阅读