1 2 // written in the D programming language 3 4 module samples.Query; 5 6 import dchip.all; 7 8 import samples.ChipmunkDemo; 9 import gameApp; 10 11 import std.math; 12 13 static cpSpace *space; 14 15 static cpShape *querySeg = null; 16 17 18 static void 19 update(int ticks) 20 { 21 //messageString[0] = '\0'; 22 23 cpVect start = cpvzero; 24 cpVect end = /*cpv(0, 85);//*/mousePos; 25 cpVect lineEnd = end; 26 27 //{ 28 // char infoString[1024]; 29 // sprintf(infoString, "Query: Dist(%f) Point%s, ", cpvdist(start, end), cpvstr(end)); 30 // strcat(messageString, infoString); 31 //} 32 33 cpSegmentQueryInfo info = {}; 34 if(cpSpaceSegmentQueryFirst(space, start, end, CP_ALL_LAYERS, CP_NO_GROUP, &info)){ 35 cpVect point = cpSegmentQueryHitPoint(start, end, info); 36 lineEnd = cpvadd(point, cpvzero);//cpvmult(info.n, 4.0f)); 37 38 //char infoString[1024]; 39 //sprintf(infoString, "Segment Query: Dist(%f) Normal%s", cpSegmentQueryHitDist(start, end, info), cpvstr(info.n)); 40 //strcat(messageString, infoString); 41 } else { 42 //strcat(messageString, "Segment Query (None)"); 43 } 44 45 cpSegmentShapeSetEndpoints(querySeg, start, lineEnd); 46 // force it to update it's collision detection data so it will draw 47 cpShapeUpdate(querySeg, cpvzero, cpv(1.0f, 0.0f)); 48 49 // normal other stuff. 50 int steps = 1; 51 cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps; 52 53 for(int i=0; i<steps; i++){ 54 cpSpaceStep(space, dt); 55 } 56 } 57 58 static cpSpace * 59 init() 60 { 61 cpResetShapeIdCounter(); 62 63 space = cpSpaceNew(); 64 space.iterations = 5; 65 66 cpBody *staticBody = space.staticBody; 67 cpShape *shape; 68 69 // add a non-collidable segment as a quick and dirty way to draw the query line 70 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpvzero, cpv(100.0f, 0.0f), 4.0f)); 71 shape.layers = 0; 72 querySeg = shape; 73 74 { // add a fat segment 75 cpFloat mass = 1.0f; 76 cpFloat length = 100.0f; 77 cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f); 78 79 cpBody *_body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b))); 80 _body.p = cpv(0.0f, 100.0f); 81 82 cpSpaceAddShape(space, cpSegmentShapeNew(_body, a, b, 20.0f)); 83 } 84 85 { // add a static segment 86 cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0, 300), cpv(300, 0), 0.0f)); 87 } 88 89 { // add a pentagon 90 cpFloat mass = 1.0f; 91 const int NUM_VERTS = 5; 92 93 cpVect verts[NUM_VERTS]; 94 for(int i=0; i<NUM_VERTS; i++){ 95 cpFloat angle = -2*PI*i/(cast(cpFloat) NUM_VERTS); 96 verts[i] = cpv(30*cos(angle), 30*sin(angle)); 97 } 98 99 cpBody *_body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts.ptr, cpvzero))); 100 _body.p = cpv(50.0f, 50.0f); 101 102 cpSpaceAddShape(space, cpPolyShapeNew(_body, NUM_VERTS, verts.ptr, cpvzero)); 103 } 104 105 { // add a circle 106 cpFloat mass = 1.0f; 107 cpFloat r = 20.0f; 108 109 cpBody *_body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, r, cpvzero))); 110 _body.p = cpv(100.0f, 100.0f); 111 112 cpSpaceAddShape(space, cpCircleShapeNew(_body, r, cpvzero)); 113 } 114 115 return space; 116 } 117 118 static void 119 destroy() 120 { 121 ChipmunkDemoFreeSpaceChildren(space); 122 cpSpaceFree(space); 123 } 124 125 chipmunkDemo Query = { 126 "Segment Query", 127 null, 128 &init, 129 &update, 130 &destroy, 131 };