0

道を歩いている昆虫もいます。センサーも追加しました。体に衝突すると体が破壊され、うまく機能します。

しかし、ボールが昆虫のセンサーに当たると、昆虫の体が上向きに動くことがあります。

虫に当たる前に

ここに画像の説明を入力

虫に当たった後

ここに画像の説明を入力

ここに私の昆虫体コードの実装があります

    -(b2Body *) createMovingAntObstacle  :(int) sPosX startPositionY :(int) sPosY transitionTime:(float)speed
{
    NSLog(@"TAG_ANT");
    //set this to avoid updating this object in the tick schedule
    _currentAnimatedSprite.userData = (void *)YES;

    b2BodyDef bodyDef_Ant;
    bodyDef_Ant.type = b2_dynamicBody;
    CGPoint startPos = ccp(520,winSize.height/7);
    bodyDef_Ant.position = [self toMeters:startPos];
    bodyDef_Ant.userData = _currentAnimatedSprite;
    bodyDef_Ant.gravityScale = 0;

    b2PolygonShape dynamicBox;

    int num = 5;
    b2Vec2 verts[] = {
        b2Vec2(-19.0f / PTM_RATIO, 1.4f / PTM_RATIO),
        b2Vec2(-18.5f / PTM_RATIO, -4.0f / PTM_RATIO),
        b2Vec2(8.8f / PTM_RATIO, -3.7f / PTM_RATIO),
        b2Vec2(7.9f / PTM_RATIO, 2.1f / PTM_RATIO),
        b2Vec2(-18.5f / PTM_RATIO, 1.8f / PTM_RATIO)
    };

    dynamicBox.Set(verts, num);

    b2FixtureDef fixtureDef_Ant;
    fixtureDef_Ant.shape = &dynamicBox;
    fixtureDef_Ant.friction = 0.7;
    fixtureDef_Ant.density = 10.0f;
    fixtureDef_Ant.restitution = 0.7;

    antBody = world->CreateBody(&bodyDef_Ant);
    antBody->CreateFixture(&fixtureDef_Ant);

    /*** Begin Tutorial Code ***/
    b2Filter defaultFilter = antBody->GetFixtureList()[0].GetFilterData();

    defaultFilter.groupIndex = k_indexD; // Negative index (Never collides with same Index group objects)
    defaultFilter.categoryBits = k_dCategory;
    defaultFilter.maskBits = k_dMask; // HITS EVERYTHING EXCEPT A & C

    antBody->GetFixtureList()[0].SetFilterData(defaultFilter);

    float padding = 1.0f;

    // RIGHT SENSOR

    b2PolygonShape shape2;

    int num2= 4;
    b2Vec2 vertices2[] = {
        b2Vec2( (_currentAnimatedSprite.contentSize.width / 2 ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / 20 ) / PTM_RATIO), //top right corner (borders source image)
        b2Vec2( (_currentAnimatedSprite.contentSize.width / 2 ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / -4 )/ PTM_RATIO), //bottom right corner (borders source image)
        b2Vec2( (_currentAnimatedSprite.contentSize.width / 1.9  * padding ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / -4 )/ PTM_RATIO), //bottom right corner (farthest out
        b2Vec2( (_currentAnimatedSprite.contentSize.width / 1.9 * padding ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / 20 ) / PTM_RATIO) //top right
    };

    shape2.Set(vertices2, num2);

    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef2;
    fixtureDef2.shape = &shape2;
    fixtureDef2.density = 0.0f;
    fixtureDef2.friction = 0.0f;
    fixtureDef2.restitution =  0.0;
    fixtureDef2.isSensor = YES;
    fixtureDef2.userData = (SensorTypes*) rightSensor;

    antBody-> CreateFixture(&fixtureDef2);

    //add a new fixture as LEFT sensor

    b2PolygonShape shape3;


    int num3 = 4;
    b2Vec2 vertices3[] = {
        b2Vec2( (_currentAnimatedSprite.contentSize.width / -2 ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / -4 ) / PTM_RATIO), //bottom left corner (borders source image)
        b2Vec2( (_currentAnimatedSprite.contentSize.width / -2 ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / 20 ) / PTM_RATIO), //top left corner (borders source image)
        b2Vec2( (_currentAnimatedSprite.contentSize.width / 1.9 * -padding ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / 20 ) / PTM_RATIO), //top left corner
        b2Vec2( (_currentAnimatedSprite.contentSize.width / 1.9 * -padding ) / PTM_RATIO, (_currentAnimatedSprite.contentSize.height / -4 ) / PTM_RATIO), //bottom left corner
    };

    shape3.Set(vertices3, num3);

    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef3;
    fixtureDef3.shape = &shape3;
    fixtureDef3.density = 0.1f;
    fixtureDef3.isSensor = YES;
    fixtureDef3.userData = (SensorTypes*) leftSensor;

    antBody-> CreateFixture(&fixtureDef3);

    VAMovingObstacle* moveableObject = [[VAMovingObstacle alloc] init];
    moveableObject.startPoint = ccp(sPosX,sPosY);
    moveableObject.endPoint = ccp(-100,sPosY);
    moveableObject.transitionTime = speed;
    moveableObject.breakTime = 1.0;
    moveableObject.obstacleSprite = _currentAnimatedSprite;
    moveableObject.physicalBody = antBody;
    [moveableObject addAnimation:_currentWalkAction];
    [moveableObject addMovement];

    if (!movingObstacles_Ant) {
        movingObstacles_Ant = [[NSMutableArray alloc] init];
    }
    [movingObstacles_Ant addObject:moveableObject];
    //[moveableObject release];
    return antBody;
}

機能させるために、密度、摩擦、または反発を変更する必要がありますか?

4

1 に答える 1