bool cpSpaceSerializer::save(cpSpace* space, const char* filename)
{
    _space = space;

    //Initialize
    _bodyMap.clear();

    //This id is always the staticBody of the space
    _bodyMap[0] = space->staticBody;

	TiXmlElement *root = new TiXmlElement("space");
	_doc.LinkEndChild(root);
	
	root->LinkEndChild(createValueElm("iterations", space->iterations));
	root->LinkEndChild(createPointElm("gravity", space->gravity));	
	root->LinkEndChild(createValueElm("damping", space->damping));
	
    cpSpaceSerializerContext context = {this, root};
    
	//Write out all types of shapes
	cpSpaceEachShape(space, writeShape, &context);

    //Write out constraints
    cpSpaceEachConstraint(space, writeConstraint, &context);
	
	return _doc.SaveFile(filename);
}
// Safe and future proof way to remove and free all objects that have been added to the space.
void ChipmunkFreeSpaceChildren(cpSpace *space)
{
	// Must remove these BEFORE freeing the body or you will access dangling pointers.
	cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)postShapeFree, space);
	cpSpaceEachConstraint(space, (cpSpaceConstraintIteratorFunc)postConstraintFree, space);
	
	cpSpaceEachBody(space, (cpSpaceBodyIteratorFunc)postBodyFree, space);
}
void CCPhysicsDebugNode::draw()
{
    if (! m_pSpacePtr)
    {
        return;
    }
    
    cpSpaceEachShape(m_pSpacePtr, (cpSpaceShapeIteratorFunc)DrawShape, this);
	cpSpaceEachConstraint(m_pSpacePtr, (cpSpaceConstraintIteratorFunc)DrawConstraint, this);
    
    CCDrawNode::draw();
    CCDrawNode::clear();
}
void PhysicsDebugNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    if (! _spacePtr)
    {
        return;
    }
#if CC_ENABLE_CHIPMUNK_INTEGRATION
    // clear the shapes information before draw current shapes.
    DrawNode::clear();

    cpSpaceEachShape(_spacePtr, (cpSpaceShapeIteratorFunc)DrawShape, this);
    cpSpaceEachConstraint(_spacePtr, (cpSpaceConstraintIteratorFunc)DrawConstraint, this);
    
    DrawNode::draw(renderer, transform, flags);
#endif
}
Beispiel #5
0
inline void space_each_constraint(cpSpace *space, void *f) {
  cpSpaceEachConstraint(space, eachConstraint_space, f);
}
Beispiel #6
0
void Space::eachConstraint(SpaceConstraintIteratorFunc func)
{
		cpSpaceEachConstraint(space,*SpaceEachConstraint,&func);
}
Beispiel #7
0
void Space::eachConstraint(cpSpaceConstraintIteratorFunc func,void *data)
{
		cpSpaceEachConstraint(space,func,data);
}
Beispiel #8
0
static void
draw_shape(cpShape* shape, SDL_Surface* screen)
{
    cpBody* body = shape->body;
    struct draw_options opts = {
        .surface = screen,
        .colour = colour(200, 200, 200)
    };

    switch (shape->CP_PRIVATE(klass)->type)
    {
        case CP_CIRCLE_SHAPE:
            draw_circle_shape(opts, shape, body);
            break;
        case CP_SEGMENT_SHAPE:
            draw_segment_shape(opts, shape, body);
            break;
        case CP_POLY_SHAPE:
            draw_poly_shape(opts, shape, body);
            break;
        default:
            debug_putsf("ignoring unrecognised shape type %d",
                    shape->CP_PRIVATE(klass)->type);
            break;
    }
}

static void
draw_constraint(cpConstraint* constraint, SDL_Surface *screen)
{
    struct draw_options opts = {
        .surface = screen,
        .colour = colour(100, 100, 200)
    };

    cpVect vect_a = cpBodyGetPos(cpConstraintGetA(constraint));
    cpVect vect_b = cpBodyGetPos(cpConstraintGetB(constraint));
    draw_line(opts, vect_a, vect_b);
}

static void
draw_rotation_vector(cpBody *body, SDL_Surface *screen)
{
    struct draw_options opts = {
        .surface = screen,
        .colour = colour(200, 100, 100)
    };

    cpFloat rotation_vector_length = 30;
    cpVect pos = cpBodyGetPos(body);
    cpVect rotation = cpvmult(cpvforangle(cpBodyGetAngle(body)),
                              rotation_vector_length);
    draw_line(opts, pos, cpvadd(pos, rotation));
}

static void
draw_forces(cpBody *body, SDL_Surface *screen)
{
    struct draw_options opts = {
        .surface = screen,
        .colour = colour(100, 200, 100)
    };

    cpVect pos = cpBodyGetPos(body);
    cpVect force = cpBodyGetForce(body);
    draw_line(opts, pos, cpvadd(pos, force));
}

static void
draw_body(cpBody *body, SDL_Surface *screen)
{
    draw_rotation_vector(body, screen);
    draw_forces(body, screen);
}

void
debug_draw_space(cpSpace* space, SDL_Surface* screen)
{
    cpSpaceEachShape(space,
        (cpSpaceShapeIteratorFunc)draw_shape,
        screen);
    cpSpaceEachConstraint(space,
        (cpSpaceConstraintIteratorFunc)draw_constraint,
        screen);
    cpSpaceEachBody(space,
        (cpSpaceBodyIteratorFunc)draw_body,
        screen);
}