Пример #1
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
}
Пример #2
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::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());
        }
    }
}
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);
    }
}
Пример #6
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
    }
}
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]);
			}
		}
    }
}
Пример #8
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
    }
}