| 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"cxx . . $ `<file>/include/chipmunk/chipmunk.h`/ |
| 23 | #include "ChipmunkDemo.h"cxx . . $ `<file>/demo/ChipmunkDemo.h`/ |
| 24 | |
| 25 | // static body that we will be making into a scale |
| 26 | static cpBodycxx . . $ cpBody# *scaleStaticBodycxx . . $ scaleStaticBody.; |
| 27 | static cpBodycxx . . $ cpBody# *ballBodycxx . . $ 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 | ScaleIteratorcxx . . $ ScaleIterator(25ac1e1eb88bed0).(cpBodycxx . . $ cpBody# *body, cpArbitercxx . . $ cpArbiter# *arb, cpVectcxx . . $ cpVect# *sum) |
| 44 | { |
| 45 | (*sum) = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(*sum, cpArbiterTotalImpulsecxx . . $ cpArbiterTotalImpulse(bcbe2dc24ab42fef).(arb)); |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | BallIteratorcxx . . $ BallIterator(4ff5fbc19b7da089).(cpBodycxx . . $ cpBody# *body, cpArbitercxx . . $ 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_SHAPEScxx . . $ `include/chipmunk/cpArbiter.h:73:9`!(arb, ball, other); |
| 54 | ChipmunkDebugDrawBBcxx . . $ ChipmunkDebugDrawBB(a084cc6f3de926d8).(cpShapeGetBBcxx . . $ cpShapeGetBB(44a9343cdf942fdb).(other), RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(1, 0, 0, 1)); |
| 55 | |
| 56 | (*count)++; |
| 57 | } |
| 58 | |
| 59 | struct CrushingContextcxx . . $ CrushingContext# { |
| 60 | cpFloatcxx . . $ cpFloat# magnitudeSumcxx . . $ CrushingContext#magnitudeSum.; |
| 61 | cpVectcxx . . $ cpVect# vectorSumcxx . . $ CrushingContext#vectorSum.; |
| 62 | }; |
| 63 | |
| 64 | static void |
| 65 | EstimateCrushingcxx . . $ EstimateCrushing(ac1aab85066c12ea).(cpBodycxx . . $ cpBody# *body, cpArbitercxx . . $ cpArbiter# *arb, struct CrushingContextcxx . . $ CrushingContext# *context) |
| 66 | { |
| 67 | cpVectcxx . . $ cpVect# j = cpArbiterTotalImpulsecxx . . $ cpArbiterTotalImpulse(bcbe2dc24ab42fef).(arb); |
| 68 | context->magnitudeSumcxx . . $ CrushingContext#magnitudeSum. += cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(j); |
| 69 | context->vectorSumcxx . . $ CrushingContext#vectorSum. = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(context->vectorSumcxx . . $ CrushingContext#vectorSum., j); |
| 70 | } |
| 71 | |
| 72 | #endif |
| 73 | |
| 74 | static void |
| 75 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 76 | { |
| 77 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 78 | |
| 79 | ChipmunkDemoPrintStringcxx . . $ ChipmunkDemoPrintString(326aeb464d16410c).("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 | cpVectcxx . . $ cpVect# impulseSum = cpvzerocxx . . $ cpvzero.; |
| 91 | cpBodyEachArbitercxx . . $ cpBodyEachArbiter(d9db403274f2bd29).(scaleStaticBodycxx . . $ scaleStaticBody., (cpBodyArbiterIteratorFunccxx . . $ cpBodyArbiterIteratorFunc#)ScaleIteratorcxx . . $ ScaleIterator(25ac1e1eb88bed0)., &impulseSum); |
| 92 | #endif |
| 93 | |
| 94 | // Force is the impulse divided by the timestep. |
| 95 | cpFloatcxx . . $ cpFloat# force = cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(impulseSum)/dt; |
| 96 | |
| 97 | // Weight can be found similarly from the gravity vector. |
| 98 | cpVectcxx . . $ cpVect# g = cpSpaceGetGravitycxx . . $ cpSpaceGetGravity(9a5a38e87209f086).(space); |
| 99 | cpFloatcxx . . $ cpFloat# weight = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(g, impulseSum)/(cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(g)*dt); |
| 100 | |
| 101 | ChipmunkDemoPrintStringcxx . . $ ChipmunkDemoPrintString(326aeb464d16410c).("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 | cpBodyEachArbitercxx . . $ cpBodyEachArbiter(d9db403274f2bd29).(ballBodycxx . . $ ballBody., (cpBodyArbiterIteratorFunccxx . . $ cpBodyArbiterIteratorFunc#)BallIteratorcxx . . $ BallIterator(4ff5fbc19b7da089)., &count); |
| 118 | #endif |
| 119 | |
| 120 | ChipmunkDemoPrintStringcxx . . $ ChipmunkDemoPrintString(326aeb464d16410c).("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 CrushingContextcxx . . $ CrushingContext# crush = {0.0f, cpvzerocxx . . $ cpvzero.}; |
| 134 | cpBodyEachArbitercxx . . $ cpBodyEachArbiter(d9db403274f2bd29).(ballBodycxx . . $ ballBody., (cpBodyArbiterIteratorFunccxx . . $ cpBodyArbiterIteratorFunc#)EstimateCrushingcxx . . $ EstimateCrushing(ac1aab85066c12ea)., &crush); |
| 135 | |
| 136 | cpFloatcxx . . $ cpFloat# crushForce = (crush.magnitudeSumcxx . . $ CrushingContext#magnitudeSum. - cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(crush.vectorSumcxx . . $ CrushingContext#vectorSum.))*dt; |
| 137 | #endif |
| 138 | |
| 139 | |
| 140 | if(crushForce > 10.0f){ |
| 141 | ChipmunkDemoPrintStringcxx . . $ ChipmunkDemoPrintString(326aeb464d16410c).("The ball is being crushed. (f: %.2f)", crushForce); |
| 142 | } else { |
| 143 | ChipmunkDemoPrintStringcxx . . $ ChipmunkDemoPrintString(326aeb464d16410c).("The ball is not being crushed. (f: %.2f)", crushForce); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #define WIDTHcxx . . $ `demo/ContactGraph.c:147:9`! 4.0f |
| 148 | #define HEIGHTcxx . . $ `demo/ContactGraph.c:148:9`! 30.0f |
| 149 | |
| 150 | static cpSpacecxx . . $ cpSpace# * |
| 151 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 152 | { |
| 153 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 154 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 30); |
| 155 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -300)); |
| 156 | cpSpaceSetCollisionSlopcxx . . $ cpSpaceSetCollisionSlop(fa66a0a48611083e).(space, 0.5); |
| 157 | cpSpaceSetSleepTimeThresholdcxx . . $ cpSpaceSetSleepTimeThreshold(fa66a0a48611083e).(space, 1.0f); |
| 158 | |
| 159 | cpBodycxx . . $ cpBody# *body, *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 160 | cpShapecxx . . $ cpShape# *shape; |
| 161 | |
| 162 | // Create segments around the edge of the screen. |
| 163 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), 0.0f)); |
| 164 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 165 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 166 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 167 | |
| 168 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f)); |
| 169 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 170 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 171 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 172 | |
| 173 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), 0.0f)); |
| 174 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 175 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 176 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 177 | |
| 178 | scaleStaticBodycxx . . $ scaleStaticBody. = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewStaticcxx . . $ cpBodyNewStatic(af6db6e743345a49).()); |
| 179 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(scaleStaticBodycxx . . $ scaleStaticBody., cpvcxx . . $ cpv(3142d57d978b2176).(-240,-180), cpvcxx . . $ cpv(3142d57d978b2176).(-140,-180), 4.0f)); |
| 180 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 181 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 182 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 183 | |
| 184 | // add some boxes to stack on the scale |
| 185 | for(int i=0; i<5; i++){ |
| 186 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(1.0f, cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(1.0f, 30.0f, 30.0f))); |
| 187 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0, i*32 - 220)); |
| 188 | |
| 189 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(body, 30.0f, 30.0f, 0.0)); |
| 190 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 0.0f); |
| 191 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.8f); |
| 192 | } |
| 193 | |
| 194 | // Add a ball that we'll track which objects are beneath it. |
| 195 | cpFloatcxx . . $ cpFloat# radius = 15.0f; |
| 196 | ballBodycxx . . $ ballBody. = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(10.0f, cpMomentForCirclecxx . . $ cpMomentForCircle(a539e2d95614f770).(10.0f, 0.0f, radius, cpvzerocxx . . $ cpvzero.))); |
| 197 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(ballBodycxx . . $ ballBody., cpvcxx . . $ cpv(3142d57d978b2176).(120, -240 + radius+5)); |
| 198 | |
| 199 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpCircleShapeNewcxx . . $ cpCircleShapeNew(e5e4e3a52c89533b).(ballBodycxx . . $ ballBody., radius, cpvzerocxx . . $ cpvzero.)); |
| 200 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 0.0f); |
| 201 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.9f); |
| 202 | |
| 203 | return space; |
| 204 | } |
| 205 | |
| 206 | static void |
| 207 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 208 | { |
| 209 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 210 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 211 | } |
| 212 | |
| 213 | ChipmunkDemocxx . . $ ChipmunkDemo# ContactGraphcxx . . $ ContactGraph. = { |
| 214 | "Contact Graph", |
| 215 | 1.0/60.0, |
| 216 | initcxx . . $ init(1f0082e822a7ce76)., |
| 217 | updatecxx . . $ update(fa66a0a48611083e)., |
| 218 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 219 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 220 | }; |