简体   繁体   中英

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

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.

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