Mohammed
YaBB Newbies
Offline
Posts: 12
Pick an 3d object
Jul 22nd , 2009, 8:47am
Hello, i try to pick one of my object but it doesn t work. Maybe the overlaping of the cubes prevents me from picking. If one object is picked, i have a message but it never happens.I don t know why. here the code : int samples = 20; PImage b; 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(500,500, P3D); directionalLight(126, 126, 126, 0, 0, -1); camera(-15,15,-20,0,0,0,0,0,1); } void move(){ 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); move(); 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 mousePressed(){ for(int i=0; i<samples; i++){ for(int j=0; j<samples; j++){ pos[i][j].pick(mouseX,mouseY); //pick an object }}} class PVector{ float x, y, z; boolean picked = false; PVector(float xInput, float yInput, float zInput){ x = xInput; y = yInput; z = zInput; } void pick(float xp, float yp){ for(int i=0; i<samples; i++){ for(int j=0; j<samples; j++){ if(dist(xp,yp,screenX(pos[i][j].x,pos[i][j].y,pos[i][j].z),screenY(pos[i][j].x,pos[i][j].y,pos[i][j].z))<10) picked = true; else picked = false; }} //println(pos[1][10].x); } void display(){ if(picked==true) //if picked println("picked"); // then write picked else //println("unPicked"); // else write unPicked pushMatrix(); translate(x, y, z); box(1); popMatrix(); } }