Number of Bodies Limit?

Hi

I have a model that extends AbstractExample from the TestBed in which I generate a number of circles. It works for a couple of thousand of circles but as I increase the number of circles beyond a certain point I get the following exception:

======

Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 2147483647
at org.jbox2d.collision.BroadPhase.createProxy(BroadPhase.java:230)
at org.jbox2d.collision.shapes.Shape.createProxy(Shape.java:337)
at org.jbox2d.dynamics.Body.createShape(Body.java:308)
at jbox2d_sample1.HourGlassTest.createCircles(HourGlassTest.java:113)
at jbox2d_sample1.HourGlassTest.create(HourGlassTest.java:41)
at jbox2d_sample1.AbstractExample.initialize(AbstractExample.java:229)
at jbox2d_sample1.TestbedMain.draw(TestbedMain.java:178)
at processing.core.PApplet.handleDraw(PApplet.java:1425)
at processing.core.PApplet.run(PApplet.java:1327)
at java.lang.Thread.run(Thread.java:619)

======

Is there a limit to how many objects PApplet can handle?

Thanks

Graham
Graham,

The problem you are having is that the number of pairs is too large. There are a couple ways you can get around this. The easiest way is to increase the maximum number of pairs (maxPairs) in Settings.java. (That's org.jbox2d.common.Settings.) The other way, in my experience, is to stagger the creation of your bodies. If you try to create all of them in a single time step, then the number of pairs is going to be massive. I think the engine may filter out some pairs after an additional time step.

Nathan
Hi

Thanks for your reply. I tried your first suggestion of increasing maxPairs, which didn't work.

The thrown java.lang.ArrayIndexOutOfBoundsException: 2147483647 is at the limit of type long, and as a result doubt if increasing maxPairs would handle this value.

I'm not sure about how to stagger body creation. I create all my bodies in the testbed create() method.

Graham
Hi

I forgot to mention that a way of throwing the above mentioned exception is to run the LiquidTest example and increase the number of particles (nParticles) to, say, 5,000.

Graham
Hi again

Just discovered that increasing both maxProxies and maxPairs I'm able to increase the number of bodies added in a single step significantly:

/** Must be a power of two. */
//public static final int maxProxies = 2048;
public static final int maxProxies = (int)Math.pow(2,20);
/** Must be a power of two. */
//public static final int maxPairs = 8 * maxProxies;
public static final int maxPairs = (int)Math.pow(2,20);

Graham
Good. I didn't think about maxProxies, but that makes sense.

Original thread: https://box2d.org/forum/viewtopic.php?t=5577