We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › coordinate of an object in a 2dim array
Page Index Toggle Pages: 1
coordinate of an object in a 2dim array (Read 656 times)
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();
}
}
Re: coordinate of an object in a 2dim array
Reply #1 - Jul 21st, 2009, 3:29am
 
Note that PVector class already exists in Processing. That's not a real problem but in general I try and avoid to mask native classes.

And you guessed it, you should put the calculation part in a separate function, to call each time you update the parameters.
Either you keep it as is, letting Java garbage collecting the old PVectors (might result in a performance hit), or you separate object creation and update, and just change the x, y, z values in the existing PVectors.
Re: coordinate of an object in a 2dim array
Reply #2 - Jul 21st, 2009, 5:44am
 
Great ! i put my calculation in a function, it works perfectly, i can control my object. Thank you PhiLho.
But i don t manage to find the coordinates x, y, z of each cube. I tried with a println("x") but the result is strange. Maybe i have to create an  array for my cubes in order to identify each of them. How can i find these coordinates ?
Re: coordinate of an object in a 2dim array
Reply #3 - Jul 21st, 2009, 8:52am
 
Mohammed wrote on Jul 21st, 2009, 5:44am:
But i don t manage to find the coordinates x, y, z of each cube.
I don't understand your problem. The coordinates are in your PVector, no
Re: coordinate of an object in a 2dim array
Reply #4 - Jul 21st, 2009, 9:27am
 
I don t understand how i can find the coordinates of my cube. What is
the synthax ? i wrote println(pos[1][10].x); but it doesn t work.
Println function doesn t return anything, i ve got a NullPointerException.
Re: coordinate of an object in a 2dim array
Reply #5 - Jul 21st, 2009, 10:44am
 
I don't know where you put your println, but it works for me.
For example, taking your first code above, I put the line:
println(pos[1][10].x + " " + pos[1][10].y + " " + pos[1][10].z);

just at the end of setup() and it works.
Re: coordinate of an object in a 2dim array
Reply #6 - Jul 22nd, 2009, 8:50am
 
you are right !! thank you for your help PhiLo !!
Page Index Toggle Pages: 1