| 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 "chipmunk/chipmunk_unsafe.h"cxx . . $ `<file>/include/chipmunk/chipmunk_unsafe.h`/ |
| 24 | |
| 25 | #include "ChipmunkDemo.h"cxx . . $ `<file>/demo/ChipmunkDemo.h`/ |
| 26 | |
| 27 | #define DENSITYcxx . . $ `demo/Convex.c:27:9`! (1.0/10000.0) |
| 28 | |
| 29 | static cpShapecxx . . $ cpShape# *shapecxx . . $ shape.; |
| 30 | |
| 31 | static void |
| 32 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 33 | { |
| 34 | cpFloatcxx . . $ cpFloat# tolerance = 2.0; |
| 35 | |
| 36 | if(ChipmunkDemoRightClickcxx . . $ ChipmunkDemoRightClick. && cpShapePointQuerycxx . . $ cpShapePointQuery(fba695676919fb41).(shapecxx . . $ shape., ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) > tolerance){ |
| 37 | cpBodycxx . . $ cpBody# *body = cpShapeGetBodycxx . . $ cpShapeGetBody(abc772114277478f).(shapecxx . . $ shape.); |
| 38 | int count = cpPolyShapeGetCountcxx . . $ cpPolyShapeGetCount(46d926dc73b3f622).(shapecxx . . $ shape.); |
| 39 | |
| 40 | // Allocate the space for the new vertexes on the stack. |
| 41 | cpVectcxx . . $ cpVect# *verts = (cpVectcxx . . $ cpVect# *)allocacxx . . $ `<external>/11612401604547820122/alloca.h:35:10`!((count + 1)*sizeof(cpVect)); |
| 42 | |
| 43 | for(int i=0; i<count; i++){ |
| 44 | verts[i] = cpPolyShapeGetVertcxx . . $ cpPolyShapeGetVert(9392d0fc785db450).(shapecxx . . $ shape., i); |
| 45 | } |
| 46 | |
| 47 | verts[count] = cpBodyWorldToLocalcxx . . $ cpBodyWorldToLocal(4ff4900d49a97e73).(body, ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse.); |
| 48 | |
| 49 | // This function builds a convex hull for the vertexes. |
| 50 | // Because the result array is the same as verts, it will reduce it in place. |
| 51 | int hullCount = cpConvexHullcxx . . $ cpConvexHull(ca3a36ade7aeec92).(count + 1, verts, verts, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, tolerance); |
| 52 | |
| 53 | // Figure out how much to shift the body by. |
| 54 | cpVectcxx . . $ cpVect# centroid = cpCentroidForPolycxx . . $ cpCentroidForPoly(740f059a8e4cff9d).(hullCount, verts); |
| 55 | |
| 56 | // Recalculate the body properties to match the updated shape. |
| 57 | cpFloatcxx . . $ cpFloat# mass = cpAreaForPolycxx . . $ cpAreaForPoly(c1e6bc8c2660eac5).(hullCount, verts, 0.0f)*DENSITYcxx . . $ `demo/Convex.c:27:9`!; |
| 58 | cpBodySetMasscxx . . $ cpBodySetMass(797bd3bb7a653b39).(body, mass); |
| 59 | cpBodySetMomentcxx . . $ cpBodySetMoment(797bd3bb7a653b39).(body, cpMomentForPolycxx . . $ cpMomentForPoly(89d843ff16ae6b28).(mass, hullCount, verts, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(centroid), 0.0f)); |
| 60 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpBodyLocalToWorldcxx . . $ cpBodyLocalToWorld(4ff4900d49a97e73).(body, centroid)); |
| 61 | |
| 62 | // Use the setter function from chipmunk_unsafe.h. |
| 63 | // You could also remove and recreate the shape if you wanted. |
| 64 | cpPolyShapeSetVertscxx . . $ cpPolyShapeSetVerts(1d0447bf15bf2ec5).(shapecxx . . $ shape., hullCount, verts, cpTransformTranslatecxx . . $ cpTransformTranslate(9ddf098b65af1f2f).(cpvnegcxx . . $ cpvneg(e06142a62109ae08).(centroid))); |
| 65 | } |
| 66 | |
| 67 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 68 | } |
| 69 | |
| 70 | static cpSpacecxx . . $ cpSpace# * |
| 71 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 72 | { |
| 73 | ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = "Right click and drag to change the blocks's shape."; |
| 74 | |
| 75 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 76 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 30); |
| 77 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -500)); |
| 78 | cpSpaceSetSleepTimeThresholdcxx . . $ cpSpaceSetSleepTimeThreshold(fa66a0a48611083e).(space, 0.5f); |
| 79 | cpSpaceSetCollisionSlopcxx . . $ cpSpaceSetCollisionSlop(fa66a0a48611083e).(space, 0.5f); |
| 80 | |
| 81 | cpBodycxx . . $ cpBody# *body, *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 82 | |
| 83 | // Create segments around the edge of the screen. |
| 84 | shapecxx . . $ shape. = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), 0.0f)); |
| 85 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shapecxx . . $ shape., 1.0f); |
| 86 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shapecxx . . $ shape., 1.0f); |
| 87 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shapecxx . . $ shape., NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 88 | |
| 89 | cpFloatcxx . . $ cpFloat# width = 50.0f; |
| 90 | cpFloatcxx . . $ cpFloat# height = 70.0f; |
| 91 | cpFloatcxx . . $ cpFloat# mass = width*height*DENSITYcxx . . $ `demo/Convex.c:27:9`!; |
| 92 | cpFloatcxx . . $ cpFloat# moment = cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(mass, width, height); |
| 93 | |
| 94 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment)); |
| 95 | |
| 96 | shapecxx . . $ shape. = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(body, width, height, 0.0)); |
| 97 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shapecxx . . $ shape., 0.6f); |
| 98 | |
| 99 | return space; |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 104 | { |
| 105 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 106 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 107 | } |
| 108 | |
| 109 | ChipmunkDemocxx . . $ ChipmunkDemo# Convexcxx . . $ Convex. = { |
| 110 | "Convex.", |
| 111 | 1.0/60.0, |
| 112 | initcxx . . $ init(1f0082e822a7ce76)., |
| 113 | updatecxx . . $ update(fa66a0a48611083e)., |
| 114 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 115 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 116 | }; |