Mercurial > projects > chipmunkd
comparison trunk/tests/ChipmunkDemos/samples/Tumble.d @ 16:af2f61a96318
ported chipmunk demos
author | Extrawurst |
---|---|
date | Sat, 04 Dec 2010 02:02:29 +0100 |
parents | |
children | 4604c914f2ab |
comparison
equal
deleted
inserted
replaced
15:df4ebc8add66 | 16:af2f61a96318 |
---|---|
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 int foo; | |
86 // Add the bricks. | |
87 for(int i=0; i<3; i++){ | |
88 for(int j=0; j<7; j++){ | |
89 _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts.ptr, cpvzero))); | |
90 _body.p = cpv(i*60 - 150, j*30 - 150); | |
91 | |
92 //BUG: crazy fucking dmd codegen bug when optimizing (2.050) | |
93 foo = i+j; | |
94 | |
95 shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, num, verts.ptr, cpvzero)); | |
96 shape.e = 0.0f; shape.u = 0.7f; | |
97 } | |
98 } | |
99 | |
100 return space; | |
101 } | |
102 | |
103 static void | |
104 destroy() | |
105 { | |
106 cpBodyFree(staticBody); | |
107 cpSpaceFreeChildren(space); | |
108 cpSpaceFree(space); | |
109 } | |
110 | |
111 chipmunkDemo Tumble = { | |
112 "Tumble", | |
113 null, | |
114 &init, | |
115 &update, | |
116 &destroy, | |
117 }; |