【问题标题】:Processing Particle加工颗粒
【发布时间】:2016-05-29 15:31:18
【问题描述】:

我一直在尝试对这个项目做点什么,但到目前为止总是失败:) 所以决定在这里问:)

我希望粒子围绕 Rock 类中的椭圆,而不是穿过它,而是围绕它,就像河流中的一块岩石,水在它周围流动。有什么建议 ?

int NUM_PARTICLES = 1000;
ParticleSystem p;
Rock r;
void setup()
{
  smooth();
  fullScreen(P2D);
  //size(700,700,P2D);
  //background(0);
  p = new ParticleSystem();
  r = new Rock();
}

void draw()
{
  background(0);
  p.update();
  p.render();
  r.rock();

}

float speed = 1;
class Particle
{
  PVector position, velocity;

  Particle()
  {
    position = new PVector(random(width),random(height));
    velocity = new PVector();
  }

  void update()
  {
    velocity.x = speed*(noise(position.y));
    velocity.y = speed*(noise(position.x));
    position.add(velocity);

    if(position.x<0)position.x+=width;
    if(position.x>width)position.x-=width;
    if(position.y<0)position.y+=height;
    if(position.y>height)position.y-=height;
  }

  void render()
  {
    stroke(0, 0, 255, 80);
    line(position.x,position.y,position.x-velocity.x,position.y-velocity.y);
  }
}

class ParticleSystem
{
  Particle[] particles;

  ParticleSystem()
  {
    particles = new Particle[NUM_PARTICLES];
    for(int i = 0; i < NUM_PARTICLES; i++)
    {
      particles[i]= new Particle();
    }
  }

  void update()
  {
    for(int i = 0; i < NUM_PARTICLES; i++)
    {
      particles[i].update();
    }
  }

  void render()
  {
    for(int i = 0; i < NUM_PARTICLES; i++)
    {
      particles[i].render();
    }
  }
}
class Rock{

  void rock()
  {
  noFill();
  stroke(255);
  strokeWeight(4);
  ellipse(mouseX,mouseY,50,50);

}



}

编辑:1

我昨天做了一些自己的工作,我已经接近我想要的了,但仍然有一些视觉问题。我想摆脱流程的边缘,当我移动鼠标时,我仍然可以看到从力的椭圆线。这是结果。

int NUM_PARTICLES = 9000;
ParticleSystem p;
Rock r;
void setup()
{
  smooth();
  size(700,700,P2D);
  p = new ParticleSystem();
  r = new Rock();
}

void draw()
{
  background(0);
  p.update();
  p.render();
  r.rock();

}

float speed = 2;
float rad = 100;
class Particle
{
  PVector position, velocity;
  float initialPosY;

  Particle()
  {
    position = new PVector(random(width), random(height));
    initialPosY = position.y;
    velocity = new PVector();
  }

  void update()
  {

    velocity.x = speed;
    velocity.y = 0;

    float d = dist (position.x, position.y, mouseX, mouseY);
    if (d < rad) {
      float force = map(d, 0, rad, speed, 0);
      if (position.x < mouseX) {
        if (position.y < mouseY) {
          velocity.y = -force;
        } else {
          velocity.y = force;
        }
      } else {
        if (position.y < mouseY) {
          velocity.y = force;
        } else {
          velocity.y = -force;
        }
      }
      position.add(velocity);
    } else {
      position = new PVector(position.x+speed, initialPosY);
    }



    if (position.x<0)position.x+=width;
    if (position.x>width)position.x-=width;
    if (position.y<0)position.y+=height;
    if (position.y>height)position.y-=height;
  }

  void render()
  {
    stroke(255, 255, 255, 80);
    point(position.x, position.y);
  }
}

class ParticleSystem
{
  Particle[] particles;

  ParticleSystem()
  {
    particles = new Particle[NUM_PARTICLES];
    for (int i = 0; i < NUM_PARTICLES; i++)
    {
      particles[i]= new Particle();
    }
  }

  void update()
  {
    for (int i = 0; i < NUM_PARTICLES; i++)
    {
      particles[i].update();
    }
  }

  void render()
  {
    for (int i = 0; i < NUM_PARTICLES; i++)
    {
      particles[i].render();
    }
  }
}

class Rock{

  void rock()
  {
  noFill();
  stroke(255);
  strokeWeight(4);
  ellipse(mouseX,mouseY,50,50);

}



}

【问题讨论】:

    标签: processing noise particles


    【解决方案1】:

    让我们从更基本的开始:

    PVector position;
    PVector speed;
    
    void setup() {
      size(500, 500);
      position = new PVector(250, 0);
      speed = new PVector(0, 1);
    }
    
    void draw() {
    
      background(0);
    
      ellipse(position.x, position.y, 20, 20);
    
      position.add(speed);
    
      if (position.y > height) {
        position.y = 0;
      }
    
      if (position.x < 0) {
        position.x = width;
      } else if (position.x > width) {
        position.x = 0;
      }
    }
    

    既然我们有了这个,我们需要将您的问题分解为更小的步骤。

    第 1 步:在草图中添加“岩石”。让我们把我们的放在鼠标位置:

    void draw() {
    
      background(0);
    
      fill(0, 255, 0);
      ellipse(mouseX, mouseY, 100, 100);
    
      fill(0, 0, 255);
      ellipse(position.x, position.y, 20, 20);
    
      position.add(speed);
    
      //rest of code unchanged
    

    第 2 步: 添加确定粒子何时接近岩石的逻辑。现在,只需做一些简单的事情,比如改变岩石的颜色:

      if(dist(position.x, position.y, mouseX, mouseY) < 100){
        fill(255, 0, 0);
      }
      else{
        fill(0, 255, 0);
      }
    
      ellipse(mouseX, mouseY, 100, 100);
    

    第 3 步: 现在我们知道粒子何时靠近岩石,添加用于在岩石周围移动粒子的逻辑。这是一个非常基本的方法:

      if (dist(position.x, position.y, mouseX, mouseY) < 100) {
        fill(255, 0, 0);
        if (position.x < mouseX) {
          position.x--;
        } else {
          position.x++;
        }
      } else {
        fill(0, 255, 0);
      }
    

    你可以让这个逻辑变得更复杂,我建议你尝试一下,直到你找到合适的效果。

    把它们放在一起,它看起来像这样:

    PVector position;
    PVector speed;
    
    void setup() {
      size(500, 500);
      position = new PVector(250, 0);
      speed = new PVector(0, 1);
    }
    
    void draw() {
    
      background(0);
    
      if (dist(position.x, position.y, mouseX, mouseY) < 100) {
        fill(255, 0, 0);
        if (position.x < mouseX) {
          position.x--;
        } else {
          position.x++;
        }
      } else {
        fill(0, 255, 0);
      }
    
      ellipse(mouseX, mouseY, 100, 100);
    
      fill(0, 0, 255);
      ellipse(position.x, position.y, 20, 20);
    
      position.add(speed);
    
      if (position.y > height) {
        position.y = 0;
      }
    
      if (position.x < 0) {
        position.x = width;
      } else if (position.x > width) {
        position.x = 0;
      }
    }
    

    【讨论】:

    • 谢谢 :) 但我已经做了一些更改,也许你可以给我一些关于我当前问题的想法
    • @PoYo 如果您有其他问题,请发布一个新问题(与此问题分开发布一个新帖子)以及MCVE。如果你尝试一些事情而不是简单地告诉我们你想要什么,你也会有更好的运气。
    • 其实我回答了我自己的问题,我打算按照你的建议做一个新帖子,但有人评论了我的答案并告诉我我不应该回答我的问题而只是添加它作为对我原始帖子的编辑。
    • @PoYo 如果您对自己的问题有答案,请将其作为对此问题的答案发布。如果您对此问题有后续问题,请将其作为新问题发布在其自己的帖子中。
    【解决方案2】:

    我昨天做了一些自己的工作,我已经接近我想要的了,但仍然有一些视觉问题。我想摆脱流程的边缘,当我移动鼠标时,我仍然可以看到从力的椭圆线。这是结果。

    int NUM_PARTICLES = 9000;
    ParticleSystem p;
    Rock r;
    void setup()
    {
      smooth();
      size(700,700,P2D);
      p = new ParticleSystem();
      r = new Rock();
    }
    
    void draw()
    {
      background(0);
      p.update();
      p.render();
      r.rock();
    
    }
    
    float speed = 2;
    float rad = 100;
    class Particle
    {
      PVector position, velocity;
      float initialPosY;
    
      Particle()
      {
        position = new PVector(random(width), random(height));
        initialPosY = position.y;
        velocity = new PVector();
      }
    
      void update()
      {
    
        velocity.x = speed;
        velocity.y = 0;
    
        float d = dist (position.x, position.y, mouseX, mouseY);
        if (d < rad) {
          float force = map(d, 0, rad, speed, 0);
          if (position.x < mouseX) {
            if (position.y < mouseY) {
              velocity.y = -force;
            } else {
              velocity.y = force;
            }
          } else {
            if (position.y < mouseY) {
              velocity.y = force;
            } else {
              velocity.y = -force;
            }
          }
          position.add(velocity);
        } else {
          position = new PVector(position.x+speed, initialPosY);
        }
    
    
    
        if (position.x<0)position.x+=width;
        if (position.x>width)position.x-=width;
        if (position.y<0)position.y+=height;
        if (position.y>height)position.y-=height;
      }
    
      void render()
      {
        stroke(255, 255, 255, 80);
        point(position.x, position.y);
      }
    }
    
    class ParticleSystem
    {
      Particle[] particles;
    
      ParticleSystem()
      {
        particles = new Particle[NUM_PARTICLES];
        for (int i = 0; i < NUM_PARTICLES; i++)
        {
          particles[i]= new Particle();
        }
      }
    
      void update()
      {
        for (int i = 0; i < NUM_PARTICLES; i++)
        {
          particles[i].update();
        }
      }
    
      void render()
      {
        for (int i = 0; i < NUM_PARTICLES; i++)
        {
          particles[i].render();
        }
      }
    }
    
    class Rock{
    
      void rock()
      {
      noFill();
      stroke(255);
      strokeWeight(4);
      ellipse(mouseX,mouseY,50,50);
    
    }
    
    
    
    }
    

    【讨论】:

    • 您应该将此作为编辑添加到您的问题中,而不是答案,因为它不是答案
    猜你喜欢
    • 2018-07-27
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2020-04-07
    • 2016-06-14
    相关资源
    最近更新 更多