Particles moving away from center, how?

Hello everyone!

First of all, I am quite a newby to Processing and coding in general, so my code might not look very 'professional.

I have created a program that has particles moving around. When the mouse is pressed, the particles move towards the top right corner of te screen, but I want all the particles to move away from the center of the screen. I have been struggling with this for the past two days, and I can’t seem to figure it out.

This is my first post, so I don’t know how to properly add my code to this topic. I am sorry if this is confusing. Does someone might know what I am doing wrong or what I could do to make this work?

This is my code:

//the particle class is originally in a different tab
class Particle {
  int x, y, particlewidth, particleheight; 
  boolean go = false;
  int n = 50;
  
  Particle(int xpos, int ypos, int pwidth, int pheight) {
    x = xpos;
    y = ypos;
    particlewidth = pwidth;
    particleheight = pheight;
  }
  
  void moving() {
    x = x + int(random(-20, 20));
    y = y + int(random(-20, 20));
  }
  
  void behavior() {
    PVector tr = new PVector (-1, -2, 2);
    PVector bl = new PVector (-6, -8, 6);
    PVector cross = tr.cross(bl);
    if (mousePressed) {
      x += cross.x;
      y += cross.y;
    }
  }
   
   
  void display(){  
    ellipse(x, y, 6, 6); 
  }
}

Particle[] particles;

void setup (){
  size(800, 800); 
  ellipseMode(CENTER);
  particles = new Particle[1500];
  
  for (int i = 0; i < particles.length; i++){
    particles[i] = new Particle(int(random(800)), int(random(800)), 8, 8); 
  }
}


void draw (){
  background(0);
  for(int i = 0; i < particles.length; i++){
    particles[i].behavior(); 
    particles[i].display();
    particles[i].moving();
  }
}
1 Like

same assignment?

Thank you for your reaction!

But no, that is not quite what I mean unfortunately. If I can explain myself better this time: I want to draw an ellipse in the center of the screen that is increasing in scale, and I want te particles to ‘move away’ from that ellipse. I want to create sort of a ‘black hole’ in the middle, like how a mosh-pit originates in a crowd.

I have the code to do exactly this with ‘mouseMoved’, so whereever the mouse is on the screen, the particles are not: the particles move away from the mouse. But now I want this type of behavior, but then with a centered ellipse.

I hope this is clear?

check on the

function, you can calculate the distance from center point to the object,
with variable radius of the center object, the distance is shorter by radius.
if that distance is smaller as ? a fixed minimum ? start moving the objects away.

now as that world is centered you might better move yourself into the middle:

void draw () {
  background(0);
  translate(width/2,height/2);
  draw_center_object();
...

but the particles need to be started like that too:

    particles[i] = new Particle(int(random(-400,400)), int(random(-400,400)), 8, 8);

and a start for the growing middle monster?

float Cr = 10.0;   // start radius center object

void draw_center_object() {
  Cr += 0.1;
  pushMatrix();
  pushStyle();
  fill(200,200,0);
  noStroke();
  ellipse(0, 0, Cr, Cr);
  popStyle();
  popMatrix();
}

in

void behavior() {

you need only the distance check and the increase of x,y of the “burning” objects

1 Like

Great. If you post that code we’ll know exactly what you want. In fact it might be as simple as that code with the effect centered in the middle of the sketch instead of at the mouse’s position. But without seeing that code, we have to start by writing the whole thing…

2 Likes

If you want the same effect but going away from the center, you need to create a vector that point from the center to your particule to know the direction in which it should go.

Here is the updated behavior() function

 void behavior() {
    PVector tr = new PVector (-1, -2, 2);
    PVector bl = new PVector (-6, -8, 6);
    PVector cross = tr.cross(bl);
    
    
    if (mousePressed) {
      PVector vec = new PVector(x - 400, y -400);
      vec.normalize();
      vec.mult(10);
      
      x += vec.x;
      y += vec.y;
    }
  }

amh, i left that open, but ok,
my idea was

  void behavior() {
  if ( dist(0,0,x,y) < Cr*2 ) {
      x = int(x*2);
      y = int(y*2);
    }
  }

as the moving criteria is the growing center object

Ho sorry I admit I read the previous post a bit quickly… My bad!

Thank you guys so much for all your reactions! This helps me a lot :slight_smile: