1

I am facing difficulty with the translate() function for objects as well as objects in general in Processing. I went through the examples and tried to replicate the manners by which they instantiated the objects but cannot seem to even get the shapes to appear on the screen no less move them. I instantiate the objects into an array using a nested for loop and expect a grid of the objects to be rendered. However, nothing at all is rendered.

My nested for loop structure to instantiate the tiles:

for(int i=0; i<102; i++){
   for(int j=0; j<102; j++){
      tiles[i][j]=new tile(i,0,j);
      tiles[i][j].display();
   }
}

And the constructors for the tile class:

tile(int x, int y, int z){
this.x=x;
this.y=y;
this.z=z;
beginShape();
 vertex(x,y,z);
 vertex(x+1,y,z);
 vertex(x+1,y,z-1);
 vertex(x,y,z-1);
endShape();
}

Nothing is rendered at all when this runs. Furthermore, if this is of any concern, my translations(movements) are done in a method I wrote for the tile class called move which simply calls translate. Is this the correct way? How should one approach this? I can't seem to understand at all how to render/create/translate individual objects/shapes.

4

3 回答 3

0

您绝对可以将 pushMatrix() 和 translate() 与 beginShape() 等一起使用,它可能不完全符合您的期望,但它肯定会从默认原点移动东西。

你上面的例子出了什么问题,你把drawing()代码放在构造函数中,你应该把它放在显示函数中。

所以:

公共无效显示(处理过程){proc.beginShape()等}

display() 也需要在 draw() 循环中调用,所以初始化你的图块一次,然后在 draw() 中显示它们。

于 2010-06-14T13:46:49.690 回答
0

如果您使用beginShape() ,则转换(例如平移、旋转等)不起作用,因为您只是指定要绘制的直接坐标。如果您依赖翻译的结果将对象放入可见位置,这可能就是您没有任何结果的原因。

此外,根据你如何看待你的场景,你可能有 z 朝向相机,所以你的对象是在你从侧面看它们的时候被绘制的,因为它们是 2d 对象,所以你什么都看不到,尝试使用 x/y 或 y/z 而不是您现在正在做的 x/z。

于 2010-06-02T01:11:36.207 回答
0

您应该遵循@Tyler 关于在 2D 平面(x/y、y/z、x/z)中绘图的建议。

您的形状可能不会渲染,因为您可能会绘制一次,并在 draw() 方法中清除屏幕,但我不确定,因为我看不到您的其余代码。

这就是我的意思:

tile[][] tiles;
int numTiles = 51;//x and y number of tiles

void setup() {
  size(400,400,P3D);
  tiles = new tile[numTiles][numTiles];
  for(int i=0; i<numTiles; i++)
    for(int j=0; j<numTiles; j++)
      tiles[i][j]=new tile(i,j,0,5);
}
void draw() {
  background(255);
  translate(width * .5,height * .5);
  rotateY((float)mouseX/width * PI);
  rotateX((float)mouseY/height * PI);
  for(int i=0; i<numTiles; i++)
    for(int j=0; j<numTiles; j++)
      tiles[i][j].display();
}
class tile {
  int x,y,z;
  tile(int x, int y, int z,int s) {//s for size
    this.x=x * s;
    this.y=y * s;
    this.z=z * s;
  }
  void display(){
    beginShape(QUADS);
    //XY plane
    //*
    vertex(x,y,z);
    vertex(x+x,y,z);
    vertex(x+x,y+y,z);
    vertex(x,y+y,z);
    //*/
    endShape();
  }
}

由于您只绘制正方形,因此可以使用 rect() 函数。

int numSquares = 51,squareSize = 10;
void setup(){
  size(400,400,P3D);
  smooth();
}
void draw(){
  background(255);
  translate(width * .5, height * .5);
  rotateY((float)mouseX/width * PI);
  for(int j = 0 ; j < numSquares ; j++)
    for(int i = 0 ; i < numSquares ; i++)
      rect(i*squareSize,j*squareSize,squareSize,squareSize);
}

高温高压

于 2011-03-17T11:32:36.027 回答