处理粒子

问题描述:

我一直在努力做这个项目我有什么,但总是失败到目前为止:)于是决定在这里问:)处理粒子

我希望粒子四处椭圆从岩类,而不是穿过它但是在它周围,就像河流里的水流动的岩石一样。有什么建议么 ?

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

} 



} 

我昨天做的就可以了一些我自己的工作,我很接近我想要的东西,仍然有一些视觉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); 

} 



} 
+0

您应该将此添加为您的问题的编辑,而不是答案,因为它不是答案 –

让我们先用一些更基本:

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

谢谢:)不过我已经做了一些改变,也许你可以给我一些想法对我目前的问题 – PoYo

+0

@PoYo如果你有一个不同的问题,请发布新问题(使一个新的职位分开这一个)以及[MCVE](http://*.com/help/mcve)。如果你尝试**而不是简单地告诉我们你想要什么,你也会有更好的运气。 –

+0

嗯,其实我回答了我自己的问题,我会做你的建议,做一个新的职位,但有人评论我的答案,并告诉我,我不应该回答我的问题,但只是将其添加为我的原始文章的编辑。 – PoYo