Speed with Springs

Official forum for the Chipmunk2D Physics Library.
Post Reply
brettc
Posts: 10
Joined: Thu Mar 08, 2012 9:12 pm
Contact:

Speed with Springs

Post by brettc »

Hi,

I'm using pymunk to create a simple 2-d simulation of cell-growth.

I grow my cells, then they can "bud" a new cell that remains attached. I attached them via 2 springs. Like this in sophisticated ASCII-Art...

0=0

... except the cells are actually touching.

Cells can bud 2 others, so I get a branching of connected cells. They all bump into each other and the springs are forced to stretch and when they got too long I break them. It all works pretty well, once you fiddle with the spring parameters. But it slows down a lot at about 200 cells. It is really sluggish and unresponsive. Now, I'm writing in python, so that has something to do with it. But when I profile it, most of it occurs in the "step" call to chipmunk.

So there are 200 cells with 400 springs. Should it really be slow? I've tried putting the spatial hash into action (this should be perfect, as all cells end up the same size), but to no effect.

The main loop looks like this:

Code: Select all

        fps = 50
        iterations = 10
        dt = 1.0/float(fps)/float(iterations)
        for x in range(iterations): # 10 iterations to get a more stable simulation
            self.space.step(dt)
        for c in self.cells:
            c.step()
        self.time_step += 1
If I reduce the iterations, it get's unstable.

Where should I begin looking to try and speed this up?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Speed with Springs

Post by slembcke »

Woah. You are using 1/500th of a second as your timestep. That is crazy small. Most simulations use between 1/60th to 1/180th of a second.

Also what are you using for solver iterations (space.iterations, not to be confused with your iterations variable you are using for substeps).
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
brettc
Posts: 10
Joined: Thu Mar 08, 2012 9:12 pm
Contact:

Re: Speed with Springs

Post by brettc »

OK. You've confirmed I was doing something dopey! I think I just cut and pasted this code from a pymunk example. The space.iterations was set to 1. So I've rewritten it now with some more informed values (dt = 1/50, and space.iterations=10).

Now, however, the physics is very unstable. I've discovered this is because I'm using a very high value for the spring stiffness (> 500!). I'm really just tuning things so they "look right", rather than paying much attention to values. So a high spring stiffness looks like a bad idea, unless you use lots of iterations.

I'm now trying to use the PinJoint. I take it that I can make this act a little "spring-like" by tuning the parameters.

What I'm looking for is a stretchable, highly damped spring. And when the spring gets too stretched, I remove the joint.

If you have any suggestions, that would be great.

Cheers,
Brett
cjhorton
Posts: 11
Joined: Fri Mar 30, 2012 11:04 pm
Contact:

Re: Speed with Springs

Post by cjhorton »

I wonder if constraining the cells to the ground instead of each other would work?
brettc
Posts: 10
Joined: Thu Mar 08, 2012 9:12 pm
Contact:

Re: Speed with Springs

Post by brettc »

cjhorton wrote:I wonder if constraining the cells to the ground instead of each other would work?
Not sure how that would work. The cells should be free-moving, and only stuck to one another (like they were in a liquid).

Brett
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Speed with Springs

Post by slembcke »

Yeah. Using a stiffness of 500 sounds pretty high (at least guessing that your cells masses are in the 1-10 range). You could try using soft pivot or pin joints (set the maxForce of the joint to something other than infinity). That will act somewhat like an very strong, highly damped, but stable spring. It's not quite the same behavior, but maybe it will be closer to what you want anyway.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
cjhorton
Posts: 11
Joined: Fri Mar 30, 2012 11:04 pm
Contact:

Re: Speed with Springs

Post by cjhorton »

Yeah, constraining them to ground wouldn't work. I forgot that even cells in the center keep dividing after their initial divide.

I assume you are wanting to do something similar to this?

http://www.youtube.com/watch?v=MPBOcmVpbcE

You say cells can bud two others...are you limiting how many times each cell can divide(like a cell can be a parent to two other cells only?) or was this a simplification of the division of cells? It seems that you will have more than 200 springs for 400 cells unless you are removing them at some point.

Image

If the cells can only be a parent to two child cells, then the initial cell(cell 1) will only be attached to two cells. But, every cell (cells 2, 3, and ...) after that will be attached to three cells. If the cells can be a parent to more than 2 child cells, then there will be n attachments.

You are taking this into account?
larryb
Posts: 3
Joined: Mon Oct 10, 2011 12:18 am
Contact:

Re: Speed with Springs

Post by larryb »

@brettc,
You've probably seen these videos:
http://www.youtube.com/watch?v=oTRfVY1EWNs -> Scott's: Jelly Chipmunk
http://www.youtube.com/watch?v=oTRfVY1EWNs -> Soft body physics using Box2d

I guess it would be more like:
http://www.youtube.com/watch?v=NL91s-P2ALs

Are these showing similar physical properties that you want for the cell bodies?
brettc
Posts: 10
Joined: Thu Mar 08, 2012 9:12 pm
Contact:

Re: Speed with Springs

Post by brettc »

Hi. Thanks for the all the responses.

cjhorton: the video is definitely something like the idea. I've actually written something similar using box2d. The particular problem I'm interested here is much simpler. The cells actually bud, rather than split (yeast do this). At the moment a cell is simply a circle. I want to keep cells simple so I can have lots of them. And yes there are lots of springs. The first cell has 4 daughters (branches). After that, each cell can have 1 or 2 daughters. The cells quickly get crowded, and joints get stressed. Then I break them.

larryb: Thanks for the vids! I've seen some of those, and as I say above, I've even made something like this. But this one is a bit simpler. The cells are not softbody, just simple circles. I'm interested in the relationship between branching and joint strength, and how it affects the size of clumps that break off.

I've now got it working using pinjoints. It works ok, but there is a lot of "quivering" behaviour. This, I take it, is instabilities in the physics. I'll play around and see if I can improve it.

Thanks for the interest. I'll put something up on youtube soon...

Brett
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Speed with Springs

Post by slembcke »

Are you using 0 length pin joints? If you are, you should substitute pivots instead. With a large number of constraints in the space, you'll need to turn up the number of space.iterations (spring forces aren't actually constraints, only the damping is, so the iterations wouldn't have made much of a difference there).
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 31 guests