(处理中)如何在移动时旋转播放器并有跟随相机?
(Processing) How to rotate player while moving and have a following camera?
我试图让播放器旋转,但我就是想不通。我想我没有做过数学,哈哈。我开门见山了。
我一直在努力让我的播放器面向鼠标。如果我不移动播放器,则旋转没问题,但当我开始移动时,播放器会绕圈移动。
在我的 class Player
中,我有这些数据类型:
PApplet parent; //So the program knows which PApplet to draw on, I guess.
private float x,y;
/*
* For the Camera to follow the player
*/
private float cameraX;
private float cameraY;
我有 cameraFollow
方法:
public void cameraFollow() {
cameraX = x * -1 + parent.width / 2; // Calculates so the camera
cameraY = y * -1 + parent.height / 2; // follows the player
parent.translate(cameraX, cameraY); // Translate the window position
}
现在解决我尝试旋转面向鼠标的播放器的问题。如果我不移动播放器,旋转很好,但是当我移动时,当我移动鼠标时,播放器开始绕圈移动。
public void render() {
//Rotate towards mouse
float angle = PApplet.atan2(cameraY-parent.mouseY, cameraX-parent.mouseX);
parent.pushMatrix();
parent.translate(x,y);
parent.rotate(angle-PApplet.HALF_PI);
//Body
parent.noStroke(); // no black lines around the ellipse
parent.fill(playerColor.getRGB()); // Sets the color of the player
parent.ellipseMode(PConstants.CENTER); // Makes the ellipse centered
parent.ellipse(x, y, 20, 20); // creates an ellipse
//Arms
parent.stroke(0.5f);
parent.fill(armsColor.getRGB());
parent.ellipseMode(PConstants.CENTER);
parent.ellipse(x-5, y-5, 5, 5);
parent.ellipse(x+5, y-5, 5, 5);
parent.popMatrix();
}
我更改了代码,而不是:
float angle = PApplet.atan2(y-parent.mouseY, x-parent.mouseX);
//I did
float angle = PApplet.atan2(parent.mouseY-y, parent.mouseX-x);
我发现我在完全错误的位置进行了旋转,所以我现在在翻译之后进行旋转,而不是在翻译之前进行旋转。
在我将椭圆分配给 x 和 y 坐标之前,这就是椭圆在圆圈中移动的原因。但现在它实际上是原点所在。
public void render()
{
//Rotate towards mouse
float angle = PApplet.atan2(parent.mouseY-y, parent.mouseX-x);
parent.pushMatrix();
parent.translate(x,y);
parent.rotate(angle+PApplet.HALF_PI);
//Body
parent.noStroke(); // no black lines around the ellipse
parent.fill(playerColor.getRGB()); // Sets the color of the player
parent.ellipseMode(PConstants.CENTER); // Makes the ellipse centered
parent.ellipse(0, 0, 20, 20); // creates an ellipse
//Arms
parent.stroke(1);
parent.fill(armsColor.getRGB());
parent.ellipseMode(PConstants.CENTER);
parent.ellipse(0-5, 0-5, 5, 5);
parent.ellipse(0+5, 0-5, 5, 5);
parent.popMatrix();
}
编辑:
但现在每当我走出 window 位置边界时,玩家的视线就会从鼠标移开。嗯....
编辑:
我通过添加解决了它:
float angle = PApplet.atan2(parent.mouseY-cameraY-y, parent.mouseX-cameraX-x);
给代码!
再次感谢 Spektre!
我试图让播放器旋转,但我就是想不通。我想我没有做过数学,哈哈。我开门见山了。
我一直在努力让我的播放器面向鼠标。如果我不移动播放器,则旋转没问题,但当我开始移动时,播放器会绕圈移动。
在我的 class Player
中,我有这些数据类型:
PApplet parent; //So the program knows which PApplet to draw on, I guess.
private float x,y;
/*
* For the Camera to follow the player
*/
private float cameraX;
private float cameraY;
我有 cameraFollow
方法:
public void cameraFollow() {
cameraX = x * -1 + parent.width / 2; // Calculates so the camera
cameraY = y * -1 + parent.height / 2; // follows the player
parent.translate(cameraX, cameraY); // Translate the window position
}
现在解决我尝试旋转面向鼠标的播放器的问题。如果我不移动播放器,旋转很好,但是当我移动时,当我移动鼠标时,播放器开始绕圈移动。
public void render() {
//Rotate towards mouse
float angle = PApplet.atan2(cameraY-parent.mouseY, cameraX-parent.mouseX);
parent.pushMatrix();
parent.translate(x,y);
parent.rotate(angle-PApplet.HALF_PI);
//Body
parent.noStroke(); // no black lines around the ellipse
parent.fill(playerColor.getRGB()); // Sets the color of the player
parent.ellipseMode(PConstants.CENTER); // Makes the ellipse centered
parent.ellipse(x, y, 20, 20); // creates an ellipse
//Arms
parent.stroke(0.5f);
parent.fill(armsColor.getRGB());
parent.ellipseMode(PConstants.CENTER);
parent.ellipse(x-5, y-5, 5, 5);
parent.ellipse(x+5, y-5, 5, 5);
parent.popMatrix();
}
我更改了代码,而不是:
float angle = PApplet.atan2(y-parent.mouseY, x-parent.mouseX);
//I did
float angle = PApplet.atan2(parent.mouseY-y, parent.mouseX-x);
我发现我在完全错误的位置进行了旋转,所以我现在在翻译之后进行旋转,而不是在翻译之前进行旋转。
在我将椭圆分配给 x 和 y 坐标之前,这就是椭圆在圆圈中移动的原因。但现在它实际上是原点所在。
public void render()
{
//Rotate towards mouse
float angle = PApplet.atan2(parent.mouseY-y, parent.mouseX-x);
parent.pushMatrix();
parent.translate(x,y);
parent.rotate(angle+PApplet.HALF_PI);
//Body
parent.noStroke(); // no black lines around the ellipse
parent.fill(playerColor.getRGB()); // Sets the color of the player
parent.ellipseMode(PConstants.CENTER); // Makes the ellipse centered
parent.ellipse(0, 0, 20, 20); // creates an ellipse
//Arms
parent.stroke(1);
parent.fill(armsColor.getRGB());
parent.ellipseMode(PConstants.CENTER);
parent.ellipse(0-5, 0-5, 5, 5);
parent.ellipse(0+5, 0-5, 5, 5);
parent.popMatrix();
}
编辑: 但现在每当我走出 window 位置边界时,玩家的视线就会从鼠标移开。嗯....
编辑: 我通过添加解决了它:
float angle = PApplet.atan2(parent.mouseY-cameraY-y, parent.mouseX-cameraX-x);
给代码!
再次感谢 Spektre!