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.
Page Index Toggle Pages: 1
3d grid (Read 1787 times)
3d grid
Feb 9th, 2010, 8:44pm
 
hi

I have made a grid in processing

The code I have is this which draws a grid
int gridSize = 40;



void setup() {

size(800,800);

background(0);

smooth();

noFill();

stroke(255);



//the grid

for (int i = 0; i <width; i+=gridSize) {

  for (int j = 0; j < height; j+=gridSize) {

    rect(i,j,gridSize,gridSize);

  }

}

}  


I want this grid to be in 3d view and to be able to navigate it using the keyboard arrows or the mouse

i have tried to do so usin the P3D
i have then changed the code to this:


int gridSize = 40;

void setup() {
size(800,800,P3D);
background(0);

smooth();
noFill();
stroke(255);
}

void draw(){
//the grid
translate(width/2, height/2);
rotate(frameCount*PI/60);

for (int i = 0; i <width; i+=700) {
  for (int j = 0; j < height; j+=700) {
     
    line(i,j,700,700);
  }
}
}  


but the result is not what i am expecting. i just need to see a 3d perspective view.

Any advice is very much welcome Smiley

thanks!



Re: 3d grid
Reply #1 - Feb 9th, 2010, 11:15pm
 
As a start, let's at least make this draw a grid in a 3D view.

Quote:
int gridSize = 40;

void setup() {
  size(800,800,P3D);
  background(0);
  smooth();
  noFill();
  stroke(255);
}

void draw(){
  //the grid
  background(0);
  translate(width/2, height/2);
  rotate(frameCount*PI/60);
  for (int i = 0; i <width; i+=gridSize) {
    for (int j = 0; j < height; j+=gridSize) {
      line(i,j,i+700,j);
      line(i,j,i,j+700);
    }
  }
}  



Do you need more help?
Re: 3d grid
Reply #2 - Feb 10th, 2010, 8:37am
 
hi
thank you for the reply but it is not what am after
i do not want the grid to rotate. what i need is that instead of seeing it in top view as in the very first code i posted.....I  want to see it in 3d perspective view.

never done this before so i am new to all this.

i changed the code to
int gridSize = 40;

void setup() {
 size(800,800,P3D);
 background(0);
 smooth();
 noFill();
 stroke(255);
 pushMatrix();
 
}

void draw(){
 //the grid
 background(0);
 

 
translate(width/2, height/2);
 rotate(radians(45));
 for (int i = 0; i <width; i+=gridSize) {
   for (int j = 0; j < height; j+=gridSize) {
     line(i,j,i+700,j);
     line(i,j,i,j+700);
   }
 }
}

but as you will see not at all near

thanks a lot
Re: 3d grid
Reply #3 - Feb 10th, 2010, 11:43am
 
Quote:
int gridSize = 40;

void setup() {
  size(800,800,P3D);
  background(0);
  smooth();
  noFill();
  stroke(255);
}

void draw(){
  background(0);
  pushMatrix();
  translate(width/2, height/2);
  for(int i = -width/2; i <width/2; i+=gridSize) {
    for(int j = -height/2; j < height/2; j+=gridSize) {
      int y = 200;
      line(i,          y, j,           i+gridSize, y, j          );
      line(i+gridSize, y, j,           i+gridSize, y, j+gridSize );
      line(i+gridSize, y, j+gridSize,  i,          y, j+gridSize );
      line(i,          y, j,           i,          y, j+gridSize );
    }
  }
  popMatrix();
}



Alright, here's a similar looking grid drawn in the plane y = 200.
Re: 3d grid
Reply #4 - Feb 10th, 2010, 11:48am
 
thanks you very much. this is great Smiley
how can i make it move up the screen so that i can see the whole of it.
i tried changing the int y = 200; to 300 but it got smaller. or if i put a smaller than 200 like 100 for example i could see more of it in a more perspective view.

i want to be able to move the view using the keyboard so I would say that the y=value ; should be a keyboard variable no?

thank you again very much

Re: 3d grid
Reply #5 - Feb 10th, 2010, 11:59am
 
You're dealing with a 3D view now!
You have three directions to work with!

RIGHT on the screen is the +X direction.
DOWN on screen is the +Y direction.
INTO the screen is the -Z direction.
(So LEFT, UP, and OUT OF would be -X, -Y, and +Z, respectively.)

When you change int y to something other than 200, it draws the grid at a different position in 3D space!

Try adjusting the X or Z positions of the lines too, to see where the grid moves.
Re: 3d grid
Reply #6 - Feb 10th, 2010, 12:13pm
 
Quote:
int gridSize = 40;

// New offsets to move the grid in a different place in space.
int xoffset = 0;
int yoffset = 0;
int zoffset = 0;

void setup() {
  size(800,800,P3D);
  background(0);
  smooth();
  noFill();
  stroke(255);
}

void draw(){
  background(0);
  pushMatrix();
  translate(width/2, height/2);

  stroke(255,0,0); // RED GRID
  xoffset = 0;
  yoffset = 200; // drawn at y = 200
  zoffset = 0; 
  drawGrid();
  
  stroke(0,255,0); // GREEN GRID
  xoffset = 0;
  yoffset = 250; // drawn at y = 250 ("below" the red grid)
  zoffset = -600; // drawn at z = 100 ("father away from us" that the red grid)
  drawGrid();
  
  stroke(0,0,255); // BLUE GRID
  xoffset = 600; // drawn at x = 50
  yoffset = -300; // looking at this almost on edge.
  zoffset = -600; //
  drawGrid();

  
  stroke(255,0,255); // PURPLE GRID
  xoffset = 0;
  yoffset = 0; 
  zoffset = 0; 
  pushMatrix();
  translate( -600, -300, -600); // We can also MOVE SPACE to draw the grid in a different place.
  // In fact, moving space is prefered...
  drawGrid();
  popMatrix();
  
  
  

  popMatrix();
}

void drawGrid(){
    for(int i = -width/2; i <width/2; i+=gridSize) {
    for(int j = -height/2; j < height/2; j+=gridSize) {
      int y = 200;
      line(xoffset + i,          yoffset + y, zoffset + j,           xoffset + i+gridSize, yoffset + y, zoffset + j          );
      line(xoffset + i+gridSize, yoffset + y, zoffset + j,           xoffset + i+gridSize, yoffset + y, zoffset + j+gridSize );
      line(xoffset + i+gridSize, yoffset + y, zoffset + j+gridSize,  xoffset + i,          yoffset + y, zoffset + j+gridSize );
      line(xoffset + i,          yoffset + y, zoffset + j,           xoffset + i,          yoffset + y, zoffset + j+gridSize );
    }
  }
}



Ignore comments that are obviously wrong...  Roll Eyes
Re: 3d grid
Reply #7 - Feb 16th, 2010, 12:02pm
 
Why not try the Shapes3D library. The following code uses this library and is fairly self explanatory.
Smiley

Code:
/*
To move
w = forward    s = stop    x = slow down/revrse
Travel direction and tilt is determind by the
mouse position
*/

import processing.opengl.*;
import shapes3d.utils.*;
import shapes3d.*;

Terrain terrain;
Box[] boxes;
TerrainCam cam;
long time;
final int NBRBOXES = 15;

// terrain data
float terrainSize = 1000;
float horizon = 1000;

// Camera data
float camSpeed;
int camHoversAt = 6;
float camTurnBy;
float camTilt;
final float camMinSpeed = -10;
final float camMaxSpeed = 50;

// Mouse sensitivity
final int deadZoneX = 4, deadZoneY = 3;
final float mouseXfactor = 1.8f, mouseYfactor = 1.1f;

void setup(){
 size(400,320, OPENGL);
 cursor(CROSS);

 terrain = new Terrain(this, 60, terrainSize, horizon);
 terrain.drawMode(Shape3D.WIRE);
 terrain.stroke(color(255,255,0));
 terrain.strokeWeight(1.25);
 terrain.tag = "Ground";
 terrain.tagNo = -1;

 boxes = new Box[NBRBOXES];
 for(int i = 0; i < NBRBOXES; i++){
   float size = random(20,40);
   boxes[i] = new Box(this, 10,size,10);
   boxes[i].fill(color(random(50,70),random(160,255),random(50,70)));
   boxes[i].drawMode(Shape3D.SOLID);
   PVector pos =  getRandomPosOnTerrain(terrain, terrainSize, size/2);
   boxes[i].moveTo(pos);
   terrain.addShape(boxes[i]);
 }
 
 camSpeed = 10;
 cam = new TerrainCam(this);
 cam.adjustToTerrain(terrain, Terrain.WRAP, camHoversAt);
 cam.camera();
 cam.speed(camSpeed);
 cam.forward.set(cam.lookDir);

 // Tell the terrain what camera to use
 terrain.cam = cam;

 time = millis();
}

void draw(){
 background(32);
 ambientLight(220, 220, 220);
 directionalLight(255,255,255,-100,-200,200);
 // Get elapsed time
 long t = millis() - time;
 time = millis();
 float esecs = t/1000.0;

 // Update camera speed and direction
 updateCamera(esecs);
 // Set the camera view before drawing
 cam.camera();
 // Draw the terrain
 terrain.draw();
}

void updateCamera(float secs){
 // Update camera speed and direction
 if(keyPressed){
   if(key == 'w' || key =='W'){
     camSpeed += (secs*10);
   }
   else if(key == 'x' || key =='X'){
     camSpeed -= (secs*10);
   }
   else if(key == 's' || key =='S'){
     camSpeed = 0;
   }
 }
 camSpeed = constrain(camSpeed, camMinSpeed, camMaxSpeed);
 cam.speed(camSpeed);

 // Change direction and tilt
 camTurnBy = 0;
 if(mouseX > 0 && mouseX < width && mouseY > 0 &&  mouseY < height){
   if(abs(mouseX - width/2) > deadZoneX){
     int deltaX = abs(mouseX - width/2) - deadZoneX;
     camTurnBy = mouseXfactor * deltaX * secs / (width * HALF_PI);
     if(mouseX < width/2)
       camTurnBy *= -1;
   }
   if(abs(mouseY - height/2) > deadZoneY){
     int deltaY = abs(mouseY - height/2) - deadZoneY;
     camTilt = mouseYfactor * deltaY / (height * HALF_PI);
     if(mouseY < height/2)
       camTilt *= -1;
     cam.lookDir.y = camTilt;
     cam.lookDir.normalize();
   }
   cam.rotateViewBy(camTurnBy);
   cam.turnBy(camTurnBy);
 }
 // Calculate amount of movement based on velocity and time
 cam.move(secs);
 // Adjust the cameras position so we are over the terrain
 // at the given height.
 cam.adjustToTerrain(terrain, Terrain.WRAP, camHoversAt);
}

/**
* Get a random position on the terrain avoiding the edges
* @param t the terrain
* @param tsize the size of the terrain
* @param height height above terrain
* @return
*/
PVector getRandomPosOnTerrain(Terrain t, float tsize, float height){
 PVector p = new PVector(random(-tsize/2.1f, tsize/2.1f), 0, random(-tsize/2.1f, tsize/2.1f));
 p.y = t.getHeight(p.x, p.z) - height;
 return p;
}
Re: 3d grid
Reply #8 - Feb 16th, 2010, 2:25pm
 
amazing! cant thank you enough!!! this is a great help and very close to what i want to achieve . i am sure that it will also be a great help for many other people too!!! thanks!!!!
Page Index Toggle Pages: 1