I'm trying to create an adventure game in 2D and I'm thinking about map management/objects.

Here is what I've got in mind in terms of modelling: enter image description here

Now, I would like to create entities based on player's position, to avoid high memory and CPU load.

The goal is to have objects removed from memory when they are out of screen and far from the character (for the moment, I'm thinking about something like twice the viewport):

Here is a picture showing that:

enter image description here

Note: red spots in the image are entities.

What do you thing about that? Is there is a better technique?

  • \$\begingroup\$ Does "not created" mean "not rendered", but meshes, textures are already loaded or what do you mean by that? If you want to preserve memory wouldn't that go against that goal? \$\endgroup\$ Commented Sep 5, 2014 at 11:15
  • \$\begingroup\$ maybe "not created" means "not rendered and not getting calls on the update methods" \$\endgroup\$ Commented Sep 5, 2014 at 11:21
  • \$\begingroup\$ @BenediktVogler: Yes! I load textures (atlas), shaders and models before game's main loop, and I create entities (physic bodies, meshes, and update) after when object is near player. \$\endgroup\$
    – user30088
    Commented Sep 5, 2014 at 11:28
  • 1
    \$\begingroup\$ What is the problem you are trying to solve? Have you already implemented this method? \$\endgroup\$ Commented Sep 5, 2014 at 16:10
  • \$\begingroup\$ @SethBattin: I'm just searching to have the "best" architecture to obtain high flexibility and performances. Aside that: no, I've tried something different before but object going out of his parent pattern was problematic (removed from world). \$\endgroup\$
    – user30088
    Commented Sep 5, 2014 at 17:31

2 Answers 2


Your idea is correct, only render and update entities near the player for better performance.

To do this in a efficient manner, you will need some sort of spatial partition to know what is near the camera. You definitely don't want to loop through all the entities on your map.

One of the most efficient ways to use is a hash table, also known as a grid. But if your map is too large, a quadtree would be more efficient.

Both uses the same basic idea, divide the map in cells. Each cells contains all the entities in that specific area. So instead of looping all entities you just loop through the entities contained in the cells that are around you camera.

As a bonus you can use this spatial partition to help with you collision tests, testing only what can possible collide (in the same cell).

  • \$\begingroup\$ Thanks! I will finally use patterns system to generate random Biomes. For now, I will spawn all the Biome in one step, later I will add Octree to do what I want :) \$\endgroup\$
    – user30088
    Commented Sep 5, 2014 at 14:43
  • \$\begingroup\$ You are welcome. In 2D is a quadtree, an octree is a 3D implementation of a quadtree. \$\endgroup\$
    – Deban
    Commented Sep 5, 2014 at 14:46
  • \$\begingroup\$ looping over every entity is okay unless they are not some thousands. My game engine can manage 2300 moving objects active at the same time at 60 FPS without using octrees. And in his case we are talking about basic checks if they are in the view frustum. It should be possible to have 5000-10000 entities offsceen without performance problems. \$\endgroup\$ Commented Sep 5, 2014 at 15:23
  • \$\begingroup\$ Deban: Ok! I will use Octree because I've got 3D art but my character move on 2 axis (like New Super Mario Bros). BenediktVogler: Ok thanks, I will try without (I'm working for mobile devices) \$\endgroup\$
    – user30088
    Commented Sep 5, 2014 at 17:45

If I understood the problem correctly it's not that hard to solve. You track the position of every entities and update/render them only if they are inside or near the view field. The other way round you can of course destroy them if they are outside the view frustum (if you want that).

Just loop through the container and check x and y coordinates and compare it with the view frustum.

  • \$\begingroup\$ Thanks for our reply, I was just thinking that should be heavy to check if object is in the view frustum on each frame. I will use Octree as suggested Deban :) \$\endgroup\$
    – user30088
    Commented Sep 5, 2014 at 14:45
  • \$\begingroup\$ please check your assumption that it "should be heavy to check". >5k entities offscreen should be no problem and I don't think you need that much. \$\endgroup\$ Commented Sep 5, 2014 at 15:24

Not the answer you're looking for? Browse other questions tagged .