16
|
1
|
|
2 // written in the D programming language
|
|
3
|
|
4 module samples.Plink;
|
|
5
|
|
6 import chipmunkd.chipmunk;
|
|
7
|
|
8 import samples.ChipmunkDemo;
|
|
9
|
|
10 import std.math;
|
|
11
|
|
12 static cpSpace *space;
|
|
13
|
|
14 // Iterate over all of the bodies and reset the ones that have fallen offscreen.
|
|
15 static void
|
|
16 eachBody(cpBody *_body, void *unused)
|
|
17 {
|
|
18 if(_body.p.y < -260 || cpfabs(_body.p.x) > 340){
|
|
19 cpFloat x = frand()*640 - 320;
|
|
20 _body.p = cpv(x, 260);
|
|
21 }
|
|
22 }
|
|
23
|
|
24 static void
|
|
25 update(int ticks)
|
|
26 {
|
|
27 int steps = 1;
|
|
28 cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps;
|
|
29
|
|
30 for(int i=0; i<steps; i++){
|
|
31 cpSpaceStep(space, dt);
|
|
32 cpSpaceEachBody(space, &eachBody, null);
|
|
33 }
|
|
34 }
|
|
35
|
|
36 enum NUM_VERTS = 5;
|
|
37
|
|
38 static cpSpace *
|
|
39 init()
|
|
40 {
|
|
41 cpResetShapeIdCounter();
|
|
42
|
|
43 space = cpSpaceNew();
|
|
44 space.iterations = 5;
|
|
45 space.gravity = cpv(0, -100);
|
|
46
|
|
47 cpSpaceResizeStaticHash(space, 40.0f, 999);
|
|
48 cpSpaceResizeActiveHash(space, 30.0f, 2999);
|
|
49
|
|
50 cpBody *_body;
|
|
51 cpBody *staticBody = &space.staticBody;
|
|
52 cpShape *shape;
|
|
53
|
|
54 // Create vertexes for a pentagon shape.
|
|
55 cpVect verts[NUM_VERTS];
|
|
56 for(int i=0; i<NUM_VERTS; i++){
|
|
57 cpFloat angle = -2.0f*PI*i/(cast(cpFloat) NUM_VERTS);
|
|
58 verts[i] = cpv(10.0f*cos(angle), 10.0f*sin(angle));
|
|
59 }
|
|
60
|
|
61 // Vertexes for a triangle shape.
|
|
62 enum cpVect tris[] = [
|
|
63 cpv(-15,-15),
|
|
64 cpv( 0, 10),
|
|
65 cpv( 15,-15),
|
|
66 ];
|
|
67
|
|
68 int foo;
|
|
69
|
|
70 // Create the static triangles.
|
|
71 foreach(i; 0..9){
|
|
72 foreach(j; 0..6){
|
|
73 cpFloat stagger = (j%2)*40;
|
|
74 cpVect offset;
|
|
75 offset.x = (i*80) - 320 + stagger;
|
|
76 offset.y = (j*70) - 240;
|
|
77 //BUG: crazy fucking dmd codegen bug when optimizing (2.050)
|
|
78 foo = i;
|
|
79
|
|
80 shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, tris.ptr, offset));
|
|
81 shape.e = 1.0f; shape.u = 1.0f;
|
|
82 shape.layers = NOT_GRABABLE_MASK;
|
|
83 }
|
|
84 }
|
|
85
|
|
86 // Add lots of pentagons.
|
|
87 for(int i=0; i<300; i++){
|
|
88 _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, NUM_VERTS, verts.ptr, cpvzero)));
|
|
89 cpFloat x = frand()*640 - 320;
|
|
90 _body.p = cpv(x, 350);
|
|
91
|
|
92 shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, NUM_VERTS, verts.ptr, cpvzero));
|
|
93 shape.e = 0.0f; shape.u = 0.4f;
|
|
94 }
|
|
95
|
|
96 return space;
|
|
97 }
|
|
98
|
|
99 static void
|
|
100 destroy()
|
|
101 {
|
|
102 cpSpaceFreeChildren(space);
|
|
103 cpSpaceFree(space);
|
|
104 }
|
|
105
|
|
106 chipmunkDemo Plink = {
|
|
107 "Plink",
|
|
108 null,
|
|
109 &init,
|
|
110 &update,
|
|
111 &destroy,
|
|
112 };
|