body expansion -> mass troubles

Hello, i add at runtime an second shape to my body... after then i call 'body.SetMassFromShapes()'... the 'body.getMass()' trace before and behind the 'body.SetMassFromShapes()' execute are differentially -> and shows that the new mass ist correctly calculated.
But the physical body mass behave ist unchanged! 😕

how ist that possible ❓

thanks for help!
Hello, i think i detect the base problem in the physic engine self...

b2Body::SetMassFromShapes()
Code:
var oldType:int = m_type;
if (m_invMass == 0.0 && m_invI == 0.0)
{
   m_type = e_staticType;
}
else
{
   m_type = e_dynamicType;
}

// If the body type changed, we need to refilter the broad-phase proxies.
if (oldType != m_type)
{
   for (s = m_shapeList; s; s = s.m_next)
   {
      s.RefilterProxy(m_world.m_broadPhase, m_xf);
   }
}

The problem is that old and new type always dynamic, and therefore b2Shape.RefilterProxy never execute.
But without refilter the mass behave is wrong, when i overtake the if condition and run the for loop -> the mass behave is totally correct!

Is that an engine bug or what make i wrong?

thanks
Hmm, proxy refiltering shouldn't have anything to do with mass, it only tells the broadphase (which checks for spatial overlaps) to update itself based on new positions.

Would it be possible to post some example code that demonstrates the problem you're seeing? If that's not possible or it would be too much code, could you tell us what the shapes you're adding to your body are and the masses that are reported?
Not true, ewjordan: as refilter deletes and recreates a contact, it refreshes some cached values. Not sure mass is one of them, bit it could be.

That said, I see nothing with the quoted code. I don't understand your original problem either, if getMass reports a change, what makes you think that the body's mass is not changing?
BorisTheBrave wrote:
Not true, ewjordan: as refilter deletes and recreates a contact, it refreshes some cached values. Not sure mass is one of them, bit it could be.

Ah, good point, I forgot that the broadphase changes automatically push back on the rest of the engine through the contacts.

Indeed, it looks like contact constraint points do hold mass values; however, a brief look doesn't indicate that these are cached step to step, they seem to be re-set right before they are used. It's possible that I'm missing something, though.

In any case, yeah, it would be good to know what the perceived problem with the masses is, I don't quite get it from the explanation. I'm not sure what it means to say that the physical body mass behaves like it's unchanged while the calculated mass seems right - even if values are being cached somewhere improperly, I can't imagine it would make that much of a visual difference in any but the most contrived of situations (we should still track it down if it's happening, of course!).
Hello, general: i test the mass behave with an see-saw...

I find out the reason of my problem... in my case is the b2World doSleep property on -> and that is the problem. When my see-saw plank sleep and an body on it change his mass -> nothing happens...

It's my mistake, for this usage i musst set sleeping to false 😉

You can test it with this source sample...
Usage: on press any key the right element on the see-saw become an new shape (mass change).
Case 1: press any key before the see-saw sleep (sleep == debug draw red) -> you will see: the mass behave is correct.
Case 2: press any key when the see-saw sleep -> you will see: nothing happens.

Code:
package
{
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;
   import Box2D.Dynamics.*;
   
   import flash.display.*;
   import flash.events.*;

   [SWF(width="800", height="600", backgroundColor="0x333333", frameRate="30")]
   public class Sample extends Sprite
   {
      private var _mWorld:b2World;
      private var _mIterations:int = 10;
      private var _mTimeStep:Number = 1.0/30.0;
      private var _mPhysScale:Number = 30.0;
      
      private var _rightElement:b2Body;
      
      public function Sample()
      {
         //world
         var worldAABB:b2AABB = new b2AABB();
            worldAABB.lowerBound.Set(-100, -100);
            worldAABB.upperBound.Set(100.0, 30.0);
         _mWorld = new b2World(worldAABB, new b2Vec2(0.0, 10.0), true);
         
         //debug draw
         var dbgDraw:b2DebugDraw = new b2DebugDraw();
         var dbgSprite:Sprite = new Sprite();
         addChild(dbgSprite);
         dbgDraw.m_sprite = dbgSprite;
         dbgDraw.m_fillAlpha = 0.3;
         dbgDraw.m_lineThickness = 1.0;
         dbgDraw.m_drawScale = _mPhysScale;
         dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
         _mWorld.SetDebugDraw(dbgDraw);
         
         //ground
         var boxDef:b2PolygonDef = new b2PolygonDef();
            boxDef.SetAsBox(200/_mPhysScale, 20/_mPhysScale);
            boxDef.friction = 1.0;
            boxDef.density = 0.0;
         var bodyDef:b2BodyDef = new b2BodyDef();
            bodyDef.position.Set( 300/_mPhysScale, 300/_mPhysScale);
            bodyDef.angle = 0;
         var body:b2Body = _mWorld.CreateBody(bodyDef);
            body.CreateShape(boxDef);
            body.SetMassFromShapes();
            
         //listener
         stage.addEventListener(Event.ENTER_FRAME, render);
         stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
         
         //create elements
         drawSeeSaw();
         drawLeftElement();
         drawRightElement();
      }
      
      private function render( event:Event ):void
      {
         _mWorld.Step(_mTimeStep, _mIterations);
      }
      
      private function drawSeeSaw():void
      {
         //box
         var boxDef:b2PolygonDef = new b2PolygonDef();
         boxDef.SetAsBox(5/_mPhysScale, 5/_mPhysScale);
         boxDef.density = 10.0
         boxDef.friction = 0.3;
         boxDef.restitution = 0.0;
         var bodyDef:b2BodyDef = new b2BodyDef();
         bodyDef.position.Set( 300/_mPhysScale, 275/_mPhysScale);
         bodyDef.angle = 0;
         var body:b2Body = _mWorld.CreateBody(bodyDef);
         body.CreateShape(boxDef);
         body.SetMassFromShapes();
            
         //plank
         boxDef = new b2PolygonDef();
         boxDef.SetAsBox(150/_mPhysScale, 5/_mPhysScale);
         boxDef.density = 1.0
         boxDef.friction = 0.3;
         boxDef.restitution = 0.0;
         bodyDef = new b2BodyDef();
         bodyDef.position.Set( 300/_mPhysScale, 265/_mPhysScale);
         bodyDef.angle = 0;
         body = _mWorld.CreateBody(bodyDef);
         body.CreateShape(boxDef);
         body.SetMassFromShapes();
      }
      
      private function drawLeftElement():void
      {
         var boxDef:b2PolygonDef = new b2PolygonDef();
         boxDef.SetAsBox(20/_mPhysScale, 12/_mPhysScale);
         boxDef.density = 3.0
         boxDef.friction = 0.3;
         boxDef.restitution = 0.2;
         var bodyDef:b2BodyDef = new b2BodyDef();
         bodyDef.position.Set( 180/_mPhysScale, 245/_mPhysScale);
         bodyDef.angle = 0;
         var body:b2Body = _mWorld.CreateBody(bodyDef);
         body.CreateShape(boxDef);
         body.SetMassFromShapes();
      }
      
      private function drawRightElement():void
      {
         var boxDef:b2PolygonDef = new b2PolygonDef();
         boxDef.SetAsBox(20/_mPhysScale, 10/_mPhysScale);
         boxDef.density = 3.0
         boxDef.friction = 0.3;
         boxDef.restitution = 0.2;
         var bodyDef:b2BodyDef = new b2BodyDef();
         bodyDef.position.Set( 420/_mPhysScale, 245/_mPhysScale);
         bodyDef.angle = 0;
         var body:b2Body = _mWorld.CreateBody(bodyDef);
         body.CreateShape(boxDef);
         body.SetMassFromShapes();
         
         _rightElement = body;
      }
      
      private function onKeyDown( event:KeyboardEvent ):void
      {
         var boxDef:b2PolygonDef = new b2PolygonDef();
         boxDef.SetAsOrientedBox(20/_mPhysScale, 10/_mPhysScale, new b2Vec2(0, -15/_mPhysScale), 10);
         boxDef.density = 3.0
         boxDef.friction = 0.3;
         boxDef.restitution = 0.2;
         
         _rightElement.CreateShape(boxDef);
         trace("before: "+_rightElement.GetMass());
         _rightElement.SetMassFromShapes();
         trace("behind: "+_rightElement.GetMass());
      }
   }
}

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