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 › implement 2d interface for picking to 3d
Page Index Toggle Pages: 1
implement 2d interface for picking to 3d (Read 277 times)
implement 2d interface for picking to 3d
Aug 4th, 2009, 10:11am
 
hello,

i try to implement this 2d interface in order to use it in a 3d project.
I know that i have to use screenX and screenY but i don't understand how it function. If i put this sketch in P3D, the rectangle are not picking correctly, there is a gap between the target and the coordinate of my computer mouse. Here the code :

//press "i" to draw the grid
boolean show = false;
boolean Switch = false;
int cellsize = 15;
int COLS, ROWS;
int[][] board;

void setup()
{
 size(800, 500);
 smooth();
 //initialize rows, columns and set-up arrays
 COLS = width/cellsize;
 ROWS = height/cellsize;
 board = new int[COLS][ROWS];
 background(0);
 //call function to fill array with random values 0
  for (int i =0;i < COLS;i++) {
   for (int j =0;j < ROWS;j++) {
     board[i][j] = 0;
   }}
 frameRate(30);
}

void draw()
{
 background(0);
 if(key=='i') show = true;
 else show = false;
 if(show){
 grid();
 render();
  }}

void grid() {
 for (int a=0; a<=COLS; a++) {
   for (int b=0; b<=ROWS; b++) {
     stroke(0, 0, 255, 45);
     //stroke(15);
     noFill();
     rectMode(CENTER);
     rect(a*cellsize, b*cellsize, cellsize, cellsize);
     }}}

void render() {
 for ( int i = 0; i < COLS;i++) {
   for ( int j = 0; j < ROWS;j++) {
     if ((board[i][j] == 1)) {
       fill(255);
       noStroke();
       rect(i*cellsize,j*cellsize,cellsize,cellsize);
     }}}}
void mousePressed() {
 if (mouseX<width && mouseX >0 && mouseY <height && mouseY > 0) {
  int new_x;
 int new_y;
 
 new_x = mouseX/cellsize;
 new_y = mouseY/cellsize;

if(Switch) Switch = false;  
else if(!Switch) Switch = true;

if(Switch)
board[new_x][new_y] = 1;
else  board[new_x][new_y] = 0;
 }}

 




Re: implement 2d interface for picking to 3d
Reply #1 - Aug 4th, 2009, 11:34am
 
so i manage to make it in 3dim but i've got a new problem, my grid
is distorded in P3D. I just put
size(800, 500, P3D);
i don't know why.
Page Index Toggle Pages: 1