Page 1 of 1

How to simulate gears?

Posted: Sun Nov 30, 2008 6:48 pm
by JoeStrout
Any suggestions on how to efficiently simulate gears with Chipmunk?

Ideally, I'd like to use two circle bodies, and when they're in contact, have a rotation of one cause a corresponding rotation of the other. But I don't suppose there's anything that would do that automatically. I could model each gear as a big set of polygons (one for each gear tooth, perhaps plus a circle for the inner part), and let Chipmunk simulate it from first principles — I would expect this to work, but it'd be pretty inefficient.

Any suggestions or recommendations?

Thanks,
- Joe

Re: How to simulate gears?

Posted: Sun Nov 30, 2008 9:17 pm
by slembcke
It is possible to simulate gears using actual shapes, but it would awfully inefficient yes.

Zany Doodle uses Chipmunk and does just that. It's performance seems to be just dandy.
http://www.youtube.com/watch?v=Gk9o2zxKPbM

The good news is that I've created a new joint type for gears in trunk. You'll have to wait for it if you want to get it in a stable release though.

Re: How to simulate gears?

Posted: Sun Nov 30, 2008 11:34 pm
by JoeStrout
Thanks. I should have added that I also need to simulate two wheels connected by a belt. Would the new gear joint type handle that as well?

If I can't wait for the next stable release, and want to avoid using actual shapes, is there any other way to fake it? In the case of two wheels connected by a belt, I wonder if a set of (say) three pin joints between the wheels would do the trick. Seems like it should, if the wheels are the same size, but not if they differ in size (since in that case one should spin faster than the other). And this wouldn't work for gears.

But what about manually doing something on each update? Maybe using cpDampedSpring, modeling a spring between (say) the top of each wheel in the case of a belt, or at the contact point in the case of two gears? Hmm, but the cpBody docs warn that it's likely to be unstable with large damping values. Perhaps there's some way to calculate the force each gear (or wheel) should apply to the other — but my physics is too rusty to see it. Any pointers?

Thanks,
- Joe

Re: How to simulate gears?

Posted: Mon Dec 01, 2008 12:12 am
by slembcke
Using a pair of pin joints would probably work OK if the ratio is 1:1.

I'm not quite sure how you'd do it otherwise. Applying torques to the objects is not very stable. I've tried that before with limited success. Creating a constraint for it was the "correct" way to do it, and wasn't too hard to implement once I had refactored the way that joints worked internally. Backporting the code would be pretty difficult.