在libgdx多个摄像机(在其他框架可能是相似的) [英] Multiple cameras in libgdx ( probably similar in other frameworks )

查看:104
本文介绍了在libgdx多个摄像机(在其他框架可能是相似的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图解决这个问题了两天,我已经放弃试图找到一个现有的解决方案。

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.

我的主要问题是与摄像头。

My main problem is with the cameras.

您需要一个摄像头为舞台(其中据我所知用于SpriteBatch的投影矩阵传递给方法的平局()的演员,如果这是错误的请指正),你需要一个摄像头在TileMa prender调用的render()方法。此外,在一些的教程有在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对象的方法,我试图使用从舞台相机和摄像机从TileMa prenderer无处不在。 例

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太多,但我不认为它与相机干扰),但演员的精灵是不可见的(可能是关闭屏幕)
  • 在我所看到的一切,我应该,但是当我尝试移动演员和摄像机,这是应该跟着他,精灵去比身体(绿色调试广场)更快。

所以我的主要问题是:

  • 我不明白,当你有多个摄像头会发生什么。 通过哪一个你实际上在montior看?
  • 我应该使用多台摄像机,以及如何?

另外,我想我应该提到,我使用的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类中,与演员,也许为可选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的比例不会是很不错的。我总是用1米= 16像素和1米= 128像素。

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);

只有

有关您的TiledMa prenderer你可以使用一个额外的摄像头为好,但是一会工夫在屏幕坐标,所以没有转换:

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,它扩展了演员和具有主体作为属性构建从scene2d到Box2D的桥梁。这将设置参与者立场的基础上,车身的每一个更新步旋转等。但在这里,你有几种选择。你可以重复使用tiledMapCam和屏幕坐标工作。在这种情况下,你需要时刻记得Constants.PIXELS_PER_METER繁殖,当你更新你的演员。或者你会用另一种凸轮与同视象physicsDebugCam。在这种情况下,没有转换是必要的,但我不知道这是否会与一些scene2d特有的东西干扰。

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您可以使用其他相机,以及,对用户界面,您可以使用一个新的阶段,另一个摄像头了...或者正确地重置他们的重用等。这是你的选择,但我想用多台摄像机没有多大影响性能。少复位和转换甚至可能改善。

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类,那么你的实体阶段,那么你的Box2DDebugging看法,那么你的UI阶段。

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多个摄像机(在其他框架可能是相似的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆