interactwithankush 600227d2e4
fix: Sonar report - fix blocker and critical ones (#1899)
* update SpatialPartitionBubbles - fix Sonar blocker issue

* fix Sonar critical issue - Define constant instead of duplicating the literal

* fix Sonar critical issue - remove unnecessary default constructor

* fix Sonar critical issue - Define constant instead of duplicating the literal

* fix Sonar critical issue - Define constant instead of duplicating the literal

* fix Sonar critical issue - Define constant instead of duplicating the literal

* fix Sonar critical issue - fix checkstyle issue

* fix Sonar critical issue - fix code smells

* fix Sonar critical issue - fix code smells

* fix Sonar critical issue - fix code smells

* fix sonarbugs - adding test cases for Commander class

* sonar fix - add assert commands in CommanderTest

* sonar fix - add test cases for CommanderTest

* sonar fix - add test cases for CommanderTest

* sonar fix - add test cases for CommanderTest

* sonar fix - add test cases for CommanderTest

* sonar fix - add test cases for CommanderTest

* sonar fix - add test cases for CommanderTest

* sonar fix - add test cases for CommanderTest

* sonar bug fix & test cases

* sonar bug fix & test cases

* sonar bug fix & test cases

* sonar bug fix & test cases

* sonar bug fix & test cases

* Revert "sonar bug fix & test cases"

This reverts commit 640dd55e35a9730e981d14665913f3d9b5b2d3b2.

* sonar bug fix & test cases

* sonar bug fix & test cases

* sonar bug fix & test cases

* sonar bug fix : avoid Thread.sleep

* sonar bug fix : cleanup Thread.sleep

* sonar bug fix: test commit

* sonar bug fix: test commit

Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
Co-authored-by: atayal <Ankush_Tayal@intuit.com>
2021-12-16 20:05:13 +05:30
..

layout, title, folder, permalink, categories, language, tags
layout title folder permalink categories language tags
pattern Spatial Partition spatial-partition /patterns/spatial-partition/ Behavioral en
Performance
Game programming

Intent

As explained in the book Game Programming Patterns by Bob Nystrom, spatial partition pattern helps to efficiently locate objects by storing them in a data structure organized by their positions.

Explanation

Say, you are building a war game with hundreds, or maybe even thousands of players, who are clashing on the battle field. Each player's position is getting updated every frame. The simple way to handle all interactions taking place on the field is to check each player's position against every other player's position:

public void handleMeLee(Unit units[], int numUnits) {
  for (var a = 0; a < numUnits - 1; a++)
  {
    for (var b = a + 1; b < numUnits; b++)
    {
      if (units[a].position() == units[b].position())
      {
        handleAttack(units[a], units[b]);
      }
    }
  }
}

This will include a lot of unnecessary checks between players which are too far apart to have any influence on each other. The nested loops gives this operation an O(n^2) complexity, which has to be performed every frame since many of the objects on the field may be moving each frame. The idea behind the Spatial Partition design pattern is to enable quick location of objects using a data structure that is organised by their positions, so when performing an operation like the one above, every object's position need not be checked against all other objects' positions. The data structure can be used to store moving and static objects, though in order to keep track of the moving objects, their positions will have to be reset each time they move. This would mean having to create a new instance of the data structure each time an object moves, which would use up additional memory. The common data structures used for this design pattern are:

  • Grid
  • Quad tree
  • K-d tree
  • BSP
  • Boundary volume hierarchy

In our implementation, we use the Quadtree data structure which will reduce the time complexity of finding the objects within a certain range from O(n^2) to O(nlogn), decreasing the computations required significantly in case of large number of objects.

Class diagram

alt text

Applicability

This pattern can be used:

  • When you need to keep track of a large number of objects' positions, which are getting updated every frame.
  • When it is acceptable to trade memory for speed, since creating and updating data structure will use up extra memory.

Credits