所以我正在尝试在处理中模拟机械臂,并使其大部分工作。我遇到的问题是最后一个臂段(不包括夹钳)需要沿其自身的相对 y 轴旋转。我当前的实现在平移后在 y 轴上旋转,如果臂段位于 x 和 z 为 0 的位置,则该解决方案有效。
问题是,如果手臂旋转离开 {0, y, 0},y 旋转似乎是相对于线段的底部应用的?我不太清楚为什么会发生这种情况。
臂段位置计算:
public float[] getArmPiecePos(ArmPiece parent, ArmPiece child){
float x_child = sin(child.total_rotation[2]) * (child.size[1] / 2) + sin(parent.total_rotation[2]) * (parent.size[1] / 2);
float y_child = - cos(child.total_rotation[2]) * (child.size[1] / 2) - cos(parent.total_rotation[2]) * (parent.size[1] / 2);
float z_child = sin(child.total_rotation[1]) * (x_child);
x_child *= cos(child.total_rotation[1]);
float x = x_child + parent.x;
float y = y_child + parent.y;
float z = -z_child + parent.z;
float[] final_pos = {x, y, z};
return final_pos;
臂段平移+旋转:
pushMatrix();
float[] piece_pos = mf.getArmPiecePos(this.parent, this);
this.x = piece_pos[0];
this.y = piece_pos[1];
this.z = piece_pos[2];
translate(this.x, this.y, this.z);
rotateX(this.total_rotation[0]);
rotateY(this.total_rotation[1]);
rotateZ(this.total_rotation[2]);
fill(100);
box(this.size[0], this.size[1], this.size[2]);
popMatrix();
默认手臂位置:
预期效果:
实际效果:
以下源代码演示了 3D 盒子沿 Y 轴的旋转,希望能帮助解决您的问题。
void setup() {
size(600, 600, P3D);
surface.setTitle("Rotate upper segments with horizontal mouse movements.");
}
void draw() {
background(209);
translate(width/2, height/2 + 100);
box(50, 150, 30);
pushMatrix();
translate(0, -125);
rotateY(map(mouseX, 0, width, -PI, PI));
box(50, 100, 30);
translate(0, -75);
box(100, 50, 30);
popMatrix();
}