Ramin Afshar
Ramin Afshar

Reputation: 1019

move a moving b2body to a specific position

I have a b2body that is moving. what I want is to move the body to a specific position and stop the body at that position, no matter how it's current vector is.

I tried calculating the vector between the 2 points and then do a ApplyLinearImpulse with the new vector but I can't seem to make it stop at the right position. Here's what I tried so far.

-(void) moveBodyToPoint {
    ball.body->SetLinearVelocity(b2Vec2(0,0)); // set to zero before applying the impulse
    CGPoint vec = CGPointMake(ball.position.x-point.position.x,ball.position.y-point.position.y);
    ball.body->ApplyLinearImpulse(b2Vec2(vec.x,vec.y), ball.body->GetWorldCenter());
}

Upvotes: 0

Views: 471

Answers (1)

North-Pole
North-Pole

Reputation: 630

What I use in my game, to simulate teleporter, is something like that:

ball.body->SetLinearVelocity( b2Vec2(0, 0) );
ball.body->SetTransform( destination, body.bBody.GetAngle() ); //destination I dont think require explaining
ball.body->SetAngularVelocity( 0 ); //might not need that, dependant on what you are doing

Remember that to call SetTransform you need to do it outside the box2d world step.

Upvotes: 1

Related Questions