Right now I have a 2D function involving balls that roam around the screen and then stick together in a certain order when the mouse is pressed. What I want to do is make these balls spheres that perform the same thing in 3D space. Can anybody help me with this? Here's my code:
import processing.opengl.*;
//import libraries import toxi.geom.*; import peasy.*; //library for PDF export import processing.pdf.*;
//declase camera PeasyCam pCam; //declare arraylist ArrayList AgentCollection;
// A boolean variable that when set to true triggers the PDF recording process boolean record = false;
class Ball { color colour; float xpos, ypos; float xvel, yvel; float radius; float mass;
//note that the tab doesn't have to have the same name as the function void ballArraySetup() { smallestBalls = new Ball[numSmallestBalls]; for (int r = 0; r < numSmallestBalls; r++) { smallestBalls[r] = new Ball(random(width), random(height), 10, color(25)); smallestBalls[r].xvel = random(-2, 2); smallestBalls[r].yvel = random(-2, 2); }
smallBalls = new Ball[numSmallBalls]; for (int b = 0; b < numSmallBalls; b++) { smallBalls[b] = new Ball(random(width), random(height), 15, color(50)); smallBalls[b].xvel = random(-5, 5); smallBalls[b].yvel = random(-5, 5); }
largeBalls = new Ball[numLargeBalls]; for (int l = 0; l < numLargeBalls; l++) { largeBalls[l] = new Ball(random(width), random(height), 20, color(75)); largeBalls[l].xvel = random(-5, 5); largeBalls[l].yvel = random(-5, 5); }
largestBalls = new Ball[numLargestBalls]; for (int g = 0; g < numLargestBalls; g++) { largestBalls[g] = new Ball(random(width), random(height), 25, color(100)); largestBalls[g].xvel = random(-5, 5); largestBalls[g].yvel = random(-5, 5); }
moreBalls = new Ball[numMoreBalls]; for (int m = 0; m < numMoreBalls; m++) { moreBalls[m] = new Ball(random(width), random(height), 30, color(125)); moreBalls[m].xvel = random(-5, 5); moreBalls[m].yvel = random(-5, 5); }
evenMoreBalls = new Ball[numEvenMoreBalls]; for (int e = 0; e < numEvenMoreBalls; e++) { evenMoreBalls[e] = new Ball(random(width), random(height), 35, color(150)); evenMoreBalls[e].xvel = random(-5, 5); evenMoreBalls[e].yvel = random(-5, 5); }
stillMoreBalls = new Ball[numStillMoreBalls]; for (int s = 0; s < numStillMoreBalls; s++) { stillMoreBalls[s] = new Ball(random(width), random(height), 40, color(175)); stillMoreBalls[s].xvel = random(-5, 5); stillMoreBalls[s].yvel = random(-5, 5); }
theRealLargestBalls = new Ball[numTheRealLargestBalls]; for (int t = 0; t < numTheRealLargestBalls; t++) { theRealLargestBalls[t] = new Ball(random(width), random(height), 45, color(200)); theRealLargestBalls[t].xvel = random(-5, 5); theRealLargestBalls[t].yvel = random(-5, 5); } }
void doGeneralBallStuff() { for (int r = 0; r < numSmallestBalls; r++) { smallestBalls[r].render(); smallestBalls[r].update(); smallestBalls[r].checkBoundaryCollision(0, width, 0, height); }
for (int b = 0; b < numSmallBalls; b++) { smallBalls[b].render(); smallBalls[b].update(); smallBalls[b].checkBoundaryCollision(0, width, 0, height); }
for (int l = 0; l < numLargeBalls; l++) { largeBalls[l].render(); largeBalls[l].update(); largeBalls[l].checkBoundaryCollision(0, width, 0, height); }
for (int g = 0; g < numLargestBalls; g++) { largestBalls[g].render(); largestBalls[g].update(); largestBalls[g].checkBoundaryCollision(0, width, 0, height); }
for (int m = 0; m < numMoreBalls; m++) { moreBalls[m].render(); moreBalls[m].update(); moreBalls[m].checkBoundaryCollision(0, width, 0, height); }
for (int e = 0; e < numEvenMoreBalls; e++) { evenMoreBalls[e].render(); evenMoreBalls[e].update(); evenMoreBalls[e].checkBoundaryCollision(0, width, 0, height); }
for (int s = 0; s < numStillMoreBalls; s++) { stillMoreBalls[s].render(); stillMoreBalls[s].update(); stillMoreBalls[s].checkBoundaryCollision(0, width, 0, height); }
PVector rotateC(float x, float y, float sine, float cosine, boolean anticlock) { PVector result = new PVector(0, 0);
if(anticlock) { result.x = x * cosine + y * sine; result.y = y * cosine - x * sine; } else { result.x = x * cosine - y * sine; result.y = y * cosine + x * sine; } return result; }
//I've created a new tab ballFunctions where I've hidden all the stuff we're happy with now: //creating and populating the arrays, and ball render(), update() and checkWalls() or whatever we called it
// I create tabs for logical groups of functions - in a recent project I had colour functions together, pdf // functions together, xml functions together and so on
// it's also useful to move functions out of the main tab once you're happy with them // and don't need to keep looking at them
int numSmallestBalls = 7; Ball[] smallestBalls;
int numSmallBalls = 7; Ball[] smallBalls;
int numLargeBalls = 7; Ball[] largeBalls;
int numLargestBalls = 7; Ball[] largestBalls;
int numMoreBalls = 7; Ball[] moreBalls;
int numEvenMoreBalls = 7; Ball[] evenMoreBalls;
int numStillMoreBalls = 7; Ball[] stillMoreBalls;
int numTheRealLargestBalls = 7; Ball[] theRealLargestBalls;
float easeThreshold = 100;
void setup() { size(900, 600, OPENGL); smooth();
//array creation and population are now hidden away //in a ballArraySetup() function in the new ballFunctions tab ballArraySetup(); }
void draw() {
//will create a PDF from current frame if p-key is pressed if (record) { //#### will add the actal frame number to the file name //pdf will be saved in same folder as the processing file beginRaw(PDF, "3D-####.pdf"); }
background(255);
//all render(), update() and checkBoundaryCollision() functions are now hidden away //in a new function in the new ballFunctions tab doGeneralBallStuff();
checkIntersections(); if (mousePressed) easeToNearest();
checkBallCollisions(); }
void checkBallCollisions() { //note the way I've done the nest here //to check each ball against itself and against each other twice //would be a waste of time //if you can't "see" this, draw a grid with columns 0 - 5 say // and rows 0 - 5, and check off which collsions this method makes for (int t = 0; t < numSmallestBalls - 1; t++) { for (int s = t + 1; s < numSmallestBalls; s++) { checkCollision(smallestBalls[t], smallestBalls[s]); } } for (int t = 0; t < numSmallBalls - 1; t++) { for (int s = t + 1; s < numSmallBalls; s++) { checkCollision(smallBalls[t], smallBalls[s]); } } for (int t = 0; t < numLargeBalls - 1; t++) { for (int s = t + 1; s < numLargeBalls; s++) { checkCollision(largeBalls[t], largeBalls[s]); } } for (int t = 0; t < numLargestBalls - 1; t++) { for (int s = t + 1; s < numLargestBalls; s++) { checkCollision(largestBalls[t], largestBalls[s]); } } for (int t = 0; t < numMoreBalls - 1; t++) { for (int s = t + 1; s < numMoreBalls; s++) { checkCollision(moreBalls[t], moreBalls[s]); } } for (int t = 0; t < numEvenMoreBalls - 1; t++) { for (int s = t + 1; s < numEvenMoreBalls; s++) { checkCollision(evenMoreBalls[t], evenMoreBalls[s]); } } for (int t = 0; t < numStillMoreBalls - 1; t++) { for (int s = t + 1; s < numStillMoreBalls; s++) { checkCollision(stillMoreBalls[t], stillMoreBalls[s]); } } for (int t = 0; t < numTheRealLargestBalls - 1; t++) { for (int s = t + 1; s < numTheRealLargestBalls; s++) { checkCollision(theRealLargestBalls[t], theRealLargestBalls[s]); } }
//to end PDF recording if (record) { endRaw(); record = false; } } void keyPressed() { if (key == 'p' || key == 'P') { record = true; } }
void easeToNearest() { // I've created a threshold for easing to try to stop the balls clumping together for (int b = 0; b < numSmallBalls; b++) { int closestsmallestBall = -1; float closestDistance = (width * height) + 10;
for (int r = 0; r < numSmallestBalls; r++) { float dx = smallestBalls[r].xpos - smallBalls[b].xpos; float dy = smallestBalls[r].ypos - smallBalls[b].ypos; float distance = sqrt(dx*dx + dy*dy); if (distance < closestDistance) { closestDistance = distance; closestsmallestBall = r; } } if (closestDistance < easeThreshold) { smallBalls[b].easeTo(smallestBalls[closestsmallestBall].xpos, smallestBalls[closestsmallestBall].ypos); } }
for (int b = 0; b < numLargeBalls; b++) { int closestsmallBall = -1; float closestDistance = (width * height) + 10;
void checkIntersections() { // for (int r = 0; r < numLargestBalls; r++) // { // for (int b = 0; b < numSmallestBalls; b++) // { // largestBalls[r].areBallsIntersecting(smallestBalls[b]); // } // } for (int r = 0; r < numSmallestBalls; r++) { for (int b = 0; b < numSmallBalls; b++) { smallestBalls[r].areBallsIntersecting(smallBalls[b]); } } for (int b = 0; b < numSmallBalls; b++) { for (int l = 0; l < numLargeBalls; l++) { smallBalls[b].areBallsIntersecting(largeBalls[l]); } } for (int l = 0; l < numLargeBalls; l++) { for (int g = 0; g < numLargestBalls; g++) { largeBalls[l].areBallsIntersecting(largestBalls[g]); } }
for (int g = 0; g < numLargestBalls; g++) { for (int m = 0; m < numMoreBalls; m++) { largestBalls[g].areBallsIntersecting(moreBalls[m]); } }
for (int m = 0; m < numMoreBalls; m++) { for (int e = 0; e < numEvenMoreBalls; e++) { moreBalls[m].areBallsIntersecting(evenMoreBalls[e]); } }
for (int e = 0; e < numEvenMoreBalls; e++) { for (int s = 0; s < numStillMoreBalls; s++) { evenMoreBalls[e].areBallsIntersecting(stillMoreBalls[s]); } }
for (int s = 0; s < numStillMoreBalls; s++) { for (int t = 0; t < numTheRealLargestBalls; t++) { stillMoreBalls[s].areBallsIntersecting(theRealLargestBalls[t]); } } }
I have this unexpected token that I really can't find and it's driving me nuts. Whenever I run the file, it returns 'unexpected token: int' and sends me to the blank tab that I have. Please help me! Also note that the following numbers are the different tabs, not the lines, I didn't know how to insert all my code at once while including the number of each individual line.
import processing.opengl.*;
//import libraries import toxi.geom.*; import peasy.*; //library for PDF export import processing.pdf.*;
//declase camera PeasyCam pCam; //declare arraylist ArrayList AgentCollection;
// A boolean variable that when set to true triggers the PDF recording process boolean record = false;
class Ball { color colour; float xpos, ypos; float xvel, yvel; float radius; float mass;
//note that the tab doesn't have to have the same name as the function void ballArraySetup() { smallestBalls = new Ball[numSmallestBalls]; for (int r = 0; r < numSmallestBalls; r++) { smallestBalls[r] = new Ball(random(width), random(height), 20, color(random(200, 255), random(64, 128), random(64, 128))); smallestBalls[r].xvel = random(-2, 2); smallestBalls[r].yvel = random(-2, 2); }
smallBalls = new Ball[numSmallBalls]; for (int b = 0; b < numSmallBalls; b++) { smallBalls[b] = new Ball(random(width), random(height), 30, color(random(64, 128), random(200, 255), random(64, 128))); smallBalls[b].xvel = random(-5, 5); smallBalls[b].yvel = random(-5, 5); }
largeBalls = new Ball[numLargeBalls]; for (int l = 0; l < numLargeBalls; l++) { largeBalls[l] = new Ball(random(width), random(height), 40, color(random(64, 128), random(64, 128), random(200, 255))); largeBalls[l].xvel = random(-5, 5); largeBalls[l].yvel = random(-5, 5); }
largestBalls = new Ball[numLargestBalls]; for (int g = 0; g < numLargestBalls; g++) { largestBalls[g] = new Ball(random(width), random(height), 50, color(125, 125, 125)); largestBalls[g].xvel = random(-5, 5); largestBalls[g].yvel = random(-5, 5); } }
void doGeneralBallStuff() { for (int r = 0; r < numSmallestBalls; r++) { smallestBalls[r].render(); smallestBalls[r].update(); smallestBalls[r].checkBoundaryCollision(0, width, 0, height); }
for (int b = 0; b < numSmallBalls; b++) { smallBalls[b].render(); smallBalls[b].update(); smallBalls[b].checkBoundaryCollision(0, width, 0, height); }
for (int l = 0; l < numLargeBalls; l++) { largeBalls[l].render(); largeBalls[l].update(); largeBalls[l].checkBoundaryCollision(0, width, 0, height); }
for (int g = 0; g < numLargestBalls; g++) { largestBalls[g].render(); largestBalls[g].update(); largestBalls[g].checkBoundaryCollision(0, width, 0, height); } }
//this is straight out of Making things move by Keith Peters //it's also in the Processing examples
PVector rotateC(float x, float y, float sine, float cosine, boolean anticlock) { PVector result = new PVector(0, 0);
if(anticlock) { result.x = x * cosine + y * sine; result.y = y * cosine - x * sine; } else { result.x = x * cosine - y * sine; result.y = y * cosine + x * sine; } return result; }
//I've created a new tab ballFunctions where I've hidden all the stuff we're happy with now: //creating and populating the arrays, and ball render(), update() and checkWalls() or whatever we called it
// I create tabs for logical groups of functions - in a recent project I had colour functions together, pdf // functions together, xml functions together and so on
// it's also useful to move functions out of the main tab once you're happy with them // and don't need to keep looking at them
int numSmallestBalls = 3; Ball[] smallestBalls;
int numSmallBalls = 3; Ball[] smallBalls;
int numLargeBalls = 3; Ball[] largeBalls;
int numLargestBalls = 3; Ball[] largestBalls;
float easeThreshold = 100;
void setup() { size(1000, 700, OPENGL); smooth();
//array creation and population are now hidden away //in a ballArraySetup() function in the new ballFunctions tab ballArraySetup(); }
void draw() {
//will create a PDF from current frame if p-key is pressed if (record) { //#### will add the actal frame number to the file name //pdf will be saved in same folder as the processing file beginRaw(PDF, "3D-####.pdf"); }
background(255);
//all render(), update() and checkBoundaryCollision() functions are now hidden away //in a new function in the new ballFunctions tab doGeneralBallStuff();
void checkBallCollisions() { //note the way I've done the nest here //to check each ball against itself and against each other twice //would be a waste of time //if you can't "see" this, draw a grid with columns 0 - 5 say // and rows 0 - 5, and check off which collsions this method makes for(int t = 0; t < numSmallestBalls - 1; t++) { for(int s = t + 1; s < numSmallestBalls; s++) { checkCollision(smallestBalls[t], smallestBalls[s]); } } for(int t = 0; t < numSmallBalls - 1; t++) { for(int s = t + 1; s < numSmallBalls; s++) { checkCollision(smallBalls[t], smallBalls[s]); } } for(int t = 0; t < numLargeBalls - 1; t++) { for(int s = t + 1; s < numLargeBalls; s++) { checkCollision(largeBalls[t], largeBalls[s]); } } for(int t = 0; t < numLargestBalls - 1; t++) { for(int s = t + 1; s < numLargestBalls; s++) { checkCollision(largestBalls[t], largestBalls[s]); } }
//to end PDF recording if (record) { endRaw(); record = false; }
} void keyPressed() { if (key == 'p' || key == 'P') { record = true; } }
void easeToNearest() { // I've created a threshold for easing to try to stop the balls clumping together for (int b = 0; b < numSmallBalls; b++) { int closestsmallestBall = -1; float closestDistance = (width * height) + 10;
for (int r = 0; r < numSmallestBalls; r++) { float dx = smallestBalls[r].xpos - smallBalls[b].xpos; float dy = smallestBalls[r].ypos - smallBalls[b].ypos; float distance = sqrt(dx*dx + dy*dy); if (distance < closestDistance) { closestDistance = distance; closestsmallestBall = r; } } if(closestDistance < easeThreshold) { smallBalls[b].easeTo(smallestBalls[closestsmallestBall].xpos, smallestBalls[closestsmallestBall].ypos); } }
for (int b = 0; b < numLargeBalls; b++) { int closestsmallBall = -1; float closestDistance = (width * height) + 10;
void checkIntersections() { // for (int r = 0; r < numLargestBalls; r++) // { // for (int b = 0; b < numSmallestBalls; b++) // { // largestBalls[r].areBallsIntersecting(smallestBalls[b]); // } // } for (int r = 0; r < numSmallestBalls; r++) { for (int b = 0; b < numSmallBalls; b++) { smallestBalls[r].areBallsIntersecting(smallBalls[b]); } } for (int r = 0; r < numSmallBalls; r++) { for (int b = 0; b < numLargeBalls; b++) { smallBalls[r].areBallsIntersecting(largeBalls[b]); } } for (int r = 0; r < numLargeBalls; r++) { for (int b = 0; b < numLargestBalls; b++) { largeBalls[r].areBallsIntersecting(largestBalls[b]); } } }
Help, please! I'm having a very hard time with this code I'm working on. I'm a super beginner to Processing so I would greatly appreciate if somebody could do this for me so I can study it.
I am trying to make a "field" of balls of two kinds. The first kind are stationary (at first). The second kind are attracted to the ball closest to them. I would like them to travel in a straight line to the closest ball to them. When two balls touch each other, I would like them to stick together. I would then like this combination of two (or more) balls to continue attracting to and sticking to more balls. Does this make sense? Sorry if I didn't explain that very well. Somebody please help!