如何在3D Libgdx中旋转透视相机

时间:2019-12-30 12:05:16

标签: java android libgdx perspectivecamera

我正在使用libgdx创建3d游戏,但是我有问题,我想在触摸屏上旋转摄像头,所以我从libgdx Wiki上添加了它

@Override
public boolean touchDragged (int screenX, int screenY, int pointer) {
    float deltaX = -Gdx.input.getDeltaX() * degreesPerPixel;
    float deltaY = -Gdx.input.getDeltaY() * degreesPerPixel;

    camera.rotate(Vector3.Y,deltaX*0.9f);
    camera.rotate(Vector3.X,360-deltaY*0.9f);
    camera.update();


    return true;
}

工作正常,但当我触摸屏幕旋转并向后看时,相机会像这样旋转

enter image description here

enter image description here

土地和世界必须旋转而不是摄像机,我之前使用过开放的Es,然后在那里旋转了世界

Gl.Glrotatef(0.0.0)

但是在libgdx中,我无法使其正常旋转。

我的代码有什么问题?

    @Override
        public void create () {

            camera = new PerspectiveCamera(75,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
            camera.position.set(0f, 5f, 0f);
            camera.lookAt(0f,0f,0f);
            camera.near =0.1f;
            camera.far = 300f;
            modelInstance=new Array<ModelInstance>();
            texture = new Texture(Gdx.files.internal("gtass4.jpg"),false);

            modelBatch = new ModelBatch();
            modelBuilder = new ModelBuilder();
            modelBuilder.begin();

            modelBuilder.node().id="Block";
            modelBuilder.part("Block",GL30.GL_TRIANGLES,VertexAttributes.Usage.Position|VertexAttributes.Usage.TextureCoordinates,
                              new Material(TextureAttribute.createDiffuse(texture)))
                .box(1000,5,1000);

            box=modelBuilder.end();

            modelInstance.add(block=new ModelInstance(box,"Block"));



            texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
            texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

            Matrix3 mat = new Matrix3();

            mat.scl(new Vector2(80.0f, 80.0f));
            box.meshes.get(0).transformUV(mat);


            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, -1f, -0.8f, -0.2f));

            camController = new CameraInputController(camera);
            Gdx.input.setInputProcessor(this);

        }

        Vector3 position = new Vector3();
        private void movement() {
            block.transform.getTranslation(position);
            position.x = x;
            position.z = z;
            position.y = y;
            block.transform.setTranslation(position);

        }


        @Override
        public void render () {
            Gdx.gl.glClearColor(.0f, .0f, .0f, 1);
            Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT|GL30.GL_DEPTH_BUFFER_BIT);

            movement();
            k=Gdx.graphics.getFramesPerSecond();
            modelBatch.begin(camera);
            modelBatch.render(modelInstance,environment);

            modelBatch.end();
            camera.update();

        }

        @Override
        public boolean keyDown(int keycode) {

            if(keycode == Input.Keys.LEFT)
                camera.rotateAround(new Vector3(0f, 0f, 0f), new Vector3(1f, 1f, 0f), 1f);
            if(keycode == Input.Keys.RIGHT)
                camera.rotateAround(new Vector3(0f,0f,0f),new Vector3(0f,1f,0f), -1f);
            return true;
        }

        @Override
        public boolean keyUp(int keycode) {
            return false;
        }

        @Override
        public boolean keyTyped(char character) {
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {



            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            return false;
        }


   @Override
    public boolean touchDragged (int screenX, int screenY, int pointer) {
        float deltaX = -Gdx.input.getDeltaX() * degreesPerPixel;
        float deltaY = -Gdx.input.getDeltaY() * degreesPerPixel;

        camera.rotate(Vector3.Y,deltaX*0.9f);
        camera.rotate(Vector3.X,360-deltaY*0.9f);
        camera.update();


        return true;
    }

0 个答案:

没有答案