Hello! I need help turning
this 2d code into 3d. I have recently started using processing, so 3d processing is a bit of a stretch for me. I am basically just trying to turn the circles into spheres, nothing more extreme, but I have no idea what I should do
The initial code is this
Box boxes;
int minSize = 20;
int maxSize = 50;
boolean collide = false;
void setup()
{
size(500, 500);
rectMode(CENTER);
ellipseMode(CENTER);
boxes = new Box[120];
for (int i = 0; i < boxes.length; i++)
{
boxes[i] = new Box(random(maxSize, width-maxSize), random(maxSize, height-maxSize), random(minSize, maxSize), color(0, random(10, 255), random(10, 255)));
}
}
void draw()
{
//background(200);
fill(200, 10);
rect(width/2, height/2, width+2, height+2);
for (int i = 0; i < boxes.length; i++)
{
boxes[i].draw();
}
//check distances between all the other boxes and attracts and collide!
for (int j = 0; j < boxes.length; j++)
{
for (int k = 0; k < boxes.length; k++)
{
if (j != k)
{
boxes[j].collide(boxes[k]);
boxes[j].attract(boxes[k]);
}
}
}
}
void mousePressed()
{
for (int i = 0; i < boxes.length; i++)
{
boxes[i].jiggle();
}
}
and the box class:
class Box
{
// step 1 = variables declaration
int frame = 0;
float posX;
float posY;
float velX;
float velY;
float side;
color c;
// step 2 = constructor, initializes the class
Box(float _posX, float _posY, float _side, color _c)
{
posX = _posX;
posY = _posY;
velX = random(-0.8, 0.8);
velY = random(-0.8, 0.8);
side = _side;
c = _c;
}
// step 3 = functions, functionality of the class
void draw()
{
posX += velX;
posY += velY;
pushMatrix();
translate(posX, posY);
fill(c);
ellipse(0, 0, side, side);
popMatrix();
//don’t let the boxes run away from the frame!
if ((posX < side || posX >= width-side)||(posY < side || posY >= height-side))
{
velX = -0.99velX;
velY = -0.99velY;
}
//slow circles down over time
velX = .98velX;
velY = .98velY;
}
void attract(Box b)
{
float d = dist(posX, posY, b.posX, b.posY); // calculate dist between boxes
float diffX = b.posX - posX; // difference of position in X
float diffY = b.posY - posY; // difference of position in Y
velX += 0.08*(diffX / (sq(d))); // change velocity by a ratio between dist and pos difference
velY += 0.08*(diffY / (sq(d)));
}
void collide(Box b)
{
float dis = dist(posX, posY, b.posX, b.posY);
//if (dis <= (sqrt(sq(side) + sq(side/2)) + sqrt(sq(b.side/2) + sq(b.side/2)))) // for boxes
float overlap = (side/2 + b.side/2) - dis;
if (overlap>0)
{
float diffX = b.posX - posX; // difference of position in X
float diffY = b.posY - posY; // difference of position in Y
float unitX = diffX/dis;
float unitY = diffY/dis;
velX -= 0.1unitXoverlap;
velY -= 0.1unitYoverlap;
}
}
void jiggle()
{
velX = random(-1, 1);
velY = random(-1, 1);
}
}
Thank you!