| 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 "ChipmunkDemo.h" |
| 24 | |
| 25 | static cpBody *KinematicBoxBody; |
| 26 | |
| 27 | static void |
| 28 | update(cpSpace *space, double dt) |
| 29 | { |
| 30 | cpSpaceStep(space, dt); |
| 31 | } |
| 32 | |
| 33 | static void |
| 34 | AddBox(cpSpace *space, cpVect pos, cpFloat mass, cpFloat width, cpFloat height) |
| 35 | { |
| 36 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height))); |
| 37 | cpBodySetPosition(body, pos); |
| 38 | |
| 39 | cpShape *shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0)); |
| 40 | cpShapeSetElasticity(shape, 0.0f); |
| 41 | cpShapeSetFriction(shape, 0.7f); |
| 42 | } |
| 43 | |
| 44 | static void |
| 45 | AddSegment(cpSpace *space, cpVect pos, cpFloat mass, cpFloat width, cpFloat height) |
| 46 | { |
| 47 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height))); |
| 48 | cpBodySetPosition(body, pos); |
| 49 | |
| 50 | cpShape *shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, cpv(0.0, (height - width)/2.0), cpv(0.0, (width - height)/2.0), width/2.0)); |
| 51 | cpShapeSetElasticity(shape, 0.0f); |
| 52 | cpShapeSetFriction(shape, 0.7f); |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | AddCircle(cpSpace *space, cpVect pos, cpFloat mass, cpFloat radius) |
| 57 | { |
| 58 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0, radius, cpvzero))); |
| 59 | cpBodySetPosition(body, pos); |
| 60 | |
| 61 | cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); |
| 62 | cpShapeSetElasticity(shape, 0.0f); |
| 63 | cpShapeSetFriction(shape, 0.7f); |
| 64 | } |
| 65 | |
| 66 | static cpSpace * |
| 67 | init(void) |
| 68 | { |
| 69 | cpSpace *space = cpSpaceNew(); |
| 70 | cpSpaceSetGravity(space, cpv(0, -600)); |
| 71 | |
| 72 | cpShape *shape; |
| 73 | |
| 74 | // We create an infinite mass rogue body to attach the line segments too |
| 75 | // This way we can control the rotation however we want. |
| 76 | KinematicBoxBody = cpSpaceAddBody(space, cpBodyNewKinematic()); |
| 77 | cpBodySetAngularVelocity(KinematicBoxBody, 0.4f); |
| 78 | |
| 79 | // Set up the static box. |
| 80 | cpVect a = cpv(-200, -200); |
| 81 | cpVect b = cpv(-200, 200); |
| 82 | cpVect c = cpv( 200, 200); |
| 83 | cpVect d = cpv( 200, -200); |
| 84 | |
| 85 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(KinematicBoxBody, a, b, 0.0f)); |
| 86 | cpShapeSetElasticity(shape, 1.0f); |
| 87 | cpShapeSetFriction(shape, 1.0f); |
| 88 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 89 | |
| 90 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(KinematicBoxBody, b, c, 0.0f)); |
| 91 | cpShapeSetElasticity(shape, 1.0f); |
| 92 | cpShapeSetFriction(shape, 1.0f); |
| 93 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 94 | |
| 95 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(KinematicBoxBody, c, d, 0.0f)); |
| 96 | cpShapeSetElasticity(shape, 1.0f); |
| 97 | cpShapeSetFriction(shape, 1.0f); |
| 98 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 99 | |
| 100 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(KinematicBoxBody, d, a, 0.0f)); |
| 101 | cpShapeSetElasticity(shape, 1.0f); |
| 102 | cpShapeSetFriction(shape, 1.0f); |
| 103 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 104 | |
| 105 | cpFloat mass = 1; |
| 106 | cpFloat width = 30; |
| 107 | cpFloat height = width*2; |
| 108 | |
| 109 | // Add the bricks. |
| 110 | for(int i=0; i<7; i++){ |
| 111 | for(int j=0; j<3; j++){ |
| 112 | cpVect pos = cpv(i*width - 150, j*height - 150); |
| 113 | |
| 114 | int type = (rand()%3000)/1000; |
| 115 | if(type ==0){ |
| 116 | AddBox(space, pos, mass, width, height); |
| 117 | } else if(type == 1){ |
| 118 | AddSegment(space, pos, mass, width, height); |
| 119 | } else { |
| 120 | AddCircle(space, cpvadd(pos, cpv(0.0, (height - width)/2.0)), mass, width/2.0); |
| 121 | AddCircle(space, cpvadd(pos, cpv(0.0, (width - height)/2.0)), mass, width/2.0); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return space; |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | destroy(cpSpace *space) |
| 131 | { |
| 132 | ChipmunkDemoFreeSpaceChildren(space); |
| 133 | cpSpaceFree(space); |
| 134 | } |
| 135 | |
| 136 | ChipmunkDemo Tumble = { |
| 137 | "Tumble", |
| 138 | 1.0/180.0, |
| 139 | init, |
| 140 | update, |
| 141 | ChipmunkDemoDefaultDrawImpl, |
| 142 | destroy, |
| 143 | }; |