#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Can't figure out how to deal with nested object for loops. In over my head.

edited June 2014

So first of all this is NOT for the most part my own work; it's a cobbled together hack of code from other people (all public, but just want to put it out there for ethics/wtf does it look like this is three different people)?

I've been working with processing for a few months and I've been messing around with progressively more complex 3d systems and I really want to get this working.

My issue is with the behavior of my particles and planets; I want a set of X particles moving around each planet; I've done this before but can't find my old code sheepish shrug. I know I need a nested for loop, calling each planet position and size, then putting that into the constructor for each array of particles, but I'm in over my head and I'm getting nowhere.

I'd be just as happy to be pointed in the right direction as I would to get a line by line solution. Thank you all for your time and your assistance. Loving this stuff more and more every day :)

And again..sorry this is some ugly ugly shit...not offended if everyone's like "nawwwwww"....

``````  // Star parameters
int         maxStarSpeed        = 5;
int         nbMaxStars          = 15000;
int         depth               = 10;
Stars[]     stars               = new Stars[nbMaxStars];

// Planet parameters

int         maxPlanetSpeed      = 5;
int         nb2MaxPlanets       = 20;
int         pdepth              = 10;
Planets[]   planets             = new Planets[nb2MaxPlanets];

// Particles parameters

int         nb3MaxParticles        = 30;
Particle [] particles               = new Particle[nb3MaxParticles];

// Drawing parameters
int       sizeX = 1920;
int       sizeY = 1080;
boolean   clearScreen = true;
int       tail = 1;
int       transparency = 255;

// Rotation variable
int         rotationMode = 3;
float       angle = 0;

void setup() {

String strX, strY;
size(sizeX, sizeY, P3D);
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);
perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*10.0);
colorMode(RGB, 255);
loop();
strokeWeight(tail);

for(int nb = 0; nb < nbMaxStars; nb++) {
stars[nb] = new Stars(random(-2*width, 2*width), random (-2*height, 2*height),
-random(depth*255), random(1, maxStarSpeed));
}
pushMatrix();
for (int nb2 = 0; nb2 < nb2MaxPlanets; nb2++) {

planets[nb2] = new Planets(random(-2*width, 2*width), random (-2*height, 2*height),
-random(depth*255), random(1, maxPlanetSpeed));

/* need particle location based on each planet location, movement area determined by size of planet

particles[nb3] = new Particle(
*/

}
popMatrix();

}

void draw() {
if(clearScreen == true) {
background(0);
}
//  to do; include easing!

translate(width/2+((mouseX-(width/2))*10)/(width/2),
height/2+((mouseY-(height/2))*10)/(height/2)
,0);
if(rotationMode == 1) {
angle += delta;
}
if(rotationMode == 2) {
angle -= delta;
}
rotateZ(angle);

for(int nb = 0; nb < nbMaxStars; nb++) {
stars[nb].position();
stars[nb].anim();
}

for (int nb2 = 0; nb2 < nb2MaxPlanets; nb2++) {
planets[nb2].position();
planets[nb2].anim();
}
/*
for (int nb3 =0; nb3 < nb3MaxParticles; nb3++) {
particles[nb3].draw;
}
*/

}

void keyPressed() {
if (key == 'x') {
clearScreen = !clearScreen;
}

if (key == 'c') {
if (transparency<255);
transparency +=5;
}

if (key == 'v') {
if(transparency>2)
transparency -= 5;
}

if (key == 'b') {
background(0);
}

if (keyCode == UP) {
translate(0,(angle += (5*delta)),0);
}

if (keyCode == DOWN) {
translate(0, (angle -= (5*delta)),0);
}

if(keyCode == RIGHT) {
translate((angle -= (5*delta)),0,0);
}

if(keyCode == LEFT) {
translate((angle += (5*delta)),0,0);
}

}

void mousePressed() {
if(mouseButton==LEFT)
rotationMode = 1;
if(mouseButton==RIGHT)
rotationMode = 2;
if(mouseButton==CENTER)
rotationMode = 3;
}

class Particle {

PVector pos;
float movingArea;
float moveSize;

PVector tail[];
int tailPosIndex;
int tailLength;

float psize;

float inc1, inc2, inc3;
float inc1Increment = random(0.01, 0.05);
float inc2Increment = random(0.01, 0.05);
float inc3Increment = random(0.01, 0.05);

Particle( PVector tempPos, float tempMoveSize) {
tempMoveSize = moveSize;
tempPos = pos = new PVector();
psize = random(0.1, 2.0);

tailPosIndex = 0;
tailLength = int(psize * 50);
tail = new PVector[tailLength];

inc1 = random(TWO_PI);
inc2 = random(TWO_PI);
inc3 = random(TWO_PI);

movingArea = moveSize - tailLength;
}

void draw() {
updatePosition();
drawTail();
}

void updatePosition() {
inc1 += inc1Increment;
inc2 += inc2Increment;
inc3 += inc3Increment;

pos.x = sin(inc1) * movingArea;
pos.y = cos(inc2) * movingArea;
pos.z = sin(inc3) * movingArea;
tail[tailPosIndex] = new PVector(pos.x, pos.y, pos.z);
}

void drawTail() {
float xp, yp, zp;
float xOff, yOff, zOff;
int nullPos = 0;
for (int i = 0; i < tailLength; i++) {
int index = (i + tailPosIndex + 1) % tailLength;
if ( i < tailLength - 1 && tail[index] != null) {
float per = (i - nullPos) / float(tailLength - nullPos);
xp = tail[index].x;
yp = tail[index].y;
zp = tail[index].z;
int nextIndex = (i + tailPosIndex + 2) % tailLength;
PVector v0 = PVector.sub(tail[index], tail[nextIndex]);
PVector v1 = v0.cross(new PVector(0, 1, 0));
v1.normalize();
PVector v2 = v0.cross(v1);
v2.normalize();
v1 = v0.cross(v2);
v1.normalize();
xOff = v1.x * psize * per;
yOff = v1.y * psize * per;
zOff = v1.z * psize * per;

fill(200, 255 * per);

vertex(xp - xOff, yp - yOff, zp - zOff);
vertex(xp + xOff, yp + yOff, zp + zOff);
}
else nullPos++;
}
endShape();

tailPosIndex++;
tailPosIndex %= tailLength;
}
}

class Planets {
float pX, pY, pZ;
float dZ;
float pSize= random(20,60);

Planets(float tempPX, float tempPY, float tempPZ, float tempDZ) {
pX = tempPX;
pY = tempPY;
pZ = tempPZ;
dZ = tempDZ;
}

void position() {
pushMatrix();
translate(pX,pY,pZ);
noStroke();
fill(255,255-map(pX,0,width,0,128));
sphere(pSize);

popMatrix();
}

void anim() {
pZ = pZ + dZ;
if(pZ >=0)
pZ = -1058.0;
}
}

class Stars {
float x,y,z;
float dZ;

Stars(float coordX, float coordY, float coordZ, float speedZ) {
x  = coordX;
y  = coordY;
z  = coordZ;
dZ = speedZ;
}

void position() {
stroke(250+z/depth, transparency);
point(x,y,z);
}

void anim() {
z = z + dZ;
if(z >= 0)
z = -1058.0;
}
}
``````
Tagged: