void Armature::setBody(b2Body *body) { if (_body == body) { return; } _body = body; _body->SetUserData(this); for(auto& object : _children) { if (Bone *bone = dynamic_cast<Bone *>(object)) { auto displayList = bone->getDisplayManager()->getDecorativeDisplayList(); for(auto displayObject : displayList) { ColliderDetector *detector = static_cast<DecorativeDisplay *>(displayObject)->getColliderDetector(); if (detector != nullptr) { detector->setBody(_body); } } } } }
void DisplayFactory::initSpriteDisplay(Bone *bone, DecorativeDisplay *decoDisplay, const char *displayName, Skin *skin) { //! remove .xxx std::string textureName = displayName; size_t startPos = textureName.find_last_of("."); if(startPos != std::string::npos) { textureName = textureName.erase(startPos); } TextureData *textureData = ArmatureDataManager::getInstance()->getTextureData(textureName.c_str()); if(textureData) { //! Init display anchorPoint, every Texture have a anchor point skin->setAnchorPoint(Vec2( textureData->pivotX, textureData->pivotY)); } #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX if (textureData && textureData->contourDataList.size() > 0) { //! create ContourSprite ColliderDetector *colliderDetector = ColliderDetector::create(bone); colliderDetector->addContourDataList(textureData->contourDataList); decoDisplay->setColliderDetector(colliderDetector); } #endif }
void CS_DISPLAY_UPDATE(Bone *bone, DecorativeDisplay *decoDisplay, float dt, bool dirty) { CS_RETURN_IF(!decoDisplay); #if ENABLE_PHYSICS_DETECT if (dirty) { ColliderDetector *detector = decoDisplay->getColliderDetector(); if (detector) { CCAffineTransform t = CCAffineTransformConcat(bone->nodeToArmatureTransform(), bone->getArmature()->nodeToWorldTransform()); detector->updateTransform(t); } } #endif switch(decoDisplay->getDisplayData()->displayType) { case CS_DISPLAY_SPRITE: CS_DISPLAY_SPRITE_UPDATE(bone, decoDisplay, dt, dirty); break; case CS_DISPLAY_PARTICLE: CS_DISPLAY_PARTICLE_UPDATE(bone, decoDisplay, dt, dirty); break; case CS_DISPLAY_ARMATURE: CS_DISPLAY_ARMATURE_UPDATE(bone, decoDisplay, dt, dirty); break; case CS_DISPLAY_NODE: CS_DISPLAY_NODE_UPDATE(bone, decoDisplay, dt, dirty); break; default: break; } }
void CCArmature::drawContour() { for(auto& element : _boneDic) { Bone *bone = element.second; ColliderDetector *detector = bone->getColliderDetector(); if (!detector) continue; const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList(); for (auto& object : bodyList) { ColliderBody *body = static_cast<ColliderBody*>(object); const std::vector<Vec2> &vertexList = body->getCalculatedVertexList(); unsigned long length = vertexList.size(); Vec2 *points = new Vec2[length]; for (unsigned long i = 0; i<length; i++) { Vec2 p = vertexList.at(i); points[i].x = p.x; points[i].y = p.y; } DrawPrimitives::drawPoly( points, (unsigned int)length, true ); delete []points; } } }
void TestColliderDetector::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) { for(auto& element : armature2->getBoneDic()) { Bone *bone = element.second; ColliderDetector *detector = bone->getColliderDetector(); if (!detector) continue; const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList(); for (auto& object : bodyList) { ColliderBody *body = static_cast<ColliderBody*>(object); const std::vector<Vec2> &vertexList = body->getCalculatedVertexList(); unsigned long length = vertexList.size(); Vec2 *points = new Vec2[length]; for (unsigned long i = 0; i<length; i++) { Vec2 p = vertexList.at(i); points[i].x = p.x; points[i].y = p.y; } drawNode->clear(); drawNode->drawPoly(points, (unsigned int)length, true, Color4F(1.0, 1.0, 1.0, 1.0)); delete []points; } } }
ColliderDetector *ColliderDetector::create(Bone *bone) { ColliderDetector *pColliderDetector = new ColliderDetector(); if (pColliderDetector && pColliderDetector->init(bone)) { pColliderDetector->autorelease(); return pColliderDetector; } CC_SAFE_DELETE(pColliderDetector); return nullptr; }
void DisplayFactory::updateDisplay(Bone *bone, float dt, bool dirty) { Node *display = bone->getDisplayRenderNode(); CS_RETURN_IF(!display); switch(bone->getDisplayRenderNodeType()) { case CS_DISPLAY_SPRITE: if (dirty) { static_cast<Skin*>(display)->updateArmatureTransform(); } break; case CS_DISPLAY_PARTICLE: updateParticleDisplay(bone, display, dt); break; case CS_DISPLAY_ARMATURE: updateArmatureDisplay(bone, display, dt); break; default: { Mat4 transform = bone->getNodeToArmatureTransform(); display->setAdditionalTransform(&transform); } break; } #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX if (dirty) { DecorativeDisplay *decoDisplay = bone->getDisplayManager()->getCurrentDecorativeDisplay(); ColliderDetector *detector = decoDisplay->getColliderDetector(); if (detector) { do { #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT CC_BREAK_IF(!detector->getBody()); #endif Mat4 displayTransform = display->getNodeToParentTransform(); Vec2 anchorPoint = display->getAnchorPointInPoints(); anchorPoint = PointApplyTransform(anchorPoint, displayTransform); displayTransform.m[12] = anchorPoint.x; displayTransform.m[13] = anchorPoint.y; Mat4 t = TransformConcat( bone->getArmature()->getNodeToParentTransform(),displayTransform); detector->updateTransform(t); } while (0); } } #endif }
ColliderDetector *ColliderDetector::create() { ColliderDetector *pColliderDetector = new (std::nothrow) ColliderDetector(); if (pColliderDetector && pColliderDetector->init()) { pColliderDetector->autorelease(); return pColliderDetector; } CC_SAFE_DELETE(pColliderDetector); return nullptr; }
ColliderDetector *ColliderDetector::create() { ColliderDetector *pColliderDetector = new ColliderDetector(); if (pColliderDetector && pColliderDetector->init()) { pColliderDetector->autorelease(); return pColliderDetector; } CC_SAFE_DELETE(pColliderDetector); return NULL; }
void CS_DISPLAY_SPRITE_CREATE(Bone *bone, DecorativeDisplay *decoDisplay) { Skin *skin = NULL; SpriteDisplayData *displayData = (SpriteDisplayData*)decoDisplay->getDisplayData(); //! remove .xxx std::string textureName = displayData->displayName; size_t startPos = textureName.find_last_of("."); if(startPos != std::string::npos) { textureName = textureName.erase(startPos); } //! create display if(textureName.compare("") == 0) { skin = Skin::create(); } else { skin = Skin::createWithSpriteFrameName((textureName + ".png").c_str()); } CCTextureAtlas *atlas = SpriteFrameCacheHelper::sharedSpriteFrameCacheHelper()->getTextureAtlas((textureName + ".png").c_str()); skin->setTextureAtlas(atlas); TextureData *textureData = ArmatureDataManager::sharedArmatureDataManager()->getTextureData(textureName.c_str()); if(textureData) { //! Init display anchorPoint£¨ every Texture have a anchor point skin->setAnchorPoint(ccp( textureData->pivotX, textureData->pivotY)); } skin->setBone(bone); skin->setSkinData(*bone->getBoneData()); decoDisplay->setDisplay(skin); #if ENABLE_PHYSICS_DETECT if (textureData && textureData->contourDataList.count() > 0) { //! create ContourSprite ColliderDetector *colliderDetector = ColliderDetector::create(bone); colliderDetector->addContourDataList(&textureData->contourDataList); decoDisplay->setColliderDetector(colliderDetector); } #endif }
void TestColliderDetector::update(float delta) { armature2->setVisible(true); Rect rect = bullet->getBoundingBox(); // This code is just telling how to get the vertex. // For a more accurate collider detection, you need to implemente yourself. const Map<std::string, cocostudio::Bone*>& map = armature2->getBoneDic(); for(const auto& element : map) { cocostudio::Bone *bone = element.second; ColliderDetector *detector = bone->getColliderDetector(); if (!detector) continue; const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList(); for (const auto& object : bodyList) { ColliderBody *body = static_cast<ColliderBody*>(object); const std::vector<Vec2> &vertexList = body->getCalculatedVertexList(); float minx = 0, miny = 0, maxx = 0, maxy = 0; size_t length = vertexList.size(); for (size_t i = 0; i<length; i++) { Vec2 vertex = vertexList.at(i); if (i == 0) { minx = maxx = vertex.x; miny = maxy = vertex.y; } else { minx = vertex.x < minx ? vertex.x : minx; miny = vertex.y < miny ? vertex.y : miny; maxx = vertex.x > maxx ? vertex.x : maxx; maxy = vertex.y > maxy ? vertex.y : maxy; } } Rect temp = Rect(minx, miny, maxx - minx, maxy - miny); if (temp.intersectsRect(rect)) { armature2->setVisible(false); } } } }
void Armature::drawContour() { for(auto& element : _boneDic) { Bone *bone = element.second; ColliderDetector *detector = bone->getColliderDetector(); if (!detector) continue; const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList(); for (auto& object : bodyList) { ColliderBody *body = static_cast<ColliderBody*>(object); const std::vector<Vec2> &vertexList = body->getCalculatedVertexList(); unsigned long length = vertexList.size(); Vec2 *points = new (std::nothrow) Vec2[length]; for (unsigned long i = 0; i<length; i++) { Vec2 p = vertexList.at(i); points[i].x = p.x; points[i].y = p.y; } #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))) #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #elif _MSC_VER >= 1400 //vs 2005 or higher #pragma warning (push) #pragma warning (disable: 4996) #endif DrawPrimitives::drawPoly( points, (unsigned int)length, true ); #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))) #pragma GCC diagnostic warning "-Wdeprecated-declarations" #elif _MSC_VER >= 1400 //vs 2005 or higher #pragma warning (pop) #endif delete []points; } } }
bool CocAnimationTest::init(Layer* layer){ do{ m_layer = layer; CC_BREAK_IF(!m_layer); ArmatureDataManager::getInstance()->addArmatureFileInfo("hero.ExportJson"); m_armature = Armature::create("hero"); // Armature* gun = Armature::create("man"); //gun->setPosition(Point(100,100)); //m_layer->addChild(gun,10); // Sprite* s = Sprite::create("CloseNormal.png"); // s->setPosition(Point(_pCenterX,_pCenterY+100)); // s->setPhysicsBody(PhysicsBody::createBox(Size(50/_screenScale, 50/_screenScale))); // m_layer->addChild(s,10); // Bone* bone1 = gun->getBone("manBone"); // gunBone->addChild(gun); CC_BREAK_IF(!m_armature); m_node = Node::create(); m_layer->addChild(m_node); m_node->setPosition(Point(_pCenterX,_pCenterY)); m_node->setAnchorPoint(Point::ANCHOR_MIDDLE); m_node->addChild(m_armature); m_node->setPhysicsBody(PhysicsBody::create()); m_armature->setPosition(Point::ZERO); Bone* _bulletout = m_armature->getBone("bulletout"); BaseData* data = _bulletout->getWorldInfo(); m_node->getPhysicsBody()->addShape(PhysicsShapeBox::create(Size(20,20),PHYSICSSHAPE_MATERIAL_DEFAULT,Point(data->x,data->y))); // Point points[4] = {Point(0,0),Point(100,100),Point(200,100)}; // m_node->getPhysicsBody()->addShape(PhysicsShapePolygon::create(points, 3)); //m_armature->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT); // m_armature->setPhysicsBody(PhysicsBody::create()); // m_armature->getPhysicsBody()->addShape(PhysicsShapeBox::create(Size(50/_screenScale, 50/_screenScale))); //// Point points[4] = {Point(0,0),Point(100,100),Point(0,100)}; //// m_armature->getPhysicsBody()->addShape(PhysicsShapePolygon::create(points, 3)); // // log("pos x=%f,y = %f",m_armature->getBone("shoulder_l")->getWorldInfo()-> x,m_armature->getBone("shoulder_r")->getWorldInfo()->y); // m_armature->getPhysicsBody()->addShape(PhysicsShapeBox::create(Size(20,20),PHYSICSSHAPE_MATERIAL_DEFAULT,Point(125,-5))); // // Map<string, Bone*> dict = m_armature->getBoneDic(); for (auto iter = dict.begin(); iter!=dict.end(); ++iter) { Bone* bone = iter->second; ColliderDetector* detector = bone->getColliderDetector(); if (!detector) { continue; } Vector<ColliderBody*> vec = detector->getColliderBodyList(); for (Vector<ColliderBody*>::iterator it = vec.begin(); it!=vec.end(); it++) { ColliderBody* body = dynamic_cast<ColliderBody*>(*it); vector<Point> vertexList = body->getCalculatedVertexList(); int length = vertexList.size(); log("length %d",length); Point* points = new Point[length]; for (int i=0; i<length; i++) { points[i] = vertexList[length-i-1]; log("point %d %f,%f",i,vertexList[i].x,vertexList[i].y); } m_node->getPhysicsBody()->addShape(PhysicsShapePolygon::create(points, length)); } } m_node->getPhysicsBody()->setRotationEnable(false); //Bone* manBone = m_armature->getBone("manBone"); //m_armature->setPhysicsBody(PhysicsBody::createBox(Size(50/_screenScale, 50/_screenScale))); //m_armature->setPosition(Point(100,100)); // m_armature->setRotation(60); // m_armature->getPhysicsBody()->setVelocity(Point(0,500)); // m_armature->getPhysicsBody()->getShape(0)->setTag(99); // m_armature->getPhysicsBody()->setContactTestBitmask(10); // m_armature->getPhysicsBody()->setRotationEnable(false); // m_armature->getPhysicsBody()->setCategoryBitmask(0xf); // m_armature->getPhysicsBody()->getShape(99)->setRestitution(0); // Bone* gunBone = gun->getBone("manBone"); // manBone->addChildBone(gunBone); // m_armature->setPhysicsBody(PhysicsBody::createEdgeBox(Size(50/_screenScale, 50/_screenScale))); // m_armature->getAnimation()->play("run"); // m_armature->getAnimation()->setSpeedScale(0.5f); // manBone->addChild(gunBone); function<void(Armature*,MovementEventType,string)> callback = [](Armature* armature, MovementEventType evt, string animationName){ switch (evt) { case cocostudio::COMPLETE: log("%s %s complete",armature->getName().c_str(),animationName.c_str()); break; case cocostudio::LOOP_COMPLETE: log("%s %s loop complete", armature->getName().c_str(),animationName.c_str()); break; case cocostudio::START: log("%s %s start", armature->getName().c_str(),animationName.c_str()); break; default: break; } }; // m_armature->getAnimation()->setMovementEventCallFunc(callback); //armature->setRotation(60); return true; //m_layer->addChild(gun,11); }while (0); return false; }