Java 在Android Libgdx上为PerspectiveCamera创建输入处理器

Java 在Android Libgdx上为PerspectiveCamera创建输入处理器,java,android,input,libgdx,Java,Android,Input,Libgdx,我正在尝试创建一个可以做很多事情的输入处理器。我设置了一个触摸板,试图沿x轴和y轴移动相机 tBounds = new Sprite(Assets.touchpadBounds); tBounds.setSize(tBounds.getWidth() * scale, tBounds.getHeight() * scale); tKnob = new Sprite(Assets.touchpad); tKnob.setSize(tKnob.getWidth() * scale, tKnob.ge

我正在尝试创建一个可以做很多事情的输入处理器。我设置了一个触摸板,试图沿x轴和y轴移动相机

tBounds = new Sprite(Assets.touchpadBounds);
tBounds.setSize(tBounds.getWidth() * scale, tBounds.getHeight() * scale);
tKnob = new Sprite(Assets.touchpad);
tKnob.setSize(tKnob.getWidth() * scale, tKnob.getHeight() * scale);
touchpad = new Skin();
touchpad.add("boundary", tBounds);
touchpad.add("circle", tKnob);
touchpadStyle = new TouchpadStyle();
bounds = touchpad.getDrawable("boundary");
knob = touchpad.getDrawable("circle");
touchpadStyle.background = bounds;
touchpadStyle.knob = knob;
pad = new Touchpad(10, touchpadStyle);
stage.addActor(pad);
//Setting Bounds
pad.setBounds(width / 14, height / 10, tBounds.getHeight(),
tBounds.getWidth());

private void setKnobAction() {
    camera.position.set(camera.position.x + (pad.getKnobPercentX() * 0.1f),
            camera.position.y + 0,
            camera.position.z - (pad.getKnobPercentY() * 0.1f));
    camera.update();
}

我在设置上遇到的问题是,它根据相机所面对的初始方向移动相机。我希望它朝着它当前所面对的方向移动。

问题是,相对于世界坐标轴移动相机。 如果旋转摄影机,则世界轴保持不变,只有摄影机方向向量发生变化。 因此,您需要相对于其方向向量移动相机

我想在使用透视照相机和3D之前,你应该先阅读一些教程。 这是一个教程,描述了FirstPersonCamera

基本上,您需要为相机存储规格化方向向量:

Vector3 direction = new Vector3(1, 0, 0); //You are looking in x-Direction
如果旋转,则需要围绕世界Y轴或摄影机向上轴旋转此矢量,具体取决于具体情况,如链接教程中所述:

direction.rotate(Vector3.Y, degrees).nor(); // Not sure if "nor()" is needed, it limits the length to 1, as it only gives the direction, not the speed.  
要前进,只需将速度缩放的方向向量添加到位置

camera.position.add(direction.scl(speed))
之后,您需要再次调用nor来重置方向长度


希望有帮助。

我已经弄明白了。对于向前和向后运动,我所需要做的就是将缩放为1的方向向量与位置向量相加或相减

// To move forward
camera.position.add(camera.direction.scl(1f));
camera.update();

// To move backwards
camera.position.sub(camera.direction.scl(1f));
camera.update();

// To move to the right
Vector3 right = camera.direction.cpy().crs(Vector3.Y).nor();
camera.position.add(right.scl(1));
camera.update();

// To move to the left
Vector3 right = camera.direction.cpy().crs(Vector3.Y).nor();
camera.position.sub(right.scl(1));
camera.update();

若要在相机朝前时向左或向右扫射,我需要加上或减去相机方向向量和相机上向量之间的叉积。

当我这样做时,什么都不会发生。您是否调用camera.update?这只是给您一些提示的伪代码,实际代码由您决定。如果你不知道如何实现它,我想你还没有准备好3D…啊,好的,相机已经准备好了方向向量。这就容易多了。