I'm trying to add a sensor shape to a body that won't interfere with the body's mass or moment properties. I set the sensor shape's density to 0 but it's still affecting the body's movement. What am I missing here?
Massless sensor?
FlashUnless you are using obscure feature SetMassData, there is nothing you are missing. Adding massless sensors should have no effect.
Actually I would like to not be using setMassData explicitly, but I can only make this work when I do so. Let me explain.
I'm creating a circle shape that is the main shape for my body. This shape has the ability to grow or shrink in size in discreet steps over the course of the simulation. I'm including a larger circle shape to test for collisions and report back whether it is okay to grow in size or not.
Here is my code for the main fixture:
Okay so I'm using some of the m_ variables to setup the shape. I'd rather not as that seems a no-no so if there's a better way for that I'd like to hear it. I should note I'm using the recent alchemy build from mayo. Doing the above creates a fixture that affects the movement of my body whereas obviously I'd rather it just be massless and sense things.
The thing is, I can just create my own massData and fill it with the properties necessary for my main shape as follows:
With the above I am setting up the mass properties for my main fixture and explicitly ignoring the test shape. Then using setMassData I get the expected behavior. Of course I'd rather not do this as it is just messy. I just don't see why a fixture with zero density is contributing to the mass properties of my body. I hope this was clearly explained and appreciate any assistance.
I'm creating a circle shape that is the main shape for my body. This shape has the ability to grow or shrink in size in discreet steps over the course of the simulation. I'm including a larger circle shape to test for collisions and report back whether it is okay to grow in size or not.
Here is my code for the main fixture:
Code:
b2Def.fixture.shape = new b2CircleShape();
b2Def.fixture.shape.m_radius = RADIUS_MARBLE / Utils.physScale;
b2Def.fixture.density = Constants.DENSITY_MARBLE;
b2Def.fixture.friction = Constants.FRICTION_MARBLE;
ballFixture = b2Def.fixture.create( body );
And for the supposedly massless sensor fixture:b2Def.fixture.shape.m_radius = RADIUS_MARBLE / Utils.physScale;
b2Def.fixture.density = Constants.DENSITY_MARBLE;
b2Def.fixture.friction = Constants.FRICTION_MARBLE;
ballFixture = b2Def.fixture.create( body );
Code:
var fix:b2FixtureDef = new b2FixtureDef();
fix.density = 0;
fix.friction = 0;
fix.isSensor = true;
fix.filter.categoryBits = ballFixture.m_filter.categoryBits;
fix.filter.maskBits = ballFixture.m_filter.maskBits;
fix.shape = new b2CircleShape();
fix.shape.m_radius = testRadius / Utils.physScale;
(fix.shape as b2CircleShape).m_p.x = 0;
(fix.shape as b2CircleShape).m_p.y = ballFixture.GetShape().m_radius - testRadius / Utils.physScale;
stateTestFixture = fix.create( body );
fix.density = 0;
fix.friction = 0;
fix.isSensor = true;
fix.filter.categoryBits = ballFixture.m_filter.categoryBits;
fix.filter.maskBits = ballFixture.m_filter.maskBits;
fix.shape = new b2CircleShape();
fix.shape.m_radius = testRadius / Utils.physScale;
(fix.shape as b2CircleShape).m_p.x = 0;
(fix.shape as b2CircleShape).m_p.y = ballFixture.GetShape().m_radius - testRadius / Utils.physScale;
stateTestFixture = fix.create( body );
Okay so I'm using some of the m_ variables to setup the shape. I'd rather not as that seems a no-no so if there's a better way for that I'd like to hear it. I should note I'm using the recent alchemy build from mayo. Doing the above creates a fixture that affects the movement of my body whereas obviously I'd rather it just be massless and sense things.
The thing is, I can just create my own massData and fill it with the properties necessary for my main shape as follows:
Code:
var mass:b2MassData = new b2MassData();
mass.mass = ballFixture.GetDensity() * Math.PI * ballFixture.m_shape.m_radius * ballFixture.m_shape.m_radius; // d * PI * R^2
mass.I = Utils.QUARTER_PI * Math.pow( ballFixture.m_shape.m_radius, 4 ); // PI * R^4 / 4
mass.center.x = 0;
mass.center.y = 0;
body.SetMassData( mass );
mass.mass = ballFixture.GetDensity() * Math.PI * ballFixture.m_shape.m_radius * ballFixture.m_shape.m_radius; // d * PI * R^2
mass.I = Utils.QUARTER_PI * Math.pow( ballFixture.m_shape.m_radius, 4 ); // PI * R^4 / 4
mass.center.x = 0;
mass.center.y = 0;
body.SetMassData( mass );
With the above I am setting up the mass properties for my main fixture and explicitly ignoring the test shape. Then using setMassData I get the expected behavior. Of course I'd rather not do this as it is just messy. I just don't see why a fixture with zero density is contributing to the mass properties of my body. I hope this was clearly explained and appreciate any assistance.
Seems fine to me. I just meant if you were usign SetMassData instead of relying on density, then the mass gets reset each time you add a fixture. Doesn't seem to be your problem.
Original thread: https://box2d.org/forum/viewtopic.php?t=4888