FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Getting global coordinates
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Getting global coordinates  (Read 833 times)
jhv2

WWW
Getting global coordinates
« on: Oct 12th, 2004, 1:56am »

Hi all,
 
I am modeling something much like a solar-system, with Suns, planets, and moons.  It was easy to calculate because with push() and pop() I can translate and rotate all over the place to draw each moon relative to each planet.
 
All I needed was to store (in an array) and calculate three angles and a radius to get all the 3d wobbles and twists I was looking for.  
 
Now, I would like to draw lines and boxes recording each step of the 3d systems for dxf export into Rhino.  The problem is I need to store the variables using a Global coordinate system, not all these angles!!!
 
The screenX and screenY commands are working for me, but as many have also noted in this forum, screenZ isn't that great.  It returns valse betwen .95 an 1 for me.
 
As a result, I'm drawing these moons' lines as flatened tubes - twisting thru space.  Very frustrating.  Has anyone found a way around this problem?  I need a way to get polar cordinates relative to push() and pop() into global coordinates?
 
(will post code & a link here in 2 hours)
 
Thanks,
 
Jason
 
 
cello

marcello3d WWW
Re: Getting global coordinates
« Reply #1 on: Oct 12th, 2004, 3:00am »

I'm probably wrong, but have you tried the objectX/Y/Z commands?
 
Marcello
 
jhv2

WWW
Re: Getting global coordinates
« Reply #2 on: Oct 12th, 2004, 4:18am »

Right,
 
I'm with you there, I used the screenX command as input for the objectX as in the code below..., it doesn't work...
 
Also I can only post the code as the applet won't expot yet, the code is here...
 
http://pantheon.yale.edu/~jhv2/Arg.html
 
but the problem is pasted below... (thanks)
 
 
Code:

 void step_Satellites(){
    if( THIS_STEP >= sat_Path.length ){
      float[][] temp = new float[ sat_Path.length+MAX_ARRAY ][3];
      System.arraycopy( sat_Path, 0, temp, 0, sat_Path.length );
      sat_Path = temp;
    }
 
    angularVelocity = angularVelocity + random(-SWOOSHYNESS, SWOOSHYNESS);
    theta += angularVelocity;
 
    // Calculate coordinates and stuff.
    // Get the last position of the particle.
    float x1 = parent.px;
    float y1 = parent.py;
    float z1 = parent.pz;
 
    // Get the current position of the particle.
    float x2 = parent.x;
    float y2 = parent.y;
    float z2 = parent.z;
 
  float[] vec = { x2-x1, y2-y1, z2-z1 };
    float mag = sqrt( pow(vec[0],2) + pow(vec[1],2) + pow(vec[2],2) );
    float mag_xy = sqrt( pow(vec[0],2) + pow(vec[1],2) );
    float mag_z = sqrt( pow(mag,2) - pow(mag_xy,2) );
    float altitude = -atan2( mag_z,mag_xy );
    if( z1>z2 ) altitude = -altitude;
    float azimuth = atan2( vec[1], vec[0] );
  float[] mid = { vec[0]/2.0f+x1, vec[1]/2.0f+y1, vec[2]/2.0f+z1 };
 
    push(); //pushMatrix();
 
    // Translate to the current position of the particle.
    translate( x2, y2, z2 );   //applyTranslation( x2, y2, z2 );
 
    // Rotate the coordinate axes to align with the particle's path.
    rotateZ( azimuth );    //applyRotation( 0,0,1,azimuth );
    rotateY( altitude );   //applyRotation( 0,1,0,altitude );
 
    // Rotate around the x-axis to get the position of the satellite.
    rotateX( theta );    //applyRotation( 1,0,0,theta );
 
    // Translate to the satellite's position away from the parent.
    translate( 0, radiusAroundParent, 0 );  //applyTranslation( 0, radiusAroundParent, 0 );
 
    float[] satellitePosition; //= applyCurrentMatrix( 0,0,0 );
 
    float sx, sy, sz;
    sx = screenX(0,0,0);   <-----------------------------------HERE AND BELOW
    sy = screenY(0,0,0);
    //sz = screenZ(0,0,0);
 
    push();
    rotateX(PI);
    sz = screenY(0,0,0);
    pop();
 
    pop();
 
    // Reorient to the world axes and find the satellite's real coordinates.
    sat_Path[THIS_STEP][0] = objectX(sx,sy,sz);
    sat_Path[THIS_STEP][1] = objectY(sx,sy,sz);
    sat_Path[THIS_STEP][2] = objectZ(sx,sy,sz);
  }
« Last Edit: Oct 12th, 2004, 9:13pm by jhv2 »  
fry


WWW
Re: Getting global coordinates
« Reply #3 on: Oct 12th, 2004, 5:21am »

screen* and object* aren't supposed to be used together like that, they're not functions of one another in any way. use objectX/Y/Z to get the transformed version of the coordinates in object space, these will be suitable for writing out to a file.  
 
screenX/Y are just literally that--the on-screen coordinates. as mentioned elsewhere, since a screen has no Z, the value isn't useful except in comparison, because it's weighted against the distance away so that resolution in z decreases as you get away from the screen.  
 
someday we'll have more complete documentation for these things, but for now we have the forum
 
jhv2

WWW
Re: Getting global coordinates
« Reply #4 on: Oct 12th, 2004, 2:38pm »

We kind of figured that the screenZ was a dead end, so we held our breath and we did our own matrix multiplication to keep track of the coordinates ourselves... Still no luck.  That code is below, and we couldn't find the error tripping us up...  
 
Perhaps anyone can think of an example program that might be doing what we're trying?  I don't even know where to look!
 
(thanks again ya'll, and continued sucess to all!!!)
 
Jason
 
 
 
Code:

 
float[][] currentTransformation = { { 1,0,0,0 },
{ 0,1,0,0 },
{ 0,0,1,0 },
{ 0,0,0,1 } };float[] applyCurrentMatrix( float x, float y, float z ){
float[] vector = { x,y,z,1 };
float[] result = new float[4]; for( int r=0;r<4;r++ )
for( int n=0;n<4;n++ )
result[r] += currentTransformation[r][n] * vector[n]; return result;
}float[][] multiplyMatrices( float matrix0[][], float matrix1[][] ){
float[][] result = new float[4][4]; for( int r=0;r<3;r++ ){
for( int c=0;c<3;c++ ){
result[r][c] = 0; for( int n=0;n<3;n++ )
result[r][c] += matrix0[r][n] * matrix1[n][c]; }
} return result;
}void applyTranslation( float x, float y, float z ){
float[][] translation = { { 1,0,0,x },
{ 0,1,0,y },
{ 0,0,1,z },
{ 0,0,0,1 } }; currentTransformation = multiplyMatrices( currentTransformation, translation );
}void applyRotation( float x, float y, float z, float angle ){
float vector[] = { x, y, z, 1 };
float mag = sqrt( pow(vector[0],2) + pow(vector[1],2) + pow(vector[2],2) );
float[] u = { vector[0]/mag, vector[1]/mag, vector[2]/mag };
float rotation[][] = { { 0,0,0,0 },
{ 0,0,0,0 },
{ 0,0,0,0 },
{ 0,0,0,1 } };float[][] S = { { 0,-u[2],u[1] },
{ u[2], 0, -u[0] },
{ -u[1], u[0], 0 } };float[][] uuT = { { u[0]*u[0], u[0]*u[1], u[0]*u[2] },
{ u[0]*u[1], u[1]*u[1], u[1]*u[2] },
{ u[0]*u[2], u[1]*u[2], u[2]*u[2] } };float[][] IuuT = { { 1-u[0]*u[0], u[0]*u[1], u[0]*u[2] },
{ u[0]*u[1], 1-u[1]*u[1], u[1]*u[2] },
{ u[0]*u[2], u[1]*u[2], 1-u[2]*u[2] } }; for( int r=0;r<3;r++ ){
for( int c=0;c<3;c++ ){
S[r][c] *= sin(angle);
IuuT[r][c] *= cos(angle);
}
} for( int r=0;r<3;r++ ){
for( int c=0;c<3;c++ ){
rotation[r][c] = uuT[r][c] + IuuT[r][c] + S[r][c];
 
« Last Edit: Oct 12th, 2004, 9:12pm by jhv2 »  
_alcys_


Re: Getting global coordinates
« Reply #5 on: Oct 12th, 2004, 2:47pm »

Hi,
is a solution perhaps in this pde?  See, in the source, the storage of the volute.
 
http://www.alcys.com/p5/Alcys_RibbonAndRoll/applet/index.html
 
TomC

WWW
Re: Getting global coordinates
« Reply #6 on: Oct 12th, 2004, 4:17pm »

Hmm, Jason... your code makes my head hurt!
 
It looks like you're trying to do too many things at once.
 
objectX/Y/Z will give the absolute x/y/z coordinates according to the current transforms.
 
screenX/Y will give the position that a point will be drawn in 2D, according to the current transforms.  (If I understand correctly, screenZ will give you the value of the depth-buffer for that point, which probably isn't any use to you here).
 
So you definitely want objectX/Y/Z.
 
For planets, the neatest way is to start with a sun, and then nest the planets inside the sun's transforms, and then nest the moon transforms inside each planet.
 
So if everything is stored in a class called Planet, with an array of Planets called children, then (pseudo)code for a draw function belonging to the planet object would be something like:
 
Code:

void draw() {
  push();
  rotateY(orbitAngle);        // or whatever wacky 3D
  translate(0,0,orbitRadius); // stuff you were doing  
  // (optionally store position using objectX/Y/Z)
  sphere(radius)
  for (int i = 0; i < children.length; i++) {
    Planet p = (Planet)children[i];
    p.draw();
  }
  pop();
}

 
 
By the way, to mark up code on this forum, use BBCode, i.e. square brackets for <code> etc...
« Last Edit: Oct 12th, 2004, 4:28pm by TomC »  
jhv2

WWW
Re: Getting global coordinates
« Reply #7 on: Oct 14th, 2004, 7:26pm »

Thanks guys,  
 
We got it to work with ScreenX, etc coordinates after all, no matrix multiplication necessary!  We thought that screen* returned cordinates like in the blender environment, and were adding extra steps needlessly.  I can appreciate how documenting ScreenX,Y,&Z in reference might cause more confusion too, as its a little crazy to keep track of.
 
Again, thanks for your help.
 
Jason
 
Pages: 1 

« Previous topic | Next topic »