I created two physics body attached to sprite. One is box another is “not dynamic” polygon body (generated by physicseditor) but when they collide passing each other. Box dropping from top to static polygon shape and passing it. I want to make them collide. My init implementation
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
Scene::initWithPhysics();
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
if (closeItem == nullptr ||
closeItem->getContentSize().width <= 0 ||
closeItem->getContentSize().height <= 0)
{
problemLoading("'CloseNormal.png' and 'CloseSelected.png'");
}
else
{
float x = origin.x + visibleSize.width - closeItem->getContentSize().width/2;
float y = origin.y + closeItem->getContentSize().height/2;
closeItem->setPosition(Vec2(x,y));
}
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
if (label == nullptr)
{
problemLoading("'fonts/Marker Felt.ttf'");
}
else
{
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);
}
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("tile.png");
auto physicsBody = PhysicsBody::createBox(sprite->getContentSize(), PhysicsMaterial(0.1f, 1.0f, 0.0f));
sprite->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height - label->getContentSize().height));
sprite->addComponent(physicsBody);
this->addChild(sprite, 1);
shapeCache = PhysicsShapeCache::getInstance();
shapeCache->addShapesWithFile("myShape.plist");
auto terrain = Sprite::create("terrainShape.png");
// attach physics body
shapeCache->setBodyOnSprite("terrainShape.png", terrain);
//sprite->setPhysicsBody(body);
terrain->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height - 300));
this->addChild(terrain,1);
return true;
}