了解 libGDX 投影矩阵

时间:2022-12-02
本文介绍了了解 libGDX 投影矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几周里,我一直在尝试学习 libGDX 库.我发现很难理解相机/视口关系系统,尤其是在我第一次尝试游戏开发时.我被告知要使用的一行代码以及 API 提到的代码是:

Over the past few weeks I've been attempting to learn the libGDX library. I'm finding it hard, especially for my first endeavor toward game development, to comprehend the system of Camera/viewport relationships. One line of code that I've been told to use, and the API mentions, is:

    batch.setProjectionMatrix(camera.combined);

尽管进行了 4 个小时的研究,但我仍然对这段代码的功能缺乏完整的了解.据我的基本理解,它告诉"相机正在寻找的批次.我缺乏理解力令人沮丧和愤怒,如果有人能帮助我,我将不胜感激.代码片段的另一个问题是我不确定何时需要实现(在渲染方法、创建方法等中).

Despite a good 4 hours of research, I'm still lacking a complete understanding of the functionality of this code. It is to my basic understanding that it "tells" the batch where the camera is looking. My lack of comprehension is depressing and angering, and I'd appreciate if anyone could assist me. Another issue with the code snippet is that I'm unsure of when it's necessary to implement (in the render method, create method, etc).

推荐答案

考虑用相机拍照.例如.使用您的智能手机相机拍摄公园长凳的照片.当你这样做时,你会在智能手机的屏幕上看到公园里的长椅.这可能看起来很明显,但让我们看看这涉及到什么.

Consider taking a picture with a camera. E.g. using your smartphone camera taking a picture of a bench in the park. When you do that, then you'll see the bench in the park on the screen of your smartphone. This might seem very obvious, but let's look at what this involves.

图片上长凳的位置是相对于您拍照时站立的位置.换句话说,它是相对于相机而言的.在典型的游戏中,您不会相对于对象放置对象.相反,您将它们放置在您的游戏世界中.在您的游戏世界和您的相机之间进行转换是使用矩阵完成的(这只是一种转换坐标的数学方法).例如.当您将相机向右移动时,长凳会在照片上向左移动.这称为视图矩阵.

The location of the bench on the picture is relative to where you were standing when taking the photo. In other words, it is relative to the camera. In a typical game, you don't place object relative to the object. Instead you place them in your game world. Translating between your game world and your camera, is done using a matrix (which is simply a mathematical way to transform coordinates). E.g. when you move the camera to the right, then the bench moves to the left on the photo. This is called the View matrix.

图片上长凳的确切位置还取决于长凳和相机之间的距离.至少,它是 3D 的(2D 非常相似,所以请继续阅读).当它更远时,它更小,当它靠近相机时,它更大.这称为透视投影.您还可以进行正交投影,在这种情况下,对象的大小不会根据与相机的距离而改变.无论哪种方式,公园长凳的位置和大小都会转换为屏幕上的位置和大小(以像素为单位).例如.公园里的长凳是两米宽,而照片上是 380 像素.这称为投影矩阵.

The exact location of the bench on the picture also depends on the distance between bench and the camera. At least, it does in 3D (2D is very similar, so keep reading). When it is further away it is smaller, when it is close to the camera it is bigger. This is called a perspective projection. You could also have an orthographic projection, in which case the size of the object does not change according to the distance to the camera. Either way, the location and size of the bench in the park is translated to the location and size in pixels on the screen. E.g. the bench is two meters wide in the park, while it is 380 pixels on the photo. This is called the projection matrix.

camera.combined 表示组合的视图和投影矩阵.换句话说:它描述了游戏世界中的事物应该在屏幕上渲染的位置.

camera.combined represents the combined view and projection matrix. In other words: it describes where things in your game world should be rendered onto the screen.

调用 batch.setProjectionMatrix(cam.combined); 指示批处理使用该组合矩阵.每当值更改时,您都应该调用它.这通常是在调用 resize 时以及在您移动或以其他方式改变相机时.

Calling batch.setProjectionMatrix(cam.combined); instruct the batch to use that combined matrix. You should call that whenever the value changes. This is typically when resize is called and also whenever you move or otherwise alter the camera.

如果您不确定,那么您可以在 render 方法的开头调用它.

If you are uncertain then you can call that in the start of your render method.

这篇关于了解 libGDX 投影矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:使用相机读取二维码 - Android 下一篇:Android camera , onPictureTaken(byte[] imgData, Camera camera

相关文章

最新文章