Debian Codegraph @

demo/PyramidTopple.c

raw symbols SCIP
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 void
26 update(cpSpace *space, double dt)
27 {
28 cpSpaceStep(space, dt);
29 }
30
31 #define WIDTH 4.0f
32 #define HEIGHT 30.0f
33
34 static void
35 add_domino(cpSpace *space, cpVect pos, cpBool flipped)
36 {
37 cpFloat mass = 1.0f;
38 cpFloat radius = 0.5f;
39 cpFloat moment = cpMomentForBox(mass, WIDTH, HEIGHT);
40
41 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
42 cpBodySetPosition(body, pos);
43
44 cpShape *shape = (flipped ? cpBoxShapeNew(body, HEIGHT, WIDTH, 0.0) : cpBoxShapeNew(body, WIDTH - radius*2.0f, HEIGHT, radius));
45 cpSpaceAddShape(space, shape);
46 cpShapeSetElasticity(shape, 0.0f);
47 cpShapeSetFriction(shape, 0.6f);
48 }
49
50 static cpSpace *
51 init(void)
52 {
53 cpSpace *space = cpSpaceNew();
54 cpSpaceSetIterations(space, 30);
55 cpSpaceSetGravity(space, cpv(0, -300));
56 cpSpaceSetSleepTimeThreshold(space, 0.5f);
57 cpSpaceSetCollisionSlop(space, 0.5f);
58
59 // Add a floor.
60 cpShape *shape = cpSpaceAddShape(space, cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpv(-600,-240), cpv(600,-240), 0.0f));
61 cpShapeSetElasticity(shape, 1.0f);
62 cpShapeSetFriction(shape, 1.0f);
63 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
64
65
66 // Add the dominoes.
67 int n = 12;
68 for(int i=0; i<n; i++){
69 for(int j=0; j<(n - i); j++){
70 cpVect offset = cpv((j - (n - 1 - i)*0.5f)*1.5f*HEIGHT, (i + 0.5f)*(HEIGHT + 2*WIDTH) - WIDTH - 240);
71 add_domino(space, offset, cpFalse);
72 add_domino(space, cpvadd(offset, cpv(0, (HEIGHT + WIDTH)/2.0f)), cpTrue);
73
74 if(j == 0){
75 add_domino(space, cpvadd(offset, cpv(0.5f*(WIDTH - HEIGHT), HEIGHT + WIDTH)), cpFalse);
76 }
77
78 if(j != n - i - 1){
79 add_domino(space, cpvadd(offset, cpv(HEIGHT*0.75f, (HEIGHT + 3*WIDTH)/2.0f)), cpTrue);
80 } else {
81 add_domino(space, cpvadd(offset, cpv(0.5f*(HEIGHT - WIDTH), HEIGHT + WIDTH)), cpFalse);
82 }
83 }
84 }
85
86 return space;
87 }
88
89 static void
90 destroy(cpSpace *space)
91 {
92 ChipmunkDemoFreeSpaceChildren(space);
93 cpSpaceFree(space);
94 }
95
96 ChipmunkDemo PyramidTopple = {
97 "Pyramid Topple",
98 1.0/180.0,
99 init,
100 update,
101 ChipmunkDemoDefaultDrawImpl,
102 destroy,
103 };