annotate trunk/chipmunkd/cpShape.d @ 14:d88862c82f06

more properties
author Extrawurst
date Fri, 03 Dec 2010 23:55:20 +0100
parents c03a41d47b60
children df4ebc8add66
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.cpShape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
5
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
6 import chipmunkd.cpBody;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
7 import chipmunkd.chipmunk;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
8 import chipmunkd.chipmunk_types_h;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
9 import chipmunkd.cpVect_h;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
10 import chipmunkd.cpBB;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
11
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
12 import std.stdio;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
13
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
14 struct cpSegmentQueryInfo {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
15 cpShape *shape; // shape that was hit, null if no collision
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
16 cpFloat t; // Distance along query segment, will always be in the range [0, 1].
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
17 cpVect n; // normal of hit surface
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
18 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
19
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
20 // Enumeration of shape types.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
21 enum cpShapeType{
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
22 CP_CIRCLE_SHAPE,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
23 CP_SEGMENT_SHAPE,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
24 CP_POLY_SHAPE,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
25 CP_NUM_SHAPES
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
26 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
27
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
28 // Shape class. Holds function pointers and type data.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
29 struct cpShapeClass {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
30 cpShapeType type;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
31
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
32 // Called by cpShapeCacheBB().
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
33 cpBB function(cpShape *shape, cpVect p, cpVect rot) cacheData;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
34 // Called to by cpShapeDestroy().
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
35 void function(cpShape *shape) destroy;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
36
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
37 // called by cpShapePointQuery().
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
38 cpBool function(cpShape *shape, cpVect p)pointQuery;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
39
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
40 // called by cpShapeSegmentQuery()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
41 void function(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info) segmentQuery;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
42 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
43
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
44 // Basic shape struct that the others inherit from.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
45 struct cpShape{
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
46 // The "class" of a shape as defined above
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
47 /+const+/ cpShapeClass *klass;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
48
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
49 // cpBody that the shape is attached to.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
50 cpBody* _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
51
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
52 // Cached BBox for the shape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
53 cpBB bb;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
54
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
55 // Sensors invoke callbacks, but do not generate collisions
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
56 cpBool sensor;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
57
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
58 // *** Surface properties.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
59
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
60 // Coefficient of restitution. (elasticity)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
61 cpFloat e;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
62 // Coefficient of friction.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
63 cpFloat u;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
64 // Surface velocity used when solving for friction.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
65 cpVect surface_v;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
66
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
67 // *** User Definable Fields
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
68
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
69 // User defined data pointer for the shape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
70 cpDataPointer data;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
71
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
72 // User defined collision type for the shape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
73 cpCollisionType collision_type;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
74 // User defined collision group for the shape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
75 cpGroup group;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
76 // User defined layer bitmask for the shape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
77 cpLayers layers;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
78
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
79 // *** Internally Used Fields
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
80
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
81 // Shapes form a linked list when added to space on a non-null body
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
82 cpShape* next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
83
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
84 // Unique id used as the hash value.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
85 cpHashValue hashid;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
86 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
87
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
88 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
89 //// Low level shape initialization func.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
90 //cpShape* cpShapeInit(cpShape *shape, const struct cpShapeClass *klass, cpBody *body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
91 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
92 //// Basic destructor functions. (allocation functions are not shared)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
93 //void cpShapeDestroy(cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
94 //void cpShapeFree(cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
95 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
96 //// Cache the BBox of the shape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
97 //cpBB cpShapeCacheBB(cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
98 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
99 //// Test if a point lies within a shape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
100 //cpBool cpShapePointQuery(cpShape *shape, cpVect p);
13
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
101
14
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
102 template CP_DefineShapeGetter(string _struct,string type,string member,string name)
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
103 {
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
104 enum CP_DefineShapeGetter =
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
105 type~" "~_struct~"Get"~name~"(cpShape *shape){"~
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
106 "assert(shape.klass == &"~_struct~"Class, \"shape is not a "~_struct~"\");"~
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
107 "return (cast("~_struct~"*)shape)."~member~";"~
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
108 "}";
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
109 }
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
110
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
111 // Circle shape structure.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
112 struct cpCircleShape{
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
113 cpShape shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
114
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
115 // Center in body space coordinates
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
116 cpVect c;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
117 // Radius.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
118 cpFloat r;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
119
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
120 // Transformed center. (world space coordinates)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
121 cpVect tc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
122 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
123
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
124 //// Basic allocation functions for cpCircleShape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
125 //cpCircleShape *cpCircleShapeAlloc(void);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
126 //cpCircleShape *cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
127 //cpShape *cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset);
14
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
128
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
129 mixin(CP_DefineShapeGetter!("cpCircleShape", "cpVect", "c", "Offset"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
130 mixin(CP_DefineShapeGetter!("cpCircleShape", "cpFloat", "r", "Radius"));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
131
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
132 // Segment shape structure.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
133 struct cpSegmentShape{
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
134 cpShape shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
135
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
136 // Endpoints and normal of the segment. (body space coordinates)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
137 cpVect a, b, n;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
138 // Radius of the segment. (Thickness)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
139 cpFloat r;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
140
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
141 // Transformed endpoints and normal. (world space coordinates)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
142 cpVect ta, tb, tn;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
143 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
144 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
145 //// Basic allocation functions for cpSegmentShape.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
146 //cpSegmentShape* cpSegmentShapeAlloc(void);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
147 //cpSegmentShape* cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat radius);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
148 //cpShape* cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat radius);
14
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
149
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
150 mixin(CP_DefineShapeGetter!("cpSegmentShape", "cpVect", "a", "A"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
151 mixin(CP_DefineShapeGetter!("cpSegmentShape", "cpVect", "b", "B"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
152 mixin(CP_DefineShapeGetter!("cpSegmentShape", "cpVect", "n", "Normal"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
153 mixin(CP_DefineShapeGetter!("cpSegmentShape", "cpFloat", "r", "Radius"));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
154 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
155 //// For determinism, you can reset the shape id counter.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
156 //void cpResetShapeIdCounter(void);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
157 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
158 //// Directed segment queries against individual shapes.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
159 //void cpSegmentQueryInfoPrint(cpSegmentQueryInfo *info);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
160 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
161 //cpBool cpShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
162 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
163 static cpVect
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
164 cpSegmentQueryHitPoint(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
165 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
166 return cpvlerp(start, end, info.t);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
167 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
168
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
169 static cpFloat
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
170 cpSegmentQueryHitDist(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
171 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
172 return cpvdist(start, end)*info.t;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
173 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
174
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
175 cpHashValue SHAPE_ID_COUNTER = 0;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
176
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
177 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
178 cpResetShapeIdCounter()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
179 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
180 SHAPE_ID_COUNTER = 0;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
181 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
182
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
183 cpShape*
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
184 cpShapeInit(cpShape *shape, /+const+/ cpShapeClass *klass, cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
185 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
186 shape.klass = klass;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
187
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
188 shape.hashid = SHAPE_ID_COUNTER;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
189 SHAPE_ID_COUNTER++;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
190
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
191 shape._body = _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
192 shape.sensor = 0;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
193
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
194 shape.e = 0.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
195 shape.u = 0.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
196 shape.surface_v = cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
197
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
198 shape.collision_type = 0;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
199 shape.group = CP_NO_GROUP;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
200 shape.layers = CP_ALL_LAYERS;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
201
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
202 shape.data = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
203 shape.next = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
204
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
205 // cpShapeCacheBB(shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
206
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
207 return shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
208 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
209
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
210 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
211 cpShapeDestroy(cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
212 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
213 if(shape.klass.destroy) shape.klass.destroy(shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
214 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
215
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
216 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
217 cpShapeFree(cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
218 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
219 if(shape){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
220 cpShapeDestroy(shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
221 cpfree(shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
222 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
223 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
224
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
225 cpBB
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
226 cpShapeCacheBB(cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
227 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
228 cpBody *_body = shape._body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
229
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
230 shape.bb = shape.klass.cacheData(shape, _body.p, _body.rot);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
231 return shape.bb;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
232 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
233
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
234 cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
235 cpShapePointQuery(cpShape *shape, cpVect p){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
236 return shape.klass.pointQuery(shape, p);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
237 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
238
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
239 cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
240 cpShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
241 cpSegmentQueryInfo blank = {null, 0.0f, cpvzero};
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
242 (*info) = blank;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
243
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
244 shape.klass.segmentQuery(shape, a, b, info);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
245 return (info.shape !is null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
246 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
247
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
248 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
249 cpSegmentQueryInfoPrint(cpSegmentQueryInfo *info)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
250 {
13
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
251 writefln("Segment Query:");
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
252 writefln("\tt: %s", info.t);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
253 // writefln("\tdist: %f\n", info.dist);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
254 // writefln("\tpoint: %s\n", cpvstr(info.point));
13
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
255 writefln("\tn: %s", cpvstr(info.n));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
256 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
257
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
258 cpCircleShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
259 cpCircleShapeAlloc()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
260 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
261 return cast(cpCircleShape *)cpcalloc(1, cpCircleShape.sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
262 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
263
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
264 static cpBB
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
265 bbFromCircle(const cpVect c, const cpFloat r)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
266 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
267 return cpBBNew(c.x-r, c.y-r, c.x+r, c.y+r);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
268 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
269
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
270 static cpBB
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
271 cpCircleShapeCacheData(cpShape *shape, cpVect p, cpVect rot)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
272 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
273 cpCircleShape *circle = cast(cpCircleShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
274
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
275 circle.tc = cpvadd(p, cpvrotate(circle.c, rot));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
276 return bbFromCircle(circle.tc, circle.r);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
277 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
278
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
279 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
280 cpCircleShapePointQuery(cpShape *shape, cpVect p){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
281 cpCircleShape *circle = cast(cpCircleShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
282 return cpvnear(circle.tc, p, circle.r);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
283 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
284
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
285 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
286 circleSegmentQuery(cpShape *shape, cpVect center, cpFloat r, cpVect a, cpVect b, cpSegmentQueryInfo *info)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
287 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
288 // offset the line to be relative to the circle
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
289 a = cpvsub(a, center);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
290 b = cpvsub(b, center);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
291
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
292 cpFloat qa = cpvdot(a, a) - 2.0f*cpvdot(a, b) + cpvdot(b, b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
293 cpFloat qb = -2.0f*cpvdot(a, a) + 2.0f*cpvdot(a, b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
294 cpFloat qc = cpvdot(a, a) - r*r;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
295
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
296 cpFloat det = qb*qb - 4.0f*qa*qc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
297
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
298 if(det >= 0.0f){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
299 cpFloat t = (-qb - cpfsqrt(det))/(2.0f*qa);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
300 if(0.0f<= t && t <= 1.0f){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
301 info.shape = shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
302 info.t = t;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
303 info.n = cpvnormalize(cpvlerp(a, b, t));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
304 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
305 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
306 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
307
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
308 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
309 cpCircleShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
310 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
311 cpCircleShape *circle = cast(cpCircleShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
312 circleSegmentQuery(shape, circle.tc, circle.r, a, b, info);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
313 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
314
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
315 static /+const+/ cpShapeClass cpCircleShapeClass = {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
316 cpShapeType.CP_CIRCLE_SHAPE,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
317 &cpCircleShapeCacheData,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
318 null,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
319 &cpCircleShapePointQuery,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
320 &cpCircleShapeSegmentQuery,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
321 };
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
322
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
323 cpCircleShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
324 cpCircleShapeInit(cpCircleShape *circle, cpBody *_body, cpFloat radius, cpVect offset)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
325 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
326 circle.c = offset;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
327 circle.r = radius;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
328
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
329 cpShapeInit(cast(cpShape *)circle, &cpCircleShapeClass, _body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
330
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
331 return circle;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
332 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
333
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
334 cpShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
335 cpCircleShapeNew(cpBody *_body, cpFloat radius, cpVect offset)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
336 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
337 return cast(cpShape *)cpCircleShapeInit(cpCircleShapeAlloc(), _body, radius, offset);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
338 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
339
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
340 cpSegmentShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
341 cpSegmentShapeAlloc()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
342 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
343 return cast(cpSegmentShape *)cpcalloc(1, cpSegmentShape.sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
344 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
345
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
346 static cpBB
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
347 cpSegmentShapeCacheData(cpShape *shape, cpVect p, cpVect rot)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
348 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
349 cpSegmentShape *seg = cast(cpSegmentShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
350
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
351 seg.ta = cpvadd(p, cpvrotate(seg.a, rot));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
352 seg.tb = cpvadd(p, cpvrotate(seg.b, rot));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
353 seg.tn = cpvrotate(seg.n, rot);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
354
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
355 cpFloat l,r,s,t;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
356
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
357 if(seg.ta.x < seg.tb.x){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
358 l = seg.ta.x;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
359 r = seg.tb.x;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
360 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
361 l = seg.tb.x;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
362 r = seg.ta.x;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
363 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
364
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
365 if(seg.ta.y < seg.tb.y){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
366 s = seg.ta.y;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
367 t = seg.tb.y;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
368 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
369 s = seg.tb.y;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
370 t = seg.ta.y;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
371 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
372
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
373 cpFloat rad = seg.r;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
374 return cpBBNew(l - rad, s - rad, r + rad, t + rad);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
375 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
376
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
377 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
378 cpSegmentShapePointQuery(cpShape *shape, cpVect p){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
379 if(!cpBBcontainsVect(shape.bb, p)) return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
380
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
381 cpSegmentShape *seg = cast(cpSegmentShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
382
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
383 // Calculate normal distance from segment.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
384 cpFloat dn = cpvdot(seg.tn, p) - cpvdot(seg.ta, seg.tn);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
385 cpFloat dist = cpfabs(dn) - seg.r;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
386 if(dist > 0.0f) return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
387
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
388 // Calculate tangential distance along segment.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
389 cpFloat dt = -cpvcross(seg.tn, p);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
390 cpFloat dtMin = -cpvcross(seg.tn, seg.ta);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
391 cpFloat dtMax = -cpvcross(seg.tn, seg.tb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
392
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
393 // Decision tree to decide which feature of the segment to collide with.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
394 if(dt <= dtMin){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
395 if(dt < (dtMin - seg.r)){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
396 return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
397 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
398 return cpvlengthsq(cpvsub(seg.ta, p)) < (seg.r*seg.r);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
399 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
400 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
401 if(dt < dtMax){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
402 return cpTrue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
403 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
404 if(dt < (dtMax + seg.r)) {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
405 return cpvlengthsq(cpvsub(seg.tb, p)) < (seg.r*seg.r);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
406 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
407 return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
408 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
409 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
410 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
411
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
412 return cpTrue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
413 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
414
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
415 static cpBool inUnitRange(cpFloat t){return (0.0f < t && t < 1.0f);}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
416
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
417 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
418 cpSegmentShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
419 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
420 // TODO this function could be optimized better.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
421
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
422 cpSegmentShape *seg = cast(cpSegmentShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
423 cpVect n = seg.tn;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
424 // flip n if a is behind the axis
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
425 if(cpvdot(a, n) < cpvdot(seg.ta, n))
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
426 n = cpvneg(n);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
427
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
428 cpFloat an = cpvdot(a, n);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
429 cpFloat bn = cpvdot(b, n);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
430
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
431 if(an != bn){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
432 cpFloat d = cpvdot(seg.ta, n) + seg.r;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
433 cpFloat t = (d - an)/(bn - an);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
434
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
435 if(0.0f < t && t < 1.0f){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
436 cpVect point = cpvlerp(a, b, t);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
437 cpFloat dt = -cpvcross(seg.tn, point);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
438 cpFloat dtMin = -cpvcross(seg.tn, seg.ta);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
439 cpFloat dtMax = -cpvcross(seg.tn, seg.tb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
440
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
441 if(dtMin < dt && dt < dtMax){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
442 info.shape = shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
443 info.t = t;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
444 info.n = n;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
445
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
446 return; // don't continue on and check endcaps
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
447 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
448 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
449 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
450
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
451 if(seg.r) {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
452 cpSegmentQueryInfo info1 = {null, 1.0f, cpvzero};
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
453 cpSegmentQueryInfo info2 = {null, 1.0f, cpvzero};
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
454 circleSegmentQuery(shape, seg.ta, seg.r, a, b, &info1);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
455 circleSegmentQuery(shape, seg.tb, seg.r, a, b, &info2);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
456
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
457 if(info1.t < info2.t){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
458 (*info) = info1;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
459 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
460 (*info) = info2;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
461 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
462 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
463 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
464
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
465 static /+const+/ cpShapeClass cpSegmentShapeClass = {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
466 cpShapeType.CP_SEGMENT_SHAPE,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
467 &cpSegmentShapeCacheData,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
468 null,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
469 &cpSegmentShapePointQuery,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
470 &cpSegmentShapeSegmentQuery,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
471 };
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
472
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
473 cpSegmentShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
474 cpSegmentShapeInit(cpSegmentShape *seg, cpBody *_body, cpVect a, cpVect b, cpFloat r)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
475 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
476 seg.a = a;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
477 seg.b = b;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
478 seg.n = cpvperp(cpvnormalize(cpvsub(b, a)));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
479
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
480 seg.r = r;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
481
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
482 cpShapeInit(cast(cpShape *)seg, &cpSegmentShapeClass, _body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
483
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
484 return seg;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
485 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
486
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
487 cpShape*
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
488 cpSegmentShapeNew(cpBody *_body, cpVect a, cpVect b, cpFloat r)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
489 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
490 return cast(cpShape *)cpSegmentShapeInit(cpSegmentShapeAlloc(), _body, a, b, r);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
491 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
492
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
493 // Unsafe API (chipmunk_unsafe.h)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
494
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
495 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
496 cpCircleShapeSetRadius(cpShape *shape, cpFloat radius)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
497 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
498 assert(shape.klass is &cpCircleShapeClass, "Shape is not a circle shape.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
499 cpCircleShape *circle = cast(cpCircleShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
500
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
501 circle.r = radius;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
502 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
503
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
504 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
505 cpCircleShapeSetOffset(cpShape *shape, cpVect offset)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
506 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
507 assert(shape.klass is &cpCircleShapeClass, "Shape is not a circle shape.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
508 cpCircleShape *circle = cast(cpCircleShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
509
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
510 circle.c = offset;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
511 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
512
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
513 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
514 cpSegmentShapeSetEndpoints(cpShape *shape, cpVect a, cpVect b)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
515 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
516 assert(shape.klass is &cpSegmentShapeClass, "Shape is not a segment shape.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
517 cpSegmentShape *seg = cast(cpSegmentShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
518
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
519 seg.a = a;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
520 seg.b = b;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
521 seg.n = cpvperp(cpvnormalize(cpvsub(b, a)));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
522 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
523
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
524 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
525 cpSegmentShapeSetRadius(cpShape *shape, cpFloat radius)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
526 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
527 assert(shape.klass is &cpSegmentShapeClass, "Shape is not a segment shape.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
528 cpSegmentShape *seg = cast(cpSegmentShape *)shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
529
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
530 seg.r = radius;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
531 }