Setting "Static" Dynamically (And other fun problems)

Refiltering didn't do anything, but setting the contact filter to NULL while processing the Do...while loop (not even the entire frame) did the trick. Thanks for the suggestions and help.
SetMass calls Refilter if IsStatic changes. This will cause remove and add events to be fired. You need to ensure your listener is aware of this.
Hm. But do I *want* them to fire, or *not* want them to fire when the change is made? I'm not sure exactly what's happening when I'm resetting the mass there.
I cannot remember why it calls refilter, but it does. No doubt something is cached in the contact, and this is easier than finding an updating it. You want the events to fire, but be ignored. Turning off the listener momentarily is a good hack, but it might come back to bite you when if you are attempting to keep track of contacts.
Bah, still having this problem. Turns out when I turn off the ContactListener (via disabling complete by setting the filter to Null, OR by parsing and ignoring things) it just stops the error from happening most of the time. But given enough attempts it does eventually crumple.

I've put traces all over my program to see where exactly it's failing. It's not failing upon a contactListener call, it's failing in the Step() update, and it can fail immediately after enabling the contact listener, or it can fail up to 70 ticks after the contactListener was fired (possibly longer, too).

The crash never happens if I never turn the contactListener on again. :/

Very strange. Always fails at the same line in b2Island though, as posted above.
I'm perplexed. The source of your error would appear to be an area independent of what your contact listener might do. Could you describe exactly why your listener does, that might help? (or show some code).
My contact listener simply changes a boolean on the player object to let me know if the player character is on the ground. "GroundSensor" is the "isSensor" object stapled to the player's feet:

Code:
      public override function Add(point:b2ContactPoint) : void {
         if (active) {
            if (point.shape1.GetUserData())
               if (point.shape1.GetUserData() == "GroundSensor") {
                  point.shape1.GetBody().GetUserData().setGrounded(true);
               }
            if (point.shape2.GetUserData())
               if (point.shape2.GetUserData() == "GroundSensor") {
                  point.shape2.GetBody().GetUserData().setGrounded(true);
               }
         }
      }


... I have very similar methods for the REMOVE and PERSIST functions.

"if (active)" is my method of "turning off" the contact listener - I set active to false when I'm running through this loop, which does prevent a boatload of crashes (this code is for setting the objects to/from static):

Code:
         var b:b2Body = m_world.GetBodyList();
         do {
            if (b.GetUserData()) {
               if (b.GetUserData().getClass() != "Player" && b.GetUserData().getClass() != "Terrain") {
                  var md:b2MassData = new b2MassData();
                  md.center = new b2Vec2(0,0);
                  md.I = 0;
                  md.mass = 0;
                  if (frozen) {
                     trace("Freezing Object...");
                     b.GetUserData().storeVelocity();
                     b.SetMass(md); //static
                  } else {
                     trace("Thawing Object...");
                     b.GetUserData().applyVelocity();
                     b.SetMassFromShapes(); //dynamic
                  }
               }
            }
         } while (b = b.GetNext());
         m_contactListener.setActive(true);


In the block of code, you can see I store the velocity information before making the item static, then I restore it again with applyVelocity.

If there's anything else to make this easier to troubleshoot, let me know. I just don't want to spam the forum with a mile of code.

Thanks for the help.

Edit: Here's the results of my tracing that helps narrow down the error source:

Code:
Freeze Transition Tick Start: 4152
FreezeItems() called @ 4152
Disablding CL...
... CL Disabled
Freezing Object...
Freeze Complete.
Freezing Object...
Freeze Complete.
Freezing Object...
Freeze Complete.
Enabling CL...
... CL Enabled.
CL fired ADD
Freeze Transition Tick End: 4152

Normal Tick Start: 4153
CL fired PERSIST
Normal Tick End: 4153

Normal Tick Start: 4154
CL fired PERSIST
Normal Tick End: 4154

Normal Tick Start: 4155
CL fired PERSIST
Normal Tick End: 4155

...

Normal Tick Start: 4168
Normal Tick End: 4168

Normal Tick Start: 4169
Normal Tick End: 4169

Normal Tick Start: 4170
RangeError: Error #1005: Array index is not a positive integer (-1).
   at Error$/throwError()
   at Array()
   at Box2D.Dynamics::b2Island()
   at Box2D.Dynamics::b2World/Solve()
   at Box2D.Dynamics::b2World/Step()
   at Game::GameWorld/updateWorld()
   at Game::GameMechanics/worldUpdate()
   at Game::GameMechanics/updateGame()
   at Main/gameUpdate()


You can see the objects were frozen in tick 4152 and it didn't crash until 4170. During tick 4170 the contactListener was not called, nor was it called in the frames leading up to it.
I've looked through the b2Island and b2World classes briefly and I think ( strong emphasis on the word think ) the problem is that the m_contactCount value is getting set to -1....

I tested this (and you can to) and if you create an array with a negative value as the size parameter for the array, then it throws the error you saw.

the following timeline code( just cause its quick ) produces your error
Code:
var array:Array = new Array(-1);

RangeError: Error #1005: Array index is not a positive integer (-1).
at Error$/throwError()
at Array()
at Untitled_fla::MainTimeline/frame1()

So, I think its important to trace the values to confirm that. By trace, I mean dig into the source code and see where the m_contact value is being changed. Its marked as m_ but its actually public... so a full search might be necessary to find out where things are changing it.

I still think its to do with changing the body from static to dynamic... the shapes are refiltered correctly, but the broadphase caches contacts to match up pairs... I suspect a pair is getting mismatched because of the refiltering and that is causing the -1 m_contact count.

Hope this helps. I'd offer more but my work schedule is limiting my community involvment right now.
The block of code that fails in b2island is:

Code:
      m_contacts = new Array(contactCapacity);
      for (i = 0; i < contactCapacity; i++)
         m_contacts[i] = null;


(right at line one there), so it's trying to define m_contacts when it fails. "contactCapacity" is designated in the constructor of b2island, so whatever is calling that seems to be pulling the wrong figures...

The calling code is on line 506 in b2World:

Code:
      // Size the island for the worst case.
      var island:b2Island = new b2Island(m_bodyCount, m_contactCount, m_jointCount, m_stackAllocator, m_contactListener);


"m_contactCount" being the invalid figure in question. There's a handy GetContactCount function so I ran it through in my loop during the crash:

Code:
Freeze transition tick start: 1255
m_contactCount 21
FreezeItems() called @ 1255
Disablding CL...
... CL Disabled
Freezing Object...
Freeze Complete.
[...]
Freezing Object...
Freeze Complete.
Enabling CL...
CL Enabled.
CL fired ADD
Normal Tick End: 1255

Normal Tick Start: 1256
m_contactCount 2
CL fired PERSIST
Normal Tick End: 1256

Normal Tick Start: 1257
m_contactCount 2
CL fired REMOVE
Normal Tick End: 1257

Normal Tick Start: 1258
m_contactCount -1
RangeError: Error #1005: Array index is not a positive integer (-1).


Around tick 1257 I "jumped away" with my player character, so the contactCount of "2" sounds like it was my player character being pressed up against the static floor. CL fires REMOVE, which sounds right - but then the contactCount reduces down to -1.... and that's when the error happens.

Here's another quick trace:

Code:

[freezing in frame 817]

Normal Tick Start: 818
m_contactCount: 0
CL fired PERSIST
m_contactCount: -1
Normal Tick End: 818

Normal Tick Start: 819
m_contactCount: -1
CL fired REMOVE
RangeError: Error #1005: Array index is not a positive integer (-2).


Looks like the m_contactCount was reset to 0 despite there being an active contact. CL is firing "persist" even though there is 0 contacts. The contacts then reset to -1 despite there being no "remove" (in frame 818). :/

I'll dig a bit deeper into what's setting the contactCount so low...
Code:
Normal Tick Start: 1442
m_contactCount: 0
CL fired REMOVE
b2ContactManager.Destroy contact called, m_contactCount--
b2ContactManager.Destroy contact called, m_contactCount--
m_contactCount: -1
Normal Tick End: 1442

Normal Tick Start: 1443
m_contactCount: -1
ERROR


looks like "Destroy" in b2ContactManager is removing two contacts, even though "0" is the number of contacts reported. I guess the variable m_contactCount is getting out of sync at some point.

As a control experiment I just put my player character into an environment with everything static. When standing on a platform, there is a contactCount of "2", and when he jumps into the air it's "0", with the appropriate CL ADD and CL REMOVE functions being fired.

So it seems that during the freezing process, it's resetting m_contactCount to "0" even though the player is currently on an object and m_contactCount should be set to "2". Then when the player jumps, the contactCount drops to -2, thus producing the error.

I suppose I should then focus on why b2ContactManager.Destroy is being called so frequently during the freezing process..

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