annotate trunk/chipmunkd/cpPolyShape.d @ 28:4541ca17975b

use __gshared as chipmunk heavily relies on globals and D would otherwise make them TLS
author Extrawurst
date Mon, 13 Dec 2010 21:40:56 +0100
parents 4ceef5833c8c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
1
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
2 // written in the D programming language
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
3
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
4 module chipmunkd.cpPolyShape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
5
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
6 import chipmunkd.chipmunk;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
7
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
8 // Axis structure used by cpPolyShape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
9 struct cpPolyShapeAxis{
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
10 // normal
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
11 cpVect n;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
12 // distance from origin
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
13 cpFloat d;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
14 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
15
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
16 // Convex polygon shape structure.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
17 struct cpPolyShape{
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
18 cpShape shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
19
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
20 // Vertex and axis lists.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
21 int numVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
22 cpVect *verts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
23 cpPolyShapeAxis *axes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
24
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
25 // Transformed vertex and axis lists.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
26 cpVect *tVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
27 cpPolyShapeAxis *tAxes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
28 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
29
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
30 //// Basic allocation functions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
31 //cpPolyShape *cpPolyShapeAlloc(void);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
32 //cpPolyShape *cpPolyShapeInit(cpPolyShape *poly, cpBody *body, int numVerts, cpVect *verts, cpVect offset);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
33 //cpShape *cpPolyShapeNew(cpBody *body, int numVerts, cpVect *verts, cpVect offset);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
34 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
35 //cpPolyShape *cpBoxShapeInit(cpPolyShape *poly, cpBody *body, cpFloat width, cpFloat height);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
36 //cpShape *cpBoxShapeNew(cpBody *body, cpFloat width, cpFloat height);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
37 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
38 //// Check that a set of vertexes has a correct winding and that they are convex
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
39 //cpBool cpPolyValidate(cpVect *verts, int numVerts);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
40 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
41 //int cpPolyShapeGetNumVerts(cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
42 //cpVect cpPolyShapeGetVert(cpShape *shape, int idx);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
43
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
44 // *** inlined utility functions
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
45
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
46 // Returns the minimum distance of the polygon to the axis.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
47 static cpFloat
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
48 cpPolyShapeValueOnAxis(const cpPolyShape *poly, const cpVect n, const cpFloat d)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
49 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
50 const cpVect *verts = poly.tVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
51 cpFloat min = cpvdot(n, verts[0]);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
52
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
53 int i;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
54 for(i=1; i<poly.numVerts; i++)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
55 min = cpfmin(min, cpvdot(n, verts[i]));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
56
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
57 return min - d;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
58 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
59
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
60 // Returns true if the polygon contains the vertex.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
61 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
62 cpPolyShapeContainsVert(const cpPolyShape *poly, const cpVect v)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
63 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
64 const cpPolyShapeAxis *axes = poly.tAxes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
65
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
66 int i;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
67 for(i=0; i<poly.numVerts; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
68 cpFloat dist = cpvdot(axes[i].n, v) - axes[i].d;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
69 if(dist > 0.0f) return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
70 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
71
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
72 return cpTrue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
73 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
74
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
75 // Same as cpPolyShapeContainsVert() but ignores faces pointing away from the normal.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
76 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
77 cpPolyShapeContainsVertPartial(const cpPolyShape *poly, const cpVect v, const cpVect n)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
78 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
79 const cpPolyShapeAxis *axes = poly.tAxes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
80
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
81 int i;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
82 for(i=0; i<poly.numVerts; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
83 if(cpvdot(axes[i].n, n) < 0.0f) continue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
84 cpFloat dist = cpvdot(axes[i].n, v) - axes[i].d;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
85 if(dist > 0.0f) return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
86 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
87
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
88 return cpTrue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
89 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
90
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
91 // cpPolyShape.c ---------------------------------
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
92
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
93
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
94 cpPolyShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
95 cpPolyShapeAlloc()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
96 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
97 return cast(cpPolyShape *)cpcalloc(1, cpPolyShape.sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
98 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
99
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
100 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
101 cpPolyShapeTransformVerts(cpPolyShape *poly, cpVect p, cpVect rot)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
102 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
103 cpVect *src = poly.verts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
104 cpVect *dst = poly.tVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
105
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
106 for(int i=0; i<poly.numVerts; i++)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
107 dst[i] = cpvadd(p, cpvrotate(src[i], rot));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
108 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
109
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
110 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
111 cpPolyShapeTransformAxes(cpPolyShape *poly, cpVect p, cpVect rot)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
112 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
113 cpPolyShapeAxis *src = poly.axes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
114 cpPolyShapeAxis *dst = poly.tAxes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
115
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
116 for(int i=0; i<poly.numVerts; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
117 cpVect n = cpvrotate(src[i].n, rot);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
118 dst[i].n = n;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
119 dst[i].d = cpvdot(p, n) + src[i].d;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
120 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
121 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
122
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
123 static cpBB
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
124 cpPolyShapeCacheData(cpShape *shape, cpVect p, cpVect rot)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
125 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
126 cpPolyShape *poly = cast(cpPolyShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
127
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
128 cpFloat l, b, r, t;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
129
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
130 cpPolyShapeTransformAxes(poly, p, rot);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
131 cpPolyShapeTransformVerts(poly, p, rot);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
132
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
133 cpVect *verts = poly.tVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
134 l = r = verts[0].x;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
135 b = t = verts[0].y;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
136
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
137 // TODO do as part of cpPolyShapeTransformVerts?
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
138 for(int i=1; i<poly.numVerts; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
139 cpVect v = verts[i];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
140
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
141 l = cpfmin(l, v.x);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
142 r = cpfmax(r, v.x);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
143
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
144 b = cpfmin(b, v.y);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
145 t = cpfmax(t, v.y);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
146 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
147
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
148 return cpBBNew(l, b, r, t);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
149 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
150
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
151 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
152 cpPolyShapeDestroy(cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
153 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
154 cpPolyShape *poly = cast(cpPolyShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
155
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
156 cpfree(poly.verts);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
157 cpfree(poly.tVerts);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
158
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
159 cpfree(poly.axes);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
160 cpfree(poly.tAxes);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
161 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
162
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
163 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
164 cpPolyShapePointQuery(cpShape *shape, cpVect p){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
165 return cpBBcontainsVect(shape.bb, p) && cpPolyShapeContainsVert(cast(cpPolyShape *)shape, p);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
166 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
167
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
168 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
169 cpPolyShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
170 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
171 cpPolyShape *poly = cast(cpPolyShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
172 cpPolyShapeAxis *axes = poly.tAxes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
173 cpVect *verts = poly.tVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
174 int numVerts = poly.numVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
175
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
176 for(int i=0; i<numVerts; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
177 cpVect n = axes[i].n;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
178 cpFloat an = cpvdot(a, n);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
179 if(axes[i].d > an) continue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
180
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
181 cpFloat bn = cpvdot(b, n);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
182 cpFloat t = (axes[i].d - an)/(bn - an);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
183 if(t < 0.0f || 1.0f < t) continue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
184
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
185 cpVect point = cpvlerp(a, b, t);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
186 cpFloat dt = -cpvcross(n, point);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
187 cpFloat dtMin = -cpvcross(n, verts[i]);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
188 cpFloat dtMax = -cpvcross(n, verts[(i+1)%numVerts]);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
189
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
190 if(dtMin <= dt && dt <= dtMax){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
191 info.shape = shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
192 info.t = t;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
193 info.n = n;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
194 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
195 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
196 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
197
28
4541ca17975b use __gshared as chipmunk heavily relies on globals and D would otherwise make them TLS
Extrawurst
parents: 23
diff changeset
198 __gshared /+const+/ cpShapeClass polyClass = {
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
199 cpShapeType.CP_POLY_SHAPE,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
200 &cpPolyShapeCacheData,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
201 &cpPolyShapeDestroy,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
202 &cpPolyShapePointQuery,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
203 &cpPolyShapeSegmentQuery,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
204 };
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
205
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
206 cpBool
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
207 cpPolyValidate(in cpVect *verts, in int numVerts)
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
208 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
209 for(int i=0; i<numVerts; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
210 cpVect a = verts[i];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
211 cpVect b = verts[(i+1)%numVerts];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
212 cpVect c = verts[(i+2)%numVerts];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
213
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
214 if(cpvcross(cpvsub(b, a), cpvsub(c, b)) > 0.0f)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
215 return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
216 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
217
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
218 return cpTrue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
219 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
220
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
221 int
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
222 cpPolyShapeGetNumVerts(cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
223 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
224 assert(shape.klass == &polyClass, "Shape is not a poly shape.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
225 return (cast(cpPolyShape *)shape).numVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
226 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
227
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
228 cpVect
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
229 cpPolyShapeGetVert(cpShape *shape, int idx)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
230 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
231 assert(shape.klass == &polyClass, "Shape is not a poly shape.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
232 assert(0 <= idx && idx < cpPolyShapeGetNumVerts(shape), "Index out of range.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
233
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
234 return (cast(cpPolyShape *)shape).verts[idx];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
235 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
236
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
237
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
238 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
239 setUpVerts(cpPolyShape *poly, int numVerts, cpVect *verts, cpVect offset)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
240 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
241 poly.numVerts = numVerts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
242
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
243 poly.verts = cast(cpVect *)cpcalloc(numVerts, cpVect.sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
244 poly.tVerts = cast(cpVect *)cpcalloc(numVerts, cpVect.sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
245 poly.axes = cast(cpPolyShapeAxis *)cpcalloc(numVerts, (cpPolyShapeAxis).sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
246 poly.tAxes = cast(cpPolyShapeAxis *)cpcalloc(numVerts, (cpPolyShapeAxis).sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
247
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
248 for(int i=0; i<numVerts; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
249 cpVect a = cpvadd(offset, verts[i]);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
250 cpVect b = cpvadd(offset, verts[(i+1)%numVerts]);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
251 cpVect n = cpvnormalize(cpvperp(cpvsub(b, a)));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
252
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
253 poly.verts[i] = a;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
254 poly.axes[i].n = n;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
255 poly.axes[i].d = cpvdot(n, a);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
256 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
257 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
258
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
259 cpPolyShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
260 cpPolyShapeInit(cpPolyShape *poly, cpBody *_body, int numVerts, cpVect *verts, cpVect offset)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
261 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
262 // Fail if the user attempts to pass a concave poly, or a bad winding.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
263 assert(cpPolyValidate(verts, numVerts), "Polygon is concave or has a reversed winding.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
264
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
265 setUpVerts(poly, numVerts, verts, offset);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
266 cpShapeInit(cast(cpShape *)poly, &polyClass, _body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
267
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
268 return poly;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
269 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
270
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
271 cpShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
272 cpPolyShapeNew(cpBody *_body, int numVerts, cpVect *verts, cpVect offset)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
273 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
274 return cast(cpShape *)cpPolyShapeInit(cpPolyShapeAlloc(), _body, numVerts, verts, offset);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
275 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
276
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
277 cpPolyShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
278 cpBoxShapeInit(cpPolyShape *poly, cpBody *_body, cpFloat width, cpFloat height)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
279 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
280 cpFloat hw = width/2.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
281 cpFloat hh = height/2.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
282
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
283 cpVect verts[] = [
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
284 cpv(-hw,-hh),
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
285 cpv(-hw, hh),
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
286 cpv( hw, hh),
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
287 cpv( hw,-hh),
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
288 ];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
289
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
290 return cpPolyShapeInit(poly, _body, 4, verts.ptr, cpvzero);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
291 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
292
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
293 cpShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
294 cpBoxShapeNew(cpBody *_body, cpFloat width, cpFloat height)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
295 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
296 return cast(cpShape *)cpBoxShapeInit(cpPolyShapeAlloc(), _body, width, height);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
297 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
298
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
299 // Unsafe API (chipmunk_unsafe.h)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
300
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
301 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
302 cpPolyShapeSetVerts(cpShape *shape, int numVerts, cpVect *verts, cpVect offset)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
303 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
304 assert(shape.klass == &polyClass, "Shape is not a poly shape.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
305 cpPolyShapeDestroy(shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
306 setUpVerts(cast(cpPolyShape *)shape, numVerts, verts, offset);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
307 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
308
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
309
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
310