Hi jb4x,
I have spent a couple of hours trying to work out where I am going wrong here. I have pasted my code below, in what respect am i not following your pointers? I understand the theory of what you have written above, but am stuggling to put it into practice!
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();
}
for (int i = 0; i < numBalls; i++) {
for (int j = i; j < numBalls; j++) {
Balls[i].collide(Balls[j]);
}
for (int j = 0; j < numBlips; j++) {
Balls[i].collide(Blip[j]);
}
}
}
class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Ball[] other;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
other = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = other[i].x - x;
float dy = other[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = other[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 - other[i].x) * spring*1.1;
float ay = (targetY - other[i].y) * spring*1.1;
vx -= ax;
vy -= ay;
other[i].vx += ax;
other[i].vy += ay;
}
collideWith(blip other);
}
}
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[] other;
Blip(float xin, float yin, float din, int idin, Blip[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
other = oin;
}
void collide() {
for (int i = id + 1; i < numBlips; i++) {
float dx = other[i].x - x;
float dy = other[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = other[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 - other[i].x) * spring * 1.2;
float ay = (targetY - other[i].y) * spring * 1.2;
vx -= ax;
vy -= ay;
other[i].vx += ax;
other[i].vy += ay;
}
collideWith(ball other);
}
}
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);
}
}```