Culling of 2D Objects out of View

I’ve looked around and all I can find is methods involving BoundingBoxes and such for culling of 3D objects. I guess they can be used for 2D games (I imagine) but I was wondering if there was a more “2D” method for removing/stop processing objects/entities that are out of view of the camera to save on performance?

My current idea is to create a Rectangle that covers the entire camera frustum/view, and then check if objects are within that rectangle to see if they should be processed or not. Sounds good to me though so far, I can’t manage to get the rectangle to move with the camera. Just stays where it is as soon as the game starts.

if you want to move the rectangle to be allways in few then render the rectangle at the camera position or render it before you translate to the camera position.

if obj.max_x >= cam.min_x && obj.min_x < cam.max_x && obj.max_y >= cam.min_y && obj.min_y < cam.max_y
then render(obj);

A couple of things…

First off, it may help to separate your thinking into two parts… the world, the place where your objects actually are, and the view, the place where you project what the screen should actually be seeing. The world can have any dimension and be any size; however, the screen is always going to have a top-left coordinate at 0,0 and a bottom right corner at whatever your screen dimensions are. Consider the difference in coordinates between your world and your view :slight_smile:

Next, your approach is a great first step! You’ve correctly identified that there are things that you want to render, and things that you don’t want to render. You’ve also created a way of determining whether or not you should render it. This can go a step further though… consider how you want to check to see if those things should be rendered. With your current approach, you have to check all of your objects… there are ways that you can skip checking objects that aren’t on the screen entirely!

Here’s two ways to consider…

  1. You can organize your objects into an array where the indexes of the array have meaning in relation to their position in the world. For example, consider a tile based game where you have a 100x100 array of tiles. If your view is in the top-left of the world and can only show a 16x16 section of tiles, you only need to consider the first 16 tiles horizontally, and the first 16 tiles vertically, in your tile array. This can be extended to start from any position and render whatever size you need.
  2. There are other data structures besides an array that can be used to organize, and retrieve data. Look into a QuadTree. It splits up virtual space into sub-divided rectangles based on how many things are in each rectangle. You can then query the QuadTree for objects that intersect with a specified rectangle (ie, the screen). This approach is really good for objects that can be in any position in space and generally aren’t contiguous, such as enemies. There’s lots of resources about QuadTrees, and MonoGame.Extended even has an implementation built in, if you’re using that.

Do keep in mind that with either of these options, you will probably still want to touch on them in your update loop since you may still need to update their positions or state, regardless of whether or not they are on the screen.

1 Like

That’s the thing though, that’s what I’m doing and yet it doesn’t move with the camera. Just stays at the first frame position as soon as the game starts