title: How do I accomplish proper trajectory with a Cocos2d-x node using Chipmunk 2D impulses and rotation?
body: <p>I'm building a game with Cocos2d-x version 3.13.1 and I've decided to go with the built-in physics engine (Chipmunk 2D) to accomplish animations and collision detection. I have a simple projectile called <code>BulletUnit</code> that inherits from <code>cocos2d::Node</code>. It has a child sprite that displays artwork, and a rectangular physics body with the same dimensions as the artwork.</p>
  
  <p>The <code>BulletUnit</code> has a method called <code>fireAtPoint</code>, which determines the angle between itself and the point specified, then sets the initial velocity based on the angle. On each update cycle, acceleration is applied to the projectile. This is done by applying impulses to the body based on an acceleration variable and the angle calculated in <code>fireAtPoint</code>. Here's the code:  </p>
  
  <pre><code>bool BulletUnit::init() {
    if (!Unit::init()) return false;
  
    displaySprite_ = Sprite::createWithSpriteFrameName(frameName_);
    this-&gt;addChild(displaySprite_);
  
    auto physicsBody = PhysicsBody::createBox(displaySprite_-&gt;getContentSize());
    physicsBody-&gt;setCollisionBitmask(0);
    this-&gt;setPhysicsBody(physicsBody);
  
    return true;
  }
  
  void BulletUnit::update(float dt) {
    auto mass = this-&gt;getPhysicsBody()-&gt;getMass();
    this-&gt;getPhysicsBody()-&gt;applyImpulse({
      acceleration_ * mass * cosf(angle_),
      acceleration_ * mass * sinf(angle_)
    });
  }
  
  void BulletUnit::fireAtPoint(const Point &amp;point) {
    angle_ = Trig::angleBetweenPoints(this-&gt;getPosition(), point);
  
    auto physicsBody = this-&gt;getPhysicsBody();
    physicsBody-&gt;setVelocityLimit(maxSpeed_);
    physicsBody-&gt;setVelocity({
      startingSpeed_ * cosf(angle_),
      startingSpeed_ * sinf(angle_)
    });
  }
  </code></pre>
  
  <p>This works exactly as I want it to. You can see in the image below, my bullets are accelerating as planned and traveling directly towards my mouse clicks.</p>
  
  <p><a href="https://i.stack.imgur.com/Wmp4C.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wmp4C.gif" alt="Physics body with no rotation applied."></a></p>
  
  <p><strong>But, there's one obvious flaw:</strong> the bullet is remaining flat instead of rotating to "point" towards the target. So, I adjust <code>fireAtPoint</code> to apply a rotation to the node. Here's the updated method:</p>
  
  <pre><code>void BulletUnit::fireAtPoint(const Point &amp;point) {
    angle_ = Trig::angleBetweenPoints(this-&gt;getPosition(), point);
  
    // This rotates the node to make it point towards the target
    this-&gt;setRotation(angle_ * -180.0f/M_PI);
  
    auto physicsBody = this-&gt;getPhysicsBody();
    physicsBody-&gt;setVelocityLimit(maxSpeed_);
    physicsBody-&gt;setVelocity({
      startingSpeed_ * cosf(angle_),
      startingSpeed_ * sinf(angle_)
    });
  }
  </code></pre>
  
  <p><strong>This almost works.</strong> The bullet is pointing in the right direction, but the trajectory is now way off and seems to be arcing away from the target as a result of the rotation: the more drastic the rotation, the more drastic the arcing. The following image illustrates what's happening:</p>
  
  <p><a href="https://i.stack.imgur.com/gg3N3.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gg3N3.gif" alt="Physics body with rotation applied, which messes up my trajectory!"></a></p>
  
  <p>So, it seems that setting the rotation is causing the physics engine to behave in a way I hadn't originally expected. I've been racking my brain on ways to correct the flight path, but so far, no luck! Any suggestions would be greatly apprecitated. Thanks!</p>