16
|
1
|
|
2 // written in the D programming language
|
|
3
|
|
4 module samples.Tumble;
|
|
5
|
|
6 import chipmunkd.chipmunk;
|
|
7
|
|
8 import samples.ChipmunkDemo;
|
|
9
|
|
10 static cpSpace *space;
|
|
11 static cpBody *staticBody;
|
|
12
|
|
13 static void
|
|
14 update(int ticks)
|
|
15 {
|
|
16 enum int steps = 3;
|
|
17 enum cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps;
|
|
18
|
|
19 for(int i=0; i<steps; i++){
|
|
20 cpSpaceStep(space, dt);
|
|
21
|
|
22 // Manually update the position of the static shape so that
|
|
23 // the box rotates.
|
|
24 cpBodyUpdatePosition(staticBody, dt);
|
|
25
|
|
26 // Because the box was added as a static shape and we moved it
|
|
27 // we need to manually rehash the static spatial hash.
|
|
28 cpSpaceRehashStatic(space);
|
|
29 }
|
|
30 }
|
|
31
|
|
32 static cpSpace *
|
|
33 init()
|
|
34 {
|
|
35 staticBody = cpBodyNew(INFINITY, INFINITY);
|
|
36
|
|
37 cpResetShapeIdCounter();
|
|
38
|
|
39 space = cpSpaceNew();
|
|
40 cpSpaceResizeActiveHash(space, 40.0f, 999);
|
|
41 cpSpaceResizeStaticHash(space, 40.0f, 99);
|
|
42 space.gravity = cpv(0, -600);
|
|
43
|
|
44 cpBody *_body;
|
|
45 cpShape *shape;
|
|
46
|
|
47 // Vertexes for the bricks
|
|
48 int num = 4;
|
|
49 cpVect verts[] = [
|
|
50 cpv(-30,-15),
|
|
51 cpv(-30, 15),
|
|
52 cpv( 30, 15),
|
|
53 cpv( 30,-15),
|
|
54 ];
|
|
55
|
|
56 // Set up the static box.
|
|
57 cpVect a = cpv(-200, -200);
|
|
58 cpVect b = cpv(-200, 200);
|
|
59 cpVect c = cpv( 200, 200);
|
|
60 cpVect d = cpv( 200, -200);
|
|
61
|
|
62 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, a, b, 0.0f));
|
|
63 shape.e = 1.0f; shape.u = 1.0f;
|
|
64 shape.layers = NOT_GRABABLE_MASK;
|
|
65
|
|
66 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, b, c, 0.0f));
|
|
67 shape.e = 1.0f; shape.u = 1.0f;
|
|
68 shape.layers = NOT_GRABABLE_MASK;
|
|
69
|
|
70 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, c, d, 0.0f));
|
|
71 shape.e = 1.0f; shape.u = 1.0f;
|
|
72 shape.layers = NOT_GRABABLE_MASK;
|
|
73
|
|
74 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, d, a, 0.0f));
|
|
75 shape.e = 1.0f; shape.u = 1.0f;
|
|
76 shape.layers = NOT_GRABABLE_MASK;
|
|
77
|
|
78 // Give the box a little spin.
|
|
79 // Because staticBody is never added to the space, we will need to
|
|
80 // update it ourselves. (see above).
|
|
81 // NOTE: Normally you would want to add the segments as normal and not static shapes.
|
|
82 // I'm just doing it to demonstrate the cpSpaceRehashStatic() function.
|
|
83 staticBody.w = 0.4f;
|
|
84
|
|
85 // Add the bricks.
|
|
86 for(int i=0; i<3; i++){
|
|
87 for(int j=0; j<7; j++){
|
|
88 _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts.ptr, cpvzero)));
|
|
89 _body.p = cpv(i*60 - 150, j*30 - 150);
|
|
90
|
|
91 shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, num, verts.ptr, cpvzero));
|
|
92 shape.e = 0.0f; shape.u = 0.7f;
|
|
93 }
|
|
94 }
|
|
95
|
|
96 return space;
|
|
97 }
|
|
98
|
|
99 static void
|
|
100 destroy()
|
|
101 {
|
|
102 cpBodyFree(staticBody);
|
|
103 cpSpaceFreeChildren(space);
|
|
104 cpSpaceFree(space);
|
|
105 }
|
|
106
|
|
107 chipmunkDemo Tumble = {
|
|
108 "Tumble",
|
|
109 null,
|
|
110 &init,
|
|
111 &update,
|
|
112 &destroy,
|
|
113 };
|