简体   繁体   中英

B2body moving up when hitting sensor rather than walking on straight path

I have some insects walking on path i have added sensor as well. when it collide with body it destroys the body and thats working fine.

But when ball hit sensor of insects than sometimes my insect body start moving upwards.

Before Hitting the insect

在此处输入图片说明

After Hitting the insect

在此处输入图片说明

here is my insect body code implementation

    -(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;
}

Do i have to change density, friction or restitution to make it work?

This one is simple my friend.... I was Having the same problem...

Go to contact Listener class...

Do this as it is necessary

 if ((bodyA->GetFixtureList()->GetFilterData().categoryBits == kCategoryBitsCharacter || bodyA->GetFixtureList()->GetFilterData().categoryBits == kCategoryBitsBurger) && (bodyB->GetFixtureList()->GetFilterData().categoryBits == kCategoryBitsBurger || bodyB->GetFixtureList()->GetFilterData().categoryBits == kCategoryBitsCharacter)) {
    contact->SetEnabled(false);
}

It helped in mycase... hope help in yours too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM