• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ardmax1

Space partitioning for flocking

14 posts in this topic

Hi,
I'm looking for space partitioning structure which will be good for moving points and will enable fast fixed radius near neighbors search.
0

Share this post


Link to post
Share on other sites
Well, you could use binary space partitions or quadtree's, which are probably easier for 2d things, or even octrees which are easier to use with 3d things.
Or, you can just use a grid, when each cell in the grid has a pointer to the objects in it, the objects only have to check for collisions with objects that are in the same cell.
1

Share this post


Link to post
Share on other sites
A simple grid is what I would try first, they're the easiest thing to implement and often very effective (especially for uniform size/fixed radius).

See Ericson's Real-Time Collision Detection for a terrific chapter on various ways to implement and use grids.
1

Share this post


Link to post
Share on other sites
But with grid I would need to search 9 cells right? And what if I would need different radius in future? Anyway I'll test the grid thing. I should also mention that this its in 2d and I'm just using points if that helps in some case.
0

Share this post


Link to post
Share on other sites
9 is pretty arbitrary. Whatever size grid works well. The key is that the grid is only for reducing the candidates for comparison, not doing the actual comparison. Say you have a flock of 64 objects. If you use a grid of size 8 by 8, on average there will be 1 object per cell (obviously for flocking some cells would have more). Based on this average you would need to do distance checks for the objects in a cell with the contents of that cell [i]and the directly neighbouring cells too[/i]. So around 8 distance comparisons per cell. Which may seem like a lot, but still miles better than comparing every object to every other object.

Excuse my late-night mathematics skills if there are errors. ;)
0

Share this post


Link to post
Share on other sites
Alternatively, skip the partitioning and instead only do a fixed number of distance checks at all. There's research that real flockers (i.e. birds, fish) do that same thing too. Not sursprising if you think about it, a fish brain isn't so terribly huge.

Looking at half a dozen randomly selected (not necessarily nearest) neighbours pretty accurately simulates real flocking.
1

Share this post


Link to post
Share on other sites
I don't think that 3*3 = 9 is an arbitrary number any more than 3*3*3 = 27 is... As long as the sphere's radius is less than or equal to the cell size.
0

Share this post


Link to post
Share on other sites
[quote name='ardmax1' timestamp='1341488866' post='4955932']
But with grid I would need to search 9 cells right? And what if I would need different radius in future? Anyway I'll test the grid thing. I should also mention that this its in 2d and I'm just using points if that helps in some case.
[/quote]

No, you only need to check for collision in the 4 neighbouring cells with a higher number. That is the neighbour to the right and the three neighbours below (assuming cell 0 is top left corner and last cell is bottom right corner).
0

Share this post


Link to post
Share on other sites
[quote name='taby' timestamp='1341518377' post='4956073']
I don't think that 3*3 = 9 is an arbitrary number any more than 3*3*3 = 27 is... As long as the sphere's radius is less than or equal to the cell size.
[/quote]

As stated, late at night. ;) I thought he meant a 3 x 3 grid (which is pretty coarse and not very helpful), but he probably meant the same thing that I said.
0

Share this post


Link to post
Share on other sites
[quote name='h4tt3n' timestamp='1341521355' post='4956094']
No, you only need to check for collision in the 4 neighbouring cells with a higher number. That is the neighbour to the right and the three neighbours below (assuming cell 0 is top left corner and last cell is bottom right corner).
[/quote]
If you need to find objects whose center is within a radius R around the center of each object, the best grid size is a 2R by 2R square: every disc straddles up to 4 cells, so if you assign objects to the cell containing the upper left corner of the 2R by 2R AABB of their region of interest you only need to check the objects in the same cell and the three adjacent cells to the right, to the bottom and diagonally to the bottom right.
0

Share this post


Link to post
Share on other sites
First try with grid i got 4 times more (1k to 4k) birds without fps drop, but I'm sure it can be faster. Any idea how to optimize it?
[source]Grid::Grid( Flock* flock, float cellsize ) {
min = flock->min - flock->max / 2;
max = flock->max * 1.5f;

this->cellsize = cellsize;
ccx = (int)((max.x - min.x) / cellsize) + 1;
ccy = (int)((max.y - min.y) / cellsize) + 1;

cells = vector< vector< vector< Bird* > > >(ccy);
for( int i = 0; i < ccy; i++ ){
cells[i] = vector< vector< Bird* > >(ccx);
for( int j = 0; j < ccx; j++ ){
cells[i][j] = vector< Bird* >();
}
}

for( auto b : flock->birds ){
int cx = (int)((b->pos.x - min.x) / cellsize);
int cy = (int)((b->pos.y - min.y) / cellsize);
b->cx = cx; b->cy = cy;
cells[cy][cx].push_back( b );
}
}

Grid::~Grid() {}

vector< Bird* > Grid::getNeighbors( float x, float y, float r ){
int cx = (x - min.x) / cellsize;
int cy = (y - min.y) / cellsize;

vector< Bird* > ret;
int m = ceil( r / cellsize );
for( int i = cy-m; i <= cy+m; i++ ){
for( int j = cx-m; j <= cx+m; j++ ){
if( j < 0 || i < 0 || j >= ccx || i >= ccy )
continue;
ret.insert( ret.end(), cells[i][j].begin(), cells[i][j].end());
}
}

return ret;
}

void Grid::update( Bird* bird ){
int cx = (bird->pos.x - min.x) / cellsize;
int cy = (bird->pos.y - min.y) / cellsize;

if( bird->cx != cx || bird->cy != cy ){
auto cell = &cells[bird->cy][bird->cx];
cell->erase( remove( cell->begin(), cell->end(), bird ) );
cells[cy][cx].push_back( bird );

bird->cx = cx; bird->cy = cy;
}
}
[/source]
0

Share this post


Link to post
Share on other sites
[quote name='LorenzoGatti' timestamp='1341593992' post='4956375']
If you need to find objects whose center is within a radius R around the center of each object, the best grid size is a 2R by 2R square: every disc straddles up to 4 cells, so if you assign objects to the cell containing the upper left corner of the 2R by 2R AABB of their region of interest you only need to check the objects in the same cell and the three adjacent cells to the right, to the bottom and diagonally to the bottom right.
[/quote]

Neat trick, will implement this in my sph simulations.


Edit: Are you sure about this? After doing some pen-and-paper experiments it was easy to construct a scenario where a particle collision is not detected. If sphere A's top left AABB corner is in cell(x, y) and Sphere B's AABB top left corner is in cell(x-1, y+1) then a collision can never be detected.

cheers,
Mike Edited by h4tt3n
0

Share this post


Link to post
Share on other sites
[quote name='h4tt3n' timestamp='1341599285' post='4956419']
[quote name='LorenzoGatti' timestamp='1341593992' post='4956375']
If you need to find objects whose center is within a radius R around the center of each object, the best grid size is a 2R by 2R square: every disc straddles up to 4 cells, so if you assign objects to the cell containing the upper left corner of the 2R by 2R AABB of their region of interest you only need to check the objects in the same cell and the three adjacent cells to the right, to the bottom and diagonally to the bottom right.
[/quote]

Neat trick, will implement this in my sph simulations.


Edit: Are you sure about this? After doing some pen-and-paper experiments it was easy to construct a scenario where a particle collision is not detected. If sphere A's top left AABB corner is in cell(x, y) and Sphere B's AABB top left corner is in cell(x-1, y+1) then a collision can never be detected.

cheers,
Mike
[/quote]

I *really* recommend Ericson's "Real-Time Collision Detection", seriously: the chapter on grids covers several different aspects of implementation, including different ways to approach querying and storing objects in cells (including the 4- vs 9-way search), different ways to approach updating the cell occupancy, etc., it's great.

Seriously... I thought I knew something about different ways to use grids, then I read the chapter on grids, and I realized how little I knew [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by raigan
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0