Help with accessing ArrayLists.....?
in
Programming Questions
•
2 years ago
Hi Guys,
I was hoping someone could help me with this pretty basic problem. I'm trying to model the attraction forces of a web of woolen threads to one another when they are wet. I have developed my script so that there are a number of threads within a bounding box, composed of particles and springs. All I have to do now is implement the attraction forces between the particles so the threads 'bundle'. However, I am having difficulty working out how to access the particle arraylists for each thread. This is important as my particle class contains a function called attraction() where the particle looks through the relevant arraylist and calculates its distance to that particle. This distance decides whether the function is initiated or not.
If anyone fancies a quick glance at my script and can offer any suggestions I would be very appreciative!
Thanks,
Farley
// IMPORT EXTERNAL LIBRARIES
import peasy.*; // the camera library
import processing.opengl.*; // openGL library
/*------------------------------------------------------------------
*** GLOBAL VARIABLES ***
------------------------------------------------------------------*/
//----------declare the camera
PeasyCam theCamera;
//----------declare the environment
int boundingBoxX = 600;
int boundingBoxY = 600;
int boundingBoxZ = 1200;
// spring variables
float SPRING_RESTLENGTH = 8.0; // the length a spring is at rest
float SPRING_DAMPING = 0.3; // equiv hydraulic damping of a piston
//----------declare the mid particles
int numParticles = 10;
//----------declare threads
int numThreads = 5;
//----------declare thread list
ArrayList threadList = new ArrayList();
/*------------------------------------------------------------------
*** GLOBAL SETUP ***
------------------------------------------------------------------*/
void setup() {
//----------general
size(900, 900, OPENGL);
smooth();
//----------make the camera
theCamera = new PeasyCam(this, boundingBoxZ*2);
theCamera.lookAt(boundingBoxX/2, boundingBoxY/2, boundingBoxZ/2);
//----------make threads
for (int i = 0; i < numThreads+1; i++) {
Thread tmpThread = new Thread();
threadList.add(tmpThread);
}
}
/*------------------------------------------------------------------
*** GLOBAL CONTINUOUS DRAW ***
------------------------------------------------------------------*/
void draw() {
background(255);
for (int i = 0; i < threadList.size(); i++) {
Thread newThread = (Thread) threadList.get(i);
newThread.createThread();
newThread.createSprings();
}
drawBox();
}
void drawBox() {
stroke(100);
strokeWeight(1);
noFill();
pushMatrix();
translate(boundingBoxX/2, boundingBoxY/2, boundingBoxZ/2);
box(boundingBoxX, boundingBoxY, boundingBoxZ);
popMatrix();
}
//-------------------------------------------------------------------------------------------------------------------------------------------
class Particle {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
PVector pos;
PVector vel;
PVector acc;
float pSize;
boolean pFixed;
ArrayList conSprings = new ArrayList();
ArrayList partList = new ArrayList();
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Particle(PVector pos_) {
pos = pos_;
vel = new PVector();
acc = new PVector();
pSize = 10.0;
pFixed = false;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
//----------MOVE AND DRAW TO SCREEN
void update() {
if (pFixed==false) {
updatePos();
//attraction();
}
showMe();
}
//----------SOLVE THE SPRINGS
void solveForce() {
if (pFixed==false) {
Particle otherP;
PVector sumForces = new PVector();
//sumForces.add(gravity);
sumForces.add(acc);
// loop thru our connections
for (int i = 0; i < conSprings.size(); ++i) {
// get a spring
Spring s = (Spring) conSprings.get(i);
// get other end of spring
if ( s.p00 == this ) {
otherP = s.p01;
}
else {
otherP = s.p00;
}
// calculate the push/pull of spring
PVector vecBet = PVector.sub(otherP.pos, pos);
float currLen = vecBet.mag();
float desiredLen = currLen-SPRING_RESTLENGTH;
vecBet.normalize();
vecBet.mult(desiredLen/2);
// add this force to our sum of forces
sumForces.add(vecBet);
}
// apply a damping to the force
sumForces.mult(SPRING_DAMPING);
// add the resultant force to our vel
vel.add(sumForces);
}
}
void updatePos() {
vel.limit(2);
pos.add(vel);
acc = new PVector(0, 0, 0);
//-------------------
/*pos.add(vel); // move the particle
vel.mult(0); */ // reset the vel for next time
}
/*void attraction() {
PVector steer = new PVector();
int count = 0;
for (int i = 0; i < ??????????.size(); i++) {
// get a PVector from the list
Particle tmpPart = (Particle) ?????????.get(i);
float distance = PVector.dist(tmpPart.pos, pos);
if (distance > 0 && distance < 100) {
PVector vecBetween = PVector.sub(tmpPart.pos, pos);
// get the length of the vector
vecBetween.normalize();
steer.add(vecBetween);
count++;
}
}
if (count > 0) {
steer.mult(1.0/count);
}
steer.mult(2);
acc.add(steer);
}*/
//----------Make the particle fixed / free
void fixMe() {
pFixed=true;
}
//----------Add a spring to my list of springs
void addSpring(Spring S) {
// store this spring
conSprings.add(S);
}
void showMe() {
// set appearance
noFill();
if (pFixed==true) {
// draw as 'FIXED' geometry
strokeWeight(0.25);
stroke(0, 0, 250);
line(pos.x-pSize, pos.y, pos.z, pos.x+pSize, pos.y, pos.z);
line(pos.x, pos.y-pSize, pos.z, pos.x, pos.y+pSize, pos.z);
line(pos.x, pos.y, pos.z-pSize, pos.x, pos.y, pos.z+pSize);
}
else {
// draw as 'FREE' geometry
strokeWeight(0.25);
stroke(140);
line(pos.x-pSize/2, pos.y, pos.z, pos.x+pSize/2, pos.y, pos.z);
line(pos.x, pos.y-pSize/2, pos.z, pos.x, pos.y+pSize/2, pos.z);
line(pos.x, pos.y, pos.z-pSize/2, pos.x, pos.y, pos.z+pSize/2);
}
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------
class Spring {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
// declare all the class variables
Particle p00;
Particle p01;
int col;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Spring (Particle INPUT_00, Particle INPUT_01, int col_) {
// assign all the class variables
p00 = INPUT_00;
p01 = INPUT_01;
col = col_;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
//----------Draw myself
void showMe() {
stroke(255-col, 0, col);
strokeWeight(0.5);
noFill();
line(p00.pos.x, p00.pos.y, p00.pos.z, p01.pos.x, p01.pos.y, p01.pos.z);
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------
class Thread {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
//----------declare particle list
ArrayList particleList = new ArrayList();
//----------declare spring list
ArrayList springList = new ArrayList();
//----------declare the springs
Spring s0, s1;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Thread() {
//----------make the particles
// make vectors
PVector tmpPos1 = new PVector(random(boundingBoxX), random(boundingBoxY), 0);
PVector tmpPos2 = new PVector(random(boundingBoxX), random(boundingBoxY), boundingBoxZ);
// sub end vectors
PVector vecBetween = PVector.sub(tmpPos1, tmpPos2);
// divide vecBetween by 10
vecBetween.div(numParticles);
for (int i = 0; i < numParticles+1; i++) {
// scale vecBetween by i
PVector vecScale = PVector.mult(vecBetween, i);
// add new vector to end vector
PVector newVec = PVector.add(vecScale, tmpPos2);
// create new Particle at newVec
Particle tmpPart = new Particle(newVec);
// fix end particles
if ( i == 0 || i == numParticles) {
tmpPart.fixMe();
}
// add particle to list
particleList.add(tmpPart);
}
//----------make the springs
for (int i = 0; i < particleList.size(); i++) {
if (i > 0) {
int col = i*20;
Particle p1 = (Particle) particleList.get(i);
Particle p2 = (Particle) particleList.get(i-1);
s0 = new Spring(p1, p2, col);
springList.add(s0);
p1.addSpring(s0);
p2.addSpring(s0);
}
}
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
void createThread() {
for (int i = 0; i < particleList.size(); i++) {
Particle tmpPart = (Particle) particleList.get(i);
tmpPart.solveForce();
}
for (int i = 0; i < particleList.size(); i++) {
Particle tmpPart = (Particle) particleList.get(i);
tmpPart.update();
}
}
void createSprings() {
for (int i = 0; i < springList.size(); i++) {
// get a spring from the arraylist
Spring currS = (Spring) springList.get(i);
currS.showMe();
}
}
}
I was hoping someone could help me with this pretty basic problem. I'm trying to model the attraction forces of a web of woolen threads to one another when they are wet. I have developed my script so that there are a number of threads within a bounding box, composed of particles and springs. All I have to do now is implement the attraction forces between the particles so the threads 'bundle'. However, I am having difficulty working out how to access the particle arraylists for each thread. This is important as my particle class contains a function called attraction() where the particle looks through the relevant arraylist and calculates its distance to that particle. This distance decides whether the function is initiated or not.
If anyone fancies a quick glance at my script and can offer any suggestions I would be very appreciative!
Thanks,
Farley
// IMPORT EXTERNAL LIBRARIES
import peasy.*; // the camera library
import processing.opengl.*; // openGL library
/*------------------------------------------------------------------
*** GLOBAL VARIABLES ***
------------------------------------------------------------------*/
//----------declare the camera
PeasyCam theCamera;
//----------declare the environment
int boundingBoxX = 600;
int boundingBoxY = 600;
int boundingBoxZ = 1200;
// spring variables
float SPRING_RESTLENGTH = 8.0; // the length a spring is at rest
float SPRING_DAMPING = 0.3; // equiv hydraulic damping of a piston
//----------declare the mid particles
int numParticles = 10;
//----------declare threads
int numThreads = 5;
//----------declare thread list
ArrayList threadList = new ArrayList();
/*------------------------------------------------------------------
*** GLOBAL SETUP ***
------------------------------------------------------------------*/
void setup() {
//----------general
size(900, 900, OPENGL);
smooth();
//----------make the camera
theCamera = new PeasyCam(this, boundingBoxZ*2);
theCamera.lookAt(boundingBoxX/2, boundingBoxY/2, boundingBoxZ/2);
//----------make threads
for (int i = 0; i < numThreads+1; i++) {
Thread tmpThread = new Thread();
threadList.add(tmpThread);
}
}
/*------------------------------------------------------------------
*** GLOBAL CONTINUOUS DRAW ***
------------------------------------------------------------------*/
void draw() {
background(255);
for (int i = 0; i < threadList.size(); i++) {
Thread newThread = (Thread) threadList.get(i);
newThread.createThread();
newThread.createSprings();
}
drawBox();
}
void drawBox() {
stroke(100);
strokeWeight(1);
noFill();
pushMatrix();
translate(boundingBoxX/2, boundingBoxY/2, boundingBoxZ/2);
box(boundingBoxX, boundingBoxY, boundingBoxZ);
popMatrix();
}
//-------------------------------------------------------------------------------------------------------------------------------------------
class Particle {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
PVector pos;
PVector vel;
PVector acc;
float pSize;
boolean pFixed;
ArrayList conSprings = new ArrayList();
ArrayList partList = new ArrayList();
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Particle(PVector pos_) {
pos = pos_;
vel = new PVector();
acc = new PVector();
pSize = 10.0;
pFixed = false;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
//----------MOVE AND DRAW TO SCREEN
void update() {
if (pFixed==false) {
updatePos();
//attraction();
}
showMe();
}
//----------SOLVE THE SPRINGS
void solveForce() {
if (pFixed==false) {
Particle otherP;
PVector sumForces = new PVector();
//sumForces.add(gravity);
sumForces.add(acc);
// loop thru our connections
for (int i = 0; i < conSprings.size(); ++i) {
// get a spring
Spring s = (Spring) conSprings.get(i);
// get other end of spring
if ( s.p00 == this ) {
otherP = s.p01;
}
else {
otherP = s.p00;
}
// calculate the push/pull of spring
PVector vecBet = PVector.sub(otherP.pos, pos);
float currLen = vecBet.mag();
float desiredLen = currLen-SPRING_RESTLENGTH;
vecBet.normalize();
vecBet.mult(desiredLen/2);
// add this force to our sum of forces
sumForces.add(vecBet);
}
// apply a damping to the force
sumForces.mult(SPRING_DAMPING);
// add the resultant force to our vel
vel.add(sumForces);
}
}
void updatePos() {
vel.limit(2);
pos.add(vel);
acc = new PVector(0, 0, 0);
//-------------------
/*pos.add(vel); // move the particle
vel.mult(0); */ // reset the vel for next time
}
/*void attraction() {
PVector steer = new PVector();
int count = 0;
for (int i = 0; i < ??????????.size(); i++) {
// get a PVector from the list
Particle tmpPart = (Particle) ?????????.get(i);
float distance = PVector.dist(tmpPart.pos, pos);
if (distance > 0 && distance < 100) {
PVector vecBetween = PVector.sub(tmpPart.pos, pos);
// get the length of the vector
vecBetween.normalize();
steer.add(vecBetween);
count++;
}
}
if (count > 0) {
steer.mult(1.0/count);
}
steer.mult(2);
acc.add(steer);
}*/
//----------Make the particle fixed / free
void fixMe() {
pFixed=true;
}
//----------Add a spring to my list of springs
void addSpring(Spring S) {
// store this spring
conSprings.add(S);
}
void showMe() {
// set appearance
noFill();
if (pFixed==true) {
// draw as 'FIXED' geometry
strokeWeight(0.25);
stroke(0, 0, 250);
line(pos.x-pSize, pos.y, pos.z, pos.x+pSize, pos.y, pos.z);
line(pos.x, pos.y-pSize, pos.z, pos.x, pos.y+pSize, pos.z);
line(pos.x, pos.y, pos.z-pSize, pos.x, pos.y, pos.z+pSize);
}
else {
// draw as 'FREE' geometry
strokeWeight(0.25);
stroke(140);
line(pos.x-pSize/2, pos.y, pos.z, pos.x+pSize/2, pos.y, pos.z);
line(pos.x, pos.y-pSize/2, pos.z, pos.x, pos.y+pSize/2, pos.z);
line(pos.x, pos.y, pos.z-pSize/2, pos.x, pos.y, pos.z+pSize/2);
}
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------
class Spring {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
// declare all the class variables
Particle p00;
Particle p01;
int col;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Spring (Particle INPUT_00, Particle INPUT_01, int col_) {
// assign all the class variables
p00 = INPUT_00;
p01 = INPUT_01;
col = col_;
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
//----------Draw myself
void showMe() {
stroke(255-col, 0, col);
strokeWeight(0.5);
noFill();
line(p00.pos.x, p00.pos.y, p00.pos.z, p01.pos.x, p01.pos.y, p01.pos.z);
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------
class Thread {
/*------------------------------------------------------------------
*** CLASS VARIABLES ***
------------------------------------------------------------------*/
//----------declare particle list
ArrayList particleList = new ArrayList();
//----------declare spring list
ArrayList springList = new ArrayList();
//----------declare the springs
Spring s0, s1;
/*------------------------------------------------------------------
*** CLASS CONSTRUCTOR ***
------------------------------------------------------------------*/
Thread() {
//----------make the particles
// make vectors
PVector tmpPos1 = new PVector(random(boundingBoxX), random(boundingBoxY), 0);
PVector tmpPos2 = new PVector(random(boundingBoxX), random(boundingBoxY), boundingBoxZ);
// sub end vectors
PVector vecBetween = PVector.sub(tmpPos1, tmpPos2);
// divide vecBetween by 10
vecBetween.div(numParticles);
for (int i = 0; i < numParticles+1; i++) {
// scale vecBetween by i
PVector vecScale = PVector.mult(vecBetween, i);
// add new vector to end vector
PVector newVec = PVector.add(vecScale, tmpPos2);
// create new Particle at newVec
Particle tmpPart = new Particle(newVec);
// fix end particles
if ( i == 0 || i == numParticles) {
tmpPart.fixMe();
}
// add particle to list
particleList.add(tmpPart);
}
//----------make the springs
for (int i = 0; i < particleList.size(); i++) {
if (i > 0) {
int col = i*20;
Particle p1 = (Particle) particleList.get(i);
Particle p2 = (Particle) particleList.get(i-1);
s0 = new Spring(p1, p2, col);
springList.add(s0);
p1.addSpring(s0);
p2.addSpring(s0);
}
}
}
/*------------------------------------------------------------------
*** CLASS FUNCTIONS ***
------------------------------------------------------------------*/
void createThread() {
for (int i = 0; i < particleList.size(); i++) {
Particle tmpPart = (Particle) particleList.get(i);
tmpPart.solveForce();
}
for (int i = 0; i < particleList.size(); i++) {
Particle tmpPart = (Particle) particleList.get(i);
tmpPart.update();
}
}
void createSprings() {
for (int i = 0; i < springList.size(); i++) {
// get a spring from the arraylist
Spring currS = (Spring) springList.get(i);
currS.showMe();
}
}
}
1