I'm making a game engine and currently I use a function for checking a new position after movement to prevent game objects from escaping the world:

const boundary = function (min, max) {
    return Math.min( Math.max(this, min), max );

I've noticed that it consumes around 9-10% of my game loop (testing with 4500 objects). Is there a better way to handle this? Do I generally need to make my world finite?

To clear this up, this is not an object-object collision detection question. For that I have a grid, and this grid is based on the size of the world, which is why I check that objects don't escape the world size limits.

  • 1
    \$\begingroup\$ You are testing with 4500 objects and it consumed around 9-10% of the game loop. Why is 10% of the game loop more than you would expect? Are 4500 objects the typical amount you expect out of testing? I think, in general, you should be against optimizing early, or optimizing for fabricated tests. Build an engine, then see what its bottlenecks are down during normal operation, then target your optimizations to where they are needed. "Premature optimization is the root of all evil." - Donald Knuth. \$\endgroup\$ Sep 28, 2018 at 18:15
  • \$\begingroup\$ @ Garrett Gutierrez That's a very good point, but to answer your question, I'm not currently developing a game, making a game engine is more of an exercise to me at the moment. \$\endgroup\$ Sep 28, 2018 at 19:33

1 Answer 1


Your code for checking the boundaries looks good enough, but the frequency of which you call this function might be brought into question. Do you really want to do this collision check every frame for every object?

Consider when you start to implement collision for individual game objects later on, would you want to check each object with every other object every frame? Your time complexity will shoot through the roof. Instead, you may want to consider implementing a Quadtree. I'm assuming you're writing a 2D engine since your boundary check is 2 dimensional. This would drastically limit the number of objects you have to call your boundary checks on.

Edit: If you already have a grid system, you could take advantage of that by only calling your boundary checks on objects in the edge rows and columns.

  • \$\begingroup\$ This is not a question about collision checks between objects. I'm creating fixed size world and coercing position of moving objects to the size of this world after they update their positions. My game world is fixed sized because I use grid for broad phase collision detection, but I think I'm doing too many bounds checks (judging by function times) to handle some rare edge cases. \$\endgroup\$ Sep 28, 2018 at 12:10
  • 4
    \$\begingroup\$ I can tell. But how do you decide which objects to check bounds and which not to? A Quadtree is the simplest solution I can think of that yields the best results. You don't have to use it for collision checks between objects if you don't want to, but it is applicable even in your boundary check. \$\endgroup\$
    – eclmist
    Sep 28, 2018 at 12:12
  • 3
    \$\begingroup\$ I saw your edit. If you already have a grid system in place, why don't you use it in your advantage? Only call boundary check on objects in the edge rows and columns of your grid. \$\endgroup\$
    – eclmist
    Sep 28, 2018 at 12:15
  • \$\begingroup\$ Reduced the number of boundary() calls from 7% of total time to 0.5% by following to your advice. Thank you. \$\endgroup\$ Sep 28, 2018 at 13:28

You must log in to answer this question.

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