【问题标题】:Creating InputProcessor for PerspectiveCamera on Android Libgdx在 Android Libgdx 上为 PerspectiveCamera 创建 InputProcessor
【发布时间】:2014-10-24 02:59:58
【问题描述】:

我正在尝试创建一个可以做很多事情的 InputProcessor。我设置了一个触摸板,我试图沿 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();
}

我在使用 setKnobAction() 时遇到的问题是它会根据它所面对的初始方向移动相机。我希望它朝着当前所面对的方向移动。

【问题讨论】:

    标签: java android input libgdx


    【解决方案1】:

    问题是,您将camera 相对移动到世界轴。
    如果你旋转你的相机,世界轴保持不变,只有相机的方向向量会改变。
    因此,您需要将您的 camera 相对移动到其方向向量。

    我想在玩PerspectiveCamera 和一般的 3D arround 之前,您应该阅读一些教程。
    Here 是一个教程,它描述了 FirstPersonCamera

    基本上你需要为相机存储一个归一化的方向向量:

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

    如果你旋转,你需要围绕世界 Y 轴或 cameras 上轴旋转这个向量(视情况而定,在链接教程中解释):

    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() 来重置directions 的长度。

    希望对你有帮助。

    【讨论】:

    • 当我这样做时没有任何反应。
    • 你打电话给camera.update()了吗?这只是伪代码给你一些提示,实际代码取决于你。如果你不知道如何实现它,我猜你还没有准备好 3D...
    【解决方案2】:

    我想通了。对于向前和向后运动,我需要做的就是将按 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 已经有了一个direction 向量。这样就容易多了。
    猜你喜欢
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多