施加脉冲时身体不稳定运动(Cocos2d-Box2d)
基本上我试图以特定的角度对圆形物体施加脉冲。 但身体却朝着不稳定的方向移动。 任何人都可以帮助我吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
b2Body::GetWorldCenter
作为b2Body::ApplyLinearImpulse
的第二个参数:根据 在 Box2D 论坛上发帖,
GetPosition
不能与强制一起使用的原因是强制起作用与身体的质心,它可能并不总是与其位置相同。You should use
b2Body::GetWorldCenter
for the second parameter ofb2Body::ApplyLinearImpulse
: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.