1
votes

I have a box2d object that is being moved down the screen via gravity

int32 velocityIterations = 6;
int32 positionIterations = 2;

self.world->Step(dt, velocityIterations, positionIterations);
self.world->ClearForces();

for(b2Body *b = self.world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) {

        id object = (id)b->GetUserData();

        if([object isKindOfClass:[FallingObject class]])
        {
            CCSprite *sprite = (CCSprite *)b->GetUserData();
            sprite.position = CGPointMake(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
            sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
        }
    }
}

When the user moves their finger across the screen either left or right i want to move the box2d object left or right while the object is still moving down the screen.

Can anyone suggest the best way to do this. I have tried applying linear velocity but it just seems to shoot of screen.

Any suggestions

Thanks

1

1 Answers

2
votes

There some ways to do this, and you need to try the best for your case.

You can apply forces, impulse, or change the body velocity manually just for X parameter:

// x axis force
b2Vec2 xAxisForce = b2Vec2(10, 0);

// Try one of these
b->ApplyForce(xAxisForce, b->GetWorldCenter());
b->ApplyForceToCenter(xAxisForce);
b->ApplyLinearImpulse(xAxisForce, b->GetWorldCenter());

// Or change the body velocity manually
b->SetLinearVelocity(b2Vec2(10, b->GetLinearVelocity().y));