Sensor attached to a dynamic body?

That is not what I meant. Try running the same model without setting the sensor _flag_.

It is not surprising that adding a body and a joint to your model changes the behavior. You seem to blame this on the sensor flag, but I think that is likely not the problem. An added body and joint affects the dynamics just as much, sensor or not. Try hooking up the Box2D debug draw so you can see what that extra body is doing.

The only time a sensor does not affect the dynamics is when you add a zero density sensor fixture to an existing body.
Hi,

The only difference between my car working and not working is when the sensor property is set. I can attach the bodies as dynamic bodies and the car functions perfectly, the dynamics are not altered at all because the objects are so small (gun muzzles).

I change these small objects to be sensors because I don't want them to be pushed back into the car by the soft joints during impacts, and the dynamics of the car change.

The fact that the car behaves perfectly before I change them to sensors, leads me to believe this is not a dynamics problem....if it was a dynamics problem caused because of the attachment of the bodies - I would see the effect before I set them to sensors?

I have all the elements of the car set to a negative collision group, because initially the gun muzzles were stopping the wheels turning...whether this is causing any kind of misbehaviour I am not sure. But it all works perfectly before isSensor = true is set.

Regards

i
....in fact Erin,

It is the combination of isSenor = true with a zero density which causes the problem. Setting it as a sensor but leaving the density as 0.3, the car still behaves. When I set its density to zero - the car dynamics change. I changed it to zero on your recommendation for sensors-but you may have given me that advice thinking I was attaching the sensor as part of multiple fixtures into a single body?

Here is a video link showing the code changes and the result:

http://www.youtube.com/watch?v=LGiV-U-0Vm8

Regards

i
If you create a dynamic body with all fixtures having zero density, you will get strange behavior. Don't do it.

Your sensor configuration is unusual. The typical use of sensors is to use multiple fixtures on a body, with some being solid and others being sensors. When you said you attached a sensor to your car, I assumed you added the sensor fixture to an existing body.

Why don't you attach the sensor fixture to an existing body?
Hi,

Thanks. I will try that.

If I create a sensor which is to sit on its own, say for example a puddle on the floor which when the floor collides with it it reduces the cars friction...what should you do on this stand alone sensor if you cant set a dynamic body to zero density. Shall I just give it density and set it to a sensor and then it will behave as it needs to?

Regards

i
If the puddle doesn't move, you can create a static body with a sensor.
Thanks Erin
Hi,

I have a car body. I need to attach a sensor to the front. For architectural reasons I need the sensor not to be part of the main car body but attached to it. So based on this thread, I didnt want to create just a sensor body and attach it, as you advised having a sensor on its own, with zero density is what was causing me the problem.

So instead of creating just a sensor and attaching it with a joint to the car body, i am adding a dummy fixture to the sensor body with a density, which isn't a sensor.

This seems to work OK now.

Code:
_collisionRadius = (bodyLength/kPhysicsPTMRatio) * 2.f;
      

               // create a sensor we can attach via a joint to the main car body to aid steering and obstacle avoidance
      b2BodyDef radiusSensorDef;
      x = carPosition.x;
      y = carPosition.y;
      radiusSensorDef.position.Set(x,y);
                radiusSensorDef.type = b2_dynamicBody;
      _radiusSensorBody = world->CreateBody(&radiusSensorDef);
      
      
      
      
      b2CircleShape radiusSensorShapeDef;
      radiusSensorShapeDef.m_radius = _collisionRadius;
      b2FixtureDef radiusSensorShapeFixtureDef;
      radiusSensorShapeFixtureDef.shape = &radiusSensorShapeDef;
      radiusSensorShapeFixtureDef.isSensor = true;
      _radiusSensorBody->CreateFixture(&radiusSensorShapeFixtureDef);
      



      //dummy non sensor fixture def to try prevent car from not moving at all

      b2CircleShape radiusDummySensorShapeDef;
      radiusDummySensorShapeDef.m_radius = 1.f/kPhysicsPTMRatio;
      b2FixtureDef radiusDummySensorShapeFixtureDef;
      radiusDummySensorShapeFixtureDef.shape = &radiusDummySensorShapeDef;
      radiusDummySensorShapeFixtureDef.density = 0.1;
      radiusDummySensorShapeFixtureDef.friction = 0.1;
      radiusDummySensorShapeFixtureDef.isSensor = false;
      _radiusSensorBody->CreateFixture(&radiusDummySensorShapeFixtureDef);
      
      // add the sensor to the main car body
      b2PrismaticJointDef radiusSensorJointDef;
      radiusSensorJointDef.Initialize(body, _radiusSensorBody, body->GetWorldCenter(),wheelAngle);
      radiusSensorJointDef.enableLimit = true;
      radiusSensorJointDef.lowerTranslation = 0;
      radiusSensorJointDef.upperTranslation = 0;
      _radiusSensorJoint = (b2PrismaticJoint*) world->CreateJoint(&radiusSensorJointDef);
      
      _radiusSensor = [[RadiusSensor alloc] initWithBody: _radiusSensorBody game:aGame nodeToAttach:self];
      [aGame addBodyNode:_radiusSensor z:20];
      [_radiusSensor release];



Regards

i

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