处理粒子

时间:2016-05-29 15:31:19

标签: processing noise particles

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

我希望粒子绕着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);

}



}

2 个答案:

答案 0 :(得分: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步:添加&#34;摇滚&#34;到草图。让我们把鼠标放在鼠标位置:

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

答案 1 :(得分:0)

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

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

}



}