简体   繁体   English

在Android Libgdx上为PerspectiveCamera创建InputProcessor

[英]Creating InputProcessor for PerspectiveCamera on Android Libgdx

I am trying to create an InputProcessor that will do many things. 我正在尝试创建一个可以执行许多操作的InputProcessor。 I have a touchpad set up that I am trying to move the camera along the x and y axes. 我设置了一个触摸板,试图将相机沿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();
}

The problem I am having with the setKnobAction() is that it moves the camera based on the initial direction it is facing. 我在setKnobAction()上遇到的问题是,它会根据所面对的初始方向来移动相机。 I am wanting it to move in the direction that it is currently facing. 我希望它朝着当前的方向移动。

The problem is, that you move the camera relativ to the worlds axis. 问题是,您将camera相对移动到了世界轴。
If you rotate your camera, the worlds axis stay the same, only the cameras direction vector changes. 如果旋转相机,则世界轴保持不变,只有相机方向向量改变。
Therefor you need to move your camera relativ to its direction vector. 因此,您需要将camera相对移动到其方向向量。

I guess before playing arround with PerspectiveCamera and 3D in general you should read a few tutorials. 我想,在通常情况下,使用PerspectiveCamera和3D玩游戏之前,您应该阅读一些教程。
Here is a tutorial, which describes a FirstPersonCamera . 是一个教程,描述了FirstPersonCamera

Basicly you need to store a normalized direction Vector for the camera: 基本上,您需要为相机存储归一化的方向向量:

Vector3 direction = new Vector3(1, 0, 0); //You are looking in x-Direction

If you rotate, you need to rotate this Vector arround the worlds Y-Axis or the camera s Up-Axis (depends on the situation, explained in the linked tutorial): 如果旋转,则需要围绕世界的Y轴或camera的Up轴旋转此Vector(取决于情况,在链接的教程中进行了说明):

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.  

To go foreward you simply need to add the direction Vector scaled by the speed to the position. 要向前走,您只需要向位置添加按速度缩放的方向矢量。

camera.position.add(direction.scl(speed))

After that you need to reset the direction s length by calling nor() again. 之后,您需要通过再次调用nor()来重置direction的长度。

Hope it helps. 希望能帮助到你。

I have figured it out. 我已经知道了。 All I needed to do for forward and backward motion was to add or subtract the direction vector scaled by 1 to the position vector. 我要做向前和向后运动的所有事情就是将位置矢量缩放比例为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();

To strafe (move to the left or right while the camera is facing forward) I needed to add or subtract the cross-product between the camera's direction vector and the camera's up vector. 为了进行划算(在摄像机面向前方时向左或向右移动),我需要添加或减去摄像机方向向量和摄像机向上向量之间的叉积。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM