0

I took a look on those 2 project that I found in here: AndEngineJb2dJson AndEngineRubeLoaderExtension

As some of you know I'm developing a rolling scene based game I'm loading all the entities from a XML file and create them in the Loading Scene recently I increase the game width , and by doing so , in the whole level I have about 500 entities (instead of 250+-) But this cause a performance problems , the game is "Lagging" \ "jumping" and everything move slow.

My question is , if those Rube Loaders support performance issues , do some improvement or its just like the normal andengine loader , that I need to manually load each entity in the XML \ JSON

Does anyone develop a rolling scene based game with the same amount +- of entities and tell me what it the correct way to implement it?

Thanks

Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69
  • What is a "rolling scene based game"? Did you mean streaming? And by Rube you mean the R.U.B.E. Box2D editor? Are you sure your performance issues are because of the file format rather than simply the number of active entities (box2d bodies I presume) in the world? 500 physics bodies is quite a lot. – CodeSmile Jun 19 '14 at 09:51
  • Hey I ment a game when your player is always going in the right direction, and platform and other stuff moving in the opposite direction. Yes I meant R.U.B.E Well that what I'm asking because I'm sure there are a lot of game lake this one , and the entities must be loaded from the start , the position is not randomized and therefore I can Create and delete the entity before I want to display it I need to load all of the entities when I'm loading the level – Aviram Fireberger Jun 19 '14 at 12:01
  • Ah, you mean "scrolling" not "rolling". Have you tried deactivating all physics objects until they appear on screen, respectively deactivating or removing them when they left the left side of the screen? That would be the easiest solution. – CodeSmile Jun 19 '14 at 12:19
  • I tried to delete them when they left the screen It help.. but only after half of the game.. I mean in the starting I have 500 Entities that already in the screen (not shown) and after a while lets say I moved 250 , So I deleted those one and than the game stop lagging , but the first part of the game was lagging. I'm not out how to implement the "activate" physics object when the appear. I have all the entities in an array, should I iterate all of them every engine update in compare their "X" position to the player position. doesn't this will cause more computing and mor lagging? – Aviram Fireberger Jun 19 '14 at 12:35

1 Answers1

2

As you don't really provide much detail about your specific implementation I will just provide you with a generic way of handling a game of this type.

First, you need to define an "active" area for your game world - this would be the area of the world that needs to be simulated (i.e. the entities in this area will move and interact with the world).

Your world layout might look something like this:

Example world

In this example your world is the grey block and extends beyond the screen boundaries (which is the black frame). The red frame indicates the active area - notice that it extends beyond the size of the display, but does not cover the entire world. The size of this area will depend on your specific implementation/needs.

The active area will generally move along with your player and hence can easily be represented as distance from player (in two directions).

The basic idea is that the entities (in blue) are only simulated while they are inside the active area. All entities to the right of the active area should be inactive until they enter it, at which point their simulation can start - for this reason, the active area should extend some amount to the right of the display area so that entities have some initial time to run their simulation before being displayed. Once the entities leave the active area (to the left) they can stop their simulation - and be removed if needed.

So in the example image:

  • E1 is has already gone through the active area and is no longer needed (it can be disabled/removed)
  • E2 is active and visible (currently displayed)
  • E3 is active but not visible yet
  • E4 and E5 is not active yet and will only become active once they enter the active area as the player gets closer to them

As for how to handle the entities, that depends on the size of the world, the number of entities, etc.

One way is to create/load the entities once they enter the active area - in which case it should extend far enough to the right of the display so that they have enough time to be created/loaded before entering the display area. This is the best approach for large worlds where there may be thousands of entities. In this case you may also need some kind of subdivision for your world so that you can exclude large amounts of entities from processing without checking each one - a array of fixed sized "blocks", each containing the entities that are within it, would work fine in most cases for a simple 2D side-scroller.

Another approach is to load all objects at the start and only perform simulation updates (such as physics, movement, collision detection) for them once they enter the active area. This approach works great for smaller worlds and provides an easier implementation.

free3dom
  • 18,729
  • 7
  • 52
  • 51
  • Wow!!! By far the best answer I got in stackoverflow!!! Thanks you so much Have another small question , lets say that my world is very big 40000 pixels , should I split this level ? lets say load 20000 pixels and when "ACTIVE AREA" rich 20,000 I'll load the second part? is that what you meant in the first implementation. Or when I'm loading all the XML of the entire level , I should divide them in the loader code lets (X entities < 20000 Save in group a) , (X entities > 20000 and smaller than 40000 Save in group b) – Aviram Fireberger Jun 20 '14 at 12:24
  • 2
    Glad it helpsd :) As for splitting, you should experiment with different sizes to get the best result, so for 40000 pixels you could do 2*20000 split, or you could do 4*10000, and so on. Testing will determine what division would work best for you. Alternately, you could set a maximum object limit and then determine the optimal split distance based on the largest area that would have that many objects. Just be sure that you start loading the next area BEFORE it becomes visible - preferably in a background thread, so that the player's game is not interrupted while new data is loading. – free3dom Jun 20 '14 at 12:36
  • hi I split the level to 3,000 pixels chunk, and load the current level and the next one. Once the player moves to the "cache" level the old one is removed. The problem that I have now is when I'm loading the next level, I got a little "jump" in the game which doesn't look good. I tried to perform the "Loading" by this thread: resourcesManager.activity.runOnUpdateThread(new Runnable() { ... } and also by a new thread: new Thread(new Runnable() { ... } Is it a possible to run it on a background thread and not having this "jump"? – Aviram Fireberger Jul 15 '14 at 20:59
  • It might be possible, but it will depend greatly on the particular device you are using and the speed of it's I/O (i.e. storage read speed) as well as it's processing power. You could try to load less than the 3000px chunk but more frequently, or allow some delay between loads. But in the end it might still happen on lower spec devices. Best idea would be to use a profiler to determine exactly where your slowdown occurs, and then use that as a starting point for optimization. – free3dom Jul 15 '14 at 21:56