This code only worked with inbuilt physics. But i want to use box2d physics separately .
bool GameScene::onTouchBegan(Touch* touch, Event* event)
{
log("Touch called");
auto location = touch->getLocation();
auto arr = sceneWorld->getShapes(location);
PhysicsBody* body = nullptr;
for (auto& obj : arr)
{
log("Touch getting");
// if ((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0)
{
body = obj->getBody();
break;
}
}
if (body != nullptr)
{
log("Touch got");
Node* mouse = Node::create();
auto physicsBody = PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY);
physicsBody->setDynamic(false);
mouse->addComponent(physicsBody);
mouse->setPosition(location);
this->addChild(mouse);
PhysicsJointPin* joint = PhysicsJointPin::construct(physicsBody, body, location);
joint->setMaxForce(3000.0f * body->getMass());
sceneWorld->addJoint(joint);
_mouses.insert(std::make_pair(touch->getID(), mouse));
return true;
}
return false;
}
void GameScene::onTouchMoved(Touch* touch, Event* event)
{
auto it = _mouses.find(touch->getID());
if (it != _mouses.end())
{
it->second->setPosition(touch->getLocation());
}
}
void GameScene::onTouchEnded(Touch* touch, Event* event)
{
auto it = _mouses.find(touch->getID());
if (it != _mouses.end())
{
this->removeChild(it->second);
_mouses.erase(it);
//scheduleOnce(schedule_selector(GameContrler::checkTagetComplete),1);
}
}