16
|
1
|
|
2 // written in the D programming language
|
|
3
|
|
4 module samples.Bounce;
|
|
5
|
|
6 import chipmunkd.chipmunk;
|
|
7
|
|
8 import samples.ChipmunkDemo;
|
|
9
|
|
10 static cpSpace *space;
|
|
11
|
|
12 static void
|
|
13 update(int ticks)
|
|
14 {
|
|
15 enum int steps = 3;
|
|
16 enum cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps;
|
|
17
|
|
18 for(int i=0; i<steps; i++){
|
|
19 cpSpaceStep(space, dt);
|
|
20 }
|
|
21 }
|
|
22
|
|
23 static void
|
|
24 add_box()
|
|
25 {
|
|
26 enum cpFloat size = 10.0f;
|
|
27 enum cpFloat mass = 1.0f;
|
|
28
|
|
29 cpVect verts[] = [
|
|
30 cpv(-size,-size),
|
|
31 cpv(-size, size),
|
|
32 cpv( size, size),
|
|
33 cpv( size,-size),
|
|
34 ];
|
|
35
|
|
36 cpFloat radius = cpvlength(cpv(size, size));
|
|
37
|
|
38 cpBody *_body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts.ptr, cpvzero)));
|
|
39 _body.p = cpv(frand()*(640 - 2*radius) - (320 - radius), frand()*(480 - 2*radius) - (240 - radius));
|
|
40 _body.v = cpvmult(cpv(2*frand() - 1, 2*frand() - 1), 200);
|
|
41
|
|
42 cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, 4, verts.ptr, cpvzero));
|
|
43 shape.e = 1.0f; shape.u = 0.0f;
|
|
44 }
|
|
45
|
|
46 static cpSpace *
|
|
47 init()
|
|
48 {
|
|
49 cpResetShapeIdCounter();
|
|
50
|
|
51 space = cpSpaceNew();
|
|
52 cpSpaceResizeActiveHash(space, 30.0f, 1000);
|
|
53 space.iterations = 10;
|
|
54
|
|
55 cpBody *_body;
|
|
56 cpBody *staticBody = &space.staticBody;
|
|
57 cpShape *shape;
|
|
58
|
|
59 // Create segments around the edge of the screen.
|
|
60 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
|
|
61 shape.e = 1.0f; shape.u = 1.0f;
|
|
62 shape.layers = NOT_GRABABLE_MASK;
|
|
63
|
|
64 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
|
|
65 shape.e = 1.0f; shape.u = 1.0f;
|
|
66 shape.layers = NOT_GRABABLE_MASK;
|
|
67
|
|
68 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
|
|
69 shape.e = 1.0f; shape.u = 1.0f;
|
|
70 shape.layers = NOT_GRABABLE_MASK;
|
|
71
|
|
72 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
|
|
73 shape.e = 1.0f; shape.u = 1.0f;
|
|
74 shape.layers = NOT_GRABABLE_MASK;
|
|
75
|
|
76 for(int i=0; i<10; i++)
|
|
77 add_box();
|
|
78
|
|
79 _body = cpSpaceAddBody(space, cpBodyNew(100.0f, 10000.0f));
|
|
80
|
|
81 shape = cpSpaceAddShape(space, cpSegmentShapeNew(_body, cpv(-75,0), cpv(75,0), 5.0f));
|
|
82 shape.e = 1.0f; shape.u = 1.0f;
|
|
83
|
|
84 cpSpaceAddConstraint(space, cpPivotJointNew2(_body, staticBody, cpvzero, cpvzero));
|
|
85
|
|
86 return space;
|
|
87 }
|
|
88
|
|
89 static void
|
|
90 destroy()
|
|
91 {
|
|
92 cpSpaceFreeChildren(space);
|
|
93 cpSpaceFree(space);
|
|
94 }
|
|
95
|
|
96 chipmunkDemo Bounce = {
|
|
97 "Bounce",
|
|
98 null,
|
|
99 &init,
|
|
100 &update,
|
|
101 &destroy,
|
|
102 };
|