marciokoko
marciokoko

Reputation: 4986

box2d bodies fall up instead of down

Im working with cocos2d v1.0.1 & the respective Box2d version. In the simulator it all works. Its just a body created at a touch location that drops down to the ground. But I ran it on the device, iphone4, and the objects float upwards.

Why would this happen?

Gravity is set to -0.3f. I have another dynamic body in the scene and it appears on the bottom of the ground as it should. Its just the touch objects that float up. Sleep is True.

I just set sleep to false and now the rover also floats up. But it should not float up. here is my world creation method:

- (void)setupWorld {
    b2Vec2 gravity = b2Vec2(0.0f, -0.3f);
    bool doSleep = false;
    world = new b2World(gravity, doSleep);
}

and here is my body creation from init:

Box2DSprite *roverSprite = [Box2DSprite spriteWithSpriteFrameName:@"rover.png"];
        [self createBoxAtLocation:ccp(100,15) withSize:CGSizeMake(50, 50) forSprite:roverSprite isBox:TRUE];
        [sceneSpriteBatchNode addChild:roverSprite];

and here is the createBox method:

- (void)createBoxAtLocation:(CGPoint)location withSize:(CGSize)size forSprite:(Box2DSprite *)sprite isBox:(BOOL)isBox{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
    b2Body *body = world->CreateBody(&bodyDef);
    //
    body->SetUserData(sprite);
    sprite.body = body;


    b2FixtureDef fixtureDef;

    //
    if (isBox) {
        b2PolygonShape shape;
        shape.SetAsBox(sprite.contentSize.width/3/PTM_RATIO,
                       sprite.contentSize.height/3/PTM_RATIO);
        fixtureDef.shape = &shape;
    } else {
        b2CircleShape shape;
        shape.m_radius = sprite.contentSize.width/2/PTM_RATIO;
        fixtureDef.shape = &shape;
    }

    fixtureDef.density = 1.0;
    fixtureDef.friction = 1.0;
    fixtureDef.restitution = 0.5;
    body->CreateFixture(&fixtureDef);
}

The same method is also used for the touch created objects which are created from these lines:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];
    //b2Vec2 locationWorld = b2Vec2(touchLocation.x/PTM_RATIO, touchLocation.y/PTM_RATIO);

    Box2DSprite *sprite = [Box2DSprite spriteWithSpriteFrameName:@"koko1.png"];
    [self createBoxAtLocation:touchLocation withSize:CGSizeMake(50, 50) forSprite:sprite isBox:TRUE];
    [sceneSpriteBatchNode addChild:sprite];
    return TRUE;
}

Upvotes: 0

Views: 279

Answers (2)

marciokoko
marciokoko

Reputation: 4986

Turns out I had the Target Settings Rotation to Right instead of Left.

Upvotes: 0

Sylvan
Sylvan

Reputation: 536

Are you in portrait or landscape mode? If in landscape, perhaps you have the device rotated left instead of right. The device orientation can swap the y-values of the accelerometer on you. Not sure if this is related to your issue.

Upvotes: 1

Related Questions