1
votes

Basically i am trying to apply impulse to a round body in a specific angle. But the body moves in a erratic direction. Can anyone please help me.

b2Vec2 v1 = bombbody->GetPosition();
b2Vec2 v2 = arrowPoint;//Value got from touch
b2Vec2 final = v2-v1;
float angle1 = CC_RADIANS_TO_DEGREES(atan2(final.y,final.x));
float factor = sqrt(final.x/PTM_RATIO * final.x/PTM_RATIO + final.y/PTM_RATIO * final.y/PTM_RATIO);
b2Vec2 ImpulseVector = b2Vec2(cos(angle1)*factor,sin(angle1)*factor);
b2Vec2 PointVector = bombbody->GetPosition();
bombbody->ApplyLinearImpulse(ImpulseVector,PointVector); 
1

1 Answers

0
votes

You should use b2Body::GetWorldCenter for the second parameter of b2Body::ApplyLinearImpulse:

  b2Vec2 PointVector = bombbody->GetWorldCenter();
  bombbody->ApplyLinearImpulse(ImpulseVector,PointVector); 

According to a post at Box2D forum, the reason why GetPosition doesn't work with force is that the force works with the body's center of mass, which might not always be the same as its position.