Rony
Rony

Reputation: 1229

Box2d:apply velocity in a direction

I apply impulse in an object in box2d iPhone app and now want to increase its speed in particuler direction....i mean i need two thing

1.through the object in a direction 2.increase speed

plz help..

Upvotes: 3

Views: 3418

Answers (2)

Liolik
Liolik

Reputation: 801

b2Vec2 vector = self.speed * b2Vec2(cos(angle), sin(angle));
self.yourbodyBody->SetLinearVelocity(vector);
[self schedule:@selector(increaseSpeed) interval:0.1];

- (void)increaseSpeed
{
    self.speed += 0.01;
    float angle = self.yourbodyBody->GetAngle();
    b2Vec2 vector = self.speed * b2Vec2(cos(angle), sin(angle));
    self.yourbodyBody->SetLinearVelocity(vector);
}

Upvotes: 0

Felixyz
Felixyz

Reputation: 19143

b2Vec2 force = b2Vec2(xAcceleration, yAcceleration);
force *= dt;  // Use this if your game engine uses an explicit time step
b2Vec2 p = myObjectBody->GetWorldPoint(b2Vec2(0.0f, 0.0f));
body->ApplyForce(force, p);

By modifying xAcceleration and yAcceleration, you can make the object move with various speeds in different directions. (If you calculate angles, you might want to use force.Normalize(); and then multiply by a velocity.)

Upvotes: 2

Related Questions