問題描述
我已經(jīng)嘗試解決這個(gè)問題兩天了,我已經(jīng)放棄嘗試找到現(xiàn)有的解決方案.
I have been trying to solve this problem for two days and I have given up trying to find an existing solution.
我已經(jīng)開始學(xué)習(xí) libgdx 并完成了一些教程.現(xiàn)在我已經(jīng)嘗試使用我所學(xué)到的一切,創(chuàng)建一個(gè)簡(jiǎn)單的橫向滾動(dòng)游戲.現(xiàn)在,我知道有這方面的 libgdx 示例,但我還沒有找到將 Box2d 與 scene2d 和演員以及平鋪地圖相結(jié)合的示例.
I have started learning libgdx and finished a couple of tutorials. And now I have tried to use all that I have learned and create a simple side scrolling game. Now, I know that there are libgdx examples of this, but I haven't found a one that incorporates Box2d with scene2d and actors as well as tiled maps.
我的主要問題是相機(jī).
您需要一個(gè)用于舞臺(tái)的相機(jī)(據(jù)我所知,它用于 SpriteBatch 的投影矩陣傳遞給演員的 draw() 方法,如果這是錯(cuò)誤的,請(qǐng)糾正我)并且您需要一個(gè)相機(jī)用于調(diào)用 render() 方法的 TileMapRender.此外,在某些教程中,GameScreen 中有一個(gè) OrthographicCamera,可在需要時(shí)使用.
You need a camera for the Stage (which as far as I know is used for the projection matrix of the SpriteBatch passed to the method draw() at actors, if this is wrong please correct me) and you need a camera for the TileMapRender for calling the render() method. Also, in some of the tutorials there is a OrthographicCamera in the GameScreen, which is used where needed.
我嘗試將 OrthographicCamera 對(duì)象傳遞給方法,我嘗試在任何地方使用舞臺(tái)中的相機(jī)和 TileMapRenderer 中的相機(jī).前任.
I have tried to pass a OrthographicCamera object to methods, I have tried to use the camera from the Stage and the camera from the TileMapRenderer everywhere. Ex.
OrthographicCamera ocam = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
stage.setCamera(ocam); // In the other cases i replace ocam with stage.getCamera() or the one i use for the tileMap Render
tileMapRenderer.render(ocam);
stage.getSpriteBatch().setProjectionMatrix(ocam.combined); // I am not sure if this is needed
我也嘗試過在各處使用不同的相機(jī).
I have also tried to use different cameras everywhere.
在嘗試了所有這些之后,我沒有注意到確切的時(shí)間發(fā)生了什么,但我會(huì)列出發(fā)生了什么:
After trying all of this I haven't noted what happens exactly when but I will list what happens :
- 屏幕上什么都沒有(可能相機(jī)遠(yuǎn)離繪制的東西)
- 我可以從 debugRenderer 中看到平鋪地圖和輪廓(我也使用 debugRender,但我不認(rèn)為它會(huì)干擾相機(jī)),但演員的精靈不可見(可能在屏幕外)李>
- 我可以看到我應(yīng)該看到的所有內(nèi)容,但是當(dāng)我嘗試移動(dòng)應(yīng)該跟隨他的 Actor 和相機(jī)時(shí),精靈的移動(dòng)速度比身體快(綠色調(diào)試方塊).
所以我的主要問題是:
- 我不明白當(dāng)您擁有多個(gè)攝像頭時(shí)會(huì)發(fā)生什么.通過"您在屏幕上實(shí)際看到的是哪一個(gè)?
- 我應(yīng)該使用多臺(tái)相機(jī)嗎?如何使用?
另外,我認(rèn)為我應(yīng)該提到我正在使用 OpenGL ES 2.0.
Also, I thought that I should mention that I am using OpenGL ES 2.0.
很抱歉這個(gè)問題太長(zhǎng)了,但我想我應(yīng)該詳細(xì)描述一下,因?yàn)檫@對(duì)我來(lái)說有點(diǎn)復(fù)雜.
I am sorry for the long question, but I thought that I should describe in detail, since it's a bit complicated for me.
推薦答案
你實(shí)際上同時(shí)看透了所有這些.雖然他們可能會(huì)看到一個(gè)完全不同的世界,但他們都將他們的觀點(diǎn)呈現(xiàn)在屏幕上.您可以使用多臺(tái)相機(jī),也可以只使用一臺(tái).如果您只使用一個(gè),則需要確保在繪制 TiledMap、使用 Actors 的舞臺(tái)以及可能用于可選的 Box2DDebugRenderer 之間正確更新投影矩陣.
You actually see through all of them at the same time. They might look at a completely different world though, but all of them render their point of view to the screen. You can use several cameras, or just one. If you use only one you need to make sure that you update the projection matrix correctly, between drawing the TiledMap, your Stage with Actors and maybe for the optional Box2DDebugRenderer.
我會(huì)為 Box2DDebugRenderer 使用額外的攝像頭,因?yàn)槟院罂梢暂p松地將其丟棄.我假設(shè)您使用轉(zhuǎn)換因子將米轉(zhuǎn)換為像素,反之亦然.1:1的比例不會(huì)很好.我總是使用介于 1m=16px 和 1m=128px 之間的東西.
I'd use an extra Camera for the Box2DDebugRenderer, because you can easily throw it away later. I assume you use a conversion factor to convert meters to pixels and the other way around. Having a 1:1 ratio wouldnt be very good. I always used something between 1m=16px and 1m=128px.
所以你以這種方式初始化它,并將它用于調(diào)試渲染器:
So you initialize it this way, and use that one for your debugging renderer:
OrthographicCamera physicsDebugCam = new OrthographicCamera(Gdx.graphics.getWidth() / Constants.PIXEL_PER_METER, Gdx.graphics.getHeight() / Constants.PIXEL_PER_METER);
對(duì)于您的 TiledMapRenderer,您也可以使用額外的攝像頭,但該攝像頭僅適用于屏幕坐標(biāo),因此無(wú)需轉(zhuǎn)換:
For your TiledMapRenderer you may use an extra camera as well, but that one will work in screen-coordinates only, so no conversion:
OrthographicCamera tiledMapCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
TiledMap 將始終在 (0, 0) 處呈現(xiàn).所以你需要使用相機(jī)在地圖上四處走動(dòng).它可能會(huì)跟隨一個(gè)主體,因此您可以通過以下方式對(duì)其進(jìn)行更新:
The TiledMap will always be rendered at (0, 0). So you need to use the camera to move around on the map. It will probably follow a body, so you can update it via:
tiledMapCam.position.set(body.getPosition().x * Constants.PIXELS_PER_METER, body.getPosition().y * Constants.PIXELS_PER_METER)
或者如果它跟隨一個(gè)演員:
Or in case it follows an Actor:
tiledMapCam.position.set(actor.getX(), actor.getY())
我實(shí)際上還沒有將scene2d 與Box2D 一起使用,因?yàn)槲也恍枰c我的游戲?qū)ο筮M(jìn)行太多交互.您需要在這里實(shí)現(xiàn)一個(gè)自定義的 PhysicsActor,它擴(kuò)展了 Actor 并通過將 body 作為屬性來(lái)構(gòu)建從 scene2d 到 Box2D 的橋梁.它必須在每個(gè)更新步驟中基于 Body 設(shè)置 Actor 的位置、旋轉(zhuǎn)等.但在這里你有幾個(gè)選擇.您可以重復(fù)使用 tiledMapCam 并在屏幕坐標(biāo)中工作.在這種情況下,您需要始終記住在更新 actor 時(shí)與 Constants.PIXELS_PER_METER 相乘.或者您將使用具有相同視口的另一個(gè)攝像頭,例如physicsDebugCam.在這種情況下,不需要轉(zhuǎn)換,但我不確定這是否會(huì)干擾某些特定于場(chǎng)景的東西.
I actually haven't used scene2d together with Box2D yet, because I didn't need to interact very much with my game objects. You need to implement a custom PhysicsActor here, which extends Actor and builds the bridge from scene2d to Box2D by having a body as a property. It will have to set the Actors position, rotation etc based on the Body at every update-step. But here you have several options. You may re-use the tiledMapCam and work in screen-coordinates. In this case you need to always remember to multiply with Constants.PIXELS_PER_METER when you update your actor. Or you will use another cam with the same viewport like the physicsDebugCam. In this case no conversion is needed, but I'm not sure if this might interfere with some scene2d-specific things.
對(duì)于 ParallaxBackground,您也可以使用另一個(gè)攝像頭,對(duì)于 UI,您可以再次使用另一個(gè)舞臺(tái)和另一個(gè)攝像頭...或者通過正確重置它們來(lái)重復(fù)使用其他攝像頭.這是您的選擇,但我認(rèn)為幾個(gè)相機(jī)不會(huì)對(duì)性能產(chǎn)生太大影響.更少的重置和轉(zhuǎn)換甚至可能會(huì)改善它.
For a ParallaxBackground you may use another camera as well, for UI you can use another stage and another camera again... or reuse others by resetting them correctly. It's your choice but I think several cameras do not influence performance much. Less resetting and conversions might even improve it.
設(shè)置完所有內(nèi)容后,您只需渲染所有內(nèi)容,使用正確的相機(jī)并將每個(gè)層"/視圖"渲染在彼此之上.首先是 ParallaxBackground,然后是 Tiledmap,然后是 Entity-Stage,然后是 Box2DDebugging 視圖,然后是 UI-stage.
After everything is setup, you just need to render everything, using the correct cameras and render every "layer"/"view" on top of each other. First a ParallaxBackground, then your Tiledmap, then your Entity-Stage, then your Box2DDebugging view, then your UI-stage.
一般記得在更改相機(jī)的任何內(nèi)容后調(diào)用 spriteBatch.setProjectionMatrix(cam.combined);
并使用 cam.update()
.
In general remember to call spriteBatch.setProjectionMatrix(cam.combined);
and using cam.update()
after you changed anything of your camera.
這篇關(guān)于libgdx 中的多個(gè)攝像頭(在其他框架中可能類似)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!