简体   繁体   English

(Libgdx和Rube Box2d)合并世界

[英](Libgdx and Rube Box2d) merge worlds

Well well well, I use the RubeLoader to load my rube files on libgdx. 好吧,好吧,我使用RubeLoader将我的rube文件加载到libgdx上。

However I noticed that I can only load ONE json file 但是我注意到我只能加载一个json文件

Code : 代码:

loader = new RubeSceneLoader();
scene = loader.loadScene(Gdx.files.internal("test.json"));
World mWorld ;
mWorld = scene.getWorld();

As you can see, If I want to load another json file, it will destroy the previous one because of this line : 如您所见,如果我要加载另一个json文件,由于此行,它将破坏前一个json文件:

mWorld = scene.getWorld();

So my problem is : how can I load multiple json file or how can I merge differents world into one ? 所以我的问题是: 如何加载多个json文件或如何将differents world合并为一个?

Thanks you in advance for your answers 预先感谢您的回答

I checked the sourcecode of RubeLoader and libGDX and cannot see why it should break the first world. 我检查了RubeLoader和libGDX的源代码,看不到为什么它应该打破第一世界。 I checked it myself after that (not very extensive, but I think I covered your usecase) and it worked fine for me. 在那之后,我自己检查了一下(不是很广泛,但是我认为我涵盖了您的用例),它对我来说很好用。

Multiple Worlds are supported by Box2D, LibGDX and the RubeLoader. Box2D,LibGDX和RubeLoader支持多个世界。 But of course you need multiple instances of the World. 但是,当然,您需要世界的多个实例。

loader1 = new RubeSceneLoader();
scene1 = loader1.loadScene(Gdx.files.internal("XXXX.json"));
World mWorld1 = scene1.getWorld();

loader2 = new RubeSceneLoader();
scene2 = loader2.loadScene(Gdx.files.internal("ABCD.json"));
World mWorld2 = scene2.getWorld();

Now you have two Worlds and they should both work fine, but merging them wouldn't be easy at this point. 现在您有了两个Worlds ,它们两个都应该工作正常,但是此时合并起来并不容易。 Because you would have to recreate everything from mWorld2 in mWorld1 or the other way around. 因为您必须在mWorld1或其他方面从mWorld2重新创建所有内容。 I'd suggest you to programmatically merge the two JSON files (libGDX has already the necessary JSON tools, but you might use other small json libraries like Jackson ) and then load this merged scene. 我建议您以编程方式合并两个JSON文件(libGDX已经具有必需的JSON工具,但是您可以使用其他小型json库,例如Jackson ),然后加载此合并的场景。 That should be a lot easier than merging two Worlds . 这比合并两个Worlds要容易得多。

Edit: If you need to merge the Worlds not right from the start, but after a while, the easiest would be to modify the RubeWorldSerializer . 编辑:如果您需要从一开始就合并Worlds ,但是过一会儿,最简单的方法就是修改RubeWorldSerializer Specifically this part: 特别是这部分:

World world = new World(gravity, allowSleep);
world.setAutoClearForces(autoClearForces);
world.setContinuousPhysics(continuousPhysics);
world.setWarmStarting(warmStarting);

Try to find a way to input your already existing mWorld1 here instead and when loading the second scene, all Bodies and Joints should automatically be added to this World , instead of a completely new one. 尝试找到一种在此处输入现有mWorld1的方法,并在加载第二个场景时,应将所有BodiesJoints自动添加到此World ,而不是一个全新的世界。

Edit2: A quick idea how it could be done: Edit2:一个简单的想法如何做到的:

Add this to the RubeWorldSerializer : public static World mergeWorld; 将其添加到RubeWorldSerializerpublic static World mergeWorld; .

Change the World initialization like this: 像这样更改World初始化:

World world;
if (RubeWorldSerializer.mergeWorld == null) {
    world = new World(gravity, allowSleep);
    world.setAutoClearForces(autoClearForces);
    world.setContinuousPhysics(continuousPhysics);
    world.setWarmStarting(warmStarting);
} else {
    world = RubeWorldSerializer.mergeWorld;
}

Now Your loading would have to look like this: 现在,您的加载必须如下所示:

loader1 = new RubeSceneLoader();
scene1 = loader1.loadScene(Gdx.files.internal("XXXX.json"));
World mWorld1 = scene1.getWorld();

RubeWorldSerializer.mergeWorld = mWorld1; // this is important in between your loading.

loader2 = new RubeSceneLoader();
scene2 = loader2.loadScene(Gdx.files.internal("ABCD.json"));
World mWorld2 = scene2.getWorld(); // in theory mWorld2 should be the same like mWorld1 now, and it should be both worlds merged

I don't know about the libGDX loader, in the C++ version of the loader the main loading function can be easily modified to take an existing world as a parameter. 我不知道libGDX加载程序,在C ++版本的加载程序中,可以轻松修改主要加载功能以将现有世界作为参数。 If the existing world is not null, it will be used instead of making a new one. 如果现有世界不为空,则将使用它而不是创建一个新世界。 Otherwise, a new world will be created as normal. 否则,将正常创建一个新世界。

Before (pseudocode!): 之前(伪代码!):

World loadWorld( File f ) {
    World w = <make a new world>
    //load the scene into w
    return w;
}

After: 后:

World loadWorld( File f, World w ) {
    if ( w == null )
        w = <make a new world>
    //load the scene into w
    return w;
}

The same idea could probably be done in many other languages. 可以用许多其他语言来完成相同的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM