static cpSpace * init(void) { cpSpace *space = cpSpaceNew(); space->iterations = 10; space->gravity = cpv(0, -GRAVITY); // space->sleepTimeThreshold = 1000; cpBody *body, *staticBody = cpSpaceGetStaticBody(space); 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; cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); // Set up the player body = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY)); body->p = cpv(0, -200); body->velocity_func = playerUpdateVelocity; playerBody = body; shape = cpSpaceAddShape(space, cpBoxShapeNew2(body, cpBBNew(-15.0, -27.5, 15.0, 27.5), 10.0)); // shape = cpSpaceAddShape(space, cpSegmentShapeNew(playerBody, cpvzero, cpv(0, radius), radius)); shape->e = 0.0f; shape->u = 0.0f; shape->type = 1; playerShape = shape; // Add some boxes to jump on for(int i=0; i<6; i++){ for(int j=0; j<3; j++){ body = cpSpaceAddBody(space, cpBodyNew(4.0f, INFINITY)); body->p = cpv(100 + j*60, -200 + i*60); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 50, 50, 0.0)); shape->e = 0.0f; shape->u = 0.7f; } } return space; }
void init_physics() { // setup space space = cpSpaceNew(); // setup tires int i; for(i=0; i<1; i++) { cpFloat x = 0, y = 0, w = 0.5, h = 1.75; cpFloat mass = 50; cpFloat moment = cpMomentForBox(mass, w, h); tire[i] = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpSpaceAddShape(space, cpBoxShapeNew2(tire[i], cpBBNew(x-(w/2), y-(h/2), x+(w/2), y+(h/2)))); cpSpaceReindexShapesForBody(space, tire[0]); cpBodySetAngle(tire[i], -M_PI/6); //cpBodySetVel(tire[i], cpv(0, 10)); //cpBodyApplyImpulse(tire[i], cpv(0, 1000), cpvzero); } }
int l_physics_newBoxShape(lua_State* state) { l_tools_checkUserDataPlusErrMsg(state, 1, "You must provide a space!"); l_physics_PhysicsData* physics = (l_physics_PhysicsData*)lua_touserdata(state, 1); l_tools_checkUserDataPlusErrMsg(state, 2, "You must provide a body"); l_physics_Body* body = (l_physics_Body*)lua_touserdata(state, 2); float width = l_tools_toNumberOrError(state, 3); float height = luaL_optnumber(state, 4, width); float radius = luaL_optnumber(state, 5, 0.0f); float offset_x = luaL_optnumber(state, 6, 0.0f); float offset_y = luaL_optnumber(state, 7, 0.0f); moduleData.shape = (l_physics_Shape*)lua_newuserdata(state, sizeof(l_physics_Shape)); moduleData.shape->physics = malloc(sizeof(physics_PhysicsData)); moduleData.shape->physics = physics->physics; if (offset_x == 0 && offset_y == 0) // No offset is needed moduleData.shape->shape = cpSpaceAddShape(physics->physics->space, cpBoxShapeNew(body->body, width, height, radius)); else { //Apply offset to shape! cpVect off = cpv(offset_x, offset_y); cpBB bb = cpBBNewForExtents(off, width * 0.5f, height * 0.5f); moduleData.shape->shape = cpSpaceAddShape(physics->physics->space, cpBoxShapeNew2(body->body, bb, radius)); } lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.shapeMT); lua_setmetatable(state, -2); return 1; }
cpSpace *Buoyancy::Init() { ChipmunkDemo::Init(); space = cpSpaceNew(); cpSpaceSetIterations(space, 30); cpSpaceSetGravity(space, cpv(0, -500)); // cpSpaceSetDamping(space, 0.5); cpSpaceSetSleepTimeThreshold(space, 0.5f); cpSpaceSetCollisionSlop(space, 0.5f); cpBody *body, *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // Create segments around the edge of the screen. shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); { // Add the edges of the bucket cpBB bb = cpBBNew(-300, -200, 100, 0); cpFloat radius = 5.0f; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.l, bb.b), cpv(bb.l, bb.t), radius)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.r, bb.b), cpv(bb.r, bb.t), radius)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.l, bb.b), cpv(bb.r, bb.b), radius)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); // Add the sensor for the water. shape = cpSpaceAddShape(space, cpBoxShapeNew2(staticBody, bb, 0.0)); cpShapeSetSensor(shape, cpTrue); cpShapeSetCollisionType(shape, 1); } { cpFloat width = 200.0f; cpFloat height = 50.0f; cpFloat mass = 0.3*FLUID_DENSITY*width*height; cpFloat moment = cpMomentForBox(mass, width, height); body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpBodySetPosition(body, cpv(-50, -100)); cpBodySetVelocity(body, cpv(0, -100)); cpBodySetAngularVelocity(body, 1); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0)); cpShapeSetFriction(shape, 0.8f); } { cpFloat width = 40.0f; cpFloat height = width*2; cpFloat mass = 0.3*FLUID_DENSITY*width*height; cpFloat moment = cpMomentForBox(mass, width, height); body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpBodySetPosition(body, cpv(-200, -50)); cpBodySetVelocity(body, cpv(0, -100)); cpBodySetAngularVelocity(body, 1); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0)); cpShapeSetFriction(shape, 0.8f); } cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space, 1, 0); handler->preSolveFunc = (cpCollisionPreSolveFunc)WaterPreSolve; handler->userData = this; return space; }