rotate 命令将 rect 平移到另一个位置处理

rotate command translates rect to another position processing

我在 processing.Here 中将矩形旋转了 35 度是我的代码。

pushMatrix();
rotate(35);
stroke(74, 73, 74);
fill(156, 153, 153);
rect(cannon1PositionX,cannon1PositionY,25,60,50);
noStroke();
fill(173, 168, 173);
rect(cannon1PositionX + 3,cannon1PositionY + 4,20,50,50);
popMatrix();

但是如果我将旋转角度更改为 40,则矩形会旋转并平移到另一个位置。如何解决这个问题?

你的问题是,当你第一次使用 rotate() 时,你是在参考整个 canvas 旋转,所以当你使用 canon1PositionXcannon1PositionY 时,取决于您当前面对的角度 canvas 位置发生变化。

您需要做的是:

pushMatrix();
// Go to the cannon position
translate(cannon1PositionX, cannon1PositionY);
// Rotate to the right angle
rotate(35);
// Draw the cannon at the current position and with the right angle
rect(0, 0, 25, 60, 50);
// Draw the second cannon next to the current one
rect(3, 4, 20, 50, 50);
popMatrix();