0

我正在可视区域周围创建边框。在这个区域内,我正在创建其他为碰撞检测启用传感器的固定装置。似乎 isSensor = true 的固定装置通过了窗口边框。我怎样才能防止这种情况发生?谢谢!

    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
    b2Body *body = world->CreateBody(&bodyDef);

    // Define another box shape for our dynamic body.
    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box

    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox; 
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    fixtureDef.isSensor = true; //causes fixtures to fall through border
    body->CreateFixture(&fixtureDef);

窗口边框:

 CGSize screenSize = [CCDirector sharedDirector].winSize;
 float widthInMeters = screenSize.width / PTM_RATIO;
 float heightInMeters = screenSize.height / PTM_RATIO;
 b2Vec2 lowerLeftCorner = b2Vec2(0, 0);
 b2Vec2 lowerRightCorner = b2Vec2(widthInMeters, 0);
 b2Vec2 upperLeftCorner = b2Vec2(0, heightInMeters);
 b2Vec2 upperRightCorner = b2Vec2(widthInMeters, heightInMeters);

b2BodyDef screenBorderDef;
screenBorderDef.position.Set(0, 0);
b2Body* screenBorderBody = world->CreateBody(&screenBorderDef);
b2EdgeShape screenBorderShape;

screenBorderShape.Set(lowerLeftCorner, lowerRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(lowerRightCorner, upperRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperRightCorner, upperLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperLeftCorner, lowerLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
4

2 回答 2

1

将其设置为 false 并在 PostCollision 方法中检查碰撞

于 2013-11-15T06:05:57.230 回答
0

一种方法是向上面有传感器的主体添加第二个固定装置,并设置碰撞过滤器,使其仅与边界发生碰撞。您将需要了解一些有关如何使用碰撞过滤器设置的知识,一开始可能会有些棘手。这可能会有所帮助:http ://www.iforce2d.net/b2dtut/collision-filtering

默认情况下,类别位值为 1,因此除非您更改任何内容,否则场景中的所有灯具都具有类别 1。要区分边框和其他灯具,您必须为它们指定不同的类别。假设您制作了 2 类地面夹具:

screenBorderFixtureDef.filter.categoryBits = 2;

掩码的默认值为 0xFFFF,因此所有现有的固定装置仍会像以前一样与边框发生碰撞,即使在这样更改类别时也是如此。然后要使新添加的第二个夹具忽略除边框以外的所有设备,您可以将遮罩设置为仅与边框碰撞:

fixtureDef.filter.maskBits = 2;


另一种方法是使现有的传感器夹具不是传感器,使其与边界发生碰撞。但当然,它也会与其他一切发生冲突。您可以使用联系人侦听器的 PreSolve 回调来表示某些联系人不应进行任何碰撞响应:

//in PreSolve
if ( this is NOT a contact between border and sensor )
    contact->SetEnabled( false );
于 2013-11-15T01:40:49.167 回答