問題描述
我已經嘗試解決這個問題兩天了,我已經放棄嘗試找到現有的解決方案.
I have been trying to solve this problem for two days and I have given up trying to find an existing solution.
我已經開始學習 libgdx 并完成了一些教程.現在我已經嘗試使用我所學到的一切,創建一個簡單的橫向滾動游戲.現在,我知道有這方面的 libgdx 示例,但我還沒有找到將 Box2d 與 scene2d 和演員以及平鋪地圖相結合的示例.
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.
我的主要問題是相機.
您需要一個用于舞臺的相機(據我所知,它用于 SpriteBatch 的投影矩陣傳遞給演員的 draw() 方法,如果這是錯誤的,請糾正我)并且您需要一個相機用于調用 render() 方法的 TileMapRender.此外,在某些教程中,GameScreen 中有一個 OrthographicCamera,可在需要時使用.
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 對象傳遞給方法,我嘗試在任何地方使用舞臺中的相機和 TileMapRenderer 中的相機.前任.
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
我也嘗試過在各處使用不同的相機.
I have also tried to use different cameras everywhere.
在嘗試了所有這些之后,我沒有注意到確切的時間發生了什么,但我會列出發生了什么:
After trying all of this I haven't noted what happens exactly when but I will list what happens :
- 屏幕上什么都沒有(可能相機遠離繪制的東西)
- 我可以從 debugRenderer 中看到平鋪地圖和輪廓(我也使用 debugRender,但我不認為它會干擾相機),但演員的精靈不可見(可能在屏幕外)李>
- 我可以看到我應該看到的所有內容,但是當我嘗試移動應該跟隨他的 Actor 和相機時,精靈的移動速度比身體快(綠色調試方塊).
所以我的主要問題是:
- 我不明白當您擁有多個攝像頭時會發生什么.通過"您在屏幕上實際看到的是哪一個?
- 我應該使用多臺相機嗎?如何使用?
另外,我認為我應該提到我正在使用 OpenGL ES 2.0.
Also, I thought that I should mention that I am using OpenGL ES 2.0.
很抱歉這個問題太長了,但我想我應該詳細描述一下,因為這對我來說有點復雜.
I am sorry for the long question, but I thought that I should describe in detail, since it's a bit complicated for me.
推薦答案
你實際上同時看透了所有這些.雖然他們可能會看到一個完全不同的世界,但他們都將他們的觀點呈現在屏幕上.您可以使用多臺相機,也可以只使用一臺.如果您只使用一個,則需要確保在繪制 TiledMap、使用 Actors 的舞臺以及可能用于可選的 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.
我會為 Box2DDebugRenderer 使用額外的攝像頭,因為您以后可以輕松地將其丟棄.我假設您使用轉換因子將米轉換為像素,反之亦然.1:1的比例不會很好.我總是使用介于 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.
所以你以這種方式初始化它,并將它用于調試渲染器:
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);
對于您的 TiledMapRenderer,您也可以使用額外的攝像頭,但該攝像頭僅適用于屏幕坐標,因此無需轉換:
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) 處呈現.所以你需要使用相機在地圖上四處走動.它可能會跟隨一個主體,因此您可以通過以下方式對其進行更新:
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)
或者如果它跟隨一個演員:
Or in case it follows an Actor:
tiledMapCam.position.set(actor.getX(), actor.getY())
我實際上還沒有將scene2d 與Box2D 一起使用,因為我不需要與我的游戲對象進行太多交互.您需要在這里實現一個自定義的 PhysicsActor,它擴展了 Actor 并通過將 body 作為屬性來構建從 scene2d 到 Box2D 的橋梁.它必須在每個更新步驟中基于 Body 設置 Actor 的位置、旋轉等.但在這里你有幾個選擇.您可以重復使用 tiledMapCam 并在屏幕坐標中工作.在這種情況下,您需要始終記住在更新 actor 時與 Constants.PIXELS_PER_METER 相乘.或者您將使用具有相同視口的另一個攝像頭,例如physicsDebugCam.在這種情況下,不需要轉換,但我不確定這是否會干擾某些特定于場景的東西.
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.
對于 ParallaxBackground,您也可以使用另一個攝像頭,對于 UI,您可以再次使用另一個舞臺和另一個攝像頭...或者通過正確重置它們來重復使用其他攝像頭.這是您的選擇,但我認為幾個相機不會對性能產生太大影響.更少的重置和轉換甚至可能會改善它.
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.
設置完所有內容后,您只需渲染所有內容,使用正確的相機并將每個層"/視圖"渲染在彼此之上.首先是 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.
一般記得在更改相機的任何內容后調用 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.
這篇關于libgdx 中的多個攝像頭(在其他框架中可能類似)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!