Ejemplo n.º 1
0
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);
}
Ejemplo n.º 2
0
static void
display(void)
{
	demos[demoIndex].drawFunc();
	
	if(!paused || step){
		cpVect newPoint = cpvlerp(mouseBody->p, ChipmunkDemoMouse, 0.25f);
		mouseBody->v = cpvmult(cpvsub(newPoint, mouseBody->p), 60.0f);
		mouseBody->p = newPoint;
		
		demos[demoIndex].updateFunc(ticks);
		
		ticks++;
		step = cpFalse;
	}
  
	if(drawBBs) cpSpaceEachShape(space, drawShapeBB, NULL);
	
	drawInstructions();
	drawInfo();
	drawString(-300, -200, ChipmunkDemoMessageString);
		
	glutSwapBuffers();
	glClear(GL_COLOR_BUFFER_BIT);
}
Ejemplo n.º 3
0
// 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);
}
Ejemplo n.º 4
0
//destroy all allocated memory of cpSpace struct
void core_destroy_space ( cpSpace *space ) {
    
    //iterates through shapes and bodies and destroys them
    cpSpaceEachShape ( space, &core_destroy_shape, (void *) NULL );
    cpSpaceEachBody ( space, &core_destroy_body, (void *) NULL );
    
    destroy_body_info(space->staticBody->data);
    
    game_info_destroy ( (GameInfo *) space->data );
    //frees space
    cpSpaceFree ( space );
}
Ejemplo n.º 5
0
void CCPhysicsDebugNode::draw()
{
    if (! m_pSpacePtr)
    {
        return;
    }
    
    cpSpaceEachShape(m_pSpacePtr, (cpSpaceShapeIteratorFunc)DrawShape, this);
	cpSpaceEachConstraint(m_pSpacePtr, (cpSpaceConstraintIteratorFunc)DrawConstraint, this);
    
    CCDrawNode::draw();
    CCDrawNode::clear();
}
Ejemplo n.º 6
0
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
}
Ejemplo n.º 7
0
void PlayLayer::step(float delta) {
    
	int steps = 2;
	double dt = delta/(double)steps;
	
	if (dt > MAX_DELTA_FRAME) 
		dt = MAX_DELTA_FRAME;
	
    cpSpace * space = DoodleTruck::sharedDoodleTruck()->getSpace();
	for(int i = 0; i < steps; i++){
		cpSpaceStep(space, dt);
	}
    
	cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)&eachShape, NULL);
   	//cpSpaceHashEach(space->staticShapes, &eachShape, NULL);

}
Ejemplo n.º 8
0
static void
display(void)
{
	PrintStringBuffer[0] = 0;
	PrintStringCursor = PrintStringBuffer;
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glScalef(scale, scale, 1.0);
	glTranslatef(translate_x, translate_y, 0.0);
	
	demos[demoIndex].drawFunc();
	
	if(!paused || step){
		cpVect newPoint = cpvlerp(mouseBody->p, ChipmunkDemoMouse, 0.25f);
		mouseBody->v = cpvmult(cpvsub(newPoint, mouseBody->p), 60.0f);
		mouseBody->p = newPoint;
		
		demos[demoIndex].updateFunc(ticks);
		
		ticks++;
		ChipmunkDemoTime = ticks/60.0;
		
		step = cpFalse;
	}
  
	if(drawBBs) cpSpaceEachShape(space, drawShapeBB, NULL);
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix(); {
		// Draw the text at fixed positions,
		// but save the drawing matrix for the mouse picking
		glLoadIdentity();
		
		drawInstructions();
		drawInfo();
		drawString(-300, -200, ChipmunkDemoMessageString);
	} glPopMatrix();
		
	glutSwapBuffers();
	glClear(GL_COLOR_BUFFER_BIT);
}
Ejemplo n.º 9
0
inline void space_each_shape(cpSpace *space, void *f) {
  cpSpaceEachShape(space, eachShape_space, f);
}
Ejemplo n.º 10
0
void Space::eachShape(SpaceShapeIteratorFunc func)
{
		cpSpaceEachShape(space,*SpaceEachShape,&func);
}
Ejemplo n.º 11
0
void Space::eachShape(cpSpaceShapeIteratorFunc func,void *data)
{
		cpSpaceEachShape(space,func,data);
}
Ejemplo n.º 12
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);
}