Eats 100% of CPU when i add bodies to space, low FPS

Official forum for the Chipmunk2D Physics Library.
Post Reply
p1p1
Posts: 7
Joined: Sun Jul 19, 2015 7:17 am
Contact:

Eats 100% of CPU when i add bodies to space, low FPS

Post by p1p1 »

Hello!

I've been experimenting with Objective Chipmunk and I am experiencing a strange lag after my bodies are added to the space. I am adding about 10-50 bodies to the space, and right after that the next call to ChipmunkHastSpace step eats up all my CPU for about 5-10 seconds. I am using 10 iterations. The amount of iterations and the amount of bodies both seem to be proportional to the duration of the lag.
Here what I get from XCode Instruments (scroll down):
Screen-Shot-2015-07-30-at-08.02.55.jpg
Screen-Shot-2015-07-30-at-08.02.55.jpg (229.16 KiB) Viewed 7170 times
It clearly says, that most of the time is being spent in cpArbiterPreStep inside space stepping function. I've run into another topic on this forum with the same problem (this one: viewtopic.php?f=1&t=827 ), but I still don't really get the reason that's causing this huge lag. Can anybody explain that in more detail?
p1p1
Posts: 7
Joined: Sun Jul 19, 2015 7:17 am
Contact:

Re: Eats 100% of CPU when i add bodies to space, low FPS

Post by p1p1 »

Ok, I think I got closer to the reason of the lag that was happening after bodies were added to the scene. I finally came up with the following solution.

I've been thinking about what could cause the arbiters (which are the essence of collision mechanism in Chipmunk) to waste a lot of CPU cycles, and the only thing that came into my mind is – those arbiters must be busy doing some kind of collision handling for a very long time after my bodies were added. Well, there's only one way to provoke a growing number of collisions – that is when bodies are created in exactly same locations or so close to each other, that they begin overlapping immediately after being created and that induces an exponentially growing number of collisions between those bodies. The more bodies - more collisions between all of them happen.

What I was trying to do is pre-create and pre-add those bodies to my space with some 'distant nonexistent coordinates', so that i would not have to add those bodies to Chipmunk space on the fly. So, my bodies were created in the same location, and after i separated them with some random distance the lag that involved multiple unnecessary collisions disappeared as those bodies were not touching anymore.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 12 guests