Exemple #1
0
static cpSpace *initSpace(void) {
    int i;
    cpBody *staticBody;
    cpShape *shape;
    cpVect cannonPos;

    cpResetShapeIdCounter();

    g_Space = cpSpaceNew();
    g_Space->iterations = 30;
    g_Space->gravity = cpv(0, -300);

    staticBody = &g_Space->staticBody;
    shape = cpSpaceAddShape(g_Space, cpSegmentShapeNew(staticBody, cpv(-400,-290), cpv(-400,300), 0.0f));
    shape->e = 1.0f; shape->u = 0.0f;
    shape->collision_type = PLATFORM_TYPE;

    cannonPos = cpv(-350, -215);
    g_Cannon = createCannon(cannonPos, 30.0f, 6.0f);
    g_Cannon->ai = 0;
    for (i = 0; i < MAX_PROJECTILES; ++i) {
        g_Cannon->ammo[i] = createProjectile(6.0f, 1.0f);
    }

    platforms[0] = createPlatform(staticBody, cpv(-390, -240), cpv(1600, -240), 10.0f);

    fprintf(stderr, "Loading dominoes disabled\n");
    InitializeDominoes();

    cpSpaceAddCollisionHandler(g_Space, PROJECTILE_TYPE, DOMINO_OBJECT_TYPE,
                               NULL, NULL, postSolveProjectileDomino, NULL, NULL);

    return g_Space;
}
Exemple #2
0
static cpSpace *
init(void)
{
	staticBody = cpBodyNew(INFINITY, INFINITY);
	
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	cpSpaceResizeActiveHash(space, 30.0f, 1000);
	space->iterations = 10;
	
	cpShape *shape;
		
	// Create segments around the edge of the screen.
	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	
	for(int i=0; i<50; i++){
		cpBody *body = add_box(10.0, 1.0);
		
		cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, body, cpvzero, cpvzero));
		pivot->biasCoef = 0.0f; // disable joint correction
		pivot->maxForce = 1000.0f; // emulate linear friction
		
		cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(staticBody, body, 0.0f, 1.0f));
		gear->biasCoef = 0.0f; // disable joint correction
		gear->maxForce = 5000.0f; // emulate angular friction
	}
	
	// We joint the tank to the control body and control the tank indirectly by modifying the control body.
	tankControlBody = cpBodyNew(INFINITY, INFINITY);
	tankBody = add_box(15.0, 10.0);
	
	cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(tankControlBody, tankBody, cpvzero, cpvzero));
	pivot->biasCoef = 0.0f; // disable joint correction
	pivot->maxForce = 10000.0f; // emulate linear friction
	
	cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(tankControlBody, tankBody, 0.0f, 1.0f));
	gear->biasCoef = 1.0f; // limit angular correction rate
	gear->maxBias = 1.0f; // limit angular correction rate
	gear->maxForce = 500000.0f; // emulate angular friction
		
	return space;
}
Exemple #3
0
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->iterations = 5;
	space->gravity = cpv(0, -100);
	
	cpSpaceResizeStaticHash(space, 40.0f, 999);
	cpSpaceResizeActiveHash(space, 30.0f, 2999);
	
	cpBody *body, *staticBody = &space->staticBody;
	cpShape *shape;
	
	// Create vertexes for a pentagon shape.
	cpVect verts[NUM_VERTS];
	for(int i=0; i<NUM_VERTS; i++){
		cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
		verts[i] = cpv(10*cos(angle), 10*sin(angle));
	}
	
	// Vertexes for a triangle shape.
	cpVect tris[] = {
		cpv(-15,-15),
		cpv(  0, 10),
		cpv( 15,-15),
	};

	// Create the static triangles.
	for(int i=0; i<9; i++){
		for(int j=0; j<6; j++){
			cpFloat stagger = (j%2)*40;
			cpVect offset = cpv(i*80 - 320 + stagger, j*70 - 240);
			shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, tris, offset));
			shape->e = 1.0f; shape->u = 1.0f;
			shape->layers = NOT_GRABABLE_MASK;
		}
	}
	
	// Add lots of pentagons.
	for(int i=0; i<300; i++){
		body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, NUM_VERTS, verts, cpvzero)));
		cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320;
		body->p = cpv(x, 350);
		
		shape = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
		shape->e = 0.0f; shape->u = 0.4f;
	}
	
	return space;
}
Exemple #4
0
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->iterations = 10;
	space->gravity = cpv(0, -100);

	cpBody *body, *staticBody = &space->staticBody;
	cpShape *shape;

	// Create segments around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	
	// Add our one way segment
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->collision_type = 1;
	shape->layers = NOT_GRABABLE_MASK;
	
	// We'll use the data pointer for the OneWayPlatform struct
	platformInstance.n = cpv(0, 1); // let objects pass upwards
	platformInstance.passThruList = cpArrayNew(0);
	shape->data = &platformInstance;
	
	
	// Add a ball to make things more interesting
	cpFloat radius = 15.0f;
	body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
	body->p = cpv(0, -200);
	body->v = cpv(0, 170);

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	shape->e = 0.0f; shape->u = 0.9f;
	shape->collision_type = 2;
	
	cpSpaceAddCollisionHandler(space, 1, 2, NULL, preSolve, NULL, NULL, NULL);
	
	return space;
}
Exemple #5
0
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->iterations = 30;
	cpSpaceResizeStaticHash(space, 40.0f, 1000);
	cpSpaceResizeActiveHash(space, 40.0f, 1000);
	space->gravity = cpv(0, -100);
	space->sleepTimeThreshold = 0.5f;
	
	cpBody *body, *staticBody = &space->staticBody;
	cpShape *shape;
	
	// Create segments around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	
	// Add lots of boxes.
	for(int i=0; i<14; i++){
		for(int j=0; j<=i; j++){
			body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForBox(1.0f, 30.0f, 30.0f)));
			body->p = cpv(j*32 - i*16, 300 - i*32);
			
			shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 30.0f, 30.0f));
			shape->e = 0.0f; shape->u = 0.8f;
		}
	}
	
	// Add a ball to make things more interesting
	cpFloat radius = 15.0f;
	body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
	body->p = cpv(0, -240 + radius+5);

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	shape->e = 0.0f; shape->u = 0.9f;
	
	return space;
}
Exemple #6
0
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	cpSpaceResizeActiveHash(space, 30.0f, 1000);
	space->iterations = 10;

	cpShape *shape;

	// Create segments around the edge of the screen.
	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(NULL, cpv(-320,-240), cpv(-320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(NULL, cpv(320,-240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(NULL, cpv(-320,-240), cpv(320,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(NULL, cpv(-320,240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	
	for(int i=0; i<10; i++)
		add_box();
	
	cpBody *body = cpSpaceAddBody(space, cpBodyNew(100.0f, 10000.0f));

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, cpv(-75,0), cpv(75,0), 5.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	
	cpSpaceAddConstraint(space, cpPivotJointNew2(body, NULL, cpvzero, cpvzero));
	
	return space;
}
Exemple #7
0
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->iterations = 5;
	space->gravity = cpv(0, -100);
	
	cpSpaceResizeStaticHash(space, 40.0f, 999);
	cpSpaceResizeActiveHash(space, 30.0f, 2999);
	
	cpBody *body, *staticBody = &space->staticBody;
	cpShape *shape;
	
	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	
	for(int i=0; i<NUM_CIRCLES; i++){
		body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 0.0f, circleRadius, cpvzero)));
		body->p = cpvmult(cpv(frand()*2.0f - 1.0f, frand()*2.0f - 1.0f), circleRadius*5.0f);
		
		circles[i] = shape = cpSpaceAddShape(space, cpCircleShapeNew(body, circleRadius, cpvzero));
		shape->e = 0.0f; shape->u = 1.0f;
	}
	
	strcat(messageString,
		"chipmunk_unsafe.h Contains functions for changing shapes, but they can cause severe stability problems if used incorrectly.\n"
		"Shape changes occur as instantaneous changes to position without an accompanying velocity change. USE WITH CAUTION!");
	return space;
}
Exemple #8
0
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->iterations = 10;
	space->gravity = cpv(0, -100);
	
	cpBody *staticBody = &space->staticBody;
	cpShape *shape;
	
	// Data structure for our ball emitter
	// We'll use two sensors for it, one to see if the emitter is blocked
	// a second to catch the balls and add them back to the emitter
	emitterInstance.queue = 5;
	emitterInstance.blocked = 0;
	emitterInstance.position = cpv(0, 150);
	
	// Create our blocking sensor, so we know when the emitter is clear to emit another ball
	shape = cpSpaceAddShape(space, cpCircleShapeNew(staticBody, 15.0f, emitterInstance.position));
	shape->sensor = 1;
	shape->collision_type = BLOCKING_SENSOR_TYPE;
	shape->data = &emitterInstance;
	
	// Create our catch sensor to requeue the balls when they reach the bottom of the screen
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-2000, -200), cpv(2000, -200), 15.0f));
	shape->sensor = 1;
	shape->collision_type = CATCH_SENSOR_TYPE;
	shape->data = &emitterInstance;
	
	cpSpaceAddCollisionHandler(space, BLOCKING_SENSOR_TYPE, BALL_TYPE, blockerBegin, NULL, NULL, blockerSeparate, NULL);
	cpSpaceAddCollisionHandler(space, CATCH_SENSOR_TYPE, BALL_TYPE, catcherBarBegin, NULL, NULL, NULL, NULL);
	
	return space;
}
Exemple #9
0
static VALUE
rb_cpShapeResetIdCounter(VALUE self)
{
	cpResetShapeIdCounter();
	return Qnil;
}
Exemple #10
0
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->elasticIterations = 0;
	space->iterations = 5;

	cpSpaceResizeStaticHash(space, 40.0f, 999);
	cpSpaceResizeActiveHash(space, 30.0f, 2999);

	cpBody *staticBody = &space->staticBody;
	cpShape *shape;
	
	// add a non-collidable segment as a quick and dirty way to draw the query line
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpvzero, cpv(100.0f, 0.0f), 4.0f));
	shape->layers = 0;
	querySeg = shape;
	
	{ // add a fat segment
		cpFloat mass = 1.0f;
		cpFloat length = 100.0f;
		cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f);
		
		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
		body->p = cpv(0.0f, 100.0f);
		
		cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 20.0f));
	}
	
	{ // add a static segment
		cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0, 300), cpv(300, 0), 0.0f));
	}
	
	{ // add a pentagon
		cpFloat mass = 1.0f;
		const int NUM_VERTS = 5;
		
		cpVect verts[NUM_VERTS];
		for(int i=0; i<NUM_VERTS; i++){
			cpFloat angle = -2*(cpFloat)M_PI*i/((cpFloat) NUM_VERTS);
			verts[i] = cpv(30*cosf(angle), 30*sinf(angle));
		}
		
		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
		body->p = cpv(50.0f, 50.0f);
		
		cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
	}
	
	{ // add a circle
		cpFloat mass = 1.0f;
		cpFloat r = 20.0f;
		
		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, r, cpvzero)));
		body->p = cpv(100.0f, 100.0f);
		
		cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero));
	}
	
	return space;
}
Exemple #11
0
 void CDynamics2DEngine::Init(TConfigurationNode& t_tree) {
    try {
       /* Init parent */
       CPhysicsEngine::Init(t_tree);
       /* Parse XML */
       GetNodeAttributeOrDefault(t_tree, "static_cell_size", m_fStaticHashCellSize, m_fStaticHashCellSize);
       GetNodeAttributeOrDefault(t_tree, "active_cell_size", m_fActiveHashCellSize, m_fActiveHashCellSize);
       GetNodeAttributeOrDefault(t_tree, "static_cells",     m_nStaticHashCells,    m_nStaticHashCells);
       GetNodeAttributeOrDefault(t_tree, "active_cells",     m_nActiveHashCells,    m_nActiveHashCells);
       GetNodeAttributeOrDefault(t_tree, "elevation",        m_fElevation,          m_fElevation);
       if(NodeExists(t_tree, "boundaries")) {
          /* Parse the boundary definition */
          TConfigurationNode& tBoundaries = GetNode(t_tree, "boundaries");
          SBoundarySegment sBoundSegment;
          CVector2 cLastPoint, cCurPoint;
          std::string strConnectWith;
          TConfigurationNodeIterator tVertexIt("vertex");
          /* Get the first vertex */
          tVertexIt = tVertexIt.begin(&tBoundaries);
          if(tVertexIt == tVertexIt.end()) {
             THROW_ARGOSEXCEPTION("Physics engine of type \"dynamics2d\", id \"" << GetId() << "\": you didn't specify any <vertex>!");
          }
          GetNodeAttribute(*tVertexIt, "point", cLastPoint);
          m_vecVertices.push_back(cLastPoint);
          /* Go through the other vertices */
          ++tVertexIt;
          while(tVertexIt != tVertexIt.end()) {
             /* Read vertex data and fill in segment struct */
             GetNodeAttribute(*tVertexIt, "point", cCurPoint);
             m_vecVertices.push_back(cCurPoint);
             sBoundSegment.Segment.SetStart(cLastPoint);
             sBoundSegment.Segment.SetEnd(cCurPoint);
             GetNodeAttribute(*tVertexIt, "connect_with", strConnectWith);
             if(strConnectWith == "gate") {
                /* Connect to previous vertex with a gate */
                sBoundSegment.Type = SBoundarySegment::SEGMENT_TYPE_GATE;
                GetNodeAttribute(*tVertexIt, "to_engine", sBoundSegment.EngineId);
             }
             else if(strConnectWith == "wall") {
                /* Connect to previous vertex with a wall */
                sBoundSegment.Type = SBoundarySegment::SEGMENT_TYPE_WALL;
                sBoundSegment.EngineId = "";
             }
             else {
                /* Parse error */
                THROW_ARGOSEXCEPTION("Physics engine of type \"dynamics2d\", id \"" << GetId() << "\": unknown vertex connection method \"" << strConnectWith << "\". Allowed methods are \"wall\" and \"gate\".");
             }
             m_vecSegments.push_back(sBoundSegment);
             /* Next vertex */
             cLastPoint = cCurPoint;
             ++tVertexIt;
          }
          /* Check that the boundary is a closed path */
          if(m_vecVertices.front() != m_vecVertices.back()) {
             THROW_ARGOSEXCEPTION("Physics engine of type \"dynamics2d\", id \"" << GetId() << "\": the specified path is not closed. The first and last points of the boundaries MUST be the same.");
          }
       }
       /* Initialize physics */
       cpInitChipmunk();
       cpResetShapeIdCounter();
       /* Used to attach static geometries so that they won't move and to simulate friction */
       m_ptGroundBody = cpBodyNew(INFINITY, INFINITY);
       /* Create the space to contain the movable objects */
       m_ptSpace = cpSpaceNew();
       /* Subiterations to solve constraints.
          The more, the better for precision but the worse for speed
       */
       m_ptSpace->iterations = GetIterations();
       /* Resize the space hash.
          This has dramatic effects on performance.
          TODO: - find optimal parameters automatically (average entity size)
          cpSpaceReindexStaticHash(m_ptSpace, m_fStaticHashCellSize, m_nStaticHashCells);
          cpSpaceResizeActiveHash(m_ptSpace, m_fActiveHashCellSize, m_nActiveHashCells);
       */
       /* Gripper-Gripped callback functions */
       cpSpaceAddCollisionHandler(
          m_ptSpace,
          SHAPE_GRIPPER,
          SHAPE_GRIPPABLE,
          BeginCollisionBetweenGripperAndGrippable,
          ManageCollisionBetweenGripperAndGrippable,
          NULL,
          NULL,
          NULL);
       /* Add boundaries, if specified */
       if(! m_vecSegments.empty()) {
          cpShape* ptSegment;
          for(size_t i = 0; i < m_vecSegments.size(); ++i) {
             if(m_vecSegments[i].Type == SBoundarySegment::SEGMENT_TYPE_WALL) {
                ptSegment =
                   cpSpaceAddShape(
                      m_ptSpace,
                      cpSegmentShapeNew(
                         m_ptGroundBody,
                         cpv(m_vecSegments[i].Segment.GetStart().GetX(),
                             m_vecSegments[i].Segment.GetStart().GetY()),
                         cpv(m_vecSegments[i].Segment.GetEnd().GetX(),
                             m_vecSegments[i].Segment.GetEnd().GetY()),
                         0.0f));
                ptSegment->e = 0.0f; // no elasticity
                ptSegment->u = 1.0f; // max friction
             }
             else {
                /* There is at least a gate, transfer is activated */
                m_bEntityTransferActive = true;
             }
          }
       }
    }
    catch(CARGoSException& ex) {
       THROW_ARGOSEXCEPTION_NESTED("Error initializing the dynamics 2D engine \"" << GetId() << "\"", ex);
    }
 }
Exemple #12
0
 void CDynamics2DEngine::AddEntity(CEntity& c_entity) {
    CallEntityOperation<CDynamics2DOperationAddEntity, CDynamics2DEngine, void>(*this, c_entity);
    cpResetShapeIdCounter();
 }
Exemple #13
0
static cpSpace *
init(void)
{
	staticBody = cpBodyNew(INFINITY, INFINITY);
	
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	cpSpaceResizeActiveHash(space, 40.0f, 999);
	cpSpaceResizeStaticHash(space, 40.0f, 99);
	space->gravity = cpv(0, -600);
	
	cpBody *body;
	cpShape *shape;
	
	// Vertexes for the bricks
	int num = 4;
	cpVect verts[] = {
		cpv(-30,-15),
		cpv(-30, 15),
		cpv( 30, 15),
		cpv( 30,-15),
	};
	
	// Set up the static box.
	cpVect a = cpv(-200, -200);
	cpVect b = cpv(-200,  200);
	cpVect c = cpv( 200,  200);
	cpVect d = cpv( 200, -200);
	
	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, a, b, 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, b, c, 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, c, d, 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, d, a, 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	
	// Give the box a little spin.
	// Because staticBody is never added to the space, we will need to
	// update it ourselves. (see above).
	// NOTE: Normally you would want to add the segments as normal and not static shapes.
	// I'm just doing it to demonstrate the cpSpaceRehashStatic() function.
	staticBody->w = 0.4f;
	
	// Add the bricks.
	for(int i=0; i<3; i++){
		for(int j=0; j<7; j++){
			body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)));
			body->p = cpv(i*60 - 150, j*30 - 150);
			
			shape = cpSpaceAddShape(space, cpPolyShapeNew(body, num, verts, cpvzero));
			shape->e = 0.0f; shape->u = 0.7f;
		}
	}
	
	return space;
}
Exemple #14
0
static iBOOL WordInit(void)
{
	// 屏幕信息数组
	iU32 * LcdInfoArr;
    if( i51AdeMmiGetScreenScale ( &ScreenWidth , &ScreenHeight ) == 0 )
    {
        return iFALSE;
    }

    LcdInfoArr =  i51AdeMmiGetPanel();
    if( iNULL == LcdInfoArr )
    {
        return iFALSE;
    }
  
    ScreenBuffer = (iU16 *) LcdInfoArr[1] ;

	KeyQueue = i51KitKeyInputQueueCreat();
	if( iNULL==KeyQueue ) return iFALSE;	

	cpInitChipmunk();

	cpResetShapeIdCounter();
	
	/// init space.
	mmSABSpace = cpSpaceNew();
	
	mmSABSpace->iterations = 1;
	mmSABSpace->elasticIterations = 10;
	
	mmSABSpace->gravity = cpv(0, 0);
	
	mmSABSpace->sleepTimeThreshold = 0.2f;//0.2f;

	cpBody *body, *staticBody = &(mmSABSpace->staticBody);
	cpShape *shape;
	cpFloat radius = 2.0f;
	
	for(int i=1; i<60; i++ )
	{
		for( int j = 1; j < 40; j++ )
		{
			WordFlag[i][j] = 1;
		}
	}
	
	for(int i=0; i<320; i++ )
	{
		for( int j = 0; j < 240; j++ )
		{
			BackgroundType[i][j] = 1;
		}
	}
	// Create segments around the edge of the screen.
//i51AdeOsLog(0, "cpSpaceAddShape 1");
//	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(0,0), cpv(239,0), 0.0f));
//i51AdeOsLog(0, "cpShapeSetElasticity");
//	shape->e = 1.0f;
//	shape->u = 1.0f;
//	shape->layers= 0X7FFFFFFF;
//	cpShapeSetElasticity(shape, 1.0f);
//i51AdeOsLog(0, "cpShapeSetFriction");
//	mmSABSpace->fr = 1.0f;
//	cpShapeSetFriction(shape, 1.0f);
//i51AdeOsLog(0, "cpShapeSetLayers");
//	mmSABSpace->
//	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);

//i51AdeOsLog(0, "cpSpaceAddShape 2");
	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(0,0), cpv(0,319), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;
//	cpShapeSetElasticity(shape, 1.0f);
//	cpShapeSetFriction(shape, 1.0f);
//	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);

//i51AdeOsLog(0, "cpSpaceAddShape 3");
	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(239,0), cpv(239,319), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;

	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(0,319), cpv(239,319), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;

	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(0,30), cpv(110,140), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;
	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(110,200), cpv(110,140), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;
	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(110,200), cpv(90,319), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;
	
	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(239,30), cpv(135,140), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;
	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(135,200), cpv(135,140), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;
	shape = cpSpaceAddShape(mmSABSpace, cpSegmentShapeNew(staticBody, cpv(135,200), cpv(155,319), 0.0f));
	shape->e = 1.0f;
	shape->u = 1.0f;
	shape->layers= 0X7FFFFFFF;

	cpFloat mass = 1;
	cpFloat width = 10;
	cpFloat height = 20;
	
	cpFloat spacing = width*0.3;
	
	// Add lots of boxes.
	for(int i=0; i<CHAIN_COUNT; i++){
		cpBody *prev = NULL;
		
		for(int j=0; j<LINK_COUNT; j++){
//i51AdeOsLog(0, "i=%d,j=%d",i,j);
//			cpVect pos = cpv(rand()%228+5,rand()%350-400);
cpVect pos = cpv((j)*4+5,(i)*4+5);
			
//i51AdeOsLog(0, "cpSpaceAddBody");
//			body = cpSpaceAddBody(mmSABSpace, cpBodyNew(mass, cpMomentForBox(mass, width, height)));
//			body = cpSpaceAddBody(mmSABSpace, cpBodyNew(10.0f, cpMomentForCircle(30.0f, 0.0f, radius, cpvzero)));
			body = cpSpaceAddBody(mmSABSpace, cpBodyNew(60.0f, cpMomentForCircle(60.0f, 0.0f, radius, cpvzero)));
			cpBodySetPos(body, pos);
			cpBodySetID(body,i*LINK_COUNT+j);
			cpArray ** TempArr = (cpArray **)(mmSABSpace->bodies->arr+(i*LINK_COUNT+j));
			cpBody * TempPoint = (cpBody *)(*(mmSABSpace->bodies->arr+(i*LINK_COUNT+j)));
			
//i51AdeOsLog(0, "cpSpaceAddShape");
//			shape = cpSpaceAddShape(mmSABSpace, cpBoxShapeNew(body, width, height));
shape = cpSpaceAddShape(mmSABSpace, cpCircleShapeNew(body, radius, cpvzero));
shape->e = 0.2f; 
shape->u = 0.01f; 
cpPolyShape * PolyShape = (cpPolyShape *)shape;
//			cpShapeSetFriction(shape, 0.8f);
			
			cpFloat breakingForce = 80000;
			
			cpConstraint *constraint = NULL;
//i51AdeOsLog(0, "cpSpaceAddConstraint");
			if(prev == NULL){
//				constraint = cpSpaceAddConstraint(mmSABSpace, cpSlideJointNew(body, staticBody, cpv(0, -height/2), cpv(pos.x, 0), 0, spacing));
			} else {
//				constraint = cpSpaceAddConstraint(mmSABSpace, cpSlideJointNew(body, prev, cpv(0, -height/2), cpv(0, height/2), 0, spacing));
			}
//			constraint->maxForce = breakingForce;
//			constraint->
//			cpConstraintSetMaxForce(constraint, breakingForce);
//			cpConstraintSetPostSolveFunc(constraint, BreakableJointPostSolve);
			
			prev = body;
//i51AdeOsLog(0, "prev = body;");
		}
	}
	
//i51AdeOsLog(0, "cpSpaceAddBody");
//	body = cpSpaceAddBody(mmSABSpace, cpBodyNew(10.0f, cpMomentForCircle(30.0f, 0.0f, radius, cpvzero)));
//i51AdeOsLog(0, "cpBodySetPos");
//	cpBodySetPos(body, cpv(130-radius, 320 - radius*2));
//	body->v = cpv(0,-2000);
//cpBodySetPos(body, cpv(120-radius, 0));
//i51AdeOsLog(0, "cpBodySetVel");
//	cpBodySetVel(body, cpv(0, -300));

//i51AdeOsLog(0, "cpSpaceAddShape");
//	shape = cpSpaceAddShape(mmSABSpace, cpCircleShapeNew(body, radius, cpvzero));
//shape->e = 0.0f;
//shape->u = 0.9f;
//i51AdeOsLog(0, "cpShapeSetElasticity");
//	cpShapeSetElasticity(shape, 0.0f);
//i51AdeOsLog(0, "cpShapeSetFriction");
//	cpShapeSetFriction(shape, 0.9f);

//i51AdeOsLog(0, "return mmSABSpace");
/*
cpSpaceHash * activeShapes = mmSABSpace->activeShapes;
cpHashSetBin **table = activeShapes->handleSet->table;
int size = 	activeShapes->handleSet->size;
for(int i=0;i<size;i++)
{
	shape = (cpShape*)table[i];
	if(shape)
	{
		if(shape->klass->type==CP_POLY_SHAPE)
		{
			iS32 Point[4][2];
			cpPolyShape * PolyShape = (cpPolyShape *)shape;
			i51KitG2CleanScreen(0, 0);
			Point[0][0] = (iS32)PolyShape->verts[0].x;
			Point[0][1] = (iS32)PolyShape->verts[0].y;
			Point[1][0] = (iS32)PolyShape->verts[1].x;
			Point[1][1] = (iS32)PolyShape->verts[1].y;
			Point[2][0] = (iS32)PolyShape->verts[2].x;
			Point[2][1] = (iS32)PolyShape->verts[2].y;
			Point[3][0] = (iS32)PolyShape->verts[3].x;
			Point[3][1] = (iS32)PolyShape->verts[3].y;
			i51KitG2DrawPolygon(4,Point,0XFF00);
			i51KitG2Update();
		}
	}
}
*/

}
Exemple #15
0
static cpSpace *
init(void)
{
	staticBody = cpBodyNew(INFINITY, INFINITY);
	
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->iterations = 10;
	space->gravity = cpv(0, -1500);
	
	cpBody *body;
	cpShape *shape;
	
	// Create segments around the edge of the screen.
	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	shape->collision_type = 2;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	shape->collision_type = 2;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	shape->collision_type = 2;
	
	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	shape->collision_type = 2;
	
	// add some other segments to play with
	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-220,-200), cpv(-220,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	shape->collision_type = 2;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(320,-200), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	shape->collision_type = 2;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(200,-240), cpv(320,-100), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	shape->collision_type = 2;

	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-220,-80), cpv(200,-80), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	shape->collision_type = 2;
	
	// Set up the player
	cpFloat radius = 15.0f;
	body = cpSpaceAddBody(space, cpBodyNew(10.0f, INFINITY));
	body->p = cpv(0, -220);
	body->velocity_func = playerUpdateVelocity;

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	shape->e = 0.0f; shape->u = 2.0f;
	shape->collision_type = 1;
	
	playerInstance.u = shape->u;
	playerInstance.shape = shape;
	playerInstance.groundShapes = cpArrayNew(0);
	shape->data = &playerInstance;
	
	cpSpaceAddCollisionHandler(space, 1, 2, begin, preSolve, NULL, separate, NULL);
	
	return space;
}
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->iterations = 20;
	cpSpaceResizeActiveHash(space, 30.0f, 2999);
	cpSpaceResizeStaticHash(space, 30.0f, 999);
	space->gravity = cpv(0, -300);

	cpBody *body;
	
	cpShape *shape;
	
	// Vertexes for the dominos.
	int num = 4;
	cpVect verts[] = {
		cpv(-3,-20),
		cpv(-3, 20),
		cpv( 3, 20),
		cpv( 3,-20),
	};
	
	// Add a floor.
	shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(NULL, cpv(-600,-240), cpv(600,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	
	// Shared friction constant.
	cpFloat u = 0.6f;
	
	// Add the dominoes. Skim over this. It doesn't do anything fancy, and it's hard to follow.
	int n = 9;
	for(int i=1; i<=n; i++){
		cpVect offset = cpv(-i*60/2.0f, (n - i)*52);
		
		for(int j=0; j<i; j++){
			body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)));
			body->p = cpvadd(cpv(j*60, -220), offset);
			
			shape = cpSpaceAddShape(space, cpPolyShapeNew(body, num, verts, cpvzero));
			shape->e = 0.0f; shape->u = u;
			

			body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)));
			body->p = cpvadd(cpv(j*60, -197), offset);
			cpBodySetAngle(body, M_PI/2.0f);
			
			shape = cpSpaceAddShape(space, cpPolyShapeNew(body, num, verts, cpvzero));
			shape->e = 0.0f; shape->u = u;
			
			
			if(j == (i - 1)) continue;
			body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)));
			body->p = cpvadd(cpv(j*60 + 30, -191), offset);
			cpBodySetAngle(body, M_PI/2.0f);
			
			shape = cpSpaceAddShape(space, cpPolyShapeNew(body, num, verts, cpvzero));
			shape->e = 0.0f; shape->u = u;
		}

		body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)));
		body->p = cpvadd(cpv(-17, -174), offset);
		
		shape = cpSpaceAddShape(space, cpPolyShapeNew(body, num, verts, cpvzero));
		shape->e = 0.0f; shape->u = u;
		

		body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)));
		body->p = cpvadd(cpv((i - 1)*60 + 17, -174), offset);
		
		shape = cpSpaceAddShape(space, cpPolyShapeNew(body, num, verts, cpvzero));
		shape->e = 0.0f; shape->u = u;
	}
	
	// Give the last domino a little tap.
//	body->w = -1;
//	body->v = cpv(-body->w*20, 0);
	return space;
}