Populate 3d array for sphere
in
Programming Questions
•
2 months ago
Hello!
Sorry if this has been discussed before, but I can't seem to find any useful information about this. I'm extremely new to processing and I'm having issues with this asignment for a course. I don't want you to solve my homework, but I'm really stuck.
I'm using a trae.physics library, but that's not really the issue. We're required to use a fairly similar behaviour of the nodes, but instead of a 2d grid/array, we need to implement a 3d array. I can't seem to find a way to populate an array on the z direction.
If I add a third line on the draw () function to populate z, then it all goes downhill (provided that I declare the corresponding variables). And this is obviously not even going to work for a sphere, but a cube would be a start. I can't even imagine how to populate in degrees or using circular functions.
Any hint or reference would be extremely appreciated. Here is the code: Thanks in advance.
// Original code from:
http://murderandcreate.com/physics/
// modified by franklin hernandez-castro
// tec costa rica 2012Pins networkPins network
import traer.physics.*;
Particle mouse; // particle on mouse position
Particle[] particles; // the moving particle
Particle[] orgParticles; // original particles - fixed
Spring [] mySprings;
ParticleSystem myParticleSystem; // the particle system
// initial parameters
int xCount = 50;
int yCount = 30;
int zCount = 30; // I added this
int totalNodes = xCount * yCount;
float gridSizeX, gridSizeY, gapX, gapY, border;
int radio = 4; // point size
// do i need to add a gridSizeZ and a gapZ? (there's no way to use them below)
void setup() { //---------------------------------------------------------------------------
size(640, 480, P3D);
smooth();
border = 20;
gridSizeX = width -2*border;
gridSizeY = height-2*border;
gapX = gridSizeX / (xCount-1);
gapY = gridSizeY / (yCount-1);
myParticleSystem = new ParticleSystem(0, 0.05);
mouse = myParticleSystem.makeParticle(); // create a particle for the mouse
mouse.makeFixed(); // don't let forces move it
particles = new Particle[totalNodes]; // particle array
orgParticles = new Particle[totalNodes]; // fixed particle array
mySprings = new Spring [totalNodes]; // spring array
int a;
for (int y=0; y<yCount; y++) { // go through all columns
for (int x=0; x<xCount; x++) { // go through all rows
// if i add a int x=0; x<xCount; x++ I get an error because
//processing can't read Z as an axis, but I can't use translate()
// as per suggested by the documentation. Same problem in draw().
a = y*xCount+x; // 2D to 1D transformation
float xPos = x*gapX+border;
float yPos = y*gapY+border;
particles[a] = myParticleSystem.makeParticle(0.8, xPos, yPos, 0);
orgParticles[a] = myParticleSystem.makeParticle(0.8, xPos, yPos, 0);
orgParticles[a].makeFixed();
// making springs between both arrays
mySprings[a]= myParticleSystem.makeSpring(particles[a], orgParticles[a], 0.2, 0.1, 0 );
// make interaction between mouse and particles
//myParticleSystem.makeAttraction(particles[a], mouse, -10000, 0.1);
myParticleSystem.makeAttraction(particles[a], mouse, 100000, 100); // 50000, 100
}
}
} // end setup
void draw() { //--------------------------------------------------------------------------
background(250);
noStroke();
//println("framerate: " + frameRate); //if you want to tracking efficiency
mouse.position().set(mouseX, mouseY, 0 ); // tracking mouse
myParticleSystem.tick();
int a;
float posx, posy;
for (int y=0; y<yCount; y++) { // go through all columns
for (int x=0; x<xCount; x++) { // go through all rows
a = y*xCount+x; // 1D to 2D
posx = particles[a].position().x();
posy = particles[a].position().y();
//float w = map(dist(posx, posy, mouseX, mouseY), 0, sqrt(width*width + height*height), 200, 50);
fill(#689DBC);
//fill(w);
noStroke();
ellipse(posx, posy, radio, radio);
}
} // end ciclos for
// drawing springs
for (int n =0; n < mySprings.length; n++){
float x1 = mySprings[n].getOneEnd().position().x();
float y1 = mySprings[n].getOneEnd().position().y();
float x2 = mySprings[n].getTheOtherEnd().position().x();
float y2 = mySprings[n].getTheOtherEnd().position().y();
stroke(#2D79AA, 85);
line (x1,y1,x2,y2);
}
} // end draw
1