1 2 // written in the D programming language 3 4 module samples.Plink; 5 6 import dchip.all; 7 8 import samples.ChipmunkDemo; 9 10 import std.math; 11 12 static cpSpace *space; 13 14 // Iterate over all of the bodies and reset the ones that have fallen offscreen. 15 static void 16 eachBody(cpBody *_body, void *unused) 17 { 18 if(_body.p.y < -260 || cpfabs(_body.p.x) > 340){ 19 cpFloat x = frand()*640 - 320; 20 _body.p = cpv(x, 260); 21 } 22 } 23 24 static void 25 update(int ticks) 26 { 27 int steps = 1; 28 cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps; 29 30 for(int i=0; i<steps; i++){ 31 cpSpaceStep(space, dt); 32 cpSpaceEachBody(space, &eachBody, null); 33 } 34 } 35 36 enum NUM_VERTS = 5; 37 38 static cpSpace * 39 init() 40 { 41 cpResetShapeIdCounter(); 42 43 space = cpSpaceNew(); 44 space.iterations = 5; 45 space.gravity = cpv(0, -100); 46 47 cpBody *_body; 48 cpBody *staticBody = space.staticBody; 49 cpShape *shape; 50 51 // Create vertexes for a pentagon shape. 52 cpVect verts[NUM_VERTS]; 53 for(int i=0; i<NUM_VERTS; i++){ 54 cpFloat angle = -2.0f*PI*i/(cast(cpFloat) NUM_VERTS); 55 verts[i] = cpv(10.0f*cos(angle), 10.0f*sin(angle)); 56 } 57 58 // Vertexes for a triangle shape. 59 //port: ? 60 enum cpVect tris[] = [ 61 cpv(-15,-15), 62 cpv( 0, 10), 63 cpv( 15,-15), 64 ]; 65 66 // Create the static triangles. 67 foreach(i; 0..9){ 68 foreach(j; 0..6){ 69 cpFloat stagger = (j%2)*40; 70 cpVect offset; 71 offset.x = (i*80) - 320 + stagger; 72 offset.y = (j*70) - 240; 73 74 shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, tris.ptr, offset)); 75 shape.e = 1.0f; shape.u = 1.0f; 76 shape.layers = NOT_GRABABLE_MASK; 77 } 78 } 79 80 // Add lots of pentagons. 81 for(int i=0; i<300; i++){ 82 _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, NUM_VERTS, verts.ptr, cpvzero))); 83 cpFloat x = frand()*640 - 320; 84 _body.p = cpv(x, 350); 85 86 shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, NUM_VERTS, verts.ptr, cpvzero)); 87 shape.e = 0.0f; shape.u = 0.4f; 88 } 89 90 return space; 91 } 92 93 static void 94 destroy() 95 { 96 ChipmunkDemoFreeSpaceChildren(space); 97 cpSpaceFree(space); 98 } 99 100 chipmunkDemo Plink = { 101 "Plink", 102 null, 103 &init, 104 &update, 105 &destroy, 106 };