Sensor attached to a dynamic body?

Hi,

Can you attach sensors to dynamic bodies? I tried to attach a sensor to the front of a top down car, and all my forward motion dissapeared - which was working before I attached the sensor type fixturedefs?

Regards

i
Yes, you should be able to do this. If solid fixtures have non-zero density, you can give the sensor fixture a zero density. Then it will not affect the motion.
Thanks, but it doesnt work.

When its a sensor the car cant seem to turn any more.

Regards

i
When sensor with zero density:
http://www.youtube.com/watch?v=cvlS_aqRWVg

when dynamic
http://www.youtube.com/watch?v=iQ3OgLcq55Q

Code:
//muzzzles
      // left muzzle
      b2BodyDef leftMuzzleDef;
      leftMuzzleDef.type = b2_dynamicBody;
      
      x = carPosition.x - bodyWidth/kPhysicsPTMRatio + ((bodyWidth/kPhysicsPTMRatio)/3.f);
      y = carPosition.y + bodyLength/kPhysicsPTMRatio + muzzleLength/kPhysicsPTMRatio;
      leftMuzzleDef.position.Set(x,y);
      
      leftMuzzle = world->CreateBody(&leftMuzzleDef);
      
      b2PolygonShape leftMuzzleShapeDef;
      leftMuzzleShapeDef.SetAsBox(muzzleWidth/kPhysicsPTMRatio,muzzleLength/kPhysicsPTMRatio);
      b2FixtureDef leftMuzzleFixtureDef;
      leftMuzzleFixtureDef.shape = &leftMuzzleShapeDef;
      leftMuzzleFixtureDef.density = 0.F;
      
      leftMuzzleFixtureDef.isSensor = true;
      
      
      
      
      leftMuzzle->CreateFixture(&leftMuzzleFixtureDef);
      
      
      b2PrismaticJointDef leftMuzzleJointDef;
      leftMuzzleJointDef.Initialize(body, leftMuzzle, leftMuzzle->GetWorldCenter(),wheelAngle);
      leftMuzzleJointDef.enableLimit = true;
      leftMuzzleJointDef.lowerTranslation = 0;
      leftMuzzleJointDef.upperTranslation = 0;
      
      leftMuzzleJoint = (b2PrismaticJoint*) world->CreateJoint(&leftMuzzleJointDef);
      
      leftMuzzleNode = [[TopDownCarMuzzleNode alloc] initWithBody:leftMuzzle game:game_];
      leftMuzzleNode.scale = 0.3;
      [game_ addBodyNode:leftMuzzleNode z:3];
      
      
      // right muzzle
      b2BodyDef rightMuzzleDef;
      rightMuzzleDef.type = b2_dynamicBody;
      
      x = carPosition.x + bodyWidth/kPhysicsPTMRatio - ((bodyWidth/kPhysicsPTMRatio)/3.f);
      y = carPosition.y + bodyLength/kPhysicsPTMRatio + muzzleLength/kPhysicsPTMRatio;
      rightMuzzleDef.position.Set(x,y);
      
      rightMuzzle = world->CreateBody(&rightMuzzleDef);
      
      b2PolygonShape rightMuzzleShapeDef;
      rightMuzzleShapeDef.SetAsBox(muzzleWidth/kPhysicsPTMRatio,muzzleLength/kPhysicsPTMRatio);
      b2FixtureDef rightMuzzleFixtureDef;
      rightMuzzleFixtureDef.shape = &rightMuzzleShapeDef;
      rightMuzzleFixtureDef.density = 0.F;
      
      rightMuzzleFixtureDef.isSensor = true;
      
      rightMuzzle->CreateFixture(&rightMuzzleFixtureDef);
      
      
      b2PrismaticJointDef rightMuzzleJointDef;
      rightMuzzleJointDef.Initialize(body, rightMuzzle, rightMuzzle->GetWorldCenter(),wheelAngle);
      rightMuzzleJointDef.enableLimit = true;
      rightMuzzleJointDef.lowerTranslation = 0;
      rightMuzzleJointDef.upperTranslation = 0;
      
      rightMuzzleJoint = (b2PrismaticJoint*) world->CreateJoint(&rightMuzzleJointDef);
      
      rightMuzzleNode = [[TopDownCarMuzzleNode alloc] initWithBody:rightMuzzle game:game_];
      rightMuzzleNode.scale = 0.3;
      [game_ addBodyNode:rightMuzzleNode z:3];
      

Don't set the density of the sensor to zero. Things without density don't turn at all.
Erin Catto wrote:
Yes, you should be able to do this. If solid fixtures have non-zero density, you can give the sensor fixture a zero density. Then it will not affect the motion.


You said to add a zero density to the sensor before?? I am confused now (easily done!!). Based on the quote above I set the density to zero as you suggested.

These muzzles are very small objects attached to a front of a car, why would setting these as sensors with zero density effect the whole car and prevent the whole car from moving? The force is applied through the wheels, these sensors I just want to be attached to the body as a convenience for sending out bullets.

Regards

i
You can have a body with multiple fixtures. Some fixtures be sensors while some can be solid. A dynamic body just needs one fixture with non-zero density, the rest can be zero. It is up to you to decide how to distribute the mass.
Hi,


I am not using multiple fixtures per body. They are all separate bodies.

I have a car, all separate dynamic bodies with mass (i.e. not multiple fixtures per body). 2 front wheels attached to the car body with revolute joints, two rear wheels attached with prismatic joints. It all works beautifully until I attach a sensor to the body. Regardless of whether the sensor has density or not, the whole car stops responding to steering.

If what you are saying is, it's OK to create a body of multiple fixtures and one be a sensor...this is not my situation.

Regards

i
So if you don't set the sensor flag, your system works correctly? That would be likely be a bug in Box2D.
Yes, look at the two you tube videos, until I attach the sensor via prismatic - it works fine.

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