简体   繁体   English

在Box2D游戏中以特定角度抛出b2body的问题

[英]Issue with throwing b2body at particular angle in Box2D game

in my game im having one Ccsprite for arrow, and one b2body for ball... im trying to throw ball at direction which is pointed by my arrow sprite. 在我的游戏中,我有一个箭头Ccsprite和一个b2body球...我试图朝我的箭头精灵指向的方向投球。 here is my code... i'm counting rotation of arrow sprite and then applying impulse to ball at that angle... 这是我的代码...我正在计算箭头精灵的旋转,然后以那个角度对球施加脉冲...

float totalRotation = arrow.rotation ;

ballBody->ApplyLinearImpulse(b2Vec2(10.0f+cos(totalRotation)*25.0f,10.0f+sin(totalRotation)*25.0f), eggBody->GetWorldCenter());

BUt, this not working exactly...ball is getting thrown in improper direction. 但是,这不能完全正常工作……球正朝不正确的方向扔。

The rotation property of a CCNode (and CCSprite, which inherits from CCNode) is measured in degrees, with clockwise rotation being positive. CCNode(以及从CCNode继承的CCSprite)的rotation属性以度为单位,顺时针旋转为正。 The Box2D world uses angles measured in radians, with counter-clockwise rotation being positive, which is more conventional for a cartesian coordinate system. Box2D世界使用以弧度为单位的角度,逆时针旋转为正,这在笛卡尔坐标系中更为常见。 In order to provide the correct angle to a Box2D function, you will have to convert. 为了为Box2D函数提供正确的角度,您必须进行转换。 In Cocos2D, the conversion goes like this: 在Cocos2D中,转换是这样的:

float angle = - 1 * CC_DEGREES_TO_RADIANS(totalRotation);

The macro converts the totalRotation from degrees to radians, and you multiply by -1 because Box2D measures positive angles in the counter-clockwise direction, which is opposite of the CCNode rotation. 宏将totalRotation从度转换为弧度,并且乘以-1,因为Box2D在逆时针方向上测量正角,这与CCNode旋转相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM