| 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 | enum CollisionTypescxx . . $ CollisionTypes# { |
| 26 | COLLISION_TYPE_ONE_WAYcxx . . $ COLLISION_TYPE_ONE_WAY. = 1, |
| 27 | }; |
| 28 | |
| 29 | typedef struct OneWayPlatformcxx . . $ OneWayPlatform# { |
| 30 | cpVectcxx . . $ cpVect# ncxx . . $ OneWayPlatform#n.; // direction objects may pass through |
| 31 | } OneWayPlatformcxx . . $ OneWayPlatform#; |
| 32 | |
| 33 | static OneWayPlatformcxx . . $ OneWayPlatform# platformInstancecxx . . $ platformInstance.; |
| 34 | |
| 35 | static cpBoolcxx . . $ cpBool# |
| 36 | PreSolvecxx . . $ PreSolve(46926bbb19d93a13).(cpArbitercxx . . $ cpArbiter# *arb, cpSpacecxx . . $ cpSpace# *space, void *ignore) |
| 37 | { |
| 38 | CP_ARBITER_GET_SHAPEScxx . . $ `include/chipmunk/cpArbiter.h:73:9`!(arb, a, b); |
| 39 | OneWayPlatformcxx . . $ OneWayPlatform# *platform = (OneWayPlatformcxx . . $ OneWayPlatform# *)cpShapeGetUserDatacxx . . $ cpShapeGetUserData(403f0d6a0e6d1842).(a); |
| 40 | |
| 41 | if(cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(cpArbiterGetNormalcxx . . $ cpArbiterGetNormal(bcbe2dc24ab42fef).(arb), platform->ncxx . . $ OneWayPlatform#n.) < 0){ |
| 42 | return cpArbiterIgnorecxx . . $ cpArbiterIgnore(8db1597ce3e43121).(arb); |
| 43 | } |
| 44 | |
| 45 | return cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!; |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 50 | { |
| 51 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 52 | } |
| 53 | |
| 54 | static cpSpacecxx . . $ cpSpace# * |
| 55 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 56 | { |
| 57 | ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = "One way platforms are trivial in Chipmunk using a very simple collision callback."; |
| 58 | |
| 59 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 60 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 10); |
| 61 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -100)); |
| 62 | |
| 63 | cpBodycxx . . $ cpBody# *body, *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 64 | cpShapecxx . . $ cpShape# *shape; |
| 65 | |
| 66 | // Create segments around the edge of the screen. |
| 67 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), 0.0f)); |
| 68 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 69 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 70 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 71 | |
| 72 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f)); |
| 73 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 74 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 75 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 76 | |
| 77 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), 0.0f)); |
| 78 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 79 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 80 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 81 | |
| 82 | // Add our one way segment |
| 83 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-160,-100), cpvcxx . . $ cpv(3142d57d978b2176).(160,-100), 10.0f)); |
| 84 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 85 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 86 | cpShapeSetCollisionTypecxx . . $ cpShapeSetCollisionType(2dd430e3162ea2fe).(shape, COLLISION_TYPE_ONE_WAYcxx . . $ COLLISION_TYPE_ONE_WAY.); |
| 87 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 88 | |
| 89 | // We'll use the data pointer for the OneWayPlatform struct |
| 90 | platformInstancecxx . . $ platformInstance..ncxx . . $ OneWayPlatform#n. = cpvcxx . . $ cpv(3142d57d978b2176).(0, 1); // let objects pass upwards |
| 91 | cpShapeSetUserDatacxx . . $ cpShapeSetUserData(ebab24832b4ea92d).(shape, &platformInstancecxx . . $ platformInstance.); |
| 92 | |
| 93 | |
| 94 | // Add a ball to test it out |
| 95 | cpFloatcxx . . $ cpFloat# radius = 15.0f; |
| 96 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(10.0f, cpMomentForCirclecxx . . $ cpMomentForCircle(a539e2d95614f770).(10.0f, 0.0f, radius, cpvzerocxx . . $ cpvzero.))); |
| 97 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0, -200)); |
| 98 | cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0, 170)); |
| 99 | |
| 100 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpCircleShapeNewcxx . . $ cpCircleShapeNew(e5e4e3a52c89533b).(body, radius, cpvzerocxx . . $ cpvzero.)); |
| 101 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 0.0f); |
| 102 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.9f); |
| 103 | cpShapeSetCollisionTypecxx . . $ cpShapeSetCollisionType(2dd430e3162ea2fe).(shape, 2); |
| 104 | |
| 105 | cpCollisionHandlercxx . . $ cpCollisionHandler# *handler = cpSpaceAddWildcardHandlercxx . . $ cpSpaceAddWildcardHandler(abdf0636a7c470dd).(space, COLLISION_TYPE_ONE_WAYcxx . . $ COLLISION_TYPE_ONE_WAY.); |
| 106 | handler->preSolveFunccxx . . $ cpCollisionHandler#preSolveFunc. = PreSolvecxx . . $ PreSolve(46926bbb19d93a13).; |
| 107 | |
| 108 | return space; |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 113 | { |
| 114 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 115 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 116 | } |
| 117 | |
| 118 | ChipmunkDemocxx . . $ ChipmunkDemo# OneWaycxx . . $ OneWay. = { |
| 119 | "One Way Platforms", |
| 120 | 1.0/60.0, |
| 121 | initcxx . . $ init(1f0082e822a7ce76)., |
| 122 | updatecxx . . $ update(fa66a0a48611083e)., |
| 123 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 124 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 125 | }; |