在处理中塑造对象,翻译各个形状

发布于 2024-09-03 19:15:17 字数 648 浏览 5 评论 0原文

我在处理对象以及一般对象的translate()函数时遇到困难。我浏览了这些示例,并尝试复制它们实例化对象的方式,但似乎甚至无法让形状出现在屏幕上,更无法移动它们。我使用嵌套 for 循环将对象实例化到数组中,并期望渲染对象的网格。然而,什么也没有呈现。

我的嵌套 for 循环结构用于实例化图块:

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();
   }
}

以及图块类的构造函数:

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();
}

运行时根本不会渲染任何内容。此外,如果有任何问题,我的翻译(移动)是通过我为名为 move 的图块类编写的方法完成的,该方法仅调用翻译。这是正确的方法吗?应该如何处理这个问题?我似乎根本不明白如何渲染/创建/翻译单个对象/形状。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两个我 2024-09-10 19:15:17

如果您使用 beginShape() 作为您只需指定要绘制的直接坐标即可。如果您依赖翻译结果将对象放入可见位置,这可能就是您没有任何结果的原因。

另外,根据你如何看待场景,你可能有 z 朝向相机,所以你的对象是在你从侧面看它们时绘制的,并且由于它们是 2d 对象,你不会看到任何东西,尝试使用 x/y 或 y/z 而不是您现在正在执行的 x/z。

Transformations (such as translate, rotate, etc) do not work if you use beginShape() as you're simply specifying direct coordinates to draw to. If you're relying on the result of a translate to put an object into a visible location that could be why you're not having any results.

Also, depending on how you're looking at your scene, you probably have z coming towards the camera, so your objects are being drawn with you looking at them on the side, and since they are 2d objects you won't see anything, try using x/y or y/z instead of x/z which you are doing right now.

只是一片海 2024-09-10 19:15:17

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

上面的示例的问题在于,您将绘图()代码放在构造函数中,而您应该将其放在显示函数中。

所以:

公共无效显示(处理过程){
proc.beginShape()
ETC。
display

() 还需要在draw() 循环中调用,因此初始化一次图块,然后在draw() 中显示它们。

You can definitely use pushMatrix() and translate() with beginShape() and such, it may be not completely what you expect, but it will definitely move the things around from the default origin.

What is going wrong with your above example is that you are putting the drawing() code in the constructor where you should be putting it in the display function.

so:

public void display(Processing proc) {
proc.beginShape()
etc.
}

display() also needs to be called in the draw() loop, so initialize your tiles once and then display them in draw().

哑剧 2024-09-10 19:15:17

您应该遵循 @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);
}

华泰

You should follow @Tyler's advice on drawing in a 2D plane(x/y, y/z, x/z).

Your shapes probably do not render because you might be drawing them once, and clearing the screen in the draw() method, but I'm not sure as I can't see the rest of your code.

Here's what I mean:

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();
  }
}

Since you're only drawing squares, you could use the rect() function.

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);
}

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文