处理粒子

Processing Particle

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

我希望粒子绕着 Rock class 的椭圆运行,不是穿过它而是绕着它运行,就像河流中的岩石被水流过一样。有什么建议么 ?

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

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

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

}



}

让我们从更基础的东西开始:

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: 添加一个 "rock" 到草图。让我们把我们的放在鼠标位置:

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

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

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

}



}