Пример #1
0
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;
        }
    }
}
KDvoid ColliderDetector::setCPBody ( cpBody* pBody )
{
    m_pCPBody = pBody;

    for(auto object : *m_pColliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;

        ContourData *contourData = colliderBody->getContourData();

        int num = contourData->m_tVertexList.count();
        ContourVertex2 **vs = (ContourVertex2 **)contourData->m_tVertexList.data->arr;
        cpVect *verts = new cpVect[num];
        for (int i = 0; i < num; i++)
        {
            verts[num - 1 - i].x = vs[i]->x;
            verts[num - 1 - i].y = vs[i]->y;
        }

        cpShape *shape = cpPolyShapeNew(m_pCPBody, num, verts, cpvzero);

        shape->sensor = true;
        shape->data = m_pBone;

        if ( m_bActive )
        {
            cpSpaceAddShape(m_pCPBody->space, shape);
        }

        colliderBody->setShape(shape);
        colliderBody->getColliderFilter()->updateCPShape(shape);

        delete [] verts;
    }
}
Пример #3
0
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;
        }
    }
}
Пример #4
0
void ColliderDetector::setBody(cpBody *pBody)
{
    _body = pBody;

    for(auto& object : _colliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;

        ContourData *contourData = colliderBody->getContourData();

        ssize_t num = contourData->vertexList.size();
        auto vs = contourData->vertexList;
        cpVect *verts = new cpVect[num];
        for (int i = 0; i < num; i++)
        {
            verts[num - 1 - i].x = vs.at(i).x;
            verts[num - 1 - i].y = vs.at(i).y;
        }

        cpShape *shape = cpPolyShapeNew(_body, (int)num, verts, cpvzero);

        shape->sensor = true;
        shape->data = _bone;

        if (_active)
        {
            cpSpaceAddShape(_body->space_private, shape);
        }

        colliderBody->setShape(shape);
        colliderBody->getColliderFilter()->updateShape(shape);

        delete []verts;
    }
}
void ColliderDetector::removeContourData(ContourData *contourData)
{
	for(auto object : *m_pColliderBodyList)
	{
		ColliderBody *body = (ColliderBody*)object;
		if (body && body->getContourData() == contourData)
		{
			m_pColliderBodyList->removeObject(body);
		}
	}
}
Пример #6
0
ColliderDetector::~ColliderDetector()
{
	CCObject *object = NULL;
	CCARRAY_FOREACH(m_pColliderBodyList, object)
	{
		ColliderBody *colliderBody = (ColliderBody*)object;

		ContourData *contourData = colliderBody->getContourData();
		b2Body *body = colliderBody->getB2Body();
		PhysicsWorld::sharedPhysicsWorld()->getNoGravityWorld()->DestroyBody(body);
	}
Пример #7
0
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);
            }
        }
    }
}
Пример #8
0
void ColliderDetector::addContourData(ContourData *contourData)
{
    ColliderBody *colliderBody = new (std::nothrow) ColliderBody(contourData);
    _colliderBodyList.pushBack(colliderBody);
    colliderBody->release();


#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
    std::vector<Vec2> &calculatedVertexList = colliderBody->_calculatedVertexList;
    
    unsigned long num = contourData->vertexList.size();
    for (unsigned long i = 0; i < num; i++)
    {
        calculatedVertexList.push_back(Vec2());
    }
#endif
}
Пример #9
0
void TestColliderDetector::update(float delta)
{
    armature2->setVisible(true);

    CCRect rect = bullet->boundingBox();

    // This code is just telling how to get the vertex.
    // For a more accurate collider detection, you need to implemente yourself.
    CCDictElement *element = NULL;
    CCDictionary *dict = armature2->getBoneDic();
    CCDICT_FOREACH(dict, element)
    {
        CCBone *bone = static_cast<CCBone*>(element->getObject());
        CCArray *bodyList = bone->getColliderBodyList();

        CCObject *object = NULL;
        CCARRAY_FOREACH(bodyList, object)
        {
            ColliderBody *body = static_cast<ColliderBody*>(object);
            CCArray *vertexList = body->getCalculatedVertexList();

            float minx, miny, maxx, maxy = 0;
            int length = vertexList->count();
            for (int i = 0; i<length; i++)
            {
                CCContourVertex2 *vertex = static_cast<CCContourVertex2*>(vertexList->objectAtIndex(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;
                }
            }
            CCRect temp = CCRectMake(minx, miny, maxx - minx, maxy - miny);

            if (temp.intersectsRect(rect))
            {
                armature2->setVisible(false);
            }
        }
Пример #10
0
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;
        }
    }
}
void ColliderDetector::addContourData(ContourData *contourData)
{
    ColliderBody *colliderBody = new ColliderBody(contourData);
    m_pColliderBodyList->addObject(colliderBody);
    colliderBody->release();


#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
    CCArray *calculatedVertexList = colliderBody->getCalculatedVertexList();
    
    int num = contourData->m_tVertexList.count();
    for (int i = 0; i < num; i++)
    {
        ContourVertex2 *newVertex = new ContourVertex2(0, 0);
        calculatedVertexList->addObject(newVertex);
        newVertex->release();
    }
#endif
}
Пример #12
0
void ColliderDetector::setActive(bool active)
{
    if (_active == active)
    {
        return;
    }

    _active = active;

#if ENABLE_PHYSICS_BOX2D_DETECT
    if (_body)
    {
        if (active)
        {
            setBody(_body);
        }
        else
        {
            for(auto object : *_colliderBodyList)
            {
                ColliderBody *colliderBody = (ColliderBody *)object;
                b2Fixture *fixture = colliderBody->getB2Fixture();

                _body->DestroyFixture(fixture);
                colliderBody->setB2Fixture(nullptr);
            }
        }
    }
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
    if (_body)
    {
        if (_active)
        {
            for(auto object : *_colliderBodyList)
            {
                ColliderBody *colliderBody = (ColliderBody *)object;
                cpShape *shape = colliderBody->getShape();
                if(shape->space_private == nullptr)
                {
                    cpSpaceAddShape(_body->space_private, shape);
                }
            }
        }
        else
        {
            for(auto object : *_colliderBodyList)
            {
                ColliderBody *colliderBody = (ColliderBody *)object;
                cpShape *shape = colliderBody->getShape();
                if (shape->space_private != nullptr)
                {
                    cpSpaceRemoveShape(_body->space_private, shape);
                }
            }
        }
    }
#endif
}
void ColliderDetector::setActive(bool active)
{
    if (m_bActive == active)
    {
        return;
    }

    m_bActive = active;

    if (m_pB2Body)
    {
        if (active)
        {
            setB2Body(m_pB2Body);
        }
        else
        {
            for(auto object : *m_pColliderBodyList)
            {
                ColliderBody *colliderBody = (ColliderBody *)object;
                b2Fixture *fixture = colliderBody->getB2Fixture();

                m_pB2Body->DestroyFixture(fixture);
                colliderBody->setB2Fixture(nullptr);
            }
        }
    }
    else if (m_pCPBody)
    {
        if (m_bActive)
        {
            for(auto object : *m_pColliderBodyList)
            {
                ColliderBody *colliderBody = (ColliderBody *)object;
                cpShape *shape = colliderBody->getShape();
                if(shape->space == nullptr)
                {
                    cpSpaceAddShape(m_pCPBody->space, shape);
                }
            }
        }
        else
        {
            for(auto object : *m_pColliderBodyList)
            {
                ColliderBody *colliderBody = (ColliderBody *)object;
                cpShape *shape = colliderBody->getShape();
                if (shape->space != nullptr)
                {
                    cpSpaceRemoveShape(m_pCPBody->space, shape);
                }
            }
        }
    }
}
void ColliderDetector::setColliderFilter(ColliderFilter *filter)
{
    *m_pFilter = *filter;
    
    for(auto object : *m_pColliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;
        colliderBody->setColliderFilter(filter);


        if (colliderBody->getB2Fixture())
        {
            colliderBody->getColliderFilter()->updateB2Shape(colliderBody->getB2Fixture());
        }

        if (colliderBody->getShape())
        {
            colliderBody->getColliderFilter()->updateCPShape(colliderBody->getShape());
        }
    }
}
Пример #15
0
void ColliderDetector::setColliderFilter(ColliderFilter *filter)
{
    *_filter = *filter;
    
    for(auto object : *_colliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;
        colliderBody->setColliderFilter(filter);

#if ENABLE_PHYSICS_BOX2D_DETECT
        if (colliderBody->getB2Fixture())
        {
            colliderBody->getColliderFilter()->updateShape(colliderBody->getB2Fixture());
        }
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
        if (colliderBody->getShape())
        {
            colliderBody->getColliderFilter()->updateShape(colliderBody->getShape());
        }
#endif
    }
}
Пример #16
0
void ColliderDetector::setBody(b2Body *pBody)
{
    _body = pBody;

    for(auto object : *_colliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;

        ContourData *contourData = colliderBody->getContourData();
        const Array *array = &contourData->vertexList;
        Object *object = nullptr;

        b2Vec2 *b2bv = new b2Vec2[contourData->vertexList.count()];

        int i = 0;
        for(auto object : *array)
        {
            ContourVertex2 *v = (ContourVertex2 *)object;
            b2bv[i].Set(v->x / PT_RATIO, v->y / PT_RATIO);
            i++;
        }

        b2PolygonShape polygon;
        polygon.Set(b2bv, contourData->vertexList.count());

        CC_SAFE_DELETE(b2bv);

        b2FixtureDef fixtureDef;
        fixtureDef.shape = &polygon;
        fixtureDef.isSensor = true;

        b2Fixture *fixture = _body->CreateFixture(&fixtureDef);
        fixture->SetUserData(_bone);

        if (colliderBody->getB2Fixture() != nullptr)
        {
            _body->DestroyFixture(colliderBody->getB2Fixture());
        }
        colliderBody->setB2Fixture(fixture);

        colliderBody->getColliderFilter()->updateShape(fixture);
    }
}
void ColliderDetector::setB2Body(b2Body *pBody)
{
    m_pB2Body = pBody;

    for(auto object : *m_pColliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;

        ContourData *contourData = colliderBody->getContourData();

		ContourVertex2 **vs = (ContourVertex2 **)contourData->m_tVertexList.data->arr;
		int num = contourData->m_tVertexList.count();

        b2Vec2 *b2bv = new b2Vec2[num];
        for (int i = 0; i < num; i++)
        {
			b2bv[i].Set(vs[i]->x / PT_RATIO, vs[i]->y / PT_RATIO);
        }

        b2PolygonShape polygon;
        polygon.Set(b2bv, contourData->m_tVertexList.count());

        CC_SAFE_DELETE(b2bv);

        b2FixtureDef fixtureDef;
        fixtureDef.shape = &polygon;
        fixtureDef.isSensor = true;

        b2Fixture *fixture = m_pB2Body->CreateFixture(&fixtureDef);
        fixture->SetUserData(m_pBone);

        if (colliderBody->getB2Fixture() != nullptr)
        {
            m_pB2Body->DestroyFixture(colliderBody->getB2Fixture());
        }
        colliderBody->setB2Fixture(fixture);

        colliderBody->getColliderFilter()->updateB2Shape(fixture);
    }
}
void ColliderDetector::updateTransform(AffineTransform &t)
{
    if (!m_bActive)
    {
        return;
    }

    for(auto object : *m_pColliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;
        ContourData *contourData = colliderBody->getContourData();

        b2PolygonShape* b2shape = nullptr;
        if (m_pB2Body != nullptr)
        {
            b2shape = (b2PolygonShape *)colliderBody->getB2Fixture()->GetShape();
        }

        cpPolyShape* cpshape = nullptr;
        if (m_pCPBody != nullptr)
        {
            cpshape = (cpPolyShape *)colliderBody->getShape();
        }

        int num = contourData->m_tVertexList.count();
        ContourVertex2 **vs = (ContourVertex2 **)contourData->m_tVertexList.data->arr;

#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
        ContourVertex2 **cvs = (ContourVertex2 **)colliderBody->getCalculatedVertexList()->data->arr;
#endif

        for (int i = 0; i < num; i++)
        {
            helpPoint.setPoint( vs[i]->x,  vs[i]->y);
            helpPoint = PointApplyAffineTransform(helpPoint, t);


#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
            cvs[i]->x = helpPoint.x;
            cvs[i]->y = helpPoint.y;
#endif

            if ( b2shape != nullptr )
            {
                b2Vec2 &bv = b2shape->m_vertices[i];
                bv.Set(helpPoint.x / PT_RATIO, helpPoint.y / PT_RATIO);
            }

            if ( cpshape != nullptr )
            {
                cpVect v ;
                v.x = helpPoint.x;
                v.y = helpPoint.y;
                cpshape->verts[i] = v;
            }
        }

		if ( cpshape != nullptr )
		{
			cpConvexHull(num, cpshape->verts, nullptr, nullptr, 0);
			for (int i = 0; i < num; i++)
			{
				cpVect b = cpshape->verts[(i + 1) % cpshape->numVerts];
				cpVect n = cpvnormalize(cpvperp(cpvsub(b, cpshape->verts[i])));

				cpshape->planes[i].n = n;
				cpshape->planes[i].d = cpvdot(n, cpshape->verts[i]);
			}
		}
    }
}
Пример #19
0
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;
}
void CCColliderDetector::addContourData(CCContourData *contourData)
{
    ColliderBody *colliderBody = new ColliderBody(contourData);
    m_pColliderBodyList->addObject(colliderBody);
    colliderBody->release();
}
Пример #21
0
void ColliderDetector::updateTransform(Mat4 &t)
{
    if (!_active)
    {
        return;
    }

    for(auto& object : _colliderBodyList)
    {
        ColliderBody *colliderBody = (ColliderBody *)object;
        ContourData *contourData = colliderBody->getContourData();

#if ENABLE_PHYSICS_BOX2D_DETECT
        b2PolygonShape *shape = nullptr;
        if (_body != nullptr)
        {
            shape = (b2PolygonShape *)colliderBody->getB2Fixture()->GetShape();
        }
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
        cpPolyShape *shape = nullptr;
        if (_body != nullptr)
        {
            shape = (cpPolyShape *)colliderBody->getShape();
        }
#endif

        unsigned long num = contourData->vertexList.size();
        std::vector<cocos2d::Vec2> &vs = contourData->vertexList;

#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
        std::vector<cocos2d::Vec2> &cvs = colliderBody->_calculatedVertexList;
#endif

        for (unsigned long i = 0; i < num; i++)
        {
            helpPoint.setPoint( vs.at(i).x,  vs.at(i).y);
            helpPoint = PointApplyTransform(helpPoint, t);


#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
            cvs.at(i).x = helpPoint.x;
            cvs.at(i).y = helpPoint.y;
#endif

#if ENABLE_PHYSICS_BOX2D_DETECT
            if (shape != nullptr)
            {
                b2Vec2 &bv = shape->m_vertices[i];
                bv.Set(helpPoint.x / PT_RATIO, helpPoint.y / PT_RATIO);
            }
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
            if (shape != nullptr)
            {
                cpVect v ;
                v.x = helpPoint.x;
                v.y = helpPoint.y;
                shape->verts[i] = v;
            }
#endif
        }

#if ENABLE_PHYSICS_CHIPMUNK_DETECT
        cpConvexHull((int)num, shape->verts, nullptr, nullptr, 0);
        for (unsigned long i = 0; i < num; i++)
        {
            cpVect b = shape->verts[(i + 1) % shape->numVerts];
            cpVect n = cpvnormalize(cpvperp(cpvsub(b, shape->verts[i])));

            shape->planes[i].n = n;
            shape->planes[i].d = cpvdot(n, shape->verts[i]);
        }
#endif
    }
}