处理:立方体在移动时旋转停留在一个地方 [英] Processing: Cube rotating stay in one place while moving
问题描述
在处理程序中,我必须使一个立方体移动到立方体移动的位置,当按小写 x 为 x 轴逆时针时,或按大写 X 为 x 轴顺时针,按小写 y 为 y 逆时针轴,或按大写 Y 表示 y 轴的顺时针方向,按小写字母 z 表示 z 轴的逆时针方向,按大写字母 Z 表示顺时针方向表示 z 轴.问题是立方体移动了,但它确实旋转了.我如何旋转,但不移动?请帮助检查此代码.该代码有效,但它移动到位.此外,它会在第一次按下按钮时旋转.我该如何解决?
In the Processing program, I have to make a cube to move in place where the cube moves when pressing lowercase x for counterclockwise of x axis, or pressing capital X for clockwise of x axis, and pressing lowercase y for counterclockwise of y axis, or pressing capital Y for clockwise of y axis, and pressing lowercase z for counterclockwise of z axis, and pressing capital Z for for clockwise for z axis. The problem is that the cube moves out of place, but it does rotate. How do I rotate, but not move out of place? Please help check this code. The code works however it moves out of place. Also, it spins on the first time the button is pressed. How do I fix that?
float theta = 0;
void setup() {
size(600, 600, P3D);
}
void draw() {
background(255);
fill(127, 127);
String s1 = "Press x for counterclockwise of x axis, X for clockwise of x axis";
String s2 = "Press y for counterclockwise of y axis, Y for clockwise of y axis ";
String s3 = "Press z for counterclockwise of z axis, Z for for clockwise for z axis";
text(s1, 0, width/2 + 100);
text(s2, 0, width/2 + 125);
text(s3, 0, width/2 + 150);
// if(pressLowX() == true)
// cubeBox(.5, .5, .5);
pressButtons();
pushMatrix();
translate(width/2, height/2);
cubeBox(.5, .5, .5);
popMatrix();
}
void cubeBox(float x, float y, float z) {
translate(x, y, z);
rotate(theta);
beginShape(QUADS);
fill(255, 0, 0);
vertex(100, 100, 100);
vertex(-100, 100, 100);
vertex(-100, -100, 100);
vertex(100, -100, 100);
fill(255, 255, 0);
vertex(-100, -100, -100);
vertex(100, -100, -100);
vertex(100, 100, -100);
vertex(-100, 100, -100);
fill(0, 255, 0);
vertex(100, 100, 100);
vertex(100, -100, 100);
vertex(100, -100, -100);
vertex(100, 100, -100);
fill(0, 255, 255);
vertex(-100, -100, 100);
vertex(-100, -100, -100);
vertex(-100, 100, -100);
vertex(-100, 100, 100);
fill(0, 0, 255);
vertex(-100, -100, 100);
vertex(-100, -100, -100);
vertex(100, -100, -100);
vertex(100, -100, 100);
fill(255, 0, 255);
vertex(100, 100, 100);
vertex(-100, 100, 100);
vertex(-100, 100, -100);
vertex(100, 100, -100);
// rotate(-theta);
// translate(-x, -y, -z);
endShape(CLOSE);
}
void pressButtons() {
if (key == 'x') {
theta = theta - .05;
rotateX(radians(0));
} else if (key == 'X') {
theta = theta + .05;
rotateX(radians(0));
} else if (key == 'y') {
theta = theta - .05;
rotateY(radians(90));
} else if (key == 'Y') {
theta = theta + .05;
rotateY(radians(90));
} else if (key == 'z') {
theta = theta - .05;
rotateZ(radians(60));
} else if (key == 'Z') {
theta = theta + .05;
rotateZ(radians(60));
}
}
推荐答案
要在原地旋转几何体,必须在平移之前将旋转应用于几何体:
To rotate a geometry in place, the rotation has to be applied to the geometry before the translation:
P' = translation * rotation * P
这意味着您必须按以下顺序执行说明:
This means you have to do the the instructions in this order:
例如
translate(x, y, z);
rotateX(theta);
问题是theta
的旋转是在translate
之前完成的,在函数pressButtons
中.
The issue is that the rotation by the angle theta
is done before translate
, in the function pressButtons
.
要解决您的问题,请创建一个全局变量,该变量会注意到最后按下的键并更改函数 pressButtons
To solve your issue, create a global variable which notice the last pressed key and change the state of the variable in the function pressButtons
char actKey = 0;
void pressButtons() {
if (key == 'x' || key == 'X' || key == 'y' || key == 'Y' || key == 'z' || key == 'Z')
actKey= key;
}
根据actKey
void addRotation() {
if (actKey == 'x') {
theta = theta - .05;
rotateX(theta);
} else if (actKey == 'X') {
theta = theta + .05;
rotateX(theta);
} else if (actKey == 'y') {
theta = theta - .05;
rotateY(theta);
} else if (actKey == 'Y') {
theta = theta + .05;
rotateY(theta);
} else if (actKey == 'z') {
theta = theta - .05;
rotateZ(theta);
} else if (actKey == 'Z') {
theta = theta + .05;
rotateZ(theta);
}
}
在cubeBox
的开头执行平移和旋转:
Perform the translation followed by the rotation at the begin of cubeBox
:
void cubeBox(float x, float y, float z) {
translate(x, y, z);
addRotation();
// [...]
}
这篇关于处理:立方体在移动时旋转停留在一个地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!