[英]How to move a camera left and right in LibGDX
我有一個我用WASD控制的相機,但是我堅持左右移動它。 我一直在尋找互聯網,它說要找到一個垂直於另一個的向量,你可以改變x和y的輪次,並將其中一個乘以-1。 我在下面的代碼中試過這個:
void camstrafe (String dir) {
Vector3 direction = camera.direction.nor();
Vector3 old = direction;
direction.set(-old.z, 0, old.x);
camera.translate(direction.scl(0.18f));
}
我已經向前移動工作正常,並且實際上將相機轉過來,但由於某種原因這不起作用,說實話我不確定它是什么,因為當我按a
或d
(他們稱之為此功能)相機變得瘋狂,開始很快轉向,有時前進或者像一百萬英里的側面。 無論如何,有誰知道我怎么能做到這一點? 順便說一句,我也嘗試了相機的前進方向並使用.rotate()
函數向右/向左旋轉90度,然后將其翻譯成相同的方式。 我想也許相機不能像其他東西那樣側向/反向翻轉。
要在兩個矢量之間存檔相機移動,請使用相機lerp:
public class myclass {
[...]
private OrthographicCamera camera;
public Vector3 posCameraDesired;
[...]
private void processCameraMovement(){
/// make some camera movement
posCameraDesired.x+=100.0f * Gdx.graphics.getDeltaTime();
posCameraDesired.y+=100.0f * Gdx.graphics.getDeltaTime();
}
[...]
//render method
public void draw(){
[...]
processCameraMovement();
camera.position.lerp(posCameraDesired,0.1f);//vector of the camera desired position and smoothness of the movement
[...]
}
首先,在你的課堂上,你必須有一個Animations對象,我們稱之為Anim。 在您的類instanciation中,您必須創建一個OrthographicCamera
對象,它將是您的相機實例。 你必須給它一個值,例如:
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
可以在第一次使用時設置此camera
協調,您可以使用方法.translate()
:
camera.translate(camera.viewportWidth / 2, camera.viewportHeight);
否則,positio將設置為0; 0。
在render()
方法中,您必須使用名為update()
的camera
對象中的方法,如下所示:
@Override
public void render(float delta) {
anim.load();
camera.update();
....
game();
....
}
在您開發的游戲/應用程序中,此方法始終運行。 因此,每次運行該方法時, camera
都會更新,其位置也會更新。
然后,在你game()
方法,或以其他方法(取決於你的架構),在那里你正在處理用戶的輸入,移動相機的位置,並修改camera.position
在里面。 像轟鳴聲:
public void game() {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { // or A
moveBack(anim); // The method to move back
camera.position.x -= 3; // if conditions are ok, move the camera back.
} else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { or D
moveForward(anim); // The method to move forward
camera.position.x += 3; // if conditions are ok, move the camera to the front.
}
當用戶移動時,調用移動方法,並修改攝像機位置。 每次調用方法render()
,都會使用新位置更新camera
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.