我在Procesisng中编写了一个程序,它使用随机颜色和旋转的不透明立方体呈现在彼此之上,但我希望在程序运行时单独连续旋转每个立方体.这是我目前的代码,
int boxval = 1;
void setup(){
size (640, 320, P3D);
frameRate(60);
}
void draw(){
for (int i = 0; i < boxval; i++){
translate(random(0,640), random(0,320), 0);
rotateY(random(0,360));
rotateX(random(0,360));
rotateZ(random(0,360));
fill(random(0,255),random(0,255),random(0,255),50);
noStroke();
box(64,64,64);
}
}
Run Code Online (Sandbox Code Playgroud)
这是截图,如果它有帮助,

这是使用面向对象编程的好时机!如果我正确理解了这个问题,您希望每个立方体独立于其他立方体旋转.让我们做一个Cube课程.将每个多维数据集视为我们将单独处理的对象.
class Cube {
float x, y, z; // position of the cube
float size; // size of cube
color c; // color of cube
float xAngle, yAngle, zAngle; // current rotation amount of cube's x, y, z axes
float xSpeed, ySpeed, zSpeed; // how quickly the cube is rotated in the x, y, z axes
// Cube constructor - create the cube and all of its parameters
Cube(float x_, float y_, float z_, float size_, color c_, float xSpeed_, float ySpeed_, float zSpeed_) {
x = x_;
y = y_;
z = z_;
size = size_;
c = c_;
xSpeed = xSpeed_;
ySpeed = ySpeed_;
zSpeed = zSpeed_;
xAngle = yAngle = zAngle = 0; // starting position
}
// update the cube
// all we're doing is rotating each axis
void update() {
xAngle += xSpeed;
yAngle += ySpeed;
zAngle += zSpeed;
}
// draw the cube to the screen
void display() {
pushMatrix(); // need this
translate(x, y, z); // position on screen
rotateX(xAngle); // rotation amounts
rotateY(yAngle);
rotateZ(zAngle);
fill(c);
noStroke();
box(size);
popMatrix(); // and this
// push and pop matrix allows for individual cube rotation
// otherwise you would rotate the whole draw window, which isn't what you're looking for
}
}
Run Code Online (Sandbox Code Playgroud)
如果您希望每个立方体在屏幕上更改颜色和位置但仍然独立旋转,则该display()功能可能是这样的:
void display() {
pushMatrix();
translate(random(0, width), random(0, height), random(-100, 100)); // random position on screen
rotateX(xAngle);
rotateY(yAngle);
rotateZ(zAngle);
fill(random(255), random(255), random(255), 50); // random color
noStroke();
box(size);
popMatrix();
}
Run Code Online (Sandbox Code Playgroud)
理解Processing中元素的旋转和转换是非常关键的.如果您还没有阅读,我强烈推荐Processing网站上的本教程.我在Cube类中加入了一些概念.
由于您希望在屏幕上绘制多个多维数据集,让我们创建一个多维数据集数组.我选择25为任意数字.
Cube[] cube = new Cube[25];
现在setup(),我们需要实际创建每个Cube并给它一些参数,比如屏幕上的位置,颜色等.以下是如何实现的.
for (int i = 0; i < cube.length; i++) {
cube[i] = new Cube(random(0, width), random(0, height), 0, // x, y, z position
random(30, 80), color(random(255), random(255), random(255), 50), // size, color
random(0.001, 0.020), random(0.001, 0.020), random(0.001, 0.020)); // xSpeed, ySpeed, zSpeed
}
Run Code Online (Sandbox Code Playgroud)
现在我们只需要将多维数据集绘制到屏幕并更新每个旋转,这只是在draw()循环中发生.
for (int i = 0; i < cube.length; i++) {
cube[i].update();
cube[i].display()
}
Run Code Online (Sandbox Code Playgroud)
这是整个计划.background()每次调用draw()循环都很重要,因此每帧都会清除显示窗口.注释看看会发生什么,但我注意到这不在您上面提供的代码段中.我想这可能是一种效果!
Cube[] cube = new Cube[25];
void setup() {
size(640, 320, P3D);
smooth();
frameRate(60);
for (int i = 0; i < cube.length; i++) {
cube[i] = new Cube(random(0, width), random(0, height), 0,
random(30, 80), color(random(255), random(255), random(255), 50),
random(0.001, 0.020), random(0.001, 0.020), random(0.001, 0.020));
}
}
void draw() {
camera();
lights();
background(50);
for (int i = 0; i < cube.length; i++) {
cube[i].update();
cube[i].display();
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定你的编程背景是什么,但是对于面向对象编程来说,处理(和其他OOP语言)非常有用,所以如果你需要一个速成课程,我建议你从Processing网站上购买这个OOP教程.当OOP终于有意义时,我的编程生活发生了变化.