Setting "Static" Dynamically (And other fun problems)

I have a need to turn objects from static to dynamic and back again at will. During this process, all userdata, velocity vectors, etc., must be retained.

I figured the easiest way to do this was to use those dreaded m_vars (lacking a way to set it manually):

Code:
         var b:b2Body = m_world.GetBodyList();
         do {
            if (b.GetUserData()) {
               if (b.GetUserData().getType() == "Type I'm Looking For") {
                  b.m_type = 1; //static
               }
            }
         } while (b = b.GetNext());


This does the trick - I can toggle them from being static to dynamic and back again, but while things are frozen I can still nudge them and move them around with other objects (though the effect is dampened). Is there a different way I can go about doing this? Anyone know how to make this work?

(edit: changed problem text)
should be able to change the boolean value of static in b2Shape, and then refilter the shape with b2World.
Maybe I'm blind, but I don't see a boolean value for b2Shape named "static", or anything else that could be used as such...
Mass == 0 if and only iff body is static.
Use SetMass to dynamically turn static on and off.

I've no idea what sketchbook is referring too. At a guess, some port (C# or D, mebbe), which has a looser attitude towards porting the API. If this is the case you need to be careful when giving advice.
BorisTheBrave wrote:
Mass == 0 if and only iff body is static.
Use SetMass to dynamically turn static on and off.


Setting the mass to 0 seems to do the trick, and I can restore the objects by calling SetMassFromShapes, which is handy. The objects also seem to retain their momentum, which is exactly what I wanted.

A few glitches though:

The bodies seem to pass their momentum onto objects that are touching them, even when they are static and not moving. Eg: A box is moving from left to right at 1m/s. I "static-ify" it, and it stops moving. Another boxes touches is, and is moved to the right at 1m/s for the duration of the contact.

Is there a way to stop that from happening?

I also occasionally get an out of range error that might be related (I'm not certain how to produce it - seems to be when applying forces to objects that are currently touching the static object? Could be wrong there)

Code:
RangeError: Error #1005: Array index is not a positive integer (-1).
   at Error$/throwError()
   at Array()
   at Box2D.Dynamics::b2Island()[\Box2D\Dynamics\b2Island.as:136]
   at Box2D.Dynamics::b2World/Solve()[\Box2D\Dynamics\b2World.as:506]
   at Box2D.Dynamics::b2World/Step()[\Box2D\Dynamics\b2World.as:440]
Static objects can still have a velocity, which they will impart to things they touch. You need to reset this with SetLinearVeloicty and SetAngularVelocity

Not sure about 2. Don't call SetMass halfway through events.
BorisTheBrave wrote:
Static objects can still have a velocity, which they will impart to things they touch. You need to reset this with SetLinearVeloicty and SetAngularVelocity


Perfect, that did the trick. (I'm storing the velocities in userdata and reapply them afterwards)

Quote:
Not sure about 2. Don't call SetMass halfway through events.


Hm. I do my static-ifying and un-static-ifying at the beginning of my main program loop and is definitely removed from the STEP call. What other events might I be interrupting? Would turning off warm starts help?

After some testing, It only seems to occur when my player character hops around on top of the formerly-dynamic-but-now-static shapes. I think it might have to do with collision detection. I'll try to test and get something more specific.
I am using a custom ContactListener class that watches for collisions between the player's feet and the ground, and if so it simply sets a flag (no state modification or anything). If I place the character upon a dynamic surface and then turn the surface static, if my contactListener class fires the "remove" event (say, stepping off the now-static surface with the character) it will produce the above error 100% of the time. No ill effects on Add, Persist, or Result.

Removing my contactListener class makes the error disappear.

I'm thinking it's storing that it's "contacted" with a certain object, and upon removal it's erroring because the old object doesn't exist.... I think??

Is there a way to turn off contactlistening programatically for a particular frame while I do the dirty work?

(edited thrice)
sorry for the confusion i must have been thinking of sensor instead of static.
Try throwing a refilter in there.. I'm not 100% sure but it sounds like an island is being created between two dynamic objects on the add( when you first touched it ), then when it goes to remove that contact it no longer exists in the same way.

As for turning off contact listening for a certain frame, just put the contact listener to null for that frame and set it back afterwards. Not sure how that will help though.

Apologies for short reply I'm at work and pressed for time. I'll try to look into it more tonight if I can and if you haven't fixed it by then.

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