Mohammed
YaBB Newbies
Offline
Posts: 12
coordinate of an object in a 2dim array
Jul 21st , 2009, 3:00am
Hello, how can i find the coordinate of each of my box ? Maybe i have to create an array for my object but i don t understand how i can link this with my 2dim array of position. Then my controls doesn t work, i think it s because my calculation is in my setup part and consequently the calculation of my variable is closed but what can i do ? Here the code : int samples = 20; PVector [][]pos = new PVector[samples][samples]; float a1 = 10., a2 = 10., a3 = 10.; float u1 = 0., u2 = 20., v1 = 0., v2 = 20.; float dU = (u2 - u1) / samples; float dV = (v2 - v1) / samples; float n = 1., e = 1.; void setup(){ background(0); // size(screen.width,screen.height, P3D); size(500,500, P3D); directionalLight(126, 126, 126, 0, 0, -1); camera(-15,15,-20,0,0,0,0,0,1); //get a viewpoint float u = u1; for(int i=0; i<samples; i++){ float v = v1; for(int j=0; j<samples; j++){ float x = a1 * sqCos (u, n) * sqCos (v, e); float y = a2 * sqCos (u, n) * sqSin (v, e); float z = a3 * sqSin (u, n); pos[i][j] = new PVector(x, y, z); v += dV; } u += dU; } } void draw(){ background(0); rotateY(frameCount * 0.01); for(int i=0; i<samples; i++){ for(int j=0; j<samples; j++){ pos[i][j].display(); }}} float sign ( float x ) { if ( x < 0 )return -1; if ( x > 0 )return 1; return 0; } float sqSin( float v, float n ) { return sign(sin(v)) * pow(abs(sin(v)),n); } float sqCos( float v, float n ) { return sign(cos(v)) * pow(abs(cos(v)),n); } void keyPressed() { if (key == CODED) { if (keyCode == UP) { n = 3.; e = 3.; } if (keyCode == LEFT) { n = 1.; e = 1.; } if (keyCode == RIGHT) { n = -0.2; e = 1.; } if (keyCode == DOWN) { n = 1; e = 0.3; } } } class PVector{ float x, y, z; PVector(float xInput, float yInput, float zInput){ x = xInput; y = yInput; z = zInput; } void display(){ pushMatrix(); translate(x, y, z); box(1); popMatrix(); } }