annotate trunk/tests/ChipmunkDemos/samples/OneWay.d @ 16:af2f61a96318

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