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

查看:101
本文介绍了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);

要移动我的对象,请使用:

To move my object I use:

// 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类已经有一个转换方法,因此您无需手动将其乘以另一个矩阵即可转换该对象.

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天全站免登陆