[英](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!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.