Creating a mouse joint
Since the catapult is a physics body constrained by a revolute joint with a motor and limits enabled, it would be rather complicated to make it move in a non-physics way. Thus, we use the b2MouseJoint
class provided by Box2D, which is specifically designed for a situation like ours. So, let's define the creation of the b2MouseJoint
class in the ccTouchesBegan
function in GameWorld.cpp
:
void GameWorld::ccTouchesBegan(CCSet* set, CCEvent* event) { if(is_catapult_ready_ && !mouse_joint_) { CCTouch* touch = (CCTouch*)(*set->begin()); CCPoint touch_point = touch->getLocationInView(); touch_point = CCDirector::sharedDirector()-> convertToGL(touch_point); // convert from screen to physics co-ordinates b2Vec2 touch_world_point = b2Vec2(SCREEN_TO_WORLD(touch_point.x), SCREEN_TO_WORLD(touch_point.y)); // only accept touches to the left of the catapult if(touch_world_point.x < catapult_body_->GetPosition().x) ...