Thermodynamics With A Partition Grid

link here

Thanks to Three.js and in under 300 lines, this models elastic collisions (ideal gas law). A simple hash grid reduces O(n²) collision loop.

  • colors change based on speed
  • logged thermo stats.
  • 6,000 particles.

Without instanced mesh and the hash partitioning scheme, the FPS grinds to around 1–2 FPS.

4 Likes

I loves me a good hash grid! :smiley:

1 Like

Just for my information, what is a hash grid?

1 Like

Sure thing, it splits the space (in my case, the cube defined by boxSize) into smaller 3D cells. Then each particle (per animation frame) is stored in one of those cells. When checking for possible collisions, you only look within its respective cell instead of the all the space.

3 Likes

@phil_crowther
Also, a generic term for sorting things into groups based on their position on a 2d or 3d grid.

Here’s a good video covering the concepts:
https://www.youtube.com/watch?v=sx4IIQL0x7c

The “hash” means taking the position.x,y,z and performing some “hash” operation on them to form a single “key” value. this “key” is then used to cluster the objects in a map.

The hash function for sorting an object into a 2d grid might be:

 let position = object.position;
 let hashKey =  ((position.x/gridCellSize)|0)+(((position.y/gridCellSize)|0)*gridWidth);

 let cell = hashMap[ hashKey ];

if (cell) cell.push( object );
else hashMap[ hashKey] = [object]
3 Likes