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();
}
}
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.
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:
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…
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;
}
}