| 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 | enumcxx . . $ $anonymous_type_4ce5ba2537616013_0# { |
| 26 | COLLISION_TYPE_STICKYcxx . . $ COLLISION_TYPE_STICKY. = 1, |
| 27 | }; |
| 28 | |
| 29 | #define STICK_SENSOR_THICKNESScxx . . $ `demo/Sticky.c:29:9`! 2.5f |
| 30 | |
| 31 | static void |
| 32 | PostStepAddJointcxx . . $ PostStepAddJoint(967c2057ff39f02d).(cpSpacecxx . . $ cpSpace# *space, void *key, void *data) |
| 33 | { |
| 34 | // printf("Adding joint for %p\n", data); |
| 35 | |
| 36 | cpConstraintcxx . . $ cpConstraint# *joint = (cpConstraintcxx . . $ cpConstraint# *)key; |
| 37 | cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, joint); |
| 38 | } |
| 39 | |
| 40 | static cpBoolcxx . . $ cpBool# |
| 41 | StickyPreSolvecxx . . $ StickyPreSolve(46926bbb19d93a13).(cpArbitercxx . . $ cpArbiter# *arb, cpSpacecxx . . $ cpSpace# *space, void *data) |
| 42 | { |
| 43 | // We want to fudge the collisions a bit to allow shapes to overlap more. |
| 44 | // This simulates their squishy sticky surface, and more importantly |
| 45 | // keeps them from separating and destroying the joint. |
| 46 | |
| 47 | // Track the deepest collision point and use that to determine if a rigid collision should occur. |
| 48 | cpFloatcxx . . $ cpFloat# deepest = INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!; |
| 49 | |
| 50 | // Grab the contact set and iterate over them. |
| 51 | cpContactPointSetcxx . . $ cpContactPointSet# contacts = cpArbiterGetContactPointSetcxx . . $ cpArbiterGetContactPointSet(b6fbca376c78b0e4).(arb); |
| 52 | for(int i=0; i<contacts.countcxx . . $ cpContactPointSet#count.; i++){ |
| 53 | // Sink the contact points into the surface of each shape. |
| 54 | contacts.pointscxx . . $ cpContactPointSet#points.[i].pointAcxx . . $ cpContactPointSet#$anonymous_type_0#pointA. = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(contacts.pointscxx . . $ cpContactPointSet#points.[i].pointAcxx . . $ cpContactPointSet#$anonymous_type_0#pointA., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(contacts.normalcxx . . $ cpContactPointSet#normal., STICK_SENSOR_THICKNESScxx . . $ `demo/Sticky.c:29:9`!)); |
| 55 | contacts.pointscxx . . $ cpContactPointSet#points.[i].pointBcxx . . $ cpContactPointSet#$anonymous_type_0#pointB. = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(contacts.pointscxx . . $ cpContactPointSet#points.[i].pointBcxx . . $ cpContactPointSet#$anonymous_type_0#pointB., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(contacts.normalcxx . . $ cpContactPointSet#normal., STICK_SENSOR_THICKNESScxx . . $ `demo/Sticky.c:29:9`!)); |
| 56 | deepest = cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(deepest, contacts.pointscxx . . $ cpContactPointSet#points.[i].distancecxx . . $ cpContactPointSet#$anonymous_type_0#distance.);// + 2.0f*STICK_SENSOR_THICKNESS); |
| 57 | } |
| 58 | |
| 59 | // Set the new contact point data. |
| 60 | cpArbiterSetContactPointSetcxx . . $ cpArbiterSetContactPointSet(e50c0b9ac054554).(arb, &contacts); |
| 61 | |
| 62 | // If the shapes are overlapping enough, then create a |
| 63 | // joint that sticks them together at the first contact point. |
| 64 | if(!cpArbiterGetUserDatacxx . . $ cpArbiterGetUserData(74f6a6b1b8167460).(arb) && deepest <= 0.0f){ |
| 65 | CP_ARBITER_GET_BODIEScxx . . $ `include/chipmunk/cpArbiter.h:81:9`!(arb, bodyA, bodyB); |
| 66 | |
| 67 | // Create a joint at the contact point to hold the body in place. |
| 68 | cpVectcxx . . $ cpVect# anchorA = cpBodyWorldToLocalcxx . . $ cpBodyWorldToLocal(4ff4900d49a97e73).(bodyA, contacts.pointscxx . . $ cpContactPointSet#points.[0].pointAcxx . . $ cpContactPointSet#$anonymous_type_0#pointA.); |
| 69 | cpVectcxx . . $ cpVect# anchorB = cpBodyWorldToLocalcxx . . $ cpBodyWorldToLocal(4ff4900d49a97e73).(bodyB, contacts.pointscxx . . $ cpContactPointSet#points.[0].pointBcxx . . $ cpContactPointSet#$anonymous_type_0#pointB.); |
| 70 | cpConstraintcxx . . $ cpConstraint# *joint = cpPivotJointNew2cxx . . $ cpPivotJointNew2(74f6d8fa0e302020).(bodyA, bodyB, anchorA, anchorB); |
| 71 | |
| 72 | // Give it a finite force for the stickyness. |
| 73 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(joint, 3e3); |
| 74 | |
| 75 | // Schedule a post-step() callback to add the joint. |
| 76 | cpSpaceAddPostStepCallbackcxx . . $ cpSpaceAddPostStepCallback(1ff9997ad0bee464).(space, PostStepAddJointcxx . . $ PostStepAddJoint(967c2057ff39f02d)., joint, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 77 | |
| 78 | // Store the joint on the arbiter so we can remove it later. |
| 79 | cpArbiterSetUserDatacxx . . $ cpArbiterSetUserData(9aa61450fdbfe78e).(arb, joint); |
| 80 | } |
| 81 | |
| 82 | // Position correction and velocity are handled separately so changing |
| 83 | // the overlap distance alone won't prevent the collision from occuring. |
| 84 | // Explicitly the collision for this frame if the shapes don't overlap using the new distance. |
| 85 | return (deepest <= 0.0f); |
| 86 | |
| 87 | // Lots more that you could improve upon here as well: |
| 88 | // * Modify the joint over time to make it plastic. |
| 89 | // * Modify the joint in the post-step to make it conditionally plastic (like clay). |
| 90 | // * Track a joint for the deepest contact point instead of the first. |
| 91 | // * Track a joint for each contact point. (more complicated since you only get one data pointer). |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | PostStepRemoveJointcxx . . $ PostStepRemoveJoint(967c2057ff39f02d).(cpSpacecxx . . $ cpSpace# *space, void *key, void *data) |
| 96 | { |
| 97 | // printf("Removing joint for %p\n", data); |
| 98 | |
| 99 | cpConstraintcxx . . $ cpConstraint# *joint = (cpConstraintcxx . . $ cpConstraint# *)key; |
| 100 | cpSpaceRemoveConstraintcxx . . $ cpSpaceRemoveConstraint(ab426c857162788d).(space, joint); |
| 101 | cpConstraintFreecxx . . $ cpConstraintFree(ce362f6e378064c6).(joint); |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | StickySeparatecxx . . $ StickySeparate(429dfc06831dcc40).(cpArbitercxx . . $ cpArbiter# *arb, cpSpacecxx . . $ cpSpace# *space, void *data) |
| 106 | { |
| 107 | cpConstraintcxx . . $ cpConstraint# *joint = (cpConstraintcxx . . $ cpConstraint# *)cpArbiterGetUserDatacxx . . $ cpArbiterGetUserData(74f6a6b1b8167460).(arb); |
| 108 | |
| 109 | if(joint){ |
| 110 | // The joint won't be removed until the step is done. |
| 111 | // Need to disable it so that it won't apply itself. |
| 112 | // Setting the force to 0 will do just that |
| 113 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(joint, 0.0f); |
| 114 | |
| 115 | // Perform the removal in a post-step() callback. |
| 116 | cpSpaceAddPostStepCallbackcxx . . $ cpSpaceAddPostStepCallback(1ff9997ad0bee464).(space, PostStepRemoveJointcxx . . $ PostStepRemoveJoint(967c2057ff39f02d)., joint, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 117 | |
| 118 | // NULL out the reference to the joint. |
| 119 | // Not required, but it's a good practice. |
| 120 | cpArbiterSetUserDatacxx . . $ cpArbiterSetUserData(9aa61450fdbfe78e).(arb, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static void |
| 125 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 126 | { |
| 127 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 128 | } |
| 129 | |
| 130 | static cpSpacecxx . . $ cpSpace# * |
| 131 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 132 | { |
| 133 | ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = "Sticky collisions using the cpArbiter data pointer."; |
| 134 | |
| 135 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 136 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 10); |
| 137 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -1000)); |
| 138 | cpSpaceSetCollisionSlopcxx . . $ cpSpaceSetCollisionSlop(fa66a0a48611083e).(space, 2.0); |
| 139 | |
| 140 | cpBodycxx . . $ cpBody# *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 141 | cpShapecxx . . $ cpShape# *shape; |
| 142 | |
| 143 | // Create segments around the edge of the screen. |
| 144 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-340,-260), cpvcxx . . $ cpv(3142d57d978b2176).(-340, 260), 20.0f)); |
| 145 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 146 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 147 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 148 | |
| 149 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).( 340,-260), cpvcxx . . $ cpv(3142d57d978b2176).( 340, 260), 20.0f)); |
| 150 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 151 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 152 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 153 | |
| 154 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-340,-260), cpvcxx . . $ cpv(3142d57d978b2176).( 340,-260), 20.0f)); |
| 155 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 156 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 157 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 158 | |
| 159 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-340, 260), cpvcxx . . $ cpv(3142d57d978b2176).( 340, 260), 20.0f)); |
| 160 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 161 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 162 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 163 | |
| 164 | for(int i=0; i<200; i++){ |
| 165 | cpFloatcxx . . $ cpFloat# mass = 0.15f; |
| 166 | cpFloatcxx . . $ cpFloat# radius = 10.0f; |
| 167 | |
| 168 | cpBodycxx . . $ cpBody# *body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, cpMomentForCirclecxx . . $ cpMomentForCircle(a539e2d95614f770).(mass, 0.0f, radius, cpvzerocxx . . $ cpvzero.))); |
| 169 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(cpflerpcxx . . $ cpflerp(7678bba071383ab).(-150.0f, 150.0f, frandcxx . . $ frand(31fa7155c10fdd13).()), cpflerpcxx . . $ cpflerp(7678bba071383ab).(-150.0f, 150.0f, frandcxx . . $ frand(31fa7155c10fdd13).()))); |
| 170 | |
| 171 | cpShapecxx . . $ cpShape# *shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpCircleShapeNewcxx . . $ cpCircleShapeNew(e5e4e3a52c89533b).(body, radius + STICK_SENSOR_THICKNESScxx . . $ `demo/Sticky.c:29:9`!, cpvzerocxx . . $ cpvzero.)); |
| 172 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.9f); |
| 173 | cpShapeSetCollisionTypecxx . . $ cpShapeSetCollisionType(2dd430e3162ea2fe).(shape, COLLISION_TYPE_STICKYcxx . . $ COLLISION_TYPE_STICKY.); |
| 174 | } |
| 175 | |
| 176 | cpCollisionHandlercxx . . $ cpCollisionHandler# *handler = cpSpaceAddWildcardHandlercxx . . $ cpSpaceAddWildcardHandler(abdf0636a7c470dd).(space, COLLISION_TYPE_STICKYcxx . . $ COLLISION_TYPE_STICKY.); |
| 177 | handler->preSolveFunccxx . . $ cpCollisionHandler#preSolveFunc. = StickyPreSolvecxx . . $ StickyPreSolve(46926bbb19d93a13).; |
| 178 | handler->separateFunccxx . . $ cpCollisionHandler#separateFunc. = StickySeparatecxx . . $ StickySeparate(429dfc06831dcc40).; |
| 179 | |
| 180 | return space; |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 185 | { |
| 186 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 187 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 188 | } |
| 189 | |
| 190 | ChipmunkDemocxx . . $ ChipmunkDemo# Stickycxx . . $ Sticky. = { |
| 191 | "Sticky Surfaces", |
| 192 | 1.0/60.0, |
| 193 | initcxx . . $ init(1f0082e822a7ce76)., |
| 194 | updatecxx . . $ update(fa66a0a48611083e)., |
| 195 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 196 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 197 | }; |