Пример #1
0
cpSpace* cpSpaceSerializer::load(cpSpace *space, const char* filename)
{
	if (!_doc.LoadFile(filename))
		return space;
	
	//Grab our space
	TiXmlElement *root = _doc.FirstChildElement("space");
	if (!root)
		return space;

    _space = space;

    //Initialize
    _bodyMap.clear();
    _shapeMap.clear();

    //A body id of zero is the space's static body
    _bodyMap[0] = space->staticBody;
	
	space->iterations = createValue<int>("iterations", root);
	space->gravity = createPoint("gravity", root);	
	space->damping = createValue<cpFloat>("damping", root);
	
	TiXmlElement *child = root->FirstChildElement("shape");
	
	//Read Shapes
	while (child)
	{
		//attempt a shape
		cpShape *shape = createShape(child);
		if (shape)
		{
			//This should not happen like this, need to reflect reality -rkb
            if (shape->body->m != INFINITY && !cpSpaceContainsBody(space, shape->body))
				cpSpaceAddBody(space, shape->body);

            cpSpaceAddShape(space, shape);
		}
		
		//Next!
		child = child->NextSiblingElement("shape");
	}
	
	//Read Constraints
	child = root->FirstChildElement("constraint");
	while (child)
	{
		//else attempt a constraint
		cpConstraint *constraint = createConstraint(child);
		if (constraint)
			cpSpaceAddConstraint(space, constraint);
		
		child = child->NextSiblingElement("constraint");
    }
	
	return space;
}
Пример #2
0
void
cpSpaceRemoveBody(cpSpace *space, cpBody *body)
{
	cpAssertHard(cpSpaceContainsBody(space, body), "Cannot remove a body that was not added to the space. (Removed twice maybe?)");
	cpAssertSpaceUnlocked(space);
	
	cpBodyActivate(body);
//	cpSpaceFilterArbiters(space, body, NULL);
	cpArrayDeleteObj(space->bodies, body);
	body->space = NULL;
}
Пример #3
0
void
cpSpaceRemoveBody(cpSpace *space, cpBody *body)
{
	cpAssertHard(body != cpSpaceGetStaticBody(space), "Cannot remove the designated static body for the space.");
	cpAssertHard(cpSpaceContainsBody(space, body), "Cannot remove a body that was not added to the space. (Removed twice maybe?)");
//	cpAssertHard(body->shapeList == NULL, "Cannot remove a body from the space before removing the bodies attached to it.");
//	cpAssertHard(body->constraintList == NULL, "Cannot remove a body from the space before removing the constraints attached to it.");
	cpAssertSpaceUnlocked(space);
	
	cpBodyActivate(body);
//	cpSpaceFilterArbiters(space, body, NULL);
	cpArrayDeleteObj(cpSpaceArrayForBodyType(space, cpBodyGetType(body)), body);
	body->space = NULL;
}
Пример #4
0
void PhysicsWorld::doRemoveBody(PhysicsBody* body)
{
    CCASSERT(body != nullptr, "the body can not be nullptr");
    
    // remove shapes
    for (auto& shape : body->getShapes())
    {
        removeShape(shape);
    }
    
    // remove body
    if (cpSpaceContainsBody(_cpSpace, body->_cpBody))
    {
        cpSpaceRemoveBody(_cpSpace, body->_cpBody);
    }
}
GeometrySpringDynamic::~GeometrySpringDynamic()
{
	clearJoints();

	if( 0 != m_Shape && true == cpSpaceContainsShape( m_Space, m_Shape ) )
	{
		cpSpaceRemoveShape( m_Space, m_Shape );
		cpShapeFree( m_Shape );
		m_Shape = 0;
	}
	if( 0 != m_Body && true == cpSpaceContainsBody( m_Space, m_Body ) )
	{
		cpSpaceRemoveBody( m_Space, m_Body );
		cpBodyFree( m_Body );
		m_Body = 0;
	}
}
Пример #6
0
void PhysicsWorld::doAddBody(PhysicsBody* body)
{
    if (body->isEnabled())
    {
        // add body to space
        if (!cpSpaceContainsBody(_cpSpace, body->_cpBody))
        {
            cpSpaceAddBody(_cpSpace, body->_cpBody);
        }
        
        // add shapes to space
        for (auto& shape : body->getShapes())
        {
            addShape(dynamic_cast<PhysicsShape*>(shape));
        }
    }
}
Пример #7
0
cpBool Space::containsBody(cp::Body *body)
{
		return cpSpaceContainsBody(space,body ? body->get() : 0);
}
Пример #8
0
static inline void _remove_body(cpBody *body)
{
    if (cpSpaceContainsBody(space, body))
        cpSpaceRemoveBody(space, body);
    cpBodyFree(body);
}