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