How can I use translate() to move a shape?

edited August 2016 in Library Questions

I have used processing for a few months now. I had a question regarding the uses of Translate. I want to make it so that the grid, which I defined using vertices and beginShape(QUAD_STRIP);, lies in the center of the window. I would also like the sphere to be in the center. The code is below. Any answers?

import peasy.*;
int w = 20;
int h = 20;
int scl = 10;
float [][] grid;
PeasyCam camera;

void setup(){
  size(1000, 1000, P3D);  
  grid = new float[w][h];
camera = new PeasyCam(this, 0, 0, 0, 100);
}
void draw(){
    lights();
   background (0);
      pushMatrix();
     fill(255); 
   sphere(50);
   // noFill();
    noStroke();

popMatrix();

pushMatrix();

//translate(width/2, height/2, 0);
 translate(width/2, height/2 +50);
   rotateX(PI/2.8);                      //setting up perspective

   translate(-w/2, -h/2);
    for(int y = 0; y < h-1; y++) {
noFill();
stroke(255);
     beginShape(TRIANGLE_STRIP);       //preset object in Processing; I can use a net of vertices to make a strip/plane of triangles. The beginShape(); object uses any vertices to create the object specified in the argument ().
     for(int x = 0; x < w; x++){
      vertex(x*scl, y*scl, grid[x][y]);
      vertex(x*scl, (y+1)*scl,grid[x][y+1]); 
     }
     endShape();
   }
popMatrix();
  
}
Tagged:

Answers

  • edited July 2016

    Well, it appears I cant format, either. This is my first time using the forums. Any tips? Anyways, this is all the code that needs to be seen.

  • Answer ✓

    Lights after background

    Then before pushMatrix say translate (111,33);

    or what ever you need there to correct

  • Answer ✓

    that code isn't complete.

    ctrl-t in the editor will indent things nicely.

    2d translates in a 3d sketch is a bit strange, especially if you're using peasycam to control the camera. use the 3d version of translate with 3 parameters.

  • edited August 2016

    Thanks for the answers. However, despite making these changes, I still cannot get the grid to remain in the center of the screen. I would like it so that the grid remains in the center, no matter the size of the window; I attempted to use width/2 and height/2 to achieve this, but it appears as though I have failed. Also- how can i fit large code into the post? It doesn't seem to let me when I "ctl-o" the entire thing- it cuts off a bit.

        import peasy.*;
        int w = 20;
        int h = 20;
        int scl = 10;
        float [][] grid;
        PeasyCam camera;
    
        void setup() {
          size(1000, 1000, P3D);  
          grid = new float[w][h];
          camera = new PeasyCam(this, 0, 0, 0, 100);
        }
    
        void draw() {
          background (0);
          lights();
    
          pushMatrix();
          fill(255); 
          sphere(50);
          noStroke();
          popMatrix();
    
          pushMatrix();
          translate(width/2, height/2, 0);
          noFill();
          stroke(255);
          for (int y = 0; y<h-1; y++) {
            beginShape(TRIANGLE_STRIP);     
            for (int x = 0; x < w; x++) {
              vertex(x*scl, y*scl, grid[x][y]);
              vertex(x*scl, (y+1)*scl, grid[x][y+1]);
            }
            endShape();
          }
          popMatrix();
        }
    
  • Answer ✓

    do a translate(-width/2,-height/2,0) to set origin back to 0,0...? just before popMatrix();

  • Didn't seem to work. Thanks for the suggestion, though.

  • Answer ✓

    Check peasy - possible it looks at 0,0 and not at width/2 as the 2D camera (see reference of camera())

  • I found a way around the problem as a whole. Thanks for the contribution though!

  • Answer ✓

    great!

Sign In or Register to comment.