how to sort data to build planar quads
in
Contributed Library Questions
•
2 years ago
Hi All,
I am doing a small project which involves creating different layers of hexagon grid with particles and springs.
I managed to create different layers with a 3D array (but not sure this is efficient - so if u know a better way tell me) now I want to build planar faces between spring on one layer and another in the top one.
so now to draw lines btw springs I call the .getOneEnd() and getOtherEnd() methods of Traer library . This gives me two points - I need 4 for planar faces. Any idea about how I could organize the code?
- import processing.opengl.*;
- import traer.physics.*;
- import peasy.*;
- PeasyCam cam;
- ParticleSystem pSys;
- ArrayList particles = new ArrayList();
- ArrayList springs = new ArrayList();
- //GLobal Variables
- int Scale = 3;
- //Grid extents
- int max_x = 20;
- int max_y = 15;
- //Top most level
- int max_z =20;
- // numebrs of layers
- int numL = max_z/4;
- Particle [][][]grid = new Particle [max_x][max_y][max_z];
- float K = .2;//.4
- float D = 0.2;
- //spring rest length
- float rest =.8; //.8
- void setup() {
- size(600, 600, OPENGL);
- smooth();
- fill(0);
- cam = new PeasyCam(this,width/8,0,0,250);
- cam.setMinimumDistance(250);
- cam.setMaximumDistance(800);
- pSys = new ParticleSystem(0, 0.25);
- //Function to add particle and connect them with springs according to hex topology
- addHex();
- }
- void draw() {
- // pSys.tick(1);
- // pSys.setGravity(0,0,2);
- background (0);
- renderParticles();
- println(pSys.numberOfSprings());
- }
- void addHex() {
- for (int i=0; i<max_x; ++i) {
- for (int j=0; j<max_y; ++j) {
- for(int k=0; k<max_z; k+=numL) {
- grid[i][j][k]= pSys.makeParticle(1,i*Scale, j*Scale, k);
- if ((i==0 || j==0) ||(i==max_x-1 ||(j==max_y-2))) {
- //make boundary fixed
- grid[i][j][k].makeFixed();
- }
- }
- }
- }
- for (int i=0;i<max_x-2; ++i) {
- for (int j=i%2*2; j<max_y-3; j+=4) {
- for(int k=0; k<max_z; k+=numL) {
- Particle a = grid[i][j+1][k];
- Particle b = grid[i][j+2][k];
- Particle c = grid[i+1][j+3][k];
- Particle d = grid[i+2][j+2][k];
- Particle e = grid[i+2][j+1][k];
- Particle f = grid[i+1][j][k];
- pSys.makeSpring(a,b, K, D, rest);
- pSys.makeSpring(b,c, K, D, rest);
- pSys.makeSpring(c,d, K, D, rest);
- pSys.makeSpring(d,e, K, D, rest);
- pSys.makeSpring(e,f, K, D, rest);
- pSys.makeSpring(f,a, K, D, rest);
- }
- }
- }
- }
- void renderParticles() {
- for (int i=0; i<pSys.numberOfParticles(); ++i) {
- Particle c = pSys.getParticle(i);
- pushMatrix();
- translate(0,0,c.position().z()*Scale);
- ellipse (c.position().x()*Scale, c.position().y()*Scale,3,3);
- popMatrix();
- }
- for (int i=0;i<pSys.numberOfSprings(); i++) {
- Spring s = pSys.getSpring( i );
- stroke(0,255,0);
- strokeWeight(1);
- Particle a = s.getOneEnd();
- Particle b = s.getTheOtherEnd();
- line(a.position().x()*Scale,a.position().y()*Scale,a.position().z()*Scale, b.position().x()*Scale, b.position().y()*Scale, b.position().z()*Scale);
- // draw a Plane btw a spring on a lower level and one on the next level
- // beginShape();
- // vertex(x,y,z);
- // vertex(x,y,z);
- //
- // vertex(x,y,z);
- // vertex(x,y,z);
- // endShape(CLOSE);
- }
- }
1