首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加工粒子

加工粒子
EN

Stack Overflow用户
提问于 2016-05-29 15:31:18
回答 2查看 204关注 0票数 0

我一直试图用这个项目来做一些事情,但到目前为止总是失败的:)所以决定在这里问:)

我想让粒子绕着椭圆从岩石类,不是通过它,而是围绕它,就像在河流中的岩石,水在它周围流动。有什么建议吗?

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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);

}



}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-01 08:11:25

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

代码语言:javascript
复制
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);

}



}
票数 0
EN

Stack Overflow用户

发布于 2016-05-31 13:27:36

让我们从一些更基本的东西开始:

代码语言:javascript
复制
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:在草图中添加一个“岩石”。让我们把我们的放在鼠标位置:

代码语言:javascript
复制
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:添加逻辑,确定粒子何时接近岩石。现在,只需做一些简单的事情,比如改变岩石的颜色:

代码语言:javascript
复制
  if(dist(position.x, position.y, mouseX, mouseY) < 100){
    fill(255, 0, 0);
  }
  else{
    fill(0, 255, 0);
  }

  ellipse(mouseX, mouseY, 100, 100);

步骤3:现在我们知道当粒子在岩石附近的时候,增加在岩石周围移动粒子的逻辑。下面是一个非常基本的方法:

代码语言:javascript
复制
  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);
  }

您可以使这个逻辑变得更加复杂,我建议您在找到正确的效果之前使用它。

把所有的东西放在一起,看起来是这样的:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37511697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档