Processing 植绒草图:如何将三角形(BOID)外观更改为png图像

Processing 植绒草图:如何将三角形(BOID)外观更改为png图像,processing,Processing,我想知道是否有办法将“粒子”的外观更改为我创建的图像。我在处理库中找到了这个草图,我一直在想如何将我创造的鸟类上传到草图中 有人愿意帮我弄清楚这是否可能吗? 谢谢大家! //https://processing.org/examples/flocking.html Flock flock; void setup() { size(640, 360); flock = new Flock(); // Add an initial set of boids into the s

我想知道是否有办法将“粒子”的外观更改为我创建的图像。我在处理库中找到了这个草图,我一直在想如何将我创造的鸟类上传到草图中

有人愿意帮我弄清楚这是否可能吗? 谢谢大家!

//https://processing.org/examples/flocking.html    
Flock flock;

void setup() {
  size(640, 360);
  flock = new Flock();
  // Add an initial set of boids into the system
  for (int i = 0; i < 150; i++) {
    flock.addBoid(new Boid(width/2,height/2));
  }
}

void draw() {
  background(50);
  flock.run();
}

// Add a new boid into the System
void mousePressed() {
  flock.addBoid(new Boid(mouseX,mouseY));
}



// The Flock (a list of Boid objects)

class Flock {
  ArrayList<Boid> boids; // An ArrayList for all the boids

  Flock() {
    boids = new ArrayList<Boid>(); // Initialize the ArrayList
  }

  void run() {
    for (Boid b : boids) {
      b.run(boids);  // Passing the entire list of boids to each boid individually
    }
  }

  void addBoid(Boid b) {
    boids.add(b);
  }

}




// The Boid class

class Boid {

  PVector location;
  PVector velocity;
  PVector acceleration;
  float r;
  float maxforce;    // Maximum steering force
  float maxspeed;    // Maximum speed

    Boid(float x, float y) {
    acceleration = new PVector(0, 0);

    // This is a new PVector method not yet implemented in JS
    // velocity = PVector.random2D();

    // Leaving the code temporarily this way so that this example runs in JS
    float angle = random(TWO_PI);
    velocity = new PVector(cos(angle), sin(angle));

    location = new PVector(x, y);
    r = 2.0;
    maxspeed = 2;
    maxforce = 0.03;
  }

  void run(ArrayList<Boid> boids) {
    flock(boids);
    update();
    borders();
    render();
  }

  void applyForce(PVector force) {
    // We could add mass here if we want A = F / M
    acceleration.add(force);
  }

  // We accumulate a new acceleration each time based on three rules
  void flock(ArrayList<Boid> boids) {
    PVector sep = separate(boids);   // Separation
    PVector ali = align(boids);      // Alignment
    PVector coh = cohesion(boids);   // Cohesion
    // Arbitrarily weight these forces
    sep.mult(1.5);
    ali.mult(1.0);
    coh.mult(1.0);
    // Add the force vectors to acceleration
    applyForce(sep);
    applyForce(ali);
    applyForce(coh);
  }

  // Method to update location
  void update() {
    // Update velocity
    velocity.add(acceleration);
    // Limit speed
    velocity.limit(maxspeed);
    location.add(velocity);
    // Reset accelertion to 0 each cycle
    acceleration.mult(0);
  }

  // A method that calculates and applies a steering force towards a target
  // STEER = DESIRED MINUS VELOCITY
  PVector seek(PVector target) {
    PVector desired = PVector.sub(target, location);  // A vector pointing from the location to the target
    // Scale to maximum speed
    desired.normalize();
    desired.mult(maxspeed);

    // Above two lines of code below could be condensed with new PVector setMag() method
    // Not using this method until Processing.js catches up
    // desired.setMag(maxspeed);

    // Steering = Desired minus Velocity
    PVector steer = PVector.sub(desired, velocity);
    steer.limit(maxforce);  // Limit to maximum steering force
    return steer;
  }

  void render() {
    // Draw a triangle rotated in the direction of velocity
    float theta = velocity.heading2D() + radians(90);
    // heading2D() above is now heading() but leaving old syntax until Processing.js catches up

    fill(200, 100);
    stroke(255);
    pushMatrix();
    translate(location.x, location.y);
    rotate(theta);
    beginShape(TRIANGLES);
    vertex(0, -r*2);
    vertex(-r, r*2);
    vertex(r, r*2);
    endShape();
    popMatrix();
  }

  // Wraparound
  void borders() {
    if (location.x < -r) location.x = width+r;
    if (location.y < -r) location.y = height+r;
    if (location.x > width+r) location.x = -r;
    if (location.y > height+r) location.y = -r;
  }

  // Separation
  // Method checks for nearby boids and steers away
  PVector separate (ArrayList<Boid> boids) {
    float desiredseparation = 25.0f;
    PVector steer = new PVector(0, 0, 0);
    int count = 0;
    // For every boid in the system, check if it's too close
    for (Boid other : boids) {
      float d = PVector.dist(location, other.location);
      // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
      if ((d > 0) && (d < desiredseparation)) {
        // Calculate vector pointing away from neighbor
        PVector diff = PVector.sub(location, other.location);
        diff.normalize();
        diff.div(d);        // Weight by distance
        steer.add(diff);
        count++;            // Keep track of how many
      }
    }
    // Average -- divide by how many
    if (count > 0) {
      steer.div((float)count);
    }

    // As long as the vector is greater than 0
    if (steer.mag() > 0) {
      // First two lines of code below could be condensed with new PVector setMag() method
      // Not using this method until Processing.js catches up
      // steer.setMag(maxspeed);

      // Implement Reynolds: Steering = Desired - Velocity
      steer.normalize();
      steer.mult(maxspeed);
      steer.sub(velocity);
      steer.limit(maxforce);
    }
    return steer;
  }

  // Alignment
  // For every nearby boid in the system, calculate the average velocity
  PVector align (ArrayList<Boid> boids) {
    float neighbordist = 50;
    PVector sum = new PVector(0, 0);
    int count = 0;
    for (Boid other : boids) {
      float d = PVector.dist(location, other.location);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.velocity);
        count++;
      }
    }
    if (count > 0) {
      sum.div((float)count);
      // First two lines of code below could be condensed with new PVector setMag() method
      // Not using this method until Processing.js catches up
      // sum.setMag(maxspeed);

      // Implement Reynolds: Steering = Desired - Velocity
      sum.normalize();
      sum.mult(maxspeed);
      PVector steer = PVector.sub(sum, velocity);
      steer.limit(maxforce);
      return steer;
    } 
    else {
      return new PVector(0, 0);
    }
  }

  // Cohesion
  // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
  PVector cohesion (ArrayList<Boid> boids) {
    float neighbordist = 50;
    PVector sum = new PVector(0, 0);   // Start with empty vector to accumulate all locations
    int count = 0;
    for (Boid other : boids) {
      float d = PVector.dist(location, other.location);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.location); // Add location
        count++;
      }
    }
    if (count > 0) {
      sum.div(count);
      return seek(sum);  // Steer towards the location
    } 
    else {
      return new PVector(0, 0);
    }
  }
}
//https://processing.org/examples/flocking.html    
羊群;
无效设置(){
尺寸(640360);
羊群=新羊群();
//向系统中添加一组初始BOID
对于(int i=0;i<150;i++){
flock.addBoid(新Boid(宽/2,高/2));
}
}
作废提款(){
背景(50);
flock.run();
}
//在系统中添加一个新的boid
void mousePressed(){
阿德博伊德(新博伊德(mouseX,mouseY));
}
//羊群(Boid对象列表)
班群{
ArrayList boids;//所有boids的ArrayList
羊群{
boids=new ArrayList();//初始化ArrayList
}
无效运行(){
for(Boid b:Boid){
b、 运行(boid);//将整个boid列表分别传递给每个boid
}
}
无效添加Boid(Boid b){
加入(b);
}
}
//博伊德班
类Boid{
PVector定位;
PVector速度;
PVector加速度;
浮子r;
float maxforce;//最大转向力
浮点最大速度;//最大速度
Boid(浮动x,浮动y){
加速度=新PVector(0,0);
//这是一个尚未在JS中实现的新PVector方法
//速度=PVector.random2D();
//以这种方式暂时保留代码,以便此示例在JS中运行
浮动角度=随机(两个π);
速度=新的PVector(cos(角度),sin(角度));
位置=新的PVector(x,y);
r=2.0;
maxspeed=2;
最大力=0.03;
}
无效运行(ArrayList boids){
羊群(博伊兹);
更新();
边界();
render();
}
无效应用力(PVector力){
//如果我们想要A=F/M,我们可以在这里加上质量
加速度。加(力);
}
//我们每次根据三条规则累积一个新的加速度
空群(ArrayList boids){
PVector sep=分离(boids);//分离
PVector ali=align(boids);//对齐
PVector coh=内聚(boids);//内聚
//任意加重这些力
九月(1.5);
阿里·穆特(1.0);
coh.mult(1.0);
//将力向量添加到加速度
应用力(sep);
applyForce(ali);
应用力(coh);
}
//方法更新位置
无效更新(){
//更新速度
速度加(加速度);
//极限速度
速度极限(最大速度);
位置。添加(速度);
//每个循环将加速度重置为0
加速度mult(0);
}
//一种计算并向目标施加转向力的方法
//转向=所需的负速度
PVector搜索(PVector目标){
所需PVector=PVector.sub(目标,位置);//从位置指向目标的向量
//缩放到最大速度
所需的。规范化();
所需。mult(最大速度);
//下面的两行代码可以用新的PVector setMag()方法压缩
//在Processing.js赶上之前不使用此方法
//所需的设定值(最大速度);
//转向=所需的负速度
PVector转向=PVector.sub(所需速度);
转向。限制(最大力);//限制到最大转向力
返回转向;
}
void render(){
//画一个沿速度方向旋转的三角形
floatθ=速度头2d()+弧度(90);
//上面的heading2D()现在是heading(),但在Processing.js赶上之前保留旧语法
填充(200100);
中风(255);
pushMatrix();
翻译(位置x,位置y);
旋转(θ);
beginShape(三角形);
顶点(0,-r*2);
顶点(-r,r*2);
顶点(r,r*2);
endShape();
popMatrix();
}
//概括的
无效边框(){
如果(位置x<-r)位置x=宽度+r;
如果(位置y<-r)位置y=高度+r;
如果(location.x>width+r)location.x=-r;
如果(location.y>height+r)location.y=-r;
}
//分离
//方法检查附近的锅炉并驶离
PVector分离(ArrayList boids){
所需浮球分离度=25.0f;
PVector转向=新PVector(0,0,0);
整数计数=0;
//对于系统中的每个boid,检查是否太近
用于(Boid其他:Boid){
浮动d=PVector.dist(位置,其他位置);
//如果距离大于0且小于任意量(当您是自己时为0)
如果((d>0)和((d0){
转向div((浮动)计数);
}
//只要向量大于0
如果(steer.mag()>0){
//下面的前两行代码可以用新的PVector setMag()方法压缩
//在Processing.js赶上之前不使用此方法
//转向设定值(最大速度);
//机具雷诺数:转向=所需-速度
steer.normalize();
steer.mult(最大速度);
转向(速度);
转向极限(最大力);
}
返回转向;
}
//对齐
//对于系统中每个附近的boid,计算平均速度
PVector对齐(ArrayList boids){
float nexturerdist=50;
PVector和=新PVector(0,0);
整数计数=0;
用于(Boid其他:Boid){
浮动d=PVector.dist(位置,其他位置);
如果((d>0)和&(d0){
总和div((浮动)计数);
//下面的前两行代码可以用新的PVector setMag()方法压缩
  void render() {
    // Draw a triangle rotated in the direction of velocity
    float theta = velocity.heading2D() + radians(90);
    // heading2D() above is now heading() but leaving old syntax until Processing.js catches up

    fill(200, 100);
    stroke(255);
    pushMatrix();
    translate(location.x, location.y);
    rotate(theta);
    beginShape(TRIANGLES);
    vertex(0, -r*2);
    vertex(-r, r*2);
    vertex(r, r*2);
    endShape();
    popMatrix();
  }
    texture(yourImageHere);
    vertex(0, -r*2);
    vertex(-r, r*2);
    vertex(r, r*2);