Hi there,
I am working on a university project with processing. This is a first time for me, so bear with me… We are creating a particle system (of sorts) that will eventually trigger sounds etc. The code I have attached below is stripped back to zoom in on the encountered problem and to ensure it doesn’t rely on external files.
The problem is that we have 2 classes of particles (named a larger blue ‘ball’ and smaller pink ‘blip’). Both collide with particles of the same type, but not with each other. We would like them to also collide with one another. in my online research there are potentially 2 ways to do this:
-
Create a parent class ‘particle’ of which particle types ‘ball’ and ‘blip’ extend this class
-
Somehow recall the value of coordinates of one class of particle, and use them to define a collide function
I have tried both approaches cant seem to get either of the ground. Some pointers or code would be greatly appreciated!
Thanks a lot!
James
int numBalls = 4;
Ball[] balls = new Ball[numBalls];
int numBlips = 15;
Blip[] blips = new Blip[numBlips];
float spring = 1.6;
float gravity = 0.001;
float friction = -0.8;
void setup() {
frameRate(80);
size(displayWidth, displayHeight, OPENGL, P2D);
for (int i = 0; i < numBalls; i++)
balls[i] = new Ball(random(300, 600), random(450, 650), (80), i, balls);
for (int a = 0; a < numBlips; a++)
blips[a] = new Blip(random(300, 600), random(450, 650), (10), a, blips);
smooth();
}
int index = 0;
void draw() {
background(0, 10);
for (Ball ball : balls) {
ball.collide();
ball.move();
ball.display();
}
for (Blip blip : blips) {
blip.collide();
blip.move();
blip.display();
}
}
class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring*1.1;
float ay = (targetY - others[i].y) * spring*1.1;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move() {
vy += gravity;
x += vx;
y += vy;
if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
//sound[index].trigger();
} else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= friction;
;
} else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
;
}
}
void display() {
float disc=40;
gradientdisc(
x,
y,
40,
disc,
color(0, 0, 255),
color(255, 255, 255)
);
noStroke();
}
void gradientdisc( float x, float y, float radiusX, float radiusY, int fromC, int toC )
{
noStroke();
beginShape(TRIANGLE_STRIP);
int halfC = lerpColor(fromC, toC, 0.4);
for (float theta=0; theta<TWO_PI; theta+=TWO_PI/36)
{
fill(halfC);
vertex(x, y);
if ( theta <= PI )
fill(lerpColor(fromC, toC, (theta%PI)/PI ));
else
fill(lerpColor(toC, fromC, (theta%PI)/PI ));
vertex(x+radiusX*cos(theta), y+radiusY*sin(theta));
}
endShape();
float disc2=50;
stroke(0, 10);
strokeWeight(1);
noFill();
ellipse(x, y, 100, disc2*2);
}
}
class Blip {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Blip[] others;
Blip(float xin, float yin, float din, int idin, Blip[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBlips; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring * 1.2;
float ay = (targetY - others[i].y) * spring * 1.2;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move() {
vy += gravity * 2;
x += vx;
y += vy;
if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
} else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= friction;
} else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
stroke(255, 0, 144);
strokeWeight(1);
noFill();
ellipse(x, y, diameter, diameter);
}
}