示例#1
0
void CCPhysicsWorld::step(float dt)
{
    cpSpaceStep(m_space, dt);
    for (CCPhysicsBodyMapIterator it = m_bodies.begin(); it != m_bodies.end(); ++it)
    {
        it->second->update(dt);
    }

    CCPhysicsBody *body;
    cpBody *cpbody;
    
    unsigned int count = m_removedShapes->count();
    if (count)
    {
        for (unsigned int i = 0; i < count; ++i)
        {
            cpSpaceRemoveShape(m_space, static_cast<CCPhysicsShape*>(m_removedShapes->objectAtIndex(i))->getShape());
        }
        m_removedShapes->removeAllObjects();
    }

    count = m_addedBodies->count();
    if (count)
    {
        for (unsigned int i = 0; i < count; ++i)
        {
            body = static_cast<CCPhysicsBody*>(m_addedBodies->objectAtIndex(i));
            cpbody = body->getBody();
            if (!cpBodyIsStatic(cpbody)) cpSpaceAddBody(m_space, cpbody);
            m_bodies[cpbody] = body;
            m_bodiesArray->addObject(body);
        }
        m_addedBodies->removeAllObjects();
    }

    count = m_removedBodies->count();
    if (count)
    {
        for (unsigned int i = 0; i < count; ++i)
        {
            cpbody = static_cast<CCPhysicsBody*>(m_removedBodies->objectAtIndex(i))->getBody();
            if (!cpBodyIsRogue(cpbody))
            {
                cpSpaceRemoveBody(m_space, cpbody);
            }
        }
        m_removedBodies->removeAllObjects();
    }

    count = m_addedShapes->count();
    if (count)
    {
        for (unsigned int i = 0; i < count; ++i)
        {
            cpSpaceAddShape(m_space, static_cast<CCPhysicsShape*>(m_addedShapes->objectAtIndex(i))->getShape());
        }
        m_addedShapes->removeAllObjects();
    }
}
示例#2
0
CCPhysicsBody *CCPhysicsWorld::createBoxBody(float mass, float width, float height)
{
    CCPhysicsBody *body;
    if (mass <= 0)
    {
        body = CCPhysicsBody::createStaticBody(this);
    }
    else
    {
        float moment = cpMomentForBox(mass, width, height);
        body = CCPhysicsBody::create(this, mass, moment);
    }
    body->addBoxShape(width, height);
    addBody(body);
    return body;
}
示例#3
0
CCPhysicsBody *CCPhysicsWorld::createCircleBody(float mass, float radius, float offsetX/*= 0*/, float offsetY/*= 0*/)
{
    CCPhysicsBody *body;
    if (mass <= 0)
    {
        body = CCPhysicsBody::createStaticBody(this);
    }
    else
    {
        float moment = cpMomentForCircle(mass, 0, radius, cpv(offsetX, offsetY));
        body = CCPhysicsBody::create(this, mass, moment);
    }
    body->addCircleShape(radius, offsetX, offsetY);
    addBody(body);
    return body;
}
示例#4
0
CCPhysicsBody *CCPhysicsWorld::createPolygonBody(float mass, int numVertexes, cpVect *vertexes, float offsetX/*= 0*/, float offsetY/*= 0*/)
{
    CCPhysicsBody *body;
    if (mass <= 0)
    {
        body = CCPhysicsBody::createStaticBody(this);
    }
    else
    {
        CCAssert(cpPolyValidate(vertexes, numVertexes), "CCPhysicsWorld::createPolygonBody() - Invalid vertexes, Polygon is concave or has a reversed winding.");
        float moment = cpMomentForPoly(mass, numVertexes, vertexes, cpv(offsetX, offsetY));
        CCAssert(moment == moment, "CCPhysicsWorld::createPolygonBody() - Invalid moment");
        body = CCPhysicsBody::create(this, mass, moment);
    }
    body->addPolygonShape(numVertexes, vertexes, offsetX, offsetY);
    addBody(body);
    return body;
}
示例#5
0
CCPhysicsBody *CCPhysicsWorld::getBodyByCpBody(cpBody *cpBody)
{
    CCPhysicsBodyMapIterator it = m_bodies.find(cpBody);
    if (it != m_bodies.end())
    {
        return it->second;
    }

    unsigned int count = m_removedBodies->count();
    for (unsigned int i = 0; i < count; ++i)
    {
        CCPhysicsBody *body = static_cast<CCPhysicsBody*>(m_removedBodies->objectAtIndex(i));
        if (body->getBody() == cpBody)
        {
            return body;
        }
    }

    CCLOG("NOT FOUND BODY 0x%08x", (unsigned int)cpBody);
    return NULL;
}