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 & HelpOpenGL and 3D Libraries › from 3D to 2D coordinates
Page Index Toggle Pages: 1
from 3D to 2D coordinates (Read 1408 times)
from 3D to 2D coordinates
Nov 7th, 2007, 2:42pm
 
Hi,

please help I can't figure out.

I have 3D matrix of 5x5x5 spheres. I'm using CONTROL and mouse movement to rotate matrix. Now I want to select one of spheres with mouse. I can't because I used two times push pop matrix so screenX, screenY are not giving correct values. I can't figure out what kind of correction I have to apply.

I'm not sure if I'm using a good method to do this.

Here is my code :

import processing.opengl.*;

final int grille = 40; // space between spheres

int zoom = -200; // zoom - mouse wheel

// angle of rotation
float rotBuffX = 0;
float rotBuffY = 0;
//
final float rotVit = 0.01; // step of rotation

boolean rotateMode = false; // mouse rotation

// coords from matrix
float x[] = new float[125];
float y[] = new float[125];
float z[] = new float[125];
//

int ledSelected = -1;

void setup() {

 size(600,600, OPENGL);
 // smooth();

 noLoop();

 sphereDetail(6);

 noStroke();

 // mouse Wheel
 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
   public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {

     if (evt.getWheelRotation()<0) {
       zoom+=evt.getScrollAmount()+3;
     }
     else {
       zoom-=evt.getScrollAmount()+3;
     }
     redraw();
   }
 }
 );
 //

}


void draw() {

 background(0);

 //////// rotate mode
 if (rotateMode) {
   rotBuffX= mouseY*rotVit;
   rotBuffY= mouseX*rotVit;
 }
 ////////  

 translate(300,300,zoom); // center cube on the screen

 rotateX(rotBuffX);
 rotateY(rotBuffY);

 pushMatrix();

 /// show center of rotation
 fill(255,0,0);
 sphere(10);
 ///

 // center of rotation
 translate(-80, -80, -80);  
 
 fill(255); // paint in white spheres
 
 int cnt = 0; // count 0-125
 for (int h=0; h<5; h++) {

   for (int i=0; i<5; i++) {
     for (int j=0; j<5; j++) {

       pushMatrix();

       translate(i*grille, j*grille, h*grille);

       x[cnt] = modelX(0, 0, 0);
       y[cnt] = modelY(0, 0, 0);
       z[cnt] = modelZ(0, 0, 0);

       sphere(5);

       popMatrix();

       cnt++;
     }
   }
 }

 popMatrix();

 // draw 5x5 2D matrix for debug purposes This matrix must be in supperposition with first side of cube
 for (int i=0; i<25; i++) {
   fill(0,255,0);
   ellipse(x[i]-300, y[i]-300, 10,10);
 }


}

void keyPressed() {
 if (keyCode == CONTROL) { // activate rotation mode
   rotateMode = true;
   loop();
 }
}

void keyReleased()
{

 if (keyCode == CONTROL) { // desactivate rotation mode
   rotateMode = false;
   noLoop();
 }

}
Re: from 3D to 2D coordinates
Reply #1 - Nov 14th, 2007, 8:46pm
 
I'm not entirely sure if this is what you're looking for, but here's a way to figure out which spheres are at/near the current mouse position using screenX/Y/Z  (spheres turn white when moused over, adjust the threshold to make more or less greedy)

import processing.opengl.*;

final int grille = 40; // space between spheres

int zoom = -200; // zoom - mouse wheel  

// angle of rotation
float rotBuffX = 0;  
float rotBuffY = 0;

final float rotVit = 0.01; // step of rotation

boolean rotateMode = false; // mouse rotation

// coords from matrix
float x[] = new float[125];
float y[] = new float[125];
float z[] = new float[125];

int ledSelected = -1;

void setup() {
 size(600,600, OPENGL);
 sphereDetail(6);
 noStroke();
 // mouse Wheel
 addMouseWheelListener(new java.awt.event.MouseWheelListener() {  
   public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
     if (evt.getWheelRotation()<0) {
       zoom+=evt.getScrollAmount()+5;
     }  else {
       zoom-=evt.getScrollAmount()+5;
     }
     redraw();
   }
 }
 );
}


void draw() {
 background(0);
lights();
 //////// rotate mode
 if (rotateMode) {
   rotBuffX= mouseY*rotVit;
   rotBuffY= mouseX*rotVit;
 }
 translate(300,300,zoom); // center cube on the screen
 rotateX(rotBuffX);
 rotateY(rotBuffY);
 pushMatrix();
 /// show center of rotation
 fill(255,0,0);
 sphere(10);  
 // center of rotation
 translate(-80, -80, -80);  
 int cnt = 0; // count 0-125
 for (int h=0; h<5; h++) {
   for (int i=0; i<5; i++) {
    for (int j=0; j<5; j++) {
     pushMatrix();
      translate(i*grille, j*grille, h*grille);
      x[cnt] = screenX(0, 0, 0);
      y[cnt] = screenY(0, 0, 0);
      z[cnt] = screenZ(0, 0, 0);
      boolean picked = checkDist(x[cnt],y[cnt],z[cnt],15);
      if (picked) {
          fill(255,255,255); // white if mouse over
      } else {
          fill(0,64,255); // otherwise blue
      }
     sphere(5);
     popMatrix();
     cnt++;
    }
   }
 }
 popMatrix();
}

void keyPressed() {
 if (keyCode == CONTROL) { // activate rotation mode
   rotateMode = true;
   loop();
 }
}

void keyReleased()   {
 if (keyCode == CONTROL) { // deactivate rotation mode
   rotateMode = false;
   loop();
 }
}

boolean checkDist(float x1,float y1,float z1, float thresh) {
// check distance between mouse & object
 float theDist = dist(mouseX,mouseY,0,x1,y1,z1);
   if (theDist< thresh) {
     return true;
   } else {
       return false;
   }
}

Re: from 3D to 2D coordinates
Reply #2 - Nov 19th, 2007, 5:07pm
 
Thanks a lot Phoberman it works!!!
Page Index Toggle Pages: 1