| 1 | /* Copyright (c) 2007 Scott Lembcke |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | * SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | #include "chipmunk/chipmunk.h" |
| 23 | #include "ChipmunkDemo.h" |
| 24 | |
| 25 | // static body that we will be making into a scale |
| 26 | static cpBody *scaleStaticBody; |
| 27 | static cpBody *ballBody; |
| 28 | |
| 29 | // If your compiler supports blocks (Clang or some GCC versions), |
| 30 | // You can use the block based iterators instead of the function ones to make your life easier. |
| 31 | #if defined(__has_extension) |
| 32 | #if __has_extension(blocks) |
| 33 | |
| 34 | #define USE_BLOCKS 1 |
| 35 | |
| 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | |
| 40 | #if !USE_BLOCKS |
| 41 | |
| 42 | static void |
| 43 | ScaleIterator(cpBody *body, cpArbiter *arb, cpVect *sum) |
| 44 | { |
| 45 | (*sum) = cpvadd(*sum, cpArbiterTotalImpulse(arb)); |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | BallIterator(cpBody *body, cpArbiter *arb, int *count) |
| 50 | { |
| 51 | // body is the body we are iterating the arbiters for. |
| 52 | // CP_ARBITER_GET_*() in an arbiter iterator always returns the body/shape for the iterated body first. |
| 53 | CP_ARBITER_GET_SHAPES(arb, ball, other); |
| 54 | ChipmunkDebugDrawBB(cpShapeGetBB(other), RGBAColor(1, 0, 0, 1)); |
| 55 | |
| 56 | (*count)++; |
| 57 | } |
| 58 | |
| 59 | struct CrushingContext { |
| 60 | cpFloat magnitudeSum; |
| 61 | cpVect vectorSum; |
| 62 | }; |
| 63 | |
| 64 | static void |
| 65 | EstimateCrushing(cpBody *body, cpArbiter *arb, struct CrushingContext *context) |
| 66 | { |
| 67 | cpVect j = cpArbiterTotalImpulse(arb); |
| 68 | context->magnitudeSum += cpvlength(j); |
| 69 | context->vectorSum = cpvadd(context->vectorSum, j); |
| 70 | } |
| 71 | |
| 72 | #endif |
| 73 | |
| 74 | static void |
| 75 | update(cpSpace *space, double dt) |
| 76 | { |
| 77 | cpSpaceStep(space, dt); |
| 78 | |
| 79 | ChipmunkDemoPrintString("Place objects on the scale to weigh them. The ball marks the shapes it's sitting on.\n"); |
| 80 | |
| 81 | // Sum the total impulse applied to the scale from all collision pairs in the contact graph. |
| 82 | // If your compiler supports blocks, your life is a little easier. |
| 83 | // You can use the "Block" versions of the functions without needing the callbacks above. |
| 84 | #if USE_BLOCKS |
| 85 | __block cpVect impulseSum = cpvzero; |
| 86 | cpBodyEachArbiter_b(scaleStaticBody, ^(cpArbiter *arb){ |
| 87 | impulseSum = cpvadd(impulseSum, cpArbiterTotalImpulse(arb)); |
| 88 | }); |
| 89 | #else |
| 90 | cpVect impulseSum = cpvzero; |
| 91 | cpBodyEachArbiter(scaleStaticBody, (cpBodyArbiterIteratorFunc)ScaleIterator, &impulseSum); |
| 92 | #endif |
| 93 | |
| 94 | // Force is the impulse divided by the timestep. |
| 95 | cpFloat force = cpvlength(impulseSum)/dt; |
| 96 | |
| 97 | // Weight can be found similarly from the gravity vector. |
| 98 | cpVect g = cpSpaceGetGravity(space); |
| 99 | cpFloat weight = cpvdot(g, impulseSum)/(cpvlengthsq(g)*dt); |
| 100 | |
| 101 | ChipmunkDemoPrintString("Total force: %5.2f, Total weight: %5.2f. ", force, weight); |
| 102 | |
| 103 | |
| 104 | // Highlight and count the number of shapes the ball is touching. |
| 105 | #if USE_BLOCKS |
| 106 | __block int count = 0; |
| 107 | cpBodyEachArbiter_b(ballBody, ^(cpArbiter *arb){ |
| 108 | // body is the body we are iterating the arbiters for. |
| 109 | // CP_ARBITER_GET_*() in an arbiter iterator always returns the body/shape for the iterated body first. |
| 110 | CP_ARBITER_GET_SHAPES(arb, ball, other); |
| 111 | ChipmunkDebugDrawBB(cpShapeGetBB(other), RGBAColor(1, 0, 0, 1)); |
| 112 | |
| 113 | count++; |
| 114 | }); |
| 115 | #else |
| 116 | int count = 0; |
| 117 | cpBodyEachArbiter(ballBody, (cpBodyArbiterIteratorFunc)BallIterator, &count); |
| 118 | #endif |
| 119 | |
| 120 | ChipmunkDemoPrintString("The ball is touching %d shapes.\n", count); |
| 121 | |
| 122 | #if USE_BLOCKS |
| 123 | __block cpFloat magnitudeSum = 0.0f; |
| 124 | __block cpVect vectorSum = cpvzero; |
| 125 | cpBodyEachArbiter_b(ballBody, ^(cpArbiter *arb){ |
| 126 | cpVect j = cpArbiterTotalImpulse(arb); |
| 127 | magnitudeSum += cpvlength(j); |
| 128 | vectorSum = cpvadd(vectorSum, j); |
| 129 | }); |
| 130 | |
| 131 | cpFloat crushForce = (magnitudeSum - cpvlength(vectorSum))*dt; |
| 132 | #else |
| 133 | struct CrushingContext crush = {0.0f, cpvzero}; |
| 134 | cpBodyEachArbiter(ballBody, (cpBodyArbiterIteratorFunc)EstimateCrushing, &crush); |
| 135 | |
| 136 | cpFloat crushForce = (crush.magnitudeSum - cpvlength(crush.vectorSum))*dt; |
| 137 | #endif |
| 138 | |
| 139 | |
| 140 | if(crushForce > 10.0f){ |
| 141 | ChipmunkDemoPrintString("The ball is being crushed. (f: %.2f)", crushForce); |
| 142 | } else { |
| 143 | ChipmunkDemoPrintString("The ball is not being crushed. (f: %.2f)", crushForce); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #define WIDTH 4.0f |
| 148 | #define HEIGHT 30.0f |
| 149 | |
| 150 | static cpSpace * |
| 151 | init(void) |
| 152 | { |
| 153 | cpSpace *space = cpSpaceNew(); |
| 154 | cpSpaceSetIterations(space, 30); |
| 155 | cpSpaceSetGravity(space, cpv(0, -300)); |
| 156 | cpSpaceSetCollisionSlop(space, 0.5); |
| 157 | cpSpaceSetSleepTimeThreshold(space, 1.0f); |
| 158 | |
| 159 | cpBody *body, *staticBody = cpSpaceGetStaticBody(space); |
| 160 | cpShape *shape; |
| 161 | |
| 162 | // Create segments around the edge of the screen. |
| 163 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); |
| 164 | cpShapeSetElasticity(shape, 1.0f); |
| 165 | cpShapeSetFriction(shape, 1.0f); |
| 166 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 167 | |
| 168 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); |
| 169 | cpShapeSetElasticity(shape, 1.0f); |
| 170 | cpShapeSetFriction(shape, 1.0f); |
| 171 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 172 | |
| 173 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); |
| 174 | cpShapeSetElasticity(shape, 1.0f); |
| 175 | cpShapeSetFriction(shape, 1.0f); |
| 176 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 177 | |
| 178 | scaleStaticBody = cpSpaceAddBody(space, cpBodyNewStatic()); |
| 179 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(scaleStaticBody, cpv(-240,-180), cpv(-140,-180), 4.0f)); |
| 180 | cpShapeSetElasticity(shape, 1.0f); |
| 181 | cpShapeSetFriction(shape, 1.0f); |
| 182 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 183 | |
| 184 | // add some boxes to stack on the scale |
| 185 | for(int i=0; i<5; i++){ |
| 186 | body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForBox(1.0f, 30.0f, 30.0f))); |
| 187 | cpBodySetPosition(body, cpv(0, i*32 - 220)); |
| 188 | |
| 189 | shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 30.0f, 30.0f, 0.0)); |
| 190 | cpShapeSetElasticity(shape, 0.0f); |
| 191 | cpShapeSetFriction(shape, 0.8f); |
| 192 | } |
| 193 | |
| 194 | // Add a ball that we'll track which objects are beneath it. |
| 195 | cpFloat radius = 15.0f; |
| 196 | ballBody = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero))); |
| 197 | cpBodySetPosition(ballBody, cpv(120, -240 + radius+5)); |
| 198 | |
| 199 | shape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero)); |
| 200 | cpShapeSetElasticity(shape, 0.0f); |
| 201 | cpShapeSetFriction(shape, 0.9f); |
| 202 | |
| 203 | return space; |
| 204 | } |
| 205 | |
| 206 | static void |
| 207 | destroy(cpSpace *space) |
| 208 | { |
| 209 | ChipmunkDemoFreeSpaceChildren(space); |
| 210 | cpSpaceFree(space); |
| 211 | } |
| 212 | |
| 213 | ChipmunkDemo ContactGraph = { |
| 214 | "Contact Graph", |
| 215 | 1.0/60.0, |
| 216 | init, |
| 217 | update, |
| 218 | ChipmunkDemoDefaultDrawImpl, |
| 219 | destroy, |
| 220 | }; |