我正在尝试在LibGDX中使用3D,但是相机倒转了,我似乎没有采取任何措施来阻止它,没有已知的原因。
我已经分别测试了每个旋转轴,并且效果很好,但是当它们一起使用时,有时会倒转。
public void create () {
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -2.5f, -5, -10));
modelBatch = new ModelBatch();
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(0, 0, 0);
cam.lookAt(-10,-10,-10);
cam.near = 1f;
cam.far = 300f;
cam.update();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5, 5, 5,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
instance.transform.translate(-10, -10, -10);
Gdx.input.setCursorCatched(true);
Gdx.input.setCursorPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
}
@Override
public void render () {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
isup = false;
ispressed = false;
Vector3 direction = cam.direction.cpy();
Vector3 crossProduct = new Vector3(); //CROSSPRODUCT OF PLAYER DIRECTION AGAINST Y AXIS
crossProduct.x = -direction.z;
crossProduct.y = 0;
crossProduct.z = direction.x;
differencey = -Gdx.input.getY() + (Gdx.graphics.getHeight() / 2);
if ((cam.direction.y > -0.8 || differencey > 0) && (cam.direction.y < 0.8 || differencey < 0)) {
if (differencey > 50) {
differencey = 50;
}
cam.rotate(crossProduct, differencey / 10);
cam.update();
}
differencex = Gdx.input.getX() - (Gdx.graphics.getWidth() / 2);
cam.rotate(new Vector3(0, -1, 0), differencex / 10);
cam.update();
modelBatch.begin(cam);
modelBatch.render(instance, environment);
modelBatch.end();
Gdx.input.setCursorPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
Gdx.app.exit();
}
应该限制摄像头的移动,以使如果用户分别直视向上或向下,则摄像头不能进一步向上或向下移动,但是无论如何,摄像头有时还是会越过此限制。