| 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 cpFloat pentagon_mass = 0.0f; |
| 26 | static cpFloat pentagon_moment = 0.0f; |
| 27 | |
| 28 | // Iterate over all of the bodies and reset the ones that have fallen offscreen. |
| 29 | static void |
| 30 | eachBody(cpBody *body, void *unused) |
| 31 | { |
| 32 | cpVect pos = cpBodyGetPosition(body); |
| 33 | if(pos.y < -260 || cpfabs(pos.x) > 340){ |
| 34 | cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320; |
| 35 | cpBodySetPosition(body, cpv(x, 260)); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static void |
| 40 | update(cpSpace *space, double dt) |
| 41 | { |
| 42 | if(ChipmunkDemoRightDown){ |
| 43 | cpShape *nearest = cpSpacePointQueryNearest(space, ChipmunkDemoMouse, 0.0, GRAB_FILTER, NULL); |
| 44 | if(nearest){ |
| 45 | cpBody *body = cpShapeGetBody(nearest); |
| 46 | if(cpBodyGetType(body) == CP_BODY_TYPE_STATIC){ |
| 47 | cpBodySetType(body, CP_BODY_TYPE_DYNAMIC); |
| 48 | cpBodySetMass(body, pentagon_mass); |
| 49 | cpBodySetMoment(body, pentagon_moment); |
| 50 | } else if(cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC) { |
| 51 | cpBodySetType(body, CP_BODY_TYPE_STATIC); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | cpSpaceEachBody(space, &eachBody, NULL); |
| 57 | cpSpaceStep(space, dt); |
| 58 | } |
| 59 | |
| 60 | #define NUM_VERTS 5 |
| 61 | |
| 62 | static cpSpace * |
| 63 | init(void) |
| 64 | { |
| 65 | ChipmunkDemoMessageString = "Right click to make pentagons static/dynamic."; |
| 66 | |
| 67 | cpSpace *space = cpSpaceNew(); |
| 68 | cpSpaceSetIterations(space, 5); |
| 69 | cpSpaceSetGravity(space, cpv(0, -100)); |
| 70 | |
| 71 | cpBody *body, *staticBody = cpSpaceGetStaticBody(space); |
| 72 | cpShape *shape; |
| 73 | |
| 74 | // Vertexes for a triangle shape. |
| 75 | cpVect tris[] = { |
| 76 | cpv(-15,-15), |
| 77 | cpv( 0, 10), |
| 78 | cpv( 15,-15), |
| 79 | }; |
| 80 | |
| 81 | // Create the static triangles. |
| 82 | for(int i=0; i<9; i++){ |
| 83 | for(int j=0; j<6; j++){ |
| 84 | cpFloat stagger = (j%2)*40; |
| 85 | cpVect offset = cpv(i*80 - 320 + stagger, j*70 - 240); |
| 86 | shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, tris, cpTransformTranslate(offset), 0.0)); |
| 87 | cpShapeSetElasticity(shape, 1.0f); |
| 88 | cpShapeSetFriction(shape, 1.0f); |
| 89 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Create vertexes for a pentagon shape. |
| 94 | cpVect verts[NUM_VERTS]; |
| 95 | for(int i=0; i<NUM_VERTS; i++){ |
| 96 | cpFloat angle = -2.0f*CP_PI*i/((cpFloat) NUM_VERTS); |
| 97 | verts[i] = cpv(10*cos(angle), 10*sin(angle)); |
| 98 | } |
| 99 | |
| 100 | pentagon_mass = 1.0; |
| 101 | pentagon_moment = cpMomentForPoly(1.0f, NUM_VERTS, verts, cpvzero, 0.0f); |
| 102 | |
| 103 | // Add lots of pentagons. |
| 104 | for(int i=0; i<300; i++){ |
| 105 | body = cpSpaceAddBody(space, cpBodyNew(pentagon_mass, pentagon_moment)); |
| 106 | cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320; |
| 107 | cpBodySetPosition(body, cpv(x, 350)); |
| 108 | |
| 109 | shape = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpTransformIdentity, 0.0)); |
| 110 | cpShapeSetElasticity(shape, 0.0f); |
| 111 | cpShapeSetFriction(shape, 0.4f); |
| 112 | } |
| 113 | |
| 114 | return space; |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | destroy(cpSpace *space) |
| 119 | { |
| 120 | ChipmunkDemoFreeSpaceChildren(space); |
| 121 | cpSpaceFree(space); |
| 122 | } |
| 123 | |
| 124 | ChipmunkDemo Plink = { |
| 125 | "Plink", |
| 126 | 1.0/60.0, |
| 127 | init, |
| 128 | update, |
| 129 | ChipmunkDemoDefaultDrawImpl, |
| 130 | destroy, |
| 131 | }; |