static cpSpace * init(void) { ChipmunkDemoMessageString = "Right click and drag to change the blocks's shape."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 30); cpSpaceSetGravity(space, cpv(0, -500)); cpSpaceSetSleepTimeThreshold(space, 0.5f); cpSpaceSetCollisionSlop(space, 0.5f); cpBody *body, *staticBody = cpSpaceGetStaticBody(space); // 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); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpFloat width = 50.0f; cpFloat height = 70.0f; cpFloat mass = width*height*DENSITY; cpFloat moment = cpMomentForBox(mass, width, height); body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height)); cpShapeSetFriction(shape, 0.6f); return space; }
Bumper::Bumper(Actor * actor, int facing_direction, float thickness) { //cout << "New bumper on " << actor->actorName << " " << actor->width << " " << actor->height << " " << facing_direction << "\n"; cpBody * body = actor->body; this->actor = actor; this->facing_direction = facing_direction; if(facing_direction == Facing::Left) { shape = cpSpaceAddShape( game_map->physSpace, boxWithOffset(body, thickness, actor->height, cpv(-actor->width/2 - 1, 0)) ); } else if(facing_direction == Facing::Right) { shape = cpSpaceAddShape( game_map->physSpace, boxWithOffset(body, thickness, actor->height, cpv(actor->width/2 + 1, 0)) ); } else if(facing_direction == Facing::Down) { shape = cpSpaceAddShape( game_map->physSpace, boxWithOffset(body, actor->width, thickness, cpv(0, -actor->height/2 - 1)) ); } else if(facing_direction == Facing::Up) { shape = cpSpaceAddShape( game_map->physSpace, boxWithOffset(body, actor->width, thickness, cpv(0, actor->height/2 + 1)) ); } shape->data = (void *) this; shape->sensor = true; shape->layers = PhysicsLayer::Map|PhysicsLayer::Enemy; shape->collision_type = PhysicsType::Bumper; shape->group = PhysicsGroup::Bumpers; grounded = 0; }
cpSpace *Slice::Init() { ChipmunkDemo::Init(); message = "Hold right bottom corner and slice with touch."; space = cpSpaceNew(); cpSpaceSetIterations(space, 30); cpSpaceSetGravity(space, cpv(0, -500)); 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(-1000,-240), cpv(1000,-240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); cpFloat width = 200.0f; cpFloat height = 300.0f; cpFloat mass = width*height*DENSITY; cpFloat moment = cpMomentForBox(mass, width, height); body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0)); cpShapeSetFriction(shape, 0.6f); return space; }
static cpSpace * init(void) { space = cpSpaceNew(); cpSpaceSetGravity(space, cpv(0, -600)); cpBody *body; cpShape *shape; // We create an infinite mass rogue body to attach the line segments too // This way we can control the rotation however we want. rogueBoxBody = cpBodyNew(INFINITY, INFINITY); cpBodySetAngVel(rogueBoxBody, 0.4f); // 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 = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, a, b, 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, b, c, 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, c, d, 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, d, a, 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpFloat mass = 1; cpFloat width = 60; cpFloat height = 30; // Add the bricks. for(int i=0; i<3; i++){ for(int j=0; j<7; j++){ body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height))); cpBodySetPos(body, cpv(i*60 - 150, j*30 - 150)); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.7f); } } return space; }
static cpSpace * init(void) { cpResetShapeIdCounter(); space = cpSpaceNew(); cpSpaceResizeActiveHash(space, 30.0f, 1000); space->iterations = 10; space->sleepTimeThreshold = 0.5f; cpBody *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; 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; for(int i=0; i<50; i++){ cpBody *body = add_box(10.0f, 1.0f); 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.0f, 10.0f); 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; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "One way platforms are trivial in Chipmunk using a very simple collision callback."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 10); cpSpaceSetGravity(space, cpv(0, -100)); 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); // Add our one way segment shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLISION_TYPE_ONE_WAY); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); // We'll use the data pointer for the OneWayPlatform struct platformInstance.n = cpv(0, 1); // let objects pass upwards cpShapeSetUserData(shape, &platformInstance); // Add a ball to test it out cpFloat radius = 15.0f; body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero))); cpBodySetPosition(body, cpv(0, -200)); cpBodySetVelocity(body, cpv(0, 170)); shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.9f); cpShapeSetCollisionType(shape, 2); cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, COLLISION_TYPE_ONE_WAY); handler->preSolveFunc = PreSolve; return space; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Sticky collisions using the cpArbiter data pointer."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 10); cpSpaceSetGravity(space, cpv(0, -1000)); cpSpaceSetCollisionSlop(space, 2.0); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // Create segments around the edge of the screen. shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv(-340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv( 340,-260), cpv( 340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv( 340,-260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340, 260), cpv( 340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); for(int i=0; i<200; i++){ cpFloat mass = 0.15f; cpFloat radius = 10.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); cpBodySetPos(body, cpv(cpflerp(-150.0f, 150.0f, frand()), cpflerp(-150.0f, 150.0f, frand()))); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius + STICK_SENSOR_THICKNESS, cpvzero)); cpShapeSetFriction(shape, 0.9f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); } cpSpaceAddCollisionHandler(space, COLLIDE_STICK_SENSOR, COLLIDE_STICK_SENSOR, NULL, StickyPreSolve, NULL, StickySeparate, NULL); return space; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Right click to make pentagons static/dynamic."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 5); cpSpaceSetGravity(space, cpv(0, -100)); cpBody *body, *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // 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)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); } } // 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)); } pentagon_mass = 1.0; pentagon_moment = cpMomentForPoly(1.0f, NUM_VERTS, verts, cpvzero); // Add lots of pentagons. for(int i=0; i<300; i++){ body = cpSpaceAddBody(space, cpBodyNew(pentagon_mass, pentagon_moment)); cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320; cpBodySetPos(body, cpv(x, 350)); shape = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.4f); } return space; }
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; }
static cpSpace * init(void) { cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 30); cpSpaceSetGravity(space, cpv(0, -100)); 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); // 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))); cpBodySetPosition(body, cpv(j*32 - i*16, 300 - i*32)); shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 30.0f, 30.0f, 0.5f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 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))); cpBodySetPosition(body, cpv(0, -240 + radius+5)); shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.9f); return space; }
static cpSpace * init(void) { QUERY_START = cpvzero; space = cpSpaceNew(); cpSpaceSetIterations(space, 5); { // 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))); cpBodySetPos(body, cpv(0.0f, 100.0f)); cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 20.0f)); } { // add a static segment cpSpaceAddShape(space, cpSegmentShapeNew(cpSpaceGetStaticBody(space), 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*M_PI*i/((cpFloat) NUM_VERTS); verts[i] = cpv(30*cos(angle), 30*sin(angle)); } cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero))); cpBodySetPos(body, 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))); cpBodySetPos(body, cpv(100.0f, 100.0f)); cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero)); } return space; }
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; }
ETERM *space_add_boundaries(ETERM *fromp, ETERM *argp) { // get the args ETERM *space_refp = erl_element(1, argp); ETERM *lower_leftp = erl_element(2, argp); ETERM *lower_rightp = erl_element(3, argp); ETERM *upper_leftp = erl_element(4, argp); ETERM *upper_rightp = erl_element(5, argp); ETERM *collision_categoryp = erl_element(6, argp); ETERM *datap = erl_element(7, argp); erlmunk_space *s; int space_id = ERL_REF_NUMBER(space_refp); HASH_FIND_INT(erlmunk_spaces, &space_id, s); cpVect lowerLeft = cpv(ERL_FLOAT_VALUE(erl_element(1, lower_leftp)), ERL_FLOAT_VALUE(erl_element(2, lower_leftp))); cpVect lowerRight = cpv(ERL_FLOAT_VALUE(erl_element(1, lower_rightp)), ERL_FLOAT_VALUE(erl_element(2, lower_rightp))); cpVect upperLeft = cpv(ERL_FLOAT_VALUE(erl_element(1, upper_leftp)), ERL_FLOAT_VALUE(erl_element(2, upper_leftp))); cpVect upperRight = cpv(ERL_FLOAT_VALUE(erl_element(1, upper_rightp)), ERL_FLOAT_VALUE(erl_element(2, upper_rightp))); // get the static body that comes with the space cpBody *static_body = cpSpaceGetStaticBody(s->space); erlmunk_body_data *data = malloc(sizeof(erlmunk_body_data)); data->id = BOUNDARY_BODY_ID; data->term = erl_copy_term(datap); cpBodySetUserData(static_body, (cpDataPointer) data); // bottom cpShape *bottomBoundaryShape = cpSegmentShapeNew(static_body, lowerLeft, lowerRight, 0.0f); cpShapeSetCollisionType(bottomBoundaryShape, ERL_INT_VALUE(collision_categoryp)); cpSpaceAddShape(s->space, bottomBoundaryShape); // top cpShape *topBoundaryShape = cpSegmentShapeNew(static_body, upperLeft, upperRight, 0.0f); cpShapeSetCollisionType(topBoundaryShape, ERL_INT_VALUE(collision_categoryp)); cpSpaceAddShape(s->space, topBoundaryShape); // left cpShape *leftBoundaryShape = cpSegmentShapeNew(static_body, lowerLeft, upperLeft, 0.0f); cpShapeSetCollisionType(leftBoundaryShape, ERL_INT_VALUE(collision_categoryp)); cpSpaceAddShape(s->space, leftBoundaryShape); // right cpShape *rightBoundaryShape = cpSegmentShapeNew(static_body, lowerRight, upperRight, 0.0f); cpShapeSetCollisionType(rightBoundaryShape, ERL_INT_VALUE(collision_categoryp)); cpSpaceAddShape(s->space, rightBoundaryShape); return NULL; }
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; }
int l_physics_newShape(lua_State* state) { l_tools_checkUserDataPlusErrMsg(state, 1, "You must provide a space"); l_tools_checkUserDataPlusErrMsg(state, 2, "You must provide a body"); l_physics_PhysicsData* physics = (l_physics_PhysicsData*)lua_touserdata(state, 1); l_physics_Body* body = (l_physics_Body*)lua_touserdata(state, 2); float x1 = l_tools_toNumberOrError(state, 3); float y1 = l_tools_toNumberOrError(state, 4); float x2 = l_tools_toNumberOrError(state, 5); float y2 = l_tools_toNumberOrError(state, 6); float radius = l_tools_toNumberOrError(state, 7); moduleData.shape = (l_physics_Shape*)lua_newuserdata(state, sizeof(l_physics_Shape)); moduleData.shape->physics = malloc(sizeof(physics_PhysicsData)); moduleData.shape->physics = physics->physics; moduleData.shape->shape = cpSpaceAddShape(physics->physics->space, cpSegmentShapeNew(body->body, cpv(x1, y1), cpv(x2, y2), radius)); lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.shapeMT); lua_setmetatable(state, -2); return 1; }
int l_physics_newCircleShape(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 radius = l_tools_toNumberOrError(state, 3); cpVect offset = cpvzero; offset.x = l_tools_toNumberOrError(state, 4); offset.y = l_tools_toNumberOrError(state, 5); moduleData.shape = (l_physics_Shape*)lua_newuserdata(state, sizeof(l_physics_Shape)); moduleData.shape->physics = malloc(sizeof(physics_PhysicsData)); moduleData.shape->physics = physics->physics; moduleData.shape->shape = cpSpaceAddShape(physics->physics->space, cpCircleShapeNew(body->body, radius, offset)); lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.shapeMT); lua_setmetatable(state, -2); return 1; }
int lc_space_AddShape(lua_State *vm){ //space, shape lc_space *space = lc_GetSpace(1, vm); if (space == NULL){ printf("chipmunk: Object can't call :AddShape\n"); return 0; } lc_shape *shape = lc_GetShape(2, vm); if (shape == NULL){ printf("space:AddShape(): Can't add a shape\n"); return 0; } char pshape[32] = {0}; snprintf(pshape, 32, "%p", shape); //If the shape doesn't exists in shapes table then add it lua_rawgeti(vm, LUA_REGISTRYINDEX, space->shapes); lua_getfield(vm, -1, pshape); if (!lua_isnil(vm, -1)){ //The shape alreadye exists return 0; } lua_pop(vm, 1); lua_pushvalue(vm, 2); lua_setfield(vm, -2, pshape); lua_pop(vm, 1);//shapes table //--- cpSpaceAddShape(space->space, shape->shape); return 0; }
static palette_object_t *createPaletteObject(cpVect p, char *file, int dam, float m) { image_t *img; palette_object_t *obj; int numVerts = 4; cpVect verts[] = { {-16, -16}, {-16, 16}, { 16, 16}, { 16, -16} }; obj = (palette_object_t *)malloc(sizeof(palette_object_t)); obj->maxDamage = dam; obj->m = m; if (file) { img = atlFindImageFile(file, false, GL_CLAMP); strncpy(obj->texfile, img->name, PATH_MAX); obj->texnum = img->texnum; } else { obj->texnum = -1; } obj->body = cpBodyNewStatic(); obj->body->p = p; obj->shape = cpPolyShapeNew(obj->body, numVerts, verts, cpvzero); obj->shape->data = obj; obj->shape->collision_type = PALETTE_TYPE; cpSpaceAddShape(g_Space, obj->shape); return obj; }
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; }
void ColliderDetector::setBody(cpBody *pBody) { _body = pBody; for(auto& object : _colliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; ContourData *contourData = colliderBody->getContourData(); ssize_t num = contourData->vertexList.size(); auto vs = contourData->vertexList; cpVect *verts = new cpVect[num]; for (int i = 0; i < num; i++) { verts[num - 1 - i].x = vs.at(i).x; verts[num - 1 - i].y = vs.at(i).y; } cpShape *shape = cpPolyShapeNew(_body, (int)num, verts, cpvzero); shape->sensor = true; shape->data = _bone; if (_active) { cpSpaceAddShape(_body->space_private, shape); } colliderBody->setShape(shape); colliderBody->getColliderFilter()->updateShape(shape); delete []verts; } }
int mkparticle(particle_kind_t kind, cpVect pos, cpVect impulse, double energy) { particle_t* p = malloc(sizeof *p); if(p == NULL) { return -1; // bad malloc } cpSpace* space = current_space(); cpBody* body = cpBodyNew(energy / 1000.0, particle_moi); cpShape* shape = cpCircleShapeNew(body, particle_r, cpvzero); cpBodySetUserData(body, p); cpShapeSetUserData(shape, p); cpSpaceAddBody(space, body); cpSpaceAddShape(space, shape); *p = (particle_t){ .id = id, .kind = kind, .energy = energy, .life = energy, .body = body }; HASH_ADD_INT(particles, id, p); return id++; }
static void do_logic(game_state *state) { switch_state_data *data = state->data->switch_data; if (data->mouse_down_last_step && state->game->mouse_down == false) { /* clicked and released; create a circle */ cpFloat radius = 15; cpFloat mass = 10; cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero); cpBody *ball = cpSpaceAddBody(data->space, cpBodyNew(mass, moment)); cpBodySetPos(ball, state->game->mouse_pos); cpShape *ball_shape = cpSpaceAddShape(data->space, cpCircleShapeNew(ball, radius, cpvzero)); cpShapeSetElasticity(ball_shape, 0.7); cpShapeSetFriction(ball_shape, 0.7); cpShapeSetLayers(ball_shape, L_PLAYER); } cpSpaceStep(data->space, TARGET_SEC_PER_FRAME); data->mouse_down_last_step = state->game->mouse_down; if (ent_switch_get_state(data->sw)) debug_puts("switch is on"); else debug_puts("switch is off"); }
void WordTreeScene::updatePhysics(Word* word) { cpShape* shape = word->getShape(); if( shape!=NULL ) { cpSpaceRemoveShape(ChipmunkManager::getInstance()->getSpace(), shape); cpShapeDestroy(shape); word->setShape(NULL); } cpBody* body = word->getBody(); if( body!=NULL ) { cpSpaceRemoveBody(ChipmunkManager::getInstance()->getSpace(), body); cpBodyDestroy(body); word->setBody(NULL); } CCLabelTTF* targetLabel = word->getLabel(); CCPoint pos = targetLabel->getPosition(); CCSize contentSize = targetLabel->getContentSize(); // init body body = cpBodyNew(1, INFINITY); body->p = cpv(targetLabel->getPosition().x, targetLabel->getPosition().y); cpSpaceAddBody(ChipmunkManager::getInstance()->getSpace(), body); float size = MAX(targetLabel->getContentSize().width, targetLabel->getContentSize().height); shape = cpBoxShapeNew(body, size+size*0.3f, size+size*0.3f); cpSpaceAddShape(ChipmunkManager::getInstance()->getSpace(), shape); word->setBody(body); word->setShape(shape); }
KDvoid ColliderDetector::setCPBody ( cpBody* pBody ) { m_pCPBody = pBody; for(auto object : *m_pColliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; ContourData *contourData = colliderBody->getContourData(); int num = contourData->m_tVertexList.count(); ContourVertex2 **vs = (ContourVertex2 **)contourData->m_tVertexList.data->arr; cpVect *verts = new cpVect[num]; for (int i = 0; i < num; i++) { verts[num - 1 - i].x = vs[i]->x; verts[num - 1 - i].y = vs[i]->y; } cpShape *shape = cpPolyShapeNew(m_pCPBody, num, verts, cpvzero); shape->sensor = true; shape->data = m_pBone; if ( m_bActive ) { cpSpaceAddShape(m_pCPBody->space, shape); } colliderBody->setShape(shape); colliderBody->getColliderFilter()->updateCPShape(shape); delete [] verts; } }
static unsigned int _shape_add(Entity ent, PhysicsShape type, cpShape *shape) { PhysicsInfo *info; ShapeInfo *shapeInfo; info = entitypool_get(pool, ent); error_assert(info); /* init ShapeInfo */ shapeInfo = array_add(info->shapes); shapeInfo->type = type; shapeInfo->shape = shape; /* init cpShape */ cpShapeSetBody(shape, info->body); cpSpaceAddShape(space, shape); cpShapeSetFriction(shapeInfo->shape, 1); cpShapeSetUserData(shapeInfo->shape, ent); /* update moment */ if (!cpBodyIsStatic(info->body)) { if (array_length(info->shapes) > 1) cpBodySetMoment(info->body, _moment(info->body, shapeInfo) + cpBodyGetMoment(info->body)); else cpBodySetMoment(info->body, _moment(info->body, shapeInfo)); } return array_length(info->shapes) - 1; }
static void post_step_body_replace_shapes(cpSpace *space, cpBody *body, void *data) { Body_data *pa = cpBodyGetUserData(body); int length = pa->x_values->len; cpBodyEachShape(body, (cpBodyShapeIteratorFunc) free_shape, NULL); cpSpaceReindexShapesForBody(space, body); //fprintf(stderr, "0Made it this far.\n"); for (int index = 1; index < length; index++) { cpFloat x = g_array_index(pa->x_values, cpFloat, index); cpFloat y = g_array_index(pa->y_values, cpFloat, index); cpShape *seg = cpSegmentShapeNew(body, cpv(x,y), cpv(g_array_index(pa->x_values, cpFloat, index - 1), g_array_index(pa->y_values, cpFloat, index - 1)), CRAYON_RADIUS); //fprintf(stderr, "1Made it this far.\n"); cpShapeSetFriction(seg, CRAYON_FRICTION); //fprintf(stderr, "2Made it this far.\n"); //cpShapeSetElasticity(cpShape *shape, cpFloat value)? cpSpaceAddShape(space, seg); //fprintf(stderr, "3Made it this far.\n"); } cpSpaceReindexShapesForBody(space, body); }
static void add_box(cpSpace *space) { const cpFloat size = 10.0f; const cpFloat mass = 1.0f; cpVect verts[] = { cpv(-size,-size), cpv(-size, size), cpv( size, size), cpv( size,-size), }; cpFloat radius = cpvlength(cpv(size, size)); cpVect pos = rand_pos(radius); cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero, 0.0f))); body->velocity_func = planetGravityVelocityFunc; cpBodySetPosition(body, pos); // Set the box's velocity to put it into a circular orbit from its // starting position. cpFloat r = cpvlength(pos); cpFloat v = cpfsqrt(gravityStrength / r) / r; cpBodySetVelocity(body, cpvmult(cpvperp(pos), v)); // Set the box's angular velocity to match its orbital period and // align its initial angle with its position. cpBodySetAngularVelocity(body, v); cpBodySetAngle(body, cpfatan2(pos.y, pos.x)); cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpTransformIdentity, 0.0)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.7f); }
ETERM *body_set_collision_circle(ETERM *fromp, ETERM *argp) { // get the args ETERM *space_refp = erl_element(1, argp); ETERM *idp = erl_element(2, argp); ETERM *radiusp = erl_element(3, argp); ETERM *collision_typep = erl_element(4, argp); erlmunk_space *s; int space_id = ERL_REF_NUMBER(space_refp); HASH_FIND_INT(erlmunk_spaces, &space_id, s); int body_id = ERL_INT_VALUE(idp); erlmunk_body *b; HASH_FIND_INT(s->bodies, &body_id, b); if (b == NULL) return NULL; cpShape *shape = cpSpaceAddShape(s->space, cpCircleShapeNew(b->body, ERL_FLOAT_VALUE(radiusp), cpvzero)); cpShapeSetCollisionType(shape, ERL_INT_VALUE(collision_typep)); // DEBUGF(("body_set_collision_circle has succeeded")); return NULL; }
bool shape::addToSpace( cpSpace *space ) { if( space == NULL ) return false; if( cpSpaceIsLocked( space ) == cpTrue ) return false; this->m_space = space; cpSpaceAddShape( space, this->m_shape ); return true; }
Line::Line(ShapeLine *src, float mass){ cpFloat moment = cpMomentForSegment(mass, toChipmunk(src->getA()), toChipmunk(src->getB()), src->getRadius()); DynamicBody::setup(cpShapeGetSpace(src->shape), mass, moment); ShapeLine::setup(src); cpSpaceRemoveShape(cpShapeGetSpace(shape), shape); cpSpaceAddShape(cpBodyGetSpace(body), shape); cpShapeSetBody(shape, body); }