施加脉冲时身体不稳定运动(Cocos2d-Box2d)

发布于 2024-10-04 04:12:23 字数 533 浏览 0 评论 0原文

基本上我试图以特定的角度对圆形物体施加脉冲。 但身体却朝着不稳定的方向移动。 任何人都可以帮助我吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-10-11 04:12:23

您应该使用 b2Body::GetWorldCenter 作为 b2Body::ApplyLinearImpulse 的第二个参数:

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

根据 在 Box2D 论坛上发帖GetPosition 不能与强制一起使用的原因是强制起作用与身体的质心,它可能并不总是与其位置相同。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文