How to make particles go around an object instead of going through

edited May 2016 in Questions about Code

I've been trying to do something with this project I have but failed always so far :) so decided to ask here :)

I want the particles to go around the ellipse from Rock class, not through it but around it, like a rock inside a river that water flows around it. Any suggestions ?

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

}



}

Answers

  • I worked on a bit more and now I have almost what I want, I want it to be smoother I can see the lines of Force variable. And the flow is not visually so good.

    here is the new code

    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);
    
    }
    
    
    
    }
    
  • One idea, did you look at repel functions in shiffmans book nature of code

    There is a chapter on particles

    2nd idea - make a 2nd bigger circle (or row of them) to make it fluent. Those are invisble but influence the particles

Sign In or Register to comment.