1 2 // written in the D programming language 3 4 module samples.UnsafeOps; 5 6 import dchip.all; 7 8 import samples.ChipmunkDemo; 9 10 import gameApp; 11 12 import std.math; 13 14 static cpSpace *space; 15 16 enum M_PI = PI; 17 enum M_PI_2 = PI*0.5f; 18 19 enum NUM_CIRCLES = 30; 20 21 static cpShape *circles[NUM_CIRCLES]; 22 static cpFloat circleRadius = 30.0f; 23 24 static void 25 update(int ticks) 26 { 27 if(arrowDirection.y){ 28 circleRadius = cpfmax(10.0f, circleRadius + arrowDirection.y); 29 30 for(int i=0; i<NUM_CIRCLES; i++){ 31 circles[i].body_.m = cpMomentForCircle(1.0f, 0.0f, circleRadius, cpvzero); 32 cpCircleShapeSetRadius(circles[i], circleRadius); 33 } 34 } 35 36 int steps = 1; 37 cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps; 38 39 for(int i=0; i<steps; i++){ 40 cpSpaceStep(space, dt); 41 } 42 } 43 44 static cpSpace * 45 init() 46 { 47 cpResetShapeIdCounter(); 48 49 space = cpSpaceNew(); 50 space.iterations = 5; 51 space.gravity = cpv(0, -100); 52 53 cpBody *body_, staticBody = space.staticBody; 54 cpShape *shape; 55 56 shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); 57 shape.e = 1.0f; shape.u = 1.0f; 58 shape.layers = NOT_GRABABLE_MASK; 59 60 shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); 61 shape.e = 1.0f; shape.u = 1.0f; 62 shape.layers = NOT_GRABABLE_MASK; 63 64 shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); 65 shape.e = 1.0f; shape.u = 1.0f; 66 shape.layers = NOT_GRABABLE_MASK; 67 68 for(int i=0; i<NUM_CIRCLES; i++){ 69 body_ = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 0.0f, circleRadius, cpvzero))); 70 body_.p = cpvmult(cpv(frand()*2.0f - 1.0f, frand()*2.0f - 1.0f), circleRadius*5.0f); 71 72 circles[i] = shape = cpSpaceAddShape(space, cpCircleShapeNew(body_, circleRadius, cpvzero)); 73 shape.e = 0.0f; shape.u = 1.0f; 74 } 75 76 //strcat(messageString, 77 // "chipmunk_unsafe.h Contains functions for changing shapes, but they can cause severe stability problems if used incorrectly.\n" 78 // "Shape changes occur as instantaneous changes to position without an accompanying velocity change. USE WITH CAUTION!"); 79 return space; 80 } 81 82 static void 83 destroy() 84 { 85 ChipmunkDemoFreeSpaceChildren(space); 86 cpSpaceFree(space); 87 } 88 89 chipmunkDemo UnsafeOps = { 90 "Unsafe Operations", 91 null, 92 &init, 93 &update, 94 &destroy, 95 };