| 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 <string.h> |
| 23 | |
| 24 | #include "chipmunk/chipmunk.h"cxx . . $ `<file>/include/chipmunk/chipmunk.h`/ |
| 25 | |
| 26 | #include "ChipmunkDemo.h"cxx . . $ `<file>/demo/ChipmunkDemo.h`/ |
| 27 | |
| 28 | #define DENSITYcxx . . $ `demo/Shatter.c:28:9`! (1.0/10000.0) |
| 29 | |
| 30 | #define MAX_VERTEXES_PER_VORONOIcxx . . $ `demo/Shatter.c:30:9`! 16 |
| 31 | |
| 32 | struct WorleyContexcxx . . $ WorleyContex# { |
| 33 | uint32_tcxx . . $ uint32_t# seedcxx . . $ WorleyContex#seed.; |
| 34 | cpFloatcxx . . $ cpFloat# cellSizecxx . . $ WorleyContex#cellSize.; |
| 35 | int widthcxx . . $ WorleyContex#width., heightcxx . . $ WorleyContex#height.; |
| 36 | cpBBcxx . . $ cpBB# bbcxx . . $ WorleyContex#bb.; |
| 37 | cpVectcxx . . $ cpVect# focuscxx . . $ WorleyContex#focus.; |
| 38 | }; |
| 39 | |
| 40 | static inline cpVectcxx . . $ cpVect# |
| 41 | HashVectcxx . . $ HashVect(3f455aa2aa9988b0).(uint32_tcxx . . $ uint32_t# x, uint32_tcxx . . $ uint32_t# y, uint32_tcxx . . $ uint32_t# seed) |
| 42 | { |
| 43 | // cpFloat border = 0.21f; |
| 44 | cpFloatcxx . . $ cpFloat# border = 0.05f; |
| 45 | uint32_tcxx . . $ uint32_t# h = (x*1640531513 ^ y*2654435789) + seed; |
| 46 | |
| 47 | return cpvcxx . . $ cpv(3142d57d978b2176).( |
| 48 | cpflerpcxx . . $ cpflerp(7678bba071383ab).(border, 1.0f - border, (cpFloatcxx . . $ cpFloat#)( h & 0xFFFF)/(cpFloatcxx . . $ cpFloat#)0xFFFF), |
| 49 | cpflerpcxx . . $ cpflerp(7678bba071383ab).(border, 1.0f - border, (cpFloatcxx . . $ cpFloat#)((h>>16) & 0xFFFF)/(cpFloatcxx . . $ cpFloat#)0xFFFF) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | static cpVectcxx . . $ cpVect# |
| 54 | WorleyPointcxx . . $ WorleyPoint(3463ba1f34eac120).(int i, int j, struct WorleyContexcxx . . $ WorleyContex# *context) |
| 55 | { |
| 56 | cpFloatcxx . . $ cpFloat# size = context->cellSizecxx . . $ WorleyContex#cellSize.; |
| 57 | int width = context->widthcxx . . $ WorleyContex#width.; |
| 58 | int height = context->heightcxx . . $ WorleyContex#height.; |
| 59 | cpBBcxx . . $ cpBB# bb = context->bbcxx . . $ WorleyContex#bb.; |
| 60 | |
| 61 | // cpVect fv = cpv(0.5, 0.5); |
| 62 | cpVectcxx . . $ cpVect# fv = HashVectcxx . . $ HashVect(3f455aa2aa9988b0).(i, j, context->seedcxx . . $ WorleyContex#seed.); |
| 63 | |
| 64 | return cpvcxx . . $ cpv(3142d57d978b2176).( |
| 65 | cpflerpcxx . . $ cpflerp(7678bba071383ab).(bb.lcxx . . $ cpBB#l., bb.rcxx . . $ cpBB#r., 0.5f) + size*(i + fv.xcxx . . $ cpVect#x. - width*0.5f), |
| 66 | cpflerpcxx . . $ cpflerp(7678bba071383ab).(bb.bcxx . . $ cpBB#b., bb.tcxx . . $ cpBB#t., 0.5f) + size*(j + fv.ycxx . . $ cpVect#y. - height*0.5f) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | ClipCellcxx . . $ ClipCell(5f3bfda582db3530).(cpShapecxx . . $ cpShape# *shape, cpVectcxx . . $ cpVect# center, int i, int j, struct WorleyContexcxx . . $ WorleyContex# *context, cpVectcxx . . $ cpVect# *verts, cpVectcxx . . $ cpVect# *clipped, int count) |
| 72 | { |
| 73 | cpVectcxx . . $ cpVect# other = WorleyPointcxx . . $ WorleyPoint(3463ba1f34eac120).(i, j, context); |
| 74 | // printf(" other %dx%d: (% 5.2f, % 5.2f) ", i, j, other.x, other.y); |
| 75 | if(cpShapePointQuerycxx . . $ cpShapePointQuery(fba695676919fb41).(shape, other, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) > 0.0f){ |
| 76 | // printf("excluded\n"); |
| 77 | memcpycxx . . $ memcpy(cc817cb175f282d).(clipped, verts, count*sizeof(cpVectcxx . . $ cpVect#)); |
| 78 | return count; |
| 79 | } else { |
| 80 | // printf("clipped\n"); |
| 81 | } |
| 82 | |
| 83 | cpVectcxx . . $ cpVect# n = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(other, center); |
| 84 | cpFloatcxx . . $ cpFloat# dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(center, other, 0.5f)); |
| 85 | |
| 86 | int clipped_count = 0; |
| 87 | for(int j=0, i=count-1; j<count; i=j, j++){ |
| 88 | cpVectcxx . . $ cpVect# a = verts[i]; |
| 89 | cpFloatcxx . . $ cpFloat# a_dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(a, n) - dist; |
| 90 | |
| 91 | if(a_dist <= 0.0){ |
| 92 | clipped[clipped_count] = a; |
| 93 | clipped_count++; |
| 94 | } |
| 95 | |
| 96 | cpVectcxx . . $ cpVect# b = verts[j]; |
| 97 | cpFloatcxx . . $ cpFloat# b_dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(b, n) - dist; |
| 98 | |
| 99 | if(a_dist*b_dist < 0.0f){ |
| 100 | cpFloatcxx . . $ cpFloat# t = cpfabscxx . . $ cpfabs(447c67c6676f75c3).(a_dist)/(cpfabscxx . . $ cpfabs(447c67c6676f75c3).(a_dist) + cpfabscxx . . $ cpfabs(447c67c6676f75c3).(b_dist)); |
| 101 | |
| 102 | clipped[clipped_count] = cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(a, b, t); |
| 103 | clipped_count++; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return clipped_count; |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | ShatterCellcxx . . $ ShatterCell(1e4f215c65f2d3c5).(cpSpacecxx . . $ cpSpace# *space, cpShapecxx . . $ cpShape# *shape, cpVectcxx . . $ cpVect# cell, int cell_i, int cell_j, struct WorleyContexcxx . . $ WorleyContex# *context) |
| 112 | { |
| 113 | // printf("cell %dx%d: (% 5.2f, % 5.2f)\n", cell_i, cell_j, cell.x, cell.y); |
| 114 | |
| 115 | cpBodycxx . . $ cpBody# *body = cpShapeGetBodycxx . . $ cpShapeGetBody(abc772114277478f).(shape); |
| 116 | |
| 117 | cpVectcxx . . $ cpVect# *ping = (cpVectcxx . . $ cpVect# *)allocacxx . . $ `<external>/11612401604547820122/alloca.h:35:10`!(MAX_VERTEXES_PER_VORONOIcxx . . $ `demo/Shatter.c:30:9`!*sizeof(cpVect)); |
| 118 | cpVectcxx . . $ cpVect# *pong = (cpVectcxx . . $ cpVect# *)allocacxx . . $ `<external>/11612401604547820122/alloca.h:35:10`!(MAX_VERTEXES_PER_VORONOIcxx . . $ `demo/Shatter.c:30:9`!*sizeof(cpVect)); |
| 119 | |
| 120 | int count = cpPolyShapeGetCountcxx . . $ cpPolyShapeGetCount(46d926dc73b3f622).(shape); |
| 121 | count = (count > MAX_VERTEXES_PER_VORONOIcxx . . $ `demo/Shatter.c:30:9`! ? MAX_VERTEXES_PER_VORONOIcxx . . $ `demo/Shatter.c:30:9`! : count); |
| 122 | |
| 123 | for(int i=0; i<count; i++){ |
| 124 | ping[i] = cpBodyLocalToWorldcxx . . $ cpBodyLocalToWorld(4ff4900d49a97e73).(body, cpPolyShapeGetVertcxx . . $ cpPolyShapeGetVert(9392d0fc785db450).(shape, i)); |
| 125 | } |
| 126 | |
| 127 | for(int i=0; i<context->widthcxx . . $ WorleyContex#width.; i++){ |
| 128 | for(int j=0; j<context->heightcxx . . $ WorleyContex#height.; j++){ |
| 129 | if( |
| 130 | !(i == cell_i && j == cell_j) && |
| 131 | cpShapePointQuerycxx . . $ cpShapePointQuery(fba695676919fb41).(shape, cell, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) < 0.0f |
| 132 | ){ |
| 133 | count = ClipCellcxx . . $ ClipCell(5f3bfda582db3530).(shape, cell, i, j, context, ping, pong, count); |
| 134 | memcpycxx . . $ memcpy(cc817cb175f282d).(ping, pong, count*sizeof(cpVectcxx . . $ cpVect#)); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | cpVectcxx . . $ cpVect# centroid = cpCentroidForPolycxx . . $ cpCentroidForPoly(740f059a8e4cff9d).(count, ping); |
| 140 | cpFloatcxx . . $ cpFloat# mass = cpAreaForPolycxx . . $ cpAreaForPoly(c1e6bc8c2660eac5).(count, ping, 0.0f)*DENSITYcxx . . $ `demo/Shatter.c:28:9`!; |
| 141 | cpFloatcxx . . $ cpFloat# moment = cpMomentForPolycxx . . $ cpMomentForPoly(89d843ff16ae6b28).(mass, count, ping, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(centroid), 0.0f); |
| 142 | |
| 143 | cpBodycxx . . $ cpBody# *new_body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment)); |
| 144 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(new_body, centroid); |
| 145 | cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(new_body, cpBodyGetVelocityAtWorldPointcxx . . $ cpBodyGetVelocityAtWorldPoint(4ff4900d49a97e73).(body, centroid)); |
| 146 | cpBodySetAngularVelocitycxx . . $ cpBodySetAngularVelocity(797bd3bb7a653b39).(new_body, cpBodyGetAngularVelocitycxx . . $ cpBodyGetAngularVelocity(2880d7999e0b34c3).(body)); |
| 147 | |
| 148 | cpTransformcxx . . $ cpTransform# transform = cpTransformTranslatecxx . . $ cpTransformTranslate(9ddf098b65af1f2f).(cpvnegcxx . . $ cpvneg(e06142a62109ae08).(centroid)); |
| 149 | cpShapecxx . . $ cpShape# *new_shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpPolyShapeNewcxx . . $ cpPolyShapeNew(119891a3da20e8eb).(new_body, count, ping, transform, 0.0)); |
| 150 | // Copy whatever properties you have set on the original shape that are important |
| 151 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(new_shape, cpShapeGetFrictioncxx . . $ cpShapeGetFriction(1bf41e75729c6179).(shape)); |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | ShatterShapecxx . . $ ShatterShape(8cb7e58670ed0268).(cpSpacecxx . . $ cpSpace# *space, cpShapecxx . . $ cpShape# *shape, cpFloatcxx . . $ cpFloat# cellSize, cpVectcxx . . $ cpVect# focus) |
| 156 | { |
| 157 | cpSpaceRemoveShapecxx . . $ cpSpaceRemoveShape(b4590e6760e37cb0).(space, shape); |
| 158 | cpSpaceRemoveBodycxx . . $ cpSpaceRemoveBody(f865e61ac2c3a7c6).(space, cpShapeGetBodycxx . . $ cpShapeGetBody(abc772114277478f).(shape)); |
| 159 | |
| 160 | cpBBcxx . . $ cpBB# bb = cpShapeGetBBcxx . . $ cpShapeGetBB(44a9343cdf942fdb).(shape); |
| 161 | int width = (int)((bb.rcxx . . $ cpBB#r. - bb.lcxx . . $ cpBB#l.)/cellSize) + 1; |
| 162 | int height = (int)((bb.tcxx . . $ cpBB#t. - bb.bcxx . . $ cpBB#b.)/cellSize) + 1; |
| 163 | // printf("Splitting as %dx%d\n", width, height); |
| 164 | struct WorleyContexcxx . . $ WorleyContex# context = {randcxx . . $ rand(b126dc7c1de90089).(), cellSize, width, height, bb, focus}; |
| 165 | |
| 166 | for(int i=0; i<context.widthcxx . . $ WorleyContex#width.; i++){ |
| 167 | for(int j=0; j<context.heightcxx . . $ WorleyContex#height.; j++){ |
| 168 | cpVectcxx . . $ cpVect# cell = WorleyPointcxx . . $ WorleyPoint(3463ba1f34eac120).(i, j, &context); |
| 169 | if(cpShapePointQuerycxx . . $ cpShapePointQuery(fba695676919fb41).(shape, cell, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) < 0.0f){ |
| 170 | ShatterCellcxx . . $ ShatterCell(1e4f215c65f2d3c5).(space, shape, cell, i, j, &context); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | cpBodyFreecxx . . $ cpBodyFree(92943d3541b63e72).(cpShapeGetBodycxx . . $ cpShapeGetBody(abc772114277478f).(shape)); |
| 176 | cpShapeFreecxx . . $ cpShapeFree(49d28a84d341894).(shape); |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 181 | { |
| 182 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 183 | |
| 184 | if(ChipmunkDemoRightDowncxx . . $ ChipmunkDemoRightDown.){ |
| 185 | cpPointQueryInfocxx . . $ cpPointQueryInfo# info; |
| 186 | if(cpSpacePointQueryNearestcxx . . $ cpSpacePointQueryNearest(b0e549d484789d).(space, ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., 0, GRAB_FILTERcxx . . $ GRAB_FILTER., &info)){ |
| 187 | cpBBcxx . . $ cpBB# bb = cpShapeGetBBcxx . . $ cpShapeGetBB(44a9343cdf942fdb).(info.shapecxx . . $ cpPointQueryInfo#shape.); |
| 188 | cpFloatcxx . . $ cpFloat# cell_size = cpfmaxcxx . . $ cpfmax(d8e7e0733fa41c87).(bb.rcxx . . $ cpBB#r. - bb.lcxx . . $ cpBB#l., bb.tcxx . . $ cpBB#t. - bb.bcxx . . $ cpBB#b.)/5.0f; |
| 189 | if(cell_size > 5.0f){ |
| 190 | ShatterShapecxx . . $ ShatterShape(8cb7e58670ed0268).(space, (cpShapecxx . . $ cpShape# *)info.shapecxx . . $ cpPointQueryInfo#shape., cell_size, ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse.); |
| 191 | } else { |
| 192 | // printf("Too small to splinter %f\n", cell_size); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static cpSpacecxx . . $ cpSpace# * |
| 199 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 200 | { |
| 201 | ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = "Right click something to shatter it."; |
| 202 | |
| 203 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 204 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 30); |
| 205 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -500)); |
| 206 | cpSpaceSetSleepTimeThresholdcxx . . $ cpSpaceSetSleepTimeThreshold(fa66a0a48611083e).(space, 0.5f); |
| 207 | cpSpaceSetCollisionSlopcxx . . $ cpSpaceSetCollisionSlop(fa66a0a48611083e).(space, 0.5f); |
| 208 | |
| 209 | cpBodycxx . . $ cpBody# *body, *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 210 | cpShapecxx . . $ cpShape# *shape; |
| 211 | |
| 212 | // Create segments around the edge of the screen. |
| 213 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-1000, -240), cpvcxx . . $ cpv(3142d57d978b2176).( 1000, -240), 0.0f)); |
| 214 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 215 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 216 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 217 | |
| 218 | cpFloatcxx . . $ cpFloat# width = 200.0f; |
| 219 | cpFloatcxx . . $ cpFloat# height = 200.0f; |
| 220 | cpFloatcxx . . $ cpFloat# mass = width*height*DENSITYcxx . . $ `demo/Shatter.c:28:9`!; |
| 221 | cpFloatcxx . . $ cpFloat# moment = cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(mass, width, height); |
| 222 | |
| 223 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment)); |
| 224 | |
| 225 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(body, width, height, 0.0)); |
| 226 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.6f); |
| 227 | |
| 228 | return space; |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 233 | { |
| 234 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 235 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 236 | } |
| 237 | |
| 238 | ChipmunkDemocxx . . $ ChipmunkDemo# Shattercxx . . $ Shatter. = { |
| 239 | "Shatter.", |
| 240 | 1.0f/60.0f, |
| 241 | initcxx . . $ init(1f0082e822a7ce76)., |
| 242 | updatecxx . . $ update(fa66a0a48611083e)., |
| 243 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 244 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 245 | }; |