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 › getting screen co-ordinates
Page Index Toggle Pages: 1
getting screen co-ordinates (Read 393 times)
getting screen co-ordinates
Jul 11th, 2007, 4:51pm
 
I am drawing a 3D line using a succession of transformations (rotateX , rotateY). I then want to use a point along the line to begin drawing another line (branch) - but am having difficulty in finding the starting co-ordinate for the new line to begin from (relative to the main line). I have tried using screenX, screenY and modelX, modelY to get the co-ordinates at which the new line should start from - but with no success.

I have a small example of what I am having problems with here: (a red ellipse is drawn at (0,0,0) using the current transformation matrix and have tried to  'get' the screen co-ordinates of this position using screenX , screenY - and represented this by drawing a blue ellipse - which seems to be way off where it should be.

Any help would be great : )

Code:

import processing.opengl.*;

// a test to replicate the 'branching 3d line problem
// create an object which is translated and re-draws itself.

float angle = 1;
int segmentCount = 2;
int branchNum = 10;
void setup () {
 frameRate (10);
 size (800, 800, OPENGL);
 background (256, 256, 256);
}


void draw () {
 background (256, 256, 256);
 /////////////////////////////////////////////////////////////
 // position / draw entire line on the stage //
 translate (width/2, height/2); // this is the starting point for the entrire environment
 rotateX (radians (angle));  // this applys to the ENTIRE line
 rotateZ(radians (angle));  // this applys to the ENTIRE line
 ////////////////////////////////////////////////////////
 ///// DRAW LINE SEGMENTS //////////////
 pushMatrix ();
 for (int i=0; i<segmentCount; i++) {
   rotateZ (radians (7)); // this applies WITHIN the line
   rotateX (radians (12));
   //rotateY (radians (angle));
   stroke (0, 0, 0);
   line (0, 0, 0, 10, 10, 0);
   fill (0,0,0);
   ellipse (0, 0, 10, 10);
   if (i == 10) {
     // CREATE A STARTING POINT FOR A BRANCH
     //  createBranch (screenX (0,0,0), screenY (0, 0, 0), modelZ (0, 0, 0));
     //  createBranch (modelX (0,0,0), modelY (0, 0, 0), modelZ (0, 0, 0));
     createBranch ();
   }

   translate (10, 10, 0); // this translation allows the next segment to be drawn
 }
 popMatrix () ;
 /////////////////////////////////////////////////////////////////////
 angle +=0.5;
 segmentCount ++;
}

void createBranch ( ) {
 //////////////////////////////////////
 // Draw red circle at position along line ///
 fill (256, 0, 0);
 stroke (256, 0, 0);
 ellipse (0, 0, 15, 15);
 ////////////////////////////////////
 float xpos = (screenX (0,0,0));
 float ypos = (screenY (0,0,0));  
 float zpos = (screenZ (0,0,0));
 pushMatrix();
 translate (xpos, ypos, zpos);
 fill (0, 0, 256);
 ellipse (0, 0, 15, 15);
 popMatrix();
}
                                                                                   
Page Index Toggle Pages: 1