如何在 LibGDX 中左右移动相机
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 度 right/left 然后以这种方式平移它,但这样做是一样的。我在想,也许相机在翻译时无法像其他东西一样工作 sideways/backwards。
首先,在您的 class 中,您必须有一个 Animations 对象,我们称它为 Anim。在您的 class 实例中,您必须创建一个 OrthographicCamera
对象,这将是您的相机实例。你必须给它一个值,例如:
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
这个camera
坐标可以在第一次使用的时候设置,可以用方法.translate()
:
camera.translate(camera.viewportWidth / 2, camera.viewportHeight);
否则positio将设置为0;0。
在您的 render()
方法中,您必须使用 camera
对象中名为 update()
的方法,如下所示:
@Override
public void render(float delta) {
anim.load();
camera.update();
....
game();
....
}
在您game/app开发期间,此方法始终运行。所以每次方法运行时,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
都会更新为新位置。
要存档 2 个向量之间的相机移动,请使用相机 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
[...]
}
我有一个使用 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 度 right/left 然后以这种方式平移它,但这样做是一样的。我在想,也许相机在翻译时无法像其他东西一样工作 sideways/backwards。
首先,在您的 class 中,您必须有一个 Animations 对象,我们称它为 Anim。在您的 class 实例中,您必须创建一个 OrthographicCamera
对象,这将是您的相机实例。你必须给它一个值,例如:
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
这个camera
坐标可以在第一次使用的时候设置,可以用方法.translate()
:
camera.translate(camera.viewportWidth / 2, camera.viewportHeight);
否则positio将设置为0;0。
在您的 render()
方法中,您必须使用 camera
对象中名为 update()
的方法,如下所示:
@Override
public void render(float delta) {
anim.load();
camera.update();
....
game();
....
}
在您game/app开发期间,此方法始终运行。所以每次方法运行时,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
都会更新为新位置。
要存档 2 个向量之间的相机移动,请使用相机 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
[...]
}