問題描述
在過去的幾周里,我一直在嘗試學(xué)習(xí) libGDX 庫.我發(fā)現(xiàn)很難理解相機(jī)/視口關(guān)系系統(tǒng),尤其是在我第一次嘗試游戲開發(fā)時(shí).我被告知要使用的一行代碼以及 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);
盡管進(jìn)行了 4 個(gè)小時(shí)的研究,但我仍然對這段代碼的功能缺乏完整的了解.據(jù)我的基本理解,它告訴"相機(jī)正在尋找的批次.我缺乏理解力令人沮喪和憤怒,如果有人能幫助我,我將不勝感激.代碼片段的另一個(gè)問題是我不確定何時(shí)需要實(shí)現(xiàn)(在渲染方法、創(chuàng)建方法等中).
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).
推薦答案
考慮用相機(jī)拍照.例如.使用您的智能手機(jī)相機(jī)拍攝公園長凳的照片.當(dāng)你這樣做時(shí),你會在智能手機(jī)的屏幕上看到公園里的長椅.這可能看起來很明顯,但讓我們看看這涉及到什么.
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.
圖片上長凳的位置是相對于您拍照時(shí)站立的位置.換句話說,它是相對于相機(jī)而言的.在典型的游戲中,您不會相對于對象放置對象.相反,您將它們放置在您的游戲世界中.在您的游戲世界和您的相機(jī)之間進(jìn)行轉(zhuǎn)換是使用矩陣完成的(這只是一種轉(zhuǎn)換坐標(biāo)的數(shù)學(xué)方法).例如.當(dāng)您將相機(jī)向右移動(dòng)時(shí),長凳會在照片上向左移動(dòng).這稱為視圖矩陣.
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.
圖片上長凳的確切位置還取決于長凳和相機(jī)之間的距離.至少,它是 3D 的(2D 非常相似,所以請繼續(xù)閱讀).當(dāng)它更遠(yuǎn)時(shí),它更小,當(dāng)它靠近相機(jī)時(shí),它更大.這稱為透視投影.您還可以進(jìn)行正交投影,在這種情況下,對象的大小不會根據(jù)與相機(jī)的距離而改變.無論哪種方式,公園長凳的位置和大小都會轉(zhuǎn)換為屏幕上的位置和大小(以像素為單位).例如.公園里的長凳是兩米寬,而照片上是 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
表示組合的視圖和投影矩陣.換句話說:它描述了游戲世界中的事物應(yīng)該在屏幕上渲染的位置.
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.
調(diào)用 batch.setProjectionMatrix(cam.combined);
指示批處理使用該組合矩陣.每當(dāng)值更改時(shí),您都應(yīng)該調(diào)用它.這通常是在調(diào)用 resize
時(shí)以及在您移動(dòng)或以其他方式改變相機(jī)時(shí).
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
方法的開頭調(diào)用它.
If you are uncertain then you can call that in the start of your render
method.
這篇關(guān)于了解 libGDX 投影矩陣的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!