Hi I'm a newbie, and I'm trying to convert this code for bouncing balls from 2D to 3D to use it in another sketch, but I'm having issues getting the balls to stay in the frame.....any help would be greatly appreciated! (I'm sure its something really simple)
int numBalls = 12;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
import processing.opengl.*;
Ball[] balls = new Ball[numBalls];
void setup()
{
size(1000, 700,OPENGL); //640
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), 0, random(20, 40), i, balls);
}
}
void draw()
{
lights();
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
balls[i].move();
balls[i].display();
}
}
class Ball {
float x, y,z;
float diameter;
float vx = 0;
float vy = 0;
float vz = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float zin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
z = zin;
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;
float ay = (targetY - others[i].y) * spring;
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;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) { //>height = 400
y = height - diameter/2;
vy *= friction;
}
else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
fill(255, 204);
noStroke();
// lights();
translate(x,y, 0);
sphere(diameter);
//sphere(x,y,z, diameter);
// ellipse(x, y, diameter, diameter);
}
}
int numBalls = 12;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
import processing.opengl.*;
Ball[] balls = new Ball[numBalls];
void setup()
{
size(1000, 700,OPENGL); //640
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), 0, random(20, 40), i, balls);
}
}
void draw()
{
lights();
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
balls[i].move();
balls[i].display();
}
}
class Ball {
float x, y,z;
float diameter;
float vx = 0;
float vy = 0;
float vz = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float zin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
z = zin;
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;
float ay = (targetY - others[i].y) * spring;
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;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) { //>height = 400
y = height - diameter/2;
vy *= friction;
}
else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
fill(255, 204);
noStroke();
// lights();
translate(x,y, 0);
sphere(diameter);
//sphere(x,y,z, diameter);
// ellipse(x, y, diameter, diameter);
}
}
1