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) { 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; }
// Init is called by the demo code to set up the demo. static cpSpace * init(void) { // Create an infinite mass body to attach ground segments and other static geometry to. // We won't be adding this body to the space, because we don't want it to be simulated at all. // Adding bodies to the space simulates them. (fall under the influence of gravity, etc) // We want the static body to stay right where it is at all times. staticBody = cpBodyNew(INFINITY, INFINITY); // Create a space, a space is a simulation world. It simulates the motions of rigid bodies, // handles collisions between them, and simulates the joints between them. space = cpSpaceNew(); // Lets set some parameters of the space: // More iterations make the simulation more accurate but slower space->iterations = 10; // These parameters tune the efficiency of the collision detection. // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpSpace cpSpaceResizeStaticHash(space, 30.0f, 1000); cpSpaceResizeActiveHash(space, 30.0f, 1000); // Give it some gravity space->gravity = cpv(0, -100); // Create A ground segment along the bottom of the screen cpShape *ground = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f); // Set some parameters of the shape. // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpShape ground->e = 1.0f; ground->u = 1.0f; ground->layers = NOT_GRABABLE_MASK; // Used by the Demo mouse grabbing code // Add the shape to the space as a static shape // If a shape never changes position, add it as static so Chipmunk knows it only needs to // calculate collision information for it once when it is added. // Do not change the postion of a static shape after adding it. cpSpaceAddStaticShape(space, ground); // Add a moving circle object. cpFloat radius = 15.0f; cpFloat mass = 10.0f; // This time we need to give a mass and moment of inertia when creating the circle. cpBody *ballBody = cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)); // Set some parameters of the body: // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpBody ballBody->p = cpv(0, -240 + radius+5); // Add the body to the space so it will be simulated and move around. cpSpaceAddBody(space, ballBody); // Add a circle shape for the ball. // Shapes are always defined relative to the center of gravity of the body they are attached to. // When the body moves or rotates, the shape will move with it. // Additionally, all of the cpSpaceAdd*() functions return the thing they added so you can create and add in one go. cpShape *ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero)); ballShape->e = 0.0f; ballShape->u = 0.9f; return space; }
static cpSpace * init(void) { cpSpace *space = cpSpaceNew(); space->iterations = 10; space->gravity = cpv(0, -GRAVITY); // space->sleepTimeThreshold = 1000; space->enableContactGraph = cpTrue; 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; 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; // Set up the player cpFloat radius = 25.0f; body = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY)); body->p = cpv(0, -200); body->velocity_func = playerUpdateVelocity; playerBody = body; shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); shape->e = 0.0f; shape->u = 0.0f; shape->collision_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)); shape->e = 0.0f; shape->u = 0.7f; } } return space; }
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; }
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 = 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 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++; }
Shell(cpVect pos, cpVect vel, float angle) { cpVect vl[4] = {cpv(0, 0), cpv(0.1, 0), cpv(0.07, 0.3), cpv(0.03, 0.3)}; int vn = sizeof(vl)/sizeof(cpVect); float mass = cpAreaForPoly(vn, vl, 0) * shell_density; float moi = cpMomentForPoly(mass, vn, vl, cpv(0, 0), 0); body = cpBodyNew(mass, moi); cpshape = cpPolyShapeNew(body, vn, vl, cpTransformIdentity, 0); cpShapeSetFriction(cpshape, 0.9); cpVect centroid = cpCentroidForPoly(vn, vl); shape.setPointCount(vn); for (int i = 0; i < vn; i++) { shape.setPoint(i, sf::Vector2f(vl[i].x, vl[i].y)); } cpBodySetCenterOfGravity(body, centroid); cpBodySetPosition(body, pos-centroid); cpBodySetVelocity(body, vel); cpBodySetAngle(body, angle); cpShapeSetCollisionType(cpshape, 2); cpShapeSetUserData(cpshape, this); }
void physics_add(Entity ent) { PhysicsInfo *info; if (entitypool_get(pool, ent)) return; /* already has physics */ transform_add(ent); info = entitypool_add(pool, ent); info->mass = 1.0; info->type = PB_DYNAMIC; /* create, init cpBody */ info->body = cpSpaceAddBody(space, cpBodyNew(info->mass, 1.0)); cpBodySetUserData(info->body, ent); /* for cpBody -> Entity mapping */ cpBodySetPos(info->body, cpv_of_vec2(transform_get_position(ent))); cpBodySetAngle(info->body, transform_get_rotation(ent)); info->last_dirty_count = transform_get_dirty_count(ent); /* initially no shapes */ info->shapes = array_new(ShapeInfo); /* initialize last_pos/last_ang info for kinematic bodies */ info->last_pos = cpBodyGetPos(info->body); info->last_ang = cpBodyGetAngle(info->body); info->collisions = NULL; }
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); }
int main(int argc, const char **argv) { cpInitChipmunk(); cp_collision_slop = 0.2f; #ifdef TIME_TRIAL sleep(1); for(int i=0; i<demoCount; i++){ if(i == 'l' - 'a') continue; time_trial(i, 1000); } // time_trial(0, 1000); exit(0); #endif mouseBody = cpBodyNew(INFINITY, INFINITY); glutStuff(argc, argv); runDemo(demos[firstDemoIndex]); glutMainLoop(); return 0; }
ETERM *space_add_body(ETERM *fromp, ETERM *argp) { // get the args ETERM *space_refp = erl_element(1, argp); ETERM *idp = erl_element(2, argp); ETERM *massp = erl_element(3, argp); // ETERM *inertiap = erl_element(4, argp); erlmunk_space *s; int space_id = ERL_REF_NUMBER(space_refp); HASH_FIND_INT(erlmunk_spaces, &space_id, s); int object_id = ERL_INT_VALUE(idp); cpBody *body = cpSpaceAddBody(s->space, cpBodyNew(ERL_FLOAT_VALUE(massp), INFINITY)); // the body is created inactive, it is explicitly activated // when all it's values have been set. cpBodySleep(body); erlmunk_body_data *data = malloc(sizeof(erlmunk_body_data)); data->id = object_id; data->term = NULL; cpBodySetUserData(body, (cpDataPointer) data); space_add_body_hash(s, object_id, body); return NULL; }
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; }
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 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); }
void TestColliderDetector::initWorld() { //! create physic space space = cpSpaceNew(); //! set space gravity as no gravity space->gravity = cpv(0, 0); //! Physics debug layer CCPhysicsDebugNode *debugLayer = CCPhysicsDebugNode::create(space); this->addChild(debugLayer, INT_MAX); //! get size of bullet CCSize size = bullet->getContentSize(); //! define bullet's collider body int num = 4; cpVect verts[] = { cpv(-size.width/2,-size.height/2), cpv(-size.width/2,size.height/2), cpv(size.width/2,size.height/2), cpv(size.width/2,-size.height/2), }; //! build body as verts' shape cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)); cpSpaceAddBody(space, body); cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero); shape->collision_type = eBulletTag; cpSpaceAddShape(space, shape); bullet->setCPBody(body); //! define armature2's body,get shape from armature data body = cpBodyNew(INFINITY, INFINITY); cpSpaceAddBody(space, body); armature2->setCPBody(body); shape = body->shapeList_private; while(shape){ cpShape *next = shape->next_private; shape->collision_type = eEnemyTag; shape = next; } cpSpaceAddCollisionHandler(space, eEnemyTag, eBulletTag, beginHit, NULL, NULL, endHit, NULL); }
cpBody* createNewBodyFrom(cpSpace* space, cpFloat xPos, cpFloat yPos, cpFloat mass, cpFloat moment, JSObject* outterJsObj) { cpBody* circularBody = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpBodySetPos(circularBody, cpv(xPos, yPos)); cpBodySetUserData(circularBody, outterJsObj); return circularBody; }
int main(int argc, const char **argv) { ChipmunkDemo demo_list[] = { LogoSmash, PyramidStack, Plink, BouncyHexagons, Tumble, PyramidTopple, Planet, Springies, Pump, TheoJansen, Query, OneWay, Joints, Tank, Chains, Crane, ContactGraph, Buoyancy, Player, Slice, Convex, Unicycle, }; demos = demo_list; demoCount = sizeof(demo_list)/sizeof(ChipmunkDemo); int trial = 0; for(int i=0; i<argc; i++){ if(strcmp(argv[i], "-bench") == 0){ demos = bench_list; demoCount = bench_count; } else if(strcmp(argv[i], "-trial") == 0){ trial = 1; } } if(trial){ // sleep(1); for(int i=0; i<demoCount; i++) time_trial(i, 1000); // time_trial('d' - 'a', 10000); exit(0); } else { mouseBody = cpBodyNew(INFINITY, INFINITY); glutStuff(argc, argv); runDemo(demoIndex); glutMainLoop(); } return 0; }
void Player::Initialize(void) { // Load the texture if (!(this->m_Texture = this->m_Hge->Texture_Load("data/particles.png"))) { MessageBox(NULL, "Can't load particles.png texture.", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL); this->m_Engine->Shutdown(); return; } // Create the player sprite this->m_Sprite = new hgeSprite(this->m_Texture, 60, 40, 20, 20); this->m_Sprite->SetColor(this->m_Color); this->m_Sprite->SetHotSpot(10, 10); // Create the particle sprite this->m_ParticleSprite = new hgeSprite(this->m_Texture, 20, 20, 20, 20); this->m_ParticleSprite->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE); this->m_ParticleSprite->SetHotSpot(10, 10); // Create the particle system this->m_ParticleSystem = new hgeParticleSystem("data/trail.psi", this->m_ParticleSprite); this->m_ParticleSystem->Fire(); // Initialize the weapon slots this->m_Weaponslots = (Weapon**) malloc(sizeof(Weapon*) * NUM_WEAPON_SLOTS); for (int i = 0 ; i < NUM_WEAPON_SLOTS ; ++i) { this->m_Weaponslots[i] = 0; } this->m_Weaponslots[0] = new Neoshooter(this, this->m_Engine->GetWorld()); // Initialize physics { // Define moment cpFloat moment = cpMomentForCircle(1, 0, 10, cpvzero); // Fetch the physics space cpSpace *space = this->m_Engine->GetWorld()->GetSpace(); // Add the physics body this->m_Body = cpSpaceAddBody(space, cpBodyNew(1, moment)); cpBodySetPos(this->m_Body, this->m_Position); // Add the physics shape this->m_Shape = cpSpaceAddShape(space, cpCircleShapeNew(this->m_Body, 10, cpvzero)); this->m_Shape->data = this; this->m_Shape->collision_type = COLLISION_TYPE_GO; cpShapeSetElasticity(this->m_Shape, 1.0f); cpShapeSetFriction(this->m_Shape, 0.7); // Define the collision handlers for various types of collisions cpSpaceAddCollisionHandler(space, COLLISION_TYPE_GO, COLLISION_TYPE_ACTIVATOR, BeginCollisionD, NULL, NULL, SeparateCollisionD, this); cpSpaceAddCollisionHandler(space, COLLISION_TYPE_GO, COLLISION_TYPE_BULLET, BeginCollisionD, NULL, NULL, NULL, this); } }
// adds a freestyle shape given a pointer to a an array of vertices and the number of vertices //returns a cpbody due to the many shapes associated with one body cpBody *core_add_freestyle_shape ( cpSpace * space, cpVect* verts , const int num_verts, Color *color, const double friction, const double elasticity, const double density, const int index ) { if ( num_verts <= 1 ) return NULL; // first determine center of mass of object cpVect center; core_freestyle_center ( verts, num_verts, ¢er ); // calculate mass and moment cpFloat mass = core_freestyle_mass ( verts, num_verts, density ); // dummy moment calculation cpFloat moment = core_freestyle_moment ( verts, num_verts, center, density ); cpBody *body = cpBodyNew ( mass, moment ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc)postStepAddBody, body, NULL ); cpBodySetPos ( body, center ); // set index of body // add body info to body BodyInfo * bi = body_info_new(num_verts); bi->index = index; bi->type = FREESTYLE_TYPE; bi->num_verts = num_verts; for ( int i = 0; i < num_verts; i++ ) { (bi->verts[i]).x = verts[i].x; (bi->verts[i]).y = verts[i].y; } bi->color->r = color->r; bi->color->g = color->g; bi->color->b = color->b; bi->friction = friction; bi->density = density; bi->elasticity = elasticity; body->data = bi; // add line segment collision shapes to body for ( int i = 0; i < num_verts - 1; i++ ) { cpVect offset_a = cpvmult ( cpvsub ( verts[i], center), 1.0 ); cpVect offset_b = cpvmult ( cpvsub ( verts[i+1], center), 1.0 ); cpShape * segment = cpSegmentShapeNew ( body, offset_a, offset_b, 0.1 ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddShape, segment, NULL); segment->u = friction; DrawShapeInfo *info = draw_shape_info_new (); info->color->r = color->r; info->color->g = color->g; info->color->b = color->b; segment->data = ( cpDataPointer ) info; } return body; }
cpBody* drawing_new(cpFloat x, cpFloat y, long color_rgb) { cpBody *drawing = cpBodyNew(INITIAL, INITIAL); cpBodySetPos(drawing, cpv(x, y)); Point_array *pa = point_array_new(x, y, color_rgb); cpBodySetUserData(drawing, pa); return drawing; }
static void make_leg(cpSpace *space, cpFloat side, cpFloat offset, cpBody *chassis, cpBody *crank, cpVect anchor) { cpVect a, b; cpShape *shape; cpFloat leg_mass = 1.0f; // make leg a = cpvzero, b = cpv(0.0f, side); cpBody *upper_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f))); cpBodySetPosition(upper_leg, cpv(offset, 0.0f)); shape = cpSpaceAddShape(space, cpSegmentShapeNew(upper_leg, a, b, seg_radius)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); cpSpaceAddConstraint(space, cpPivotJointNew2(chassis, upper_leg, cpv(offset, 0.0f), cpvzero)); // lower leg a = cpvzero, b = cpv(0.0f, -1.0f*side); cpBody *lower_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f))); cpBodySetPosition(lower_leg, cpv(offset, -side)); shape = cpSpaceAddShape(space, cpSegmentShapeNew(lower_leg, a, b, seg_radius)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); shape = cpSpaceAddShape(space, cpCircleShapeNew(lower_leg, seg_radius*2.0f, b)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 1.0f); cpSpaceAddConstraint(space, cpPinJointNew(chassis, lower_leg, cpv(offset, 0.0f), cpvzero)); cpSpaceAddConstraint(space, cpGearJointNew(upper_leg, lower_leg, 0.0f, 1.0f)); cpConstraint *constraint; cpFloat diag = cpfsqrt(side*side + offset*offset); constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, upper_leg, anchor, cpv(0.0f, side))); cpPinJointSetDist(constraint, diag); constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, lower_leg, anchor, cpvzero)); cpPinJointSetDist(constraint, diag); }
bool AnimationLayer::init() { bool bRet = false; do { CC_BREAK_IF(!Layer::init()); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("running.plist"); spriteSheet = SpriteBatchNode::create("running.png"); addChild(spriteSheet); //init running action Vector< SpriteFrame * > animFrames; for(int i = 0; i < 8; i++) { String str = String::createWithFormat("runner%d.png",i)->getCString(); SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str.getCString()); animFrames.pushBack(frame); } Animation *animation = Animation::createWithSpriteFrames(animFrames, 0.1); runningAction = RepeatForever::create(Animate::create(animation)); //sprite = Sprite::createWithSpriteFrameName("runner0.png"); //sprite->setPosition(ccp(80,85)); sprite = PhysicsSprite::createWithSpriteFrameName("runner0.png"); auto contentSize = sprite->getContentSize(); //初始化身体 this->body = cpBodyNew(1,cpMomentForBox(1, contentSize.width, contentSize.height)); this->body->p = cpv(GlobalUtils::g_runnerStartX, GlobalUtils::g_groundHeight + contentSize.height/2); cpBodyApplyImpulse(this->body,cpv(150,0), cpv(0,0)); cpSpaceAddBody(this->space, this->body); //init shape this->shape = cpBoxShapeNew(this->body,contentSize.width-14,contentSize.height); cpSpaceAddShape(this->space, this->shape); sprite->setCPBody(this->body); sprite->runAction(runningAction); spriteSheet->addChild(sprite); scheduleUpdate(); bRet = true; }while(0); return bRet; }
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; }
cpBody* PhysicsWorld::AddRectBody(cpFloat width, cpFloat height, cpFloat xPosition, cpFloat yPosition, cpFloat mass) { // Create and add the body cpBody *boxBody = cpSpaceAddBody(mySpace, cpBodyNew(mass, cpMomentForBox(mass, width, height))); // Set it's position in the world cpBodySetPos(boxBody, cpv(xPosition, yPosition)); return boxBody; }
/** * Creates and sets up the hero entity * @return The hero entity created */ Entity_T *SetupHero() { int i, x; const GLfloat SPRITE_WIDTH = 32.0f / 128; const GLfloat SPRITE_HEIGHT = 48.0f / 192; Rect frames[20]; Animation_T animations[MAX_ANIMATIONS]; Entity_T *e = NewEntity(); e->currentAnimation = 0; e->currentFrame = 0; e->sprite = NewSprite(); e->body = cpBodyNew(100.0f, 100.0f); e->size.x = TILE_WIDTH; e->size.y = 1.5f * TILE_HEIGHT; e->shape = cpBoxShapeNew(e->body, e->size.x/2, e->size.y/2); for(i = 0;i < 20;i++) { frames[i].x = (i % 4) * SPRITE_WIDTH; frames[i].y = 1.0f - (i/4+1) * SPRITE_HEIGHT; frames[i].w = SPRITE_WIDTH; frames[i].h = SPRITE_HEIGHT; } for(x = 0;x < 4;x++) { animations[x].numFrames = 1; animations[x].frames[0] = x*4; animations[x].frameLengths[0] = 100; animations[x].flags = ANIMFLAG_NONE; } for(x = 4;x < 8;x++){ animations[x].numFrames = 4; for(i = 0;i < 4;i++){ animations[x].frames[i] = (x-4)*4+i; animations[x].frameLengths[i] = 100; } animations[x].flags = ANIMFLAG_NONE; } SetupSprite(e->sprite, "hero.png", frames, 20, animations, 8); //Set up the hero's inner light e->light = NewLight(); e->light->brightness = 0.75f; e->light->color.x = 1.0f; e->light->color.y = .95f; e->light->color.z = 0.5f; e->light->span = TILE_WIDTH / 2; e->light->offset.x = TILE_WIDTH / 2; e->light->offset.y = 0; SET_FLAG(e->flags, ENTFLAG_LIGHT); e->next = NULL; return e; }
void ChipmunkTestLayer::addNewSpriteAtPosition(cocos2d::Vec2 pos) { #if CC_ENABLE_CHIPMUNK_INTEGRATION int posx, posy; auto parent = getChildByTag(kTagParentNode); posx = CCRANDOM_0_1() * 200.0f; posy = CCRANDOM_0_1() * 200.0f; // posx = CCRANDOM_0_1() * 97.0f; // posy = CCRANDOM_0_1() * 103.0f; // posx = (posx % 4) * 85; // posy = (posy % 3) * 121; posx = 0; posy = 0; int num = 4; // cpVect verts[] = { // cpv(-24,-54), // cpv(-24, 54), // cpv( 24, 54), // cpv( 24,-54), // }; cpVect verts[] = { cpv(-24,-26), cpv(-24, 26), cpv(24, 26), cpv( 24,-26), }; cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)); body->p = cpv(pos.x, pos.y); cpSpaceAddBody(_space, body); cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero); //shape->e = 0.5f; shape->u = 0.5f; shape->e = 1.0f; shape->u = 0.8f; cpSpaceAddShape(_space, shape); //auto sprite = PhysicsSprite::createWithTexture(_spriteTexture, cocos2d::Rect(posx, posy, 85, 121)); auto sprite = PhysicsSprite::createWithTexture(_spriteTexture, cocos2d::Rect(posx, posy, 50, 50)); parent->addChild(sprite); sprite->setCPBody(body); sprite->setPosition(pos); #endif }