| 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 | |
| 24 | #include "ChipmunkDemo.h"cxx . . $ `<file>/demo/ChipmunkDemo.h`/ |
| 25 | |
| 26 | #define DENSITYcxx . . $ `demo/Slice.c:26:9`! (1.0/10000.0) |
| 27 | |
| 28 | static void |
| 29 | ClipPolycxx . . $ ClipPoly(3d6911d7a183eb1c).(cpSpacecxx . . $ cpSpace# *space, cpShapecxx . . $ cpShape# *shape, cpVectcxx . . $ cpVect# n, cpFloatcxx . . $ cpFloat# dist) |
| 30 | { |
| 31 | cpBodycxx . . $ cpBody# *body = cpShapeGetBodycxx . . $ cpShapeGetBody(abc772114277478f).(shape); |
| 32 | |
| 33 | int count = cpPolyShapeGetCountcxx . . $ cpPolyShapeGetCount(46d926dc73b3f622).(shape); |
| 34 | int clippedCount = 0; |
| 35 | |
| 36 | cpVectcxx . . $ cpVect# *clipped = (cpVectcxx . . $ cpVect# *)allocacxx . . $ `<external>/11612401604547820122/alloca.h:35:10`!((count + 1)*sizeof(cpVect)); |
| 37 | |
| 38 | for(int i=0, j=count-1; i<count; j=i, i++){ |
| 39 | cpVectcxx . . $ cpVect# a = cpBodyLocalToWorldcxx . . $ cpBodyLocalToWorld(4ff4900d49a97e73).(body, cpPolyShapeGetVertcxx . . $ cpPolyShapeGetVert(9392d0fc785db450).(shape, j)); |
| 40 | cpFloatcxx . . $ cpFloat# a_dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(a, n) - dist; |
| 41 | |
| 42 | if(a_dist < 0.0){ |
| 43 | clipped[clippedCount] = a; |
| 44 | clippedCount++; |
| 45 | } |
| 46 | |
| 47 | cpVectcxx . . $ cpVect# b = cpBodyLocalToWorldcxx . . $ cpBodyLocalToWorld(4ff4900d49a97e73).(body, cpPolyShapeGetVertcxx . . $ cpPolyShapeGetVert(9392d0fc785db450).(shape, i)); |
| 48 | cpFloatcxx . . $ cpFloat# b_dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(b, n) - dist; |
| 49 | |
| 50 | if(a_dist*b_dist < 0.0f){ |
| 51 | cpFloatcxx . . $ cpFloat# t = cpfabscxx . . $ cpfabs(447c67c6676f75c3).(a_dist)/(cpfabscxx . . $ cpfabs(447c67c6676f75c3).(a_dist) + cpfabscxx . . $ cpfabs(447c67c6676f75c3).(b_dist)); |
| 52 | |
| 53 | clipped[clippedCount] = cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(a, b, t); |
| 54 | clippedCount++; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | cpVectcxx . . $ cpVect# centroid = cpCentroidForPolycxx . . $ cpCentroidForPoly(740f059a8e4cff9d).(clippedCount, clipped); |
| 59 | cpFloatcxx . . $ cpFloat# mass = cpAreaForPolycxx . . $ cpAreaForPoly(c1e6bc8c2660eac5).(clippedCount, clipped, 0.0f)*DENSITYcxx . . $ `demo/Slice.c:26:9`!; |
| 60 | cpFloatcxx . . $ cpFloat# moment = cpMomentForPolycxx . . $ cpMomentForPoly(89d843ff16ae6b28).(mass, clippedCount, clipped, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(centroid), 0.0f); |
| 61 | |
| 62 | cpBodycxx . . $ cpBody# *new_body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment)); |
| 63 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(new_body, centroid); |
| 64 | cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(new_body, cpBodyGetVelocityAtWorldPointcxx . . $ cpBodyGetVelocityAtWorldPoint(4ff4900d49a97e73).(body, centroid)); |
| 65 | cpBodySetAngularVelocitycxx . . $ cpBodySetAngularVelocity(797bd3bb7a653b39).(new_body, cpBodyGetAngularVelocitycxx . . $ cpBodyGetAngularVelocity(2880d7999e0b34c3).(body)); |
| 66 | |
| 67 | cpTransformcxx . . $ cpTransform# transform = cpTransformTranslatecxx . . $ cpTransformTranslate(9ddf098b65af1f2f).(cpvnegcxx . . $ cpvneg(e06142a62109ae08).(centroid)); |
| 68 | cpShapecxx . . $ cpShape# *new_shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpPolyShapeNewcxx . . $ cpPolyShapeNew(119891a3da20e8eb).(new_body, clippedCount, clipped, transform, 0.0)); |
| 69 | // Copy whatever properties you have set on the original shape that are important |
| 70 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(new_shape, cpShapeGetFrictioncxx . . $ cpShapeGetFriction(1bf41e75729c6179).(shape)); |
| 71 | } |
| 72 | |
| 73 | // Context structs are annoying, use blocks or closures instead if your compiler supports them. |
| 74 | struct SliceContextcxx . . $ SliceContext# { |
| 75 | cpVectcxx . . $ cpVect# acxx . . $ SliceContext#a., bcxx . . $ SliceContext#b.; |
| 76 | cpSpacecxx . . $ cpSpace# *spacecxx . . $ SliceContext#space.; |
| 77 | }; |
| 78 | |
| 79 | static void |
| 80 | SliceShapePostStepcxx . . $ SliceShapePostStep(6855e2686fd596a6).(cpSpacecxx . . $ cpSpace# *space, cpShapecxx . . $ cpShape# *shape, struct SliceContextcxx . . $ SliceContext# *context) |
| 81 | { |
| 82 | cpVectcxx . . $ cpVect# a = context->acxx . . $ SliceContext#a.; |
| 83 | cpVectcxx . . $ cpVect# b = context->bcxx . . $ SliceContext#b.; |
| 84 | |
| 85 | // Clipping plane normal and distance. |
| 86 | cpVectcxx . . $ cpVect# n = cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvperpcxx . . $ cpvperp(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(b, a))); |
| 87 | cpFloatcxx . . $ cpFloat# dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(a, n); |
| 88 | |
| 89 | ClipPolycxx . . $ ClipPoly(3d6911d7a183eb1c).(space, shape, n, dist); |
| 90 | ClipPolycxx . . $ ClipPoly(3d6911d7a183eb1c).(space, shape, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(n), -dist); |
| 91 | |
| 92 | cpBodycxx . . $ cpBody# *body = cpShapeGetBodycxx . . $ cpShapeGetBody(abc772114277478f).(shape); |
| 93 | cpSpaceRemoveShapecxx . . $ cpSpaceRemoveShape(b4590e6760e37cb0).(space, shape); |
| 94 | cpSpaceRemoveBodycxx . . $ cpSpaceRemoveBody(f865e61ac2c3a7c6).(space, body); |
| 95 | cpShapeFreecxx . . $ cpShapeFree(49d28a84d341894).(shape); |
| 96 | cpBodyFreecxx . . $ cpBodyFree(92943d3541b63e72).(body); |
| 97 | } |
| 98 | |
| 99 | static void |
| 100 | SliceQuerycxx . . $ SliceQuery(5a56ad19059d6ed1).(cpShapecxx . . $ cpShape# *shape, cpVectcxx . . $ cpVect# point, cpVectcxx . . $ cpVect# normal, cpFloatcxx . . $ cpFloat# alpha, struct SliceContextcxx . . $ SliceContext# *context) |
| 101 | { |
| 102 | cpVectcxx . . $ cpVect# a = context->acxx . . $ SliceContext#a.; |
| 103 | cpVectcxx . . $ cpVect# b = context->bcxx . . $ SliceContext#b.; |
| 104 | |
| 105 | // Check that the slice was complete by checking that the endpoints aren't in the sliced shape. |
| 106 | if(cpShapePointQuerycxx . . $ cpShapePointQuery(fba695676919fb41).(shape, a, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) > 0.0f && cpShapePointQuerycxx . . $ cpShapePointQuery(fba695676919fb41).(shape, b, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) > 0.0f){ |
| 107 | // Can't modify the space during a query. |
| 108 | // Must make a post-step callback to do the actual slicing. |
| 109 | cpSpaceAddPostStepCallbackcxx . . $ cpSpaceAddPostStepCallback(1ff9997ad0bee464).(context->spacecxx . . $ SliceContext#space., (cpPostStepFunccxx . . $ cpPostStepFunc#)SliceShapePostStepcxx . . $ SliceShapePostStep(6855e2686fd596a6)., shape, context); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 115 | { |
| 116 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 117 | |
| 118 | static cpBoolcxx . . $ cpBool# lastClickState = cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!; |
| 119 | static cpVectcxx . . $ cpVect# sliceStart = {0.0, 0.0}; |
| 120 | |
| 121 | // Annoying state tracking code that you wouldn't need |
| 122 | // in a real event driven system. |
| 123 | if(ChipmunkDemoRightClickcxx . . $ ChipmunkDemoRightClick. != lastClickState){ |
| 124 | if(ChipmunkDemoRightClickcxx . . $ ChipmunkDemoRightClick.){ |
| 125 | // MouseDown |
| 126 | sliceStart = ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse.; |
| 127 | } else { |
| 128 | // MouseUp |
| 129 | struct SliceContextcxx . . $ SliceContext# context = {sliceStart, ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., space}; |
| 130 | cpSpaceSegmentQuerycxx . . $ cpSpaceSegmentQuery(f0c0ce39a75d406e).(space, sliceStart, ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., 0.0, GRAB_FILTERcxx . . $ GRAB_FILTER., (cpSpaceSegmentQueryFunccxx . . $ cpSpaceSegmentQueryFunc#)SliceQuerycxx . . $ SliceQuery(5a56ad19059d6ed1)., &context); |
| 131 | } |
| 132 | |
| 133 | lastClickState = ChipmunkDemoRightClickcxx . . $ ChipmunkDemoRightClick.; |
| 134 | } |
| 135 | |
| 136 | if(ChipmunkDemoRightClickcxx . . $ ChipmunkDemoRightClick.){ |
| 137 | ChipmunkDebugDrawSegmentcxx . . $ ChipmunkDebugDrawSegment(307cf2b0f0a496c2).(sliceStart, ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(1, 0, 0, 1)); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static cpSpacecxx . . $ cpSpace# * |
| 142 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 143 | { |
| 144 | ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = "Right click and drag to slice up the block."; |
| 145 | |
| 146 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 147 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 30); |
| 148 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -500)); |
| 149 | cpSpaceSetSleepTimeThresholdcxx . . $ cpSpaceSetSleepTimeThreshold(fa66a0a48611083e).(space, 0.5f); |
| 150 | cpSpaceSetCollisionSlopcxx . . $ cpSpaceSetCollisionSlop(fa66a0a48611083e).(space, 0.5f); |
| 151 | |
| 152 | cpBodycxx . . $ cpBody# *body, *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 153 | cpShapecxx . . $ cpShape# *shape; |
| 154 | |
| 155 | // Create segments around the edge of the screen. |
| 156 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-1000,-240), cpvcxx . . $ cpv(3142d57d978b2176).(1000,-240), 0.0f)); |
| 157 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 158 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 159 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 160 | |
| 161 | cpFloatcxx . . $ cpFloat# width = 200.0f; |
| 162 | cpFloatcxx . . $ cpFloat# height = 300.0f; |
| 163 | cpFloatcxx . . $ cpFloat# mass = width*height*DENSITYcxx . . $ `demo/Slice.c:26:9`!; |
| 164 | cpFloatcxx . . $ cpFloat# moment = cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(mass, width, height); |
| 165 | |
| 166 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment)); |
| 167 | |
| 168 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(body, width, height, 0.0)); |
| 169 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.6f); |
| 170 | |
| 171 | return space; |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 176 | { |
| 177 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 178 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 179 | } |
| 180 | |
| 181 | ChipmunkDemocxx . . $ ChipmunkDemo# Slicecxx . . $ Slice. = { |
| 182 | "Slice.", |
| 183 | 1.0/60.0, |
| 184 | initcxx . . $ init(1f0082e822a7ce76)., |
| 185 | updatecxx . . $ update(fa66a0a48611083e)., |
| 186 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 187 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 188 | }; |