I set physics body to drawNode when touch ended but it does not fall down and it does not collide with the edge
It’s my code:
#include “MainScene.h”
#include “SimpleAudioEngine.h”
USING_NS_CC;
Scene* MainScene::createScene()
{
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
auto layer = MainScene::create();
layer->setPhysicsWorld(scene->getPhysicsWorld());
scene->addChild(layer);
return scene;
}
bool MainScene::init()
{
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(MainScene::myTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(MainScene::myTouchMoved, this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
drawNode = DrawNode::create();
this->addChild(drawNode);
auto edge = Node::create();
edge->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
auto edgeBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
edge->setPhysicsBody(edgeBody);
this->addChild(edge);
return true;
}
bool MainScene::myTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
MainScene::lastTouchPos = drawNode->convertTouchToNodeSpace(touch);
return true;
}
void MainScene::myTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event) {
Vec2 touchPos = drawNode->convertTouchToNodeSpace(touch);
drawNode->drawSegment(lastTouchPos, touchPos, 4, Color4F::WHITE);
lastTouchPos = touchPos;
}
void MainScene::myTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) {
body = PhysicsBody::createBox(drawNode->getContentSize(), PhysicsMaterial(0, 0, 1));
drawNode->setPhysicsBody(body);
}