0

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. 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.

BaSha
  • 2,356
  • 3
  • 21
  • 38

1 Answers1

0

The rotation property of a CCNode (and CCSprite, which inherits from CCNode) is measured in degrees, with clockwise rotation being positive. The Box2D world uses angles measured in radians, with counter-clockwise rotation being positive, which is more conventional for a cartesian coordinate system. In order to provide the correct angle to a Box2D function, you will have to convert. In Cocos2D, the conversion goes like this:

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.

Sylvan
  • 526
  • 3
  • 6