| 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" |
| 23 | #include "chipmunk/chipmunk_unsafe.h" |
| 24 | #include "ChipmunkDemo.h" |
| 25 | |
| 26 | static void |
| 27 | update(cpSpace *space, double dt) |
| 28 | { |
| 29 | cpSpaceStep(space, dt); |
| 30 | } |
| 31 | |
| 32 | static cpSpace * |
| 33 | init(void) |
| 34 | { |
| 35 | cpSpace *space = cpSpaceNew(); |
| 36 | cpSpaceSetIterations(space, 30); |
| 37 | cpSpaceSetGravity(space, cpv(0, -100)); |
| 38 | cpSpaceSetSleepTimeThreshold(space, 0.5f); |
| 39 | cpSpaceSetCollisionSlop(space, 0.5f); |
| 40 | |
| 41 | cpBody *body, *staticBody = cpSpaceGetStaticBody(space); |
| 42 | cpShape *shape; |
| 43 | |
| 44 | // Create segments around the edge of the screen. |
| 45 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); |
| 46 | cpShapeSetElasticity(shape, 1.0f); |
| 47 | cpShapeSetFriction(shape, 1.0f); |
| 48 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 49 | |
| 50 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); |
| 51 | cpShapeSetElasticity(shape, 1.0f); |
| 52 | cpShapeSetFriction(shape, 1.0f); |
| 53 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 54 | |
| 55 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); |
| 56 | cpShapeSetElasticity(shape, 1.0f); |
| 57 | cpShapeSetFriction(shape, 1.0f); |
| 58 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 59 | |
| 60 | // Add lots of boxes. |
| 61 | for(int i=0; i<14; i++){ |
| 62 | for(int j=0; j<=i; j++){ |
| 63 | body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForBox(1.0f, 30.0f, 30.0f))); |
| 64 | cpBodySetPosition(body, cpv(j*32 - i*16, 300 - i*32)); |
| 65 | |
| 66 | shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 30.0f, 30.0f, 0.5f)); |
| 67 | cpShapeSetElasticity(shape, 0.0f); |
| 68 | cpShapeSetFriction(shape, 0.8f); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Add a ball to make things more interesting |
| 73 | cpFloat radius = 15.0f; |
| 74 | body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero))); |
| 75 | cpBodySetPosition(body, cpv(0, -240 + radius+5)); |
| 76 | |
| 77 | shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); |
| 78 | cpShapeSetElasticity(shape, 0.0f); |
| 79 | cpShapeSetFriction(shape, 0.9f); |
| 80 | |
| 81 | return space; |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | destroy(cpSpace *space) |
| 86 | { |
| 87 | ChipmunkDemoFreeSpaceChildren(space); |
| 88 | cpSpaceFree(space); |
| 89 | } |
| 90 | |
| 91 | ChipmunkDemo PyramidStack = { |
| 92 | "Pyramid Stack", |
| 93 | 1.0/180.0, |
| 94 | init, |
| 95 | update, |
| 96 | ChipmunkDemoDefaultDrawImpl, |
| 97 | destroy, |
| 98 | }; |