mousePressed在Processing中创建3D立方体

时间:2014-12-12 06:05:43

标签: 3d rotation processing

我对Processing有点新,我试图制作一个mousePressed事件,在屏幕上创建旋转立方体。下一阶段将是在它们之间创建碰撞事件,但是现在我还有另一个问题:它可以工作,但只有在按下鼠标的情况下,我希望它们能够持续,即使未按下鼠标也是如此。

这里是代码,我尝试了一种解决方法,循环无限,并且它创造了所有这些旋转立方体的奇怪故障,只是旋转直到进入0,0 ... 实际上,空白抽奖是空的,因为一切都会被删除。

int pointmousex;
int pointmousey;
float a=0;
float r=0;
float co=100;
float Cubox[]= {
};
float Cuboy[]= {
};
float Cuboz[]= {
};
boolean bgON=true;

void setup() {
  size(800, 640, P3D);
  colorMode(HSB);
  background(0);
  smooth();
}

void draw_box(float x, float y, float box_size, float rot) {
  float z=box_size;
  translate(x, y, z);
  rotateX(rot);
  rotateY(rot);
  fill(map(co, 0, 255, 100, 255));
  box(box_size, box_size, box_size);
}

void draw() {
  if (bgON==true) {
    fill(255, 230, 200);
    rect(0, 0, width, height);
  }

  r=r+0.1;
}

void mousePressed() {
  int pointmousex = mouseX;
  int pointmousey = mouseY;
  lights();
  draw_box(pointmousex, pointmousey, 100, r*0.2);
}

void keyPressed() {
  if (key=='b' || key=='B') {
    if (bgON==true) {
      bgON = false;
    } else {
      bgON = true;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

如果你想用盒子做更复杂的东西我建议你创建类:

class Box{
  float x,y,z, box_size, rot;

  //create instance of box with specific position and starting rotation
  Box(float x, float y, float box_size, float rot){
    this.z = box_size;
    this.x = x;
    this.y = y;
    this.box_size = box_size;
    this.rot = rot;
  }

  void draw_box(){
    pushMatrix();
    translate(x, y, z);
    //adding R will rotate all boxes
    rotateX(r + rot);
    rotateY(r + rot);
    fill(map(co, 0, 255, 100, 255));
    box(box_size);
    popMatrix();
  }

  //here you can add more complex methods like colision ...
}

然后你必须创建用于存储这些框的全局数组

final static ArrayList<Box> boxes = new ArrayList();

在鼠标内部活动中,只需创建新框并将其添加到boxes

void mousePressed() {
  boxes.add( new Box(mouseX, mouseY, 100, r*0.5));
}

您需要做的最后一件事是每次调用draw()时重绘场景(每个框):

void draw() {
  background(255,230,200);
  for(Box box: boxes){
    box.draw_box();
  }
  r=r+0.01;
}