Spatial hash slow when cell count large, space empty

Official forum for the Chipmunk2D Physics Library.
Post Reply
Amral
Posts: 5
Joined: Fri Oct 24, 2014 11:11 am
Contact:

Spatial hash slow when cell count large, space empty

Post by Amral »

Hello

While doing some profiling in preparation for adding Chipmunk to my game, I noticed that adding long chains of segment shapes is significantly faster when using the spatial hash rather than the default spatial index. In fact, when using the default index, my app would crash trying to add a chain containing more than about 28500 segment shapes to the space. The crash had code EXC_BAD_ACCESS after 227 recursive calls to the cpBBTree.c function SubtreeInsert().

However, switching to the spatial hash seems to be cause cpSpaceStep() to take longer to execute (proportional to the size of the hash table). Even with a nearly empty space containing a single static body and no shapes, cpSpaceStep() is taking about 20% of the CPU time, with a 200,000 cell count specified using cpSpaceUseSpatialHash() . (In contrast, when using the default spatial index, cpSpaceStep() takes almost no time when the space is nearly empty).

Most of the time is spend in cpSpaceUnlock -> cpSpaceHashReindexQuery -> clearTable , and clearTable's running time is proportional to the number of cells in the hash table.

So .. is there anything I can do about this ?
Why is cpSpaceHashReindexQuery being called every frame (and what does it do) ?

Any help is greatly appreciated.

Regards,
Amral
Amral
Posts: 5
Joined: Fri Oct 24, 2014 11:11 am
Contact:

Re: Spatial hash slow when cell count large, space empty

Post by Amral »

Update:

I managed this performance issue by reducing the number of objects that were in the space at any one time: objects in the relative vicinity of the player are in the space, and they're added/removed dynamically as the player moves around. This allowed me to reduce the spatial hash cell-count down to 5000, making the CPU load much more reasonable.

I also ended up switching from segment shapes to polygons, as I was getting tunnelling through the segments shapes for high-speed objects.

BTW, if the developers are reading : it would be a very useful feature if Chipmunk allowed one to define an 'update zone', such that only objects in that zone are updated by cpSpaceStep(). The update zone can be moved around, to follow the player or camera.
Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests