LibGDX 3D 相机实现 [英] LibGDX 3D camera implementation

查看:34
本文介绍了LibGDX 3D 相机实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 3D 编程非常陌生,我正在尝试使用 LibGDX 实现一个将跟随我的模型的相机.我在相机实现方面遇到了问题,我非常感谢一些建议.正如我所说,我是新手(尤其是数学),下面的代码几乎肯定不会那么好:

I'm very new to 3D programming and I'm attempting to implement a camera that will follow my model using LibGDX. I'm having issues with the camera implementation and I would greatly appreciate some advice. As I said, I'm new to this (especially the maths) and the code below will almost certainly not be that great:

要同时旋转我的对象和相机,我使用以下代码,其中 inst 作为模型实例:

To rotate my object and camera at the same time I use the following code with inst being the model instance:

// right
inst.transform.rotate(new Vector3(0,1,0), -1);
Common.cam.rotate(Vector3.Y, -1f);
// left
inst.transform.rotate(new Vector3(0,1,0), 1);
Common.cam.rotate(Vector3.Y, 1f);

要移动我使用的对象:

// forward
Common.inst.transform.mul(new Matrix4(new Vector3(0,0,((float)-0.2)),
                new Quaternion(0,0,0,0), new Vector3(1,1,1)));
// back
Common.inst.transform.mul(new Matrix4(new Vector3(0,0,((float)0.2)),
                new Quaternion(0,0,0,0), new Vector3(1,1,1)));

我的相机目前设置如下:

My camera is currently setup as follows:

Vector3 pos = inst.transform.getTranslation(new Vector3(0,0,0));
pos.z += 5;
cam.position.set(pos);

这段代码工作正常,但问题是相机停留在固定位置,而实际上它应该调整自己以直接在模型面对的新方向后面.我可能没有清楚地表达过这一点,所以我在视觉上表示了这一点:

This code is working fine, but the problem is that the camera is staying in a fixed position when really it should be adjusting itself to be directly behind the new direction the model is facing. I've probably not articulated this clearly so I've represented this visually:

^ 模型(黑色)和相机(红色)无旋转

^ Model (black) and camera (red) with no rotation

^ 轮换时当前发生的事情

^ What is currently happening upon rotation

^ 需要做什么

如果我的数学更强大,我想这不会太难实现,但我完全不知道如何实现这种行为.如果有人能指出我正确的方向,那就太好了.

I'd imagine this wouldn't be too difficult to implement if my maths was stronger, but I'm at a complete loss as how to implement this behavior. If someone could point me in the right direction that would be great.

非常感谢!

推荐答案

问题是您在世界空间中平移相机,而不是旋转后的本地空间.相机的位置和旋转存储在两个 Vector3 中,而不是一个矩阵中.

The problem is that you're translating the camera in world space, not the local space after it's rotated. A camera's position and rotation are stored in two Vector3's, not a Matrix.

在应用之前,您需要旋转相机的平移向量.

You need to rotate your camera's translation vector before applying it.

顺便说一下,如果你每帧都实例化很多这样的对象,你会遇到 GC 卡顿问题.此外,Matrix4 类已经有一个 translate 方法,因此您无需手动将其乘以另一个矩阵来平移对象.

By the way, you're going to run into GC stuttering issues if you instantiate a lot of Objects like that every frame. Also, there's already a translate method for the Matrix4 class, so you don't need to be manually multiplying it by another matrix to translate the object.

我会修改如下:

//rotate player
float playerRotation = left ? 1 : -1; //TODO (should involve delta time...)
inst.transform.rotate(Vector3.Y, playerRotation);

//translate player
float playerZtranslation = forward ? 0.2f : -0.2f; //TODO (should involve delta time...)
inst.translate(0, 0, playerZtranslation );

//move camera to position of player.
inst.transform.getTranslation(camera.position);

//rotate camera to face same direction as player
inst.transform.getRotation(mTempRotation); //mTempRotation is a member Quaternion variable to avoid unnecessary instantiations every frame.
camera.direction.set(0,0,-1).rotate(mTempRotation);//reset and rotate to match current player angle

//rotate translation vector to match angle of player
mTempTranslation.set(0,0,5).rotate(mTempRotation); //mTempTranslation is a member Vector3 variable to avoid unnecessary instantiations every frame.

//and apply it.
cam.position.add(mTempTranslation);

编辑(见评论):也许试试这个:

Edit (see comments): Maybe try this:

//rotate and transform player variables
mPlayerYAngle += left ? 1 : -1; //TODO (should involve delta time...)
mPlayerPosition.add(0, 0, forward ? 0.2f : -0.2f); //TODO (should involve delta time...)

//apply changes by resetting transform to identity and applying variables
inst.transform.idt().translate(mPlayerPosition).rotate(Vector3.Y, mPlayerYAngle );

//move camera to position of player.
camera.position.set(mPlayerPosition);

//rotate camera to face same direction as player
camera.direction.set(0,0,-1).rotate(mPlayerYAngle ,0,1,0);//reset and rotate to match current player angle

//rotate translation vector to match angle of player
mTempTranslation.set(0,0,5).rotate(mPlayerYAngle ,0,1,0);

//and apply it.
cam.position.add(mTempTranslation);

这篇关于LibGDX 3D 相机实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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