Can anyone please help me how to achieve this particle effect? (repulsion)

Please help me i want to achieve this particle effect using mouse pointer.

Thanks

Tagged:

Answers

  • Well start by posting the code you have already. Hopefully it has a Particle class and setup() and draw(). If you don't have any, attempt to write some and post the attempt. We generally don't help people who put zero effort in.

  • edited October 2017

    I am new to processing... and i have done code to generate 500 particles so far. I am posting below. @TfGuy44 can you please help me with this?

    ParticleSystem ps;
    
          void setup() {
    
            size(640, 360);
    
            ps = new ParticleSystem(new PVector(width/2, 50));
    
            for (int i=0; i<500; i++)
    
            {
    
              ps.addParticle();
    
            }
    
      }
    
    
      void draw() {
    
        background(0);
    
    
        ps.run();
    
      }
    
      class ParticleSystem {
        ArrayList<Particle> particles;
        PVector origin;
    
        ParticleSystem(PVector position) {
          origin = position.copy();
          particles = new ArrayList<Particle>();
        }
    
        void addParticle() {
          particles.add(new Particle(origin));
        }
    
        void run() {
          for (int i = particles.size()-1; i >= 0; i--) {
            Particle p = particles.get(i);
            p.run();
            if (p.isDead()) {
              //    particles.remove(i);
            }
          }
        }
      }
    
    
      class Particle {
        PVector position;
        PVector velocity;
        PVector acceleration;
        float lifespan;
    
        Particle(PVector l) {
          acceleration = new PVector(0, 0);
          velocity = new PVector(random(-0.0001, 0.00001), random(-0.001, 0.0001));
    
          l=new PVector(random(0, 640), random(0, 480));
          position = l.copy();
          lifespan = 255.0;
        }
    
        void run() {
          update();
          display();
        }
    
        // Method to update position
        void update() {
          velocity.add(acceleration);
          position.add(velocity);
          //   lifespan -= 1.0;
        }
    
        // Method to display
        void display() {
          stroke(255, lifespan);
          fill(255, lifespan);
    
          ellipse(position.x, position.y, 8, 8);
        }
    
        // Is the particle still useful?
        boolean isDead() {
          if (lifespan < 0.0) {
            return true;
          } else {
            return false;
          }
        }
      }
    
  • https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest

    If you can't format it right I can't copy it properly. If I can't copy it properly I can't paste it at all. If I can't paste it, I can't run it. if I can't run it, I can't help you.

    In short, failing to format your code properly means you are unlikely to get any help. Edit you post! @-)

  • @TfGuy44 Thank you for your guidance.. i have formatted my code as you said... now can you please look at it.. and i am new to processing and particles so i have done this so far... please help me..

  • This is as close as I'm taking it. You can tweek it more if you like.

    ParticleSystem ps;
    
    void setup() {
      size(640, 360);
      ps = new ParticleSystem(new PVector(width/2, 50));
      for (int i=0; i<500; i++)
      {
        ps.addParticle();
      }
    }
    
    
    void draw() {
      background(0);
      if(mousePressed){
        ps.move_away_from(mouseX, mouseY);
      }
      ps.run();
    }
    
    class ParticleSystem {
      ArrayList<Particle> particles;
      PVector origin;
    
      ParticleSystem(PVector position) {
        origin = position.copy();
        particles = new ArrayList<Particle>();
      }
    
      void addParticle() {
        particles.add(new Particle(origin));
      }
    
      void run() {
        for (int i = particles.size()-1; i >= 0; i--) {
          Particle p = particles.get(i);
          p.run();
    //      if (p.isDead()) {
            //    particles.remove(i);
    //      }
        }
      }
      void move_away_from( float x, float y){
        for(Particle p : particles){
          float d = dist(x,y,p.position.x, p.position.y);
          if( d < 200 ){ // Only move points near click.
            p.velocity.x += map(d,0,200,0.5,0.1)*(p.position.x - x);
            p.velocity.y += map(d,0,200,0.5,0.1)*(p.position.y - y);
          }
        }
      }
    
    }
    
    
    class Particle {
      PVector position;
      PVector velocity;
      PVector acceleration;
      float lifespan;
    
      PVector home;
    
      Particle(PVector l) {
        acceleration = new PVector(0, 0);
        velocity = new PVector(0,0);//random(-0.0001, 0.00001), random(-0.001, 0.0001));
    
        l=new PVector(random(0, 640), random(0, 480));
        position = l.copy();
        home = position.copy();
        lifespan = 255.0;
      }
    
      void run() {
        update();
        display();
      }
    
      // Method to update position
      void update() {
        acceleration.x = -0.01*(position.x - home.x);
        acceleration.y = -0.01*(position.y - home.y);
        velocity.add(acceleration);
        velocity.mult(0.9);
        position.add(velocity);
        //   lifespan -= 1.0;
      }
    
      // Method to display
      void display() {
        noStroke();//stroke(255, lifespan);
        //fill(255, lifespan);
        float notblue = map(abs(velocity.mag()),0,5,255,0); 
        fill(notblue,notblue,255);
        ellipse(position.x, position.y, 8, 8);
      }
    
      // Is the particle still useful?
      boolean isDead() {
        if (lifespan < 0.0) {
          return true;
        } else {
          return false;
        }
      }
    }
    

    The real trick is to check if the mouse is pressed every frame (in draw()), and if it is, apply some forces to (well, adjust the velocity of) any Particle that is near the mouse press. Then you need to dampen their velocities, and also have them accelerate back to their home position. Finally, their color is now based on their velocity too.

Sign In or Register to comment.