Libgdx第一人称摄像头控制 [英] Libgdx first person camera controll

查看:253
本文介绍了Libgdx第一人称摄像头控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在libgdx中使用3D来玩arround。我已经知道如何绘制基本的模型 s,我试着用 CameraController 玩arround。现在我想创建一个 FirstPersonCamera FirstPersonCameraController 。我想到了扩展 PerspectiveCamera 并添加一个 MyMovingObject目标 MyMovingObject 将保存 x,y,z位置,其中 y 是一个常数值,因为我现在不能移动上/下。所以我的运动基本上在2D。 MyMovingObject 也会存储左/右旋转,它的 code> / xSpeed,zSpeed 。但是 Player 也应该能够向上和向下查找,并且 MyMovingObject ,因为它只改变视图,没有其他属性。所以我不知道我是否正确的方式。

我想通过使用向前,向左,向右,向后W,A,S,D 并使用鼠标左右旋转。另外,我想使用鼠标来查找,比如在大多数第一人游戏中。

我应该使用另一种方式,而不是创建我自己相机通过扩展 PerspectiveCamera
或者是这种方法好,我只需要在 MyMovingObject 中存储上/下旋转,如果只需要视图? >
或者最好用 W,A,S,D和鼠标来控制摄像机,并更新 MyMovingObject s位置,取决于相机的位置和旋转?

我希望你能理解我的意思。

I just started playing arround with 3D in libgdx. I allready know how to draw basic Models and i tryed to play arround with the CameraController. Now i want to create a FirstPersonCamera or FirstPersonCameraController. I thought about extending PerspectiveCamera and adding a MyMovingObject target to it. The MyMovingObject would hold a x, y, z position, where y is a constant value, cause i can't move up/down at the moment. So my movement is basicly in 2D. The MyMovingObject would also store the left/right rotation, needed for its moving direction/ xSpeed, zSpeed. But the Player should also be able to look up and down, and this up/down rotation is not really needed for the MyMovingObject, as it only changes the view and no other properties. So i am not sure if i go the right way.
I want to be able to go forward, left, right, backward by using W,A,S,D and rotate left right by using the mouse. Also i want to look up and down by using the mouse, like in most First Person games.
Should i use another way, not creating my own camera by extending PerspectiveCamera? Or is this approach good and i just have to store the up/down rotation in the MyMovingObject to, also if it is only needed for the view?
Or would it be better to controll the camera with W,A,S,D and mouse and update the MyMovingObjects position, depending on cameras position and rotation?
I hope you understand what I mean. It seems a bit complicated to explain it (at least for me).

编辑:我现在使用 Vector3我的NPC和播放器的方向 Vector3位置 Vector3 size 我通过执行以下操作计算速度: xSpeed = direction.x /(direction.x + direction.z)* speed; 同样适用于zSpeed。通过这样做,我过滤y值出它,我只得到x和y的百分比。唯一的问题是,当我直视 x z 都是 0 。我可以通过使用 UpVecotr 修复这个问题,当我做一个俯仰旋转时,旋转。但我怎么旋转他?我需要旋转它在侧向矢量。感谢

I am now using Vector3 direction, Vector3 position and Vector3 size for my NPCs and the player. I calculate the speed by doing: xSpeed = direction.x / (direction.x + direction.z) * speed; the same for zSpeed. By doing this i "filter" the y value out of it and i get only the percent of x and y. The only problem is, that when i look straight up x and z are 0. I could fix this by using an UpVecotr, which gets rotated when i do a "Pitch-rotation". But how do i rotate him? I need to rotate it arround the sideway Vector. Thanks

编辑:旋转和运动现在(请参阅我的回答),但我有真正大的问题, -回转。我使用: if(direction.y <0.9&&∠ 1)doPitchRotation(); else if(direction.y> -0.9&&< angle< 1)doPitchRotation(); 所以如果我向下旋转,我仍然看起来至少在-0.9 y,执行旋转。但真正发生的事情:我旋转到-0.9然后它旋转围绕Y轴,在另一边它旋转,即使我移动我的鼠标下来。你能解释一下为什么吗?为什么当我向下转动时,Y轴翻转?

The rotation and movement work now (see my answer), but i have really big problems with the limitation of the "Pitch-rotation". I am using: if (direction.y < 0.9 && angle > 1) doPitchRotation(); else if (direction.y > -0.9 && angle < 1) doPitchRotation(); so if i rotate down and i still look down at least at -0.9 y it just does not perform the rotation. But what really happens: I rotates to - 0.9 then it rotates arround the Y-Axis and at the other side it rotates up, even if i move my mous down. Can you explain why? Why does the Y-Axis flip when i turn arround by looking down?

编辑:现在可以工作了。有时我的upVector似乎有些错误的值。对于陆基凸轮,您还可以使用Y轴和方向矢量的交叉积。不需要upVector。

It works now. It seems like my upVector got some wrong values sometimes. For landbased cams you can also use crossproduct of Y-Axis and direction Vector. No need for upVector.

推荐答案

感谢分享 this 链接。我发现它非常有用。
这是我的旋转一个陆基相机的代码,它似乎工作没有问题。

Hey thanks for sharing this link. I found it very useful. Here's my code on rotating a land based camera and it seems to work without problems.

private int mouseX = 0;
private int mouseY = 0;
private float rotSpeed = 0.2f;

@Override
public boolean mouseMoved(int screenX, int screenY) {
    int magX = Math.abs(mouseX - screenX);
    int magY = Math.abs(mouseY - screenY);

    if (mouseX > screenX) {
        cam.rotate(Vector3.Y, 1 * magX * rotSpeed);
        cam.update();
    }

    if (mouseX < screenX) {
        cam.rotate(Vector3.Y, -1 * magX * rotSpeed);
        cam.update();
    }

    if (mouseY < screenY) {
        if (cam.direction.y > -0.965)
            cam.rotate(cam.direction.cpy().crs(Vector3.Y), -1 * magY * rotSpeed);
        cam.update();
    }

    if (mouseY > screenY) {

        if (cam.direction.y < 0.965)
            cam.rotate(cam.direction.cpy().crs(Vector3.Y), 1 * magY * rotSpeed);
        cam.update();
    }

    mouseX = screenX;
    mouseY = screenY;

    return false;
}

这适用于陆基相机。如果你想制作一个flightcontroll摄像机,你必须做一个俯仰旋转,围绕 cam.direction.crs(cam.up)。而不是使用 Vector3.cpy() i将存储一个 Vector3帮助,它获得这些临时值, code> Vector3.cpy()创建一个新 Vector3 ,并且每个渲染循环执行此操作。
对于flightcontroll摄像机,您还需要添加 roll 旋转,并执行 yaw 旋转c $ c> cam.up 向量。

This works for landbased cameras. If you want to make a flightcontroll camera, you have to do a pitch rotation arround the cam.direction.crs(cam.up). Instead of using the Vector3.cpy() i would store a Vector3 help, which gets those temporary values, because Vector3.cpy() creates a new Vector3 and this operation is performed every render loop. For flightcontroll cameras you also need to add a roll rotation and do the yaw rotation arround the cam.up Vector.

这篇关于Libgdx第一人称摄像头控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆