我刚开始在 libgdx 中玩 3D arround.我已经知道如何绘制基本的Model
,并尝试使用CameraController
来玩arround.现在我想创建一个 FirstPersonCamera
或 FirstPersonCameraController
.我考虑过扩展 PerspectiveCamera
并向其添加 MyMovingObject 目标
.MyMovingObject
将保存一个 x, y, z 位置
,其中 y
是一个常量值,因为我无法将 向上移动/down
目前.所以我的动作基本上是二维的.MyMovingObject
还将存储 左/右旋转
,这是其移动方向
/xSpeed, zSpeed
所需的.但是 Player
也应该能够向上和向下看,而 MyMovingObject
并不真正需要这种向上/向下旋转,因为它只会改变视图而不会改变其他视图特性.所以我不确定我是否走对了路.
我希望能够通过使用 W,A,S,D
向前、向左、向右、向后移动并使用鼠标向左向右旋转.我也想使用鼠标上下查看,就像在大多数 First Person
游戏中一样.
我应该使用另一种方式,而不是通过扩展 PerspectiveCamera
创建自己的相机吗?还是这种方法很好,我只需要将上/下旋转存储在 MyMovingObject
中,如果它仅用于视图?
或者使用 W,A,S,D 和鼠标
控制相机并根据相机位置和旋转更新 MyMovingObject
的位置会更好吗?
我希望你明白我的意思.解释起来似乎有点复杂(至少对我来说).
I just started playing arround with 3D in libgdx. I allready know how to draw basic Model
s 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 MyMovingObject
s 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).
我现在为我的 NPC 和播放器.我通过以下方式计算速度: xSpeed = direction.x/(direction.x + direction.z) * speed;
zSpeed 相同.通过这样做,我过滤"了 y 值,我只得到 x 和 y 的百分比.唯一的问题是,当我直视 x
和 z
是 0
.我可以通过使用 UpVecotr
来解决这个问题,当我执行Pitch-rotation"时它会旋转.但是我如何旋转他?我需要围绕侧向矢量旋转它.谢谢
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 && angle > 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.
感谢分享 这个链接.我发现它非常有用.这是我关于旋转陆基相机的代码,它似乎可以正常工作.
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;
}
这适用于陆基相机.如果要制作飞控相机,则必须围绕cam.direction.crs(cam.up)
进行俯仰旋转.而不是使用 Vector3.cpy()
我会存储一个 Vector3 help
,它会获取这些临时值,因为 Vector3.cpy()
创建一个新的 Vector3
并且这个操作在每个渲染循环中执行.对于飞行控制相机,您还需要添加 roll
旋转并围绕 cam.up
Vector 进行 yaw
旋转.
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 第一人称相机控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!