16
|
1
|
|
2 // written in the D programming language
|
|
3
|
|
4 module samples.OneWay;
|
|
5
|
|
6 import chipmunkd.chipmunk;
|
|
7
|
|
8 import samples.ChipmunkDemo;
|
|
9
|
|
10 static cpSpace *space;
|
|
11
|
|
12 struct OneWayPlatform {
|
|
13 cpVect n; // direction objects may pass through
|
|
14 cpArray *passThruList; // list of objects passing through
|
|
15 }
|
|
16
|
|
17 static OneWayPlatform platformInstance;
|
|
18
|
|
19 static cpBool
|
|
20 preSolve(cpArbiter *arb, cpSpace *space, void *ignore)
|
|
21 {
|
|
22 mixin(CP_ARBITER_GET_SHAPES!("arb", "a", "b"));
|
|
23 OneWayPlatform *platform = cast(OneWayPlatform *)a.data;
|
|
24
|
|
25 if(cpvdot(cpArbiterGetNormal(arb, 0), platform.n) < 0){
|
|
26 cpArbiterIgnore(arb);
|
|
27 return cpFalse;
|
|
28 }
|
|
29
|
|
30 return cpTrue;
|
|
31 }
|
|
32
|
|
33 static void
|
|
34 update(int ticks)
|
|
35 {
|
|
36 int steps = 1;
|
|
37 cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps;
|
|
38
|
|
39 for(int i=0; i<steps; i++){
|
|
40 cpSpaceStep(space, dt);
|
|
41 }
|
|
42 }
|
|
43
|
|
44 static cpSpace *
|
|
45 init()
|
|
46 {
|
|
47 cpResetShapeIdCounter();
|
|
48
|
|
49 space = cpSpaceNew();
|
|
50 space.iterations = 10;
|
|
51 space.gravity = cpv(0, -100);
|
|
52
|
|
53 cpBody *_body;
|
|
54 cpBody *staticBody = &space.staticBody;
|
|
55 cpShape *shape;
|
|
56
|
|
57 // Create segments around the edge of the screen.
|
|
58 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
|
|
59 shape.e = 1.0f; shape.u = 1.0f;
|
|
60 shape.layers = NOT_GRABABLE_MASK;
|
|
61
|
|
62 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
|
|
63 shape.e = 1.0f; shape.u = 1.0f;
|
|
64 shape.layers = NOT_GRABABLE_MASK;
|
|
65
|
|
66 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
|
|
67 shape.e = 1.0f; shape.u = 1.0f;
|
|
68 shape.layers = NOT_GRABABLE_MASK;
|
|
69
|
|
70 // Add our one way segment
|
|
71 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f));
|
|
72 shape.e = 1.0f; shape.u = 1.0f;
|
|
73 shape.collision_type = 1;
|
|
74 shape.layers = NOT_GRABABLE_MASK;
|
|
75
|
|
76 // We'll use the data pointer for the OneWayPlatform struct
|
|
77 platformInstance.n = cpv(0, 1); // let objects pass upwards
|
|
78 platformInstance.passThruList = cpArrayNew(0);
|
|
79 shape.data = &platformInstance;
|
|
80
|
|
81
|
|
82 // Add a ball to make things more interesting
|
|
83 cpFloat radius = 15.0f;
|
|
84 _body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
|
|
85 _body.p = cpv(0, -200);
|
|
86 _body.v = cpv(0, 170);
|
|
87
|
|
88 shape = cpSpaceAddShape(space, cpCircleShapeNew(_body, radius, cpvzero));
|
|
89 shape.e = 0.0f; shape.u = 0.9f;
|
|
90 shape.collision_type = 2;
|
|
91
|
|
92 cpSpaceAddCollisionHandler(space, 1, 2, null, &preSolve, null, null, null);
|
|
93
|
|
94 return space;
|
|
95 }
|
|
96
|
|
97 static void
|
|
98 destroy()
|
|
99 {
|
|
100 cpSpaceFreeChildren(space);
|
|
101 cpSpaceFree(space);
|
|
102
|
|
103 cpArrayFree(platformInstance.passThruList);
|
|
104 }
|
|
105
|
|
106 chipmunkDemo OneWay = {
|
|
107 "One Way Platforms",
|
|
108 null,
|
|
109 &init,
|
|
110 &update,
|
|
111 &destroy,
|
|
112 };
|