Avoiding cpBodyUpdatePosition on static bodies

Discuss new features and future development.
Post Reply
mikeash
Posts: 2
Joined: Mon Jan 05, 2009 1:39 am
Contact:

Avoiding cpBodyUpdatePosition on static bodies

Post by mikeash »

I'm working on an iPhone game using Chipmunk, and so the speed of the physics engine is a bit more critical than it otherwise might be. So far it's working great, except it's a bit slow sometimes. Really slow sometimes.

The game itself consists of just a couple of moving objects, and then a whole bunch of static objects. The static objects are built using cpBodyNew(INFINITY, INFINITY), and then they get a line segment shape which gets added to the space using cpSpaceAddStaticShape().

Running the game in the profiler, I find that over 50% of the game's time is spent in cpBodyUpdatePosition called from cpSpaceStep. And using the debugger I find that it's iterating over hundreds of bodies in that loop. Obviously this is doing far more work than it needs to do!

So I'm thinking of patching this or snagging the code into my app and modifying it a bit so that it only updates bodies that can move. (The loop with cpBodyUpdateVelocity takes up another 25% CPU so obviously that one would have to change too.) But before I do this, I wanted to ask, is there any way I might be able to get this to stop updating bodies that don't need it without modifying the code? It seems pretty straightforward to make this modification but I'd like to keep using the stock source if I can.

Thanks in advance for any help, and thanks a whole lot for making this code available. Chipmunk is very cool and the fact that you've made it available to the public is wonderful.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Avoiding cpBodyUpdatePosition on static bodies

Post by slembcke »

First of all, you can attach multiple shapes to a single body, so you could share a single static body between all static objects.

More importantly, you should not be adding a static body to a space. Adding a body to a space means that it has it's position and velocity updated. If you have gravity, that would be very bad as the static body would fall at the same speed as everything else.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mikeash
Posts: 2
Joined: Mon Jan 05, 2009 1:39 am
Contact:

Re: Avoiding cpBodyUpdatePosition on static bodies

Post by mikeash »

Ah, those tips sound like exactly what I needed to know. Apparently I've misunderstood some important things about static shapes. I changed the code to stop adding the static bodies to the space and, just as predicted, it's way faster. Thanks very much for enlightening me!
clint
Posts: 11
Joined: Mon May 11, 2009 11:55 am
Contact:

Re: Avoiding cpBodyUpdatePosition on static bodies

Post by clint »

slembcke wrote:...you should not be adding a static body to a space. Adding a body to a space means that it has it's position and velocity updated.
Apologies if this is a silly question (I've very new to Chipmunk)... One should still call cpSpaceAddStaticShape() to add the static shape (and it's corresponding static body) to the sim space, correct?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Avoiding cpBodyUpdatePosition on static bodies

Post by slembcke »

Sort of. I think you may be a little confused though. You use cpSpaceAddBody() to add dynamic moving bodies to the space so that it simulates them. Shapes attached to dynamic bodies are added using cpSpaceAddShape(). Static, non-moving bodies you simply don't add to the space because they are not simulated at all. They just sit there. Shapes attached to static bodies are added using cpSpaceAddStaticShape(). You could add static shapes using cpSpaceAddShape(), but it will waste CPU time recalculating the collision data for a shape that never actually moves.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests