annotate trunk/chipmunkd/cpShape.d @ 23:4ceef5833c8c

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