chipmunk 5.2.2 → 5.3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +21 -19
- data/README +67 -60
- data/Rakefile +85 -50
- data/ext/chipmunk/extconf.rb +42 -35
- data/ext/chipmunk/rb_chipmunk.c +250 -125
- data/ext/chipmunk/rb_chipmunk.h +105 -96
- data/ext/chipmunk/rb_cpArbiter.c +253 -225
- data/ext/chipmunk/rb_cpBB.c +210 -174
- data/ext/chipmunk/rb_cpBody.c +504 -347
- data/ext/chipmunk/rb_cpConstraint.c +336 -346
- data/ext/chipmunk/rb_cpShape.c +433 -455
- data/ext/chipmunk/rb_cpSpace.c +711 -544
- data/ext/chipmunk/rb_cpVect.c +346 -321
- data/lib/chipmunk.rb +183 -30
- metadata +12 -66
- data/ext/chipmunk/chipmunk.c +0 -105
- data/ext/chipmunk/cpArbiter.c +0 -274
- data/ext/chipmunk/cpArray.c +0 -128
- data/ext/chipmunk/cpBB.c +0 -47
- data/ext/chipmunk/cpBody.c +0 -188
- data/ext/chipmunk/cpCollision.c +0 -391
- data/ext/chipmunk/cpConstraint.c +0 -54
- data/ext/chipmunk/cpDampedRotarySpring.c +0 -106
- data/ext/chipmunk/cpDampedSpring.c +0 -117
- data/ext/chipmunk/cpGearJoint.c +0 -114
- data/ext/chipmunk/cpGrooveJoint.c +0 -138
- data/ext/chipmunk/cpHashSet.c +0 -253
- data/ext/chipmunk/cpPinJoint.c +0 -117
- data/ext/chipmunk/cpPivotJoint.c +0 -114
- data/ext/chipmunk/cpPolyShape.c +0 -241
- data/ext/chipmunk/cpRatchetJoint.c +0 -128
- data/ext/chipmunk/cpRotaryLimitJoint.c +0 -122
- data/ext/chipmunk/cpShape.c +0 -400
- data/ext/chipmunk/cpSimpleMotor.c +0 -99
- data/ext/chipmunk/cpSlideJoint.c +0 -131
- data/ext/chipmunk/cpSpace.c +0 -899
- data/ext/chipmunk/cpSpaceHash.c +0 -541
- data/ext/chipmunk/cpVect.c +0 -71
- data/ext/chipmunk/include/chipmunk/chipmunk.h +0 -148
- data/ext/chipmunk/include/chipmunk/chipmunk_ffi.h +0 -42
- data/ext/chipmunk/include/chipmunk/chipmunk_types.h +0 -80
- data/ext/chipmunk/include/chipmunk/chipmunk_unsafe.h +0 -54
- data/ext/chipmunk/include/chipmunk/constraints/cpConstraint.h +0 -92
- data/ext/chipmunk/include/chipmunk/constraints/cpDampedRotarySpring.h +0 -46
- data/ext/chipmunk/include/chipmunk/constraints/cpDampedSpring.h +0 -53
- data/ext/chipmunk/include/chipmunk/constraints/cpGearJoint.h +0 -41
- data/ext/chipmunk/include/chipmunk/constraints/cpGrooveJoint.h +0 -44
- data/ext/chipmunk/include/chipmunk/constraints/cpPinJoint.h +0 -43
- data/ext/chipmunk/include/chipmunk/constraints/cpPivotJoint.h +0 -42
- data/ext/chipmunk/include/chipmunk/constraints/cpRatchetJoint.h +0 -40
- data/ext/chipmunk/include/chipmunk/constraints/cpRotaryLimitJoint.h +0 -39
- data/ext/chipmunk/include/chipmunk/constraints/cpSimpleMotor.h +0 -37
- data/ext/chipmunk/include/chipmunk/constraints/cpSlideJoint.h +0 -44
- data/ext/chipmunk/include/chipmunk/constraints/util.h +0 -116
- data/ext/chipmunk/include/chipmunk/cpArbiter.h +0 -136
- data/ext/chipmunk/include/chipmunk/cpArray.h +0 -46
- data/ext/chipmunk/include/chipmunk/cpBB.h +0 -74
- data/ext/chipmunk/include/chipmunk/cpBody.h +0 -160
- data/ext/chipmunk/include/chipmunk/cpCollision.h +0 -23
- data/ext/chipmunk/include/chipmunk/cpHashSet.h +0 -82
- data/ext/chipmunk/include/chipmunk/cpPolyShape.h +0 -103
- data/ext/chipmunk/include/chipmunk/cpShape.h +0 -174
- data/ext/chipmunk/include/chipmunk/cpSpace.h +0 -180
- data/ext/chipmunk/include/chipmunk/cpSpaceHash.h +0 -109
- data/ext/chipmunk/include/chipmunk/cpVect.h +0 -157
- data/ext/chipmunk/prime.h +0 -68
- data/lib/chipmunk/version.rb +0 -3
data/ext/chipmunk/cpShape.c
DELETED
@@ -1,400 +0,0 @@
|
|
1
|
-
/* Copyright (c) 2007 Scott Lembcke
|
2
|
-
*
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
* of this software and associated documentation files (the "Software"), to deal
|
5
|
-
* in the Software without restriction, including without limitation the rights
|
6
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
* copies of the Software, and to permit persons to whom the Software is
|
8
|
-
* furnished to do so, subject to the following conditions:
|
9
|
-
*
|
10
|
-
* The above copyright notice and this permission notice shall be included in
|
11
|
-
* all copies or substantial portions of the Software.
|
12
|
-
*
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
-
* SOFTWARE.
|
20
|
-
*/
|
21
|
-
|
22
|
-
#include <stdlib.h>
|
23
|
-
#include <stdio.h>
|
24
|
-
#include <math.h>
|
25
|
-
|
26
|
-
#include "chipmunk.h"
|
27
|
-
#include "chipmunk_unsafe.h"
|
28
|
-
|
29
|
-
#define CP_DefineShapeGetter(struct, type, member, name) \
|
30
|
-
CP_DeclareShapeGetter(struct, type, name){ \
|
31
|
-
cpAssert(shape->klass == &struct##Class, "shape is not a "#struct); \
|
32
|
-
return ((struct *)shape)->member; \
|
33
|
-
}
|
34
|
-
cpHashValue SHAPE_ID_COUNTER = 0;
|
35
|
-
|
36
|
-
void
|
37
|
-
cpResetShapeIdCounter(void)
|
38
|
-
{
|
39
|
-
SHAPE_ID_COUNTER = 0;
|
40
|
-
}
|
41
|
-
|
42
|
-
|
43
|
-
cpShape*
|
44
|
-
cpShapeInit(cpShape *shape, const cpShapeClass *klass, cpBody *body)
|
45
|
-
{
|
46
|
-
shape->klass = klass;
|
47
|
-
|
48
|
-
shape->hashid = SHAPE_ID_COUNTER;
|
49
|
-
SHAPE_ID_COUNTER++;
|
50
|
-
|
51
|
-
shape->body = body;
|
52
|
-
shape->sensor = 0;
|
53
|
-
|
54
|
-
shape->e = 0.0f;
|
55
|
-
shape->u = 0.0f;
|
56
|
-
shape->surface_v = cpvzero;
|
57
|
-
|
58
|
-
shape->collision_type = 0;
|
59
|
-
shape->group = CP_NO_GROUP;
|
60
|
-
shape->layers = CP_ALL_LAYERS;
|
61
|
-
|
62
|
-
shape->data = NULL;
|
63
|
-
|
64
|
-
cpShapeCacheBB(shape);
|
65
|
-
|
66
|
-
return shape;
|
67
|
-
}
|
68
|
-
|
69
|
-
void
|
70
|
-
cpShapeDestroy(cpShape *shape)
|
71
|
-
{
|
72
|
-
if(shape->klass->destroy) shape->klass->destroy(shape);
|
73
|
-
}
|
74
|
-
|
75
|
-
void
|
76
|
-
cpShapeFree(cpShape *shape)
|
77
|
-
{
|
78
|
-
if(shape){
|
79
|
-
cpShapeDestroy(shape);
|
80
|
-
cpfree(shape);
|
81
|
-
}
|
82
|
-
}
|
83
|
-
|
84
|
-
cpBB
|
85
|
-
cpShapeCacheBB(cpShape *shape)
|
86
|
-
{
|
87
|
-
cpBody *body = shape->body;
|
88
|
-
|
89
|
-
shape->bb = shape->klass->cacheData(shape, body->p, body->rot);
|
90
|
-
return shape->bb;
|
91
|
-
}
|
92
|
-
|
93
|
-
int
|
94
|
-
cpShapePointQuery(cpShape *shape, cpVect p){
|
95
|
-
return shape->klass->pointQuery(shape, p);
|
96
|
-
}
|
97
|
-
|
98
|
-
int
|
99
|
-
cpShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info){
|
100
|
-
cpSegmentQueryInfo blank = {NULL, 0.0f, cpvzero};
|
101
|
-
(*info) = blank;
|
102
|
-
|
103
|
-
shape->klass->segmentQuery(shape, a, b, info);
|
104
|
-
return (info->shape != NULL);
|
105
|
-
}
|
106
|
-
|
107
|
-
void
|
108
|
-
cpSegmentQueryInfoPrint(cpSegmentQueryInfo *info)
|
109
|
-
{
|
110
|
-
printf("Segment Query:\n");
|
111
|
-
printf("\tt: %f\n", info->t);
|
112
|
-
// printf("\tdist: %f\n", info->dist);
|
113
|
-
// printf("\tpoint: %s\n", cpvstr(info->point));
|
114
|
-
printf("\tn: %s\n", cpvstr(info->n));
|
115
|
-
}
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
cpCircleShape *
|
121
|
-
cpCircleShapeAlloc(void)
|
122
|
-
{
|
123
|
-
return (cpCircleShape *)cpcalloc(1, sizeof(cpCircleShape));
|
124
|
-
}
|
125
|
-
|
126
|
-
static inline cpBB
|
127
|
-
bbFromCircle(const cpVect c, const cpFloat r)
|
128
|
-
{
|
129
|
-
return cpBBNew(c.x-r, c.y-r, c.x+r, c.y+r);
|
130
|
-
}
|
131
|
-
|
132
|
-
static cpBB
|
133
|
-
cpCircleShapeCacheData(cpShape *shape, cpVect p, cpVect rot)
|
134
|
-
{
|
135
|
-
cpCircleShape *circle = (cpCircleShape *)shape;
|
136
|
-
|
137
|
-
circle->tc = cpvadd(p, cpvrotate(circle->c, rot));
|
138
|
-
return bbFromCircle(circle->tc, circle->r);
|
139
|
-
}
|
140
|
-
|
141
|
-
static int
|
142
|
-
cpCircleShapePointQuery(cpShape *shape, cpVect p){
|
143
|
-
cpCircleShape *circle = (cpCircleShape *)shape;
|
144
|
-
return cpvnear(circle->tc, p, circle->r);
|
145
|
-
}
|
146
|
-
|
147
|
-
static void
|
148
|
-
circleSegmentQuery(cpShape *shape, cpVect center, cpFloat r, cpVect a, cpVect b, cpSegmentQueryInfo *info)
|
149
|
-
{
|
150
|
-
// umm... gross I normally frown upon such things
|
151
|
-
a = cpvsub(a, center);
|
152
|
-
b = cpvsub(b, center);
|
153
|
-
|
154
|
-
cpFloat qa = cpvdot(a, a) - 2.0f*cpvdot(a, b) + cpvdot(b, b);
|
155
|
-
cpFloat qb = -2.0f*cpvdot(a, a) + 2.0f*cpvdot(a, b);
|
156
|
-
cpFloat qc = cpvdot(a, a) - r*r;
|
157
|
-
|
158
|
-
cpFloat det = qb*qb - 4.0f*qa*qc;
|
159
|
-
|
160
|
-
if(det >= 0.0f){
|
161
|
-
cpFloat t = (-qb - cpfsqrt(det))/(2.0f*qa);
|
162
|
-
if(0.0f<= t && t <= 1.0f){
|
163
|
-
info->shape = shape;
|
164
|
-
info->t = t;
|
165
|
-
info->n = cpvnormalize(cpvlerp(a, b, t));
|
166
|
-
}
|
167
|
-
}
|
168
|
-
}
|
169
|
-
|
170
|
-
static void
|
171
|
-
cpCircleShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info)
|
172
|
-
{
|
173
|
-
cpCircleShape *circle = (cpCircleShape *)shape;
|
174
|
-
circleSegmentQuery(shape, circle->tc, circle->r, a, b, info);
|
175
|
-
}
|
176
|
-
|
177
|
-
static const cpShapeClass cpCircleShapeClass = {
|
178
|
-
CP_CIRCLE_SHAPE,
|
179
|
-
cpCircleShapeCacheData,
|
180
|
-
NULL,
|
181
|
-
cpCircleShapePointQuery,
|
182
|
-
cpCircleShapeSegmentQuery,
|
183
|
-
};
|
184
|
-
|
185
|
-
cpCircleShape *
|
186
|
-
cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset)
|
187
|
-
{
|
188
|
-
circle->c = offset;
|
189
|
-
circle->r = radius;
|
190
|
-
|
191
|
-
cpShapeInit((cpShape *)circle, &cpCircleShapeClass, body);
|
192
|
-
|
193
|
-
return circle;
|
194
|
-
}
|
195
|
-
|
196
|
-
cpShape *
|
197
|
-
cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset)
|
198
|
-
{
|
199
|
-
return (cpShape *)cpCircleShapeInit(cpCircleShapeAlloc(), body, radius, offset);
|
200
|
-
}
|
201
|
-
|
202
|
-
CP_DefineShapeGetter(cpCircleShape, cpVect, c, Offset)
|
203
|
-
CP_DefineShapeGetter(cpCircleShape, cpFloat, r, Radius)
|
204
|
-
|
205
|
-
cpSegmentShape *
|
206
|
-
cpSegmentShapeAlloc(void)
|
207
|
-
{
|
208
|
-
return (cpSegmentShape *)cpcalloc(1, sizeof(cpSegmentShape));
|
209
|
-
}
|
210
|
-
|
211
|
-
static cpBB
|
212
|
-
cpSegmentShapeCacheData(cpShape *shape, cpVect p, cpVect rot)
|
213
|
-
{
|
214
|
-
cpSegmentShape *seg = (cpSegmentShape *)shape;
|
215
|
-
|
216
|
-
seg->ta = cpvadd(p, cpvrotate(seg->a, rot));
|
217
|
-
seg->tb = cpvadd(p, cpvrotate(seg->b, rot));
|
218
|
-
seg->tn = cpvrotate(seg->n, rot);
|
219
|
-
|
220
|
-
cpFloat l,r,s,t;
|
221
|
-
|
222
|
-
if(seg->ta.x < seg->tb.x){
|
223
|
-
l = seg->ta.x;
|
224
|
-
r = seg->tb.x;
|
225
|
-
} else {
|
226
|
-
l = seg->tb.x;
|
227
|
-
r = seg->ta.x;
|
228
|
-
}
|
229
|
-
|
230
|
-
if(seg->ta.y < seg->tb.y){
|
231
|
-
s = seg->ta.y;
|
232
|
-
t = seg->tb.y;
|
233
|
-
} else {
|
234
|
-
s = seg->tb.y;
|
235
|
-
t = seg->ta.y;
|
236
|
-
}
|
237
|
-
|
238
|
-
cpFloat rad = seg->r;
|
239
|
-
return cpBBNew(l - rad, s - rad, r + rad, t + rad);
|
240
|
-
}
|
241
|
-
|
242
|
-
static int
|
243
|
-
cpSegmentShapePointQuery(cpShape *shape, cpVect p){
|
244
|
-
if(!cpBBcontainsVect(shape->bb, p)) return 0;
|
245
|
-
|
246
|
-
cpSegmentShape *seg = (cpSegmentShape *)shape;
|
247
|
-
|
248
|
-
// Calculate normal distance from segment.
|
249
|
-
cpFloat dn = cpvdot(seg->tn, p) - cpvdot(seg->ta, seg->tn);
|
250
|
-
cpFloat dist = cpfabs(dn) - seg->r;
|
251
|
-
if(dist > 0.0f) return 0;
|
252
|
-
|
253
|
-
// Calculate tangential distance along segment.
|
254
|
-
cpFloat dt = -cpvcross(seg->tn, p);
|
255
|
-
cpFloat dtMin = -cpvcross(seg->tn, seg->ta);
|
256
|
-
cpFloat dtMax = -cpvcross(seg->tn, seg->tb);
|
257
|
-
|
258
|
-
// Decision tree to decide which feature of the segment to collide with.
|
259
|
-
if(dt <= dtMin){
|
260
|
-
if(dt < (dtMin - seg->r)){
|
261
|
-
return 0;
|
262
|
-
} else {
|
263
|
-
return cpvlengthsq(cpvsub(seg->ta, p)) < (seg->r*seg->r);
|
264
|
-
}
|
265
|
-
} else {
|
266
|
-
if(dt < dtMax){
|
267
|
-
return 1;
|
268
|
-
} else {
|
269
|
-
if(dt < (dtMax + seg->r)) {
|
270
|
-
return cpvlengthsq(cpvsub(seg->tb, p)) < (seg->r*seg->r);
|
271
|
-
} else {
|
272
|
-
return 0;
|
273
|
-
}
|
274
|
-
}
|
275
|
-
}
|
276
|
-
|
277
|
-
return 1;
|
278
|
-
}
|
279
|
-
|
280
|
-
static void
|
281
|
-
cpSegmentShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info)
|
282
|
-
{
|
283
|
-
cpSegmentShape *seg = (cpSegmentShape *)shape;
|
284
|
-
cpVect n = seg->tn;
|
285
|
-
// flip n if a is behind the axis
|
286
|
-
if(cpvdot(a, n) < cpvdot(seg->ta, n))
|
287
|
-
n = cpvneg(n);
|
288
|
-
|
289
|
-
cpFloat an = cpvdot(a, n);
|
290
|
-
cpFloat bn = cpvdot(b, n);
|
291
|
-
cpFloat d = cpvdot(seg->ta, n) + seg->r;
|
292
|
-
|
293
|
-
cpFloat t = (d - an)/(bn - an);
|
294
|
-
if(0.0f < t && t < 1.0f){
|
295
|
-
cpVect point = cpvlerp(a, b, t);
|
296
|
-
cpFloat dt = -cpvcross(seg->tn, point);
|
297
|
-
cpFloat dtMin = -cpvcross(seg->tn, seg->ta);
|
298
|
-
cpFloat dtMax = -cpvcross(seg->tn, seg->tb);
|
299
|
-
|
300
|
-
if(dtMin < dt && dt < dtMax){
|
301
|
-
info->shape = shape;
|
302
|
-
info->t = t;
|
303
|
-
info->n = n;
|
304
|
-
|
305
|
-
return; // don't continue on and check endcaps
|
306
|
-
}
|
307
|
-
}
|
308
|
-
|
309
|
-
if(seg->r) {
|
310
|
-
cpSegmentQueryInfo info1; info1.shape = NULL;
|
311
|
-
cpSegmentQueryInfo info2; info2.shape = NULL;
|
312
|
-
circleSegmentQuery(shape, seg->ta, seg->r, a, b, &info1);
|
313
|
-
circleSegmentQuery(shape, seg->tb, seg->r, a, b, &info2);
|
314
|
-
|
315
|
-
if(info1.shape && !info2.shape){
|
316
|
-
(*info) = info1;
|
317
|
-
} else if(info2.shape && !info1.shape){
|
318
|
-
(*info) = info2;
|
319
|
-
} else if(info1.shape && info2.shape){
|
320
|
-
if(info1.t < info2.t){
|
321
|
-
(*info) = info1;
|
322
|
-
} else {
|
323
|
-
(*info) = info2;
|
324
|
-
}
|
325
|
-
}
|
326
|
-
}
|
327
|
-
}
|
328
|
-
|
329
|
-
static const cpShapeClass cpSegmentShapeClass = {
|
330
|
-
CP_SEGMENT_SHAPE,
|
331
|
-
cpSegmentShapeCacheData,
|
332
|
-
NULL,
|
333
|
-
cpSegmentShapePointQuery,
|
334
|
-
cpSegmentShapeSegmentQuery,
|
335
|
-
};
|
336
|
-
|
337
|
-
cpSegmentShape *
|
338
|
-
cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat r)
|
339
|
-
{
|
340
|
-
seg->a = a;
|
341
|
-
seg->b = b;
|
342
|
-
seg->n = cpvperp(cpvnormalize(cpvsub(b, a)));
|
343
|
-
|
344
|
-
seg->r = r;
|
345
|
-
|
346
|
-
cpShapeInit((cpShape *)seg, &cpSegmentShapeClass, body);
|
347
|
-
|
348
|
-
return seg;
|
349
|
-
}
|
350
|
-
|
351
|
-
cpShape*
|
352
|
-
cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat r)
|
353
|
-
{
|
354
|
-
return (cpShape *)cpSegmentShapeInit(cpSegmentShapeAlloc(), body, a, b, r);
|
355
|
-
}
|
356
|
-
|
357
|
-
CP_DefineShapeGetter(cpSegmentShape, cpVect, a, A)
|
358
|
-
CP_DefineShapeGetter(cpSegmentShape, cpVect, b, B)
|
359
|
-
CP_DefineShapeGetter(cpSegmentShape, cpVect, n, Normal)
|
360
|
-
CP_DefineShapeGetter(cpSegmentShape, cpFloat, r, Radius)
|
361
|
-
|
362
|
-
// Unsafe API (chipmunk_unsafe.h)
|
363
|
-
|
364
|
-
void
|
365
|
-
cpCircleShapeSetRadius(cpShape *shape, cpFloat radius)
|
366
|
-
{
|
367
|
-
cpAssert(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
|
368
|
-
cpCircleShape *circle = (cpCircleShape *)shape;
|
369
|
-
|
370
|
-
circle->r = radius;
|
371
|
-
}
|
372
|
-
|
373
|
-
void
|
374
|
-
cpCircleShapeSetOffset(cpShape *shape, cpVect offset)
|
375
|
-
{
|
376
|
-
cpAssert(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
|
377
|
-
cpCircleShape *circle = (cpCircleShape *)shape;
|
378
|
-
|
379
|
-
circle->c = offset;
|
380
|
-
}
|
381
|
-
|
382
|
-
void
|
383
|
-
cpSegmentShapeSetEndpoints(cpShape *shape, cpVect a, cpVect b)
|
384
|
-
{
|
385
|
-
cpAssert(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
|
386
|
-
cpSegmentShape *seg = (cpSegmentShape *)shape;
|
387
|
-
|
388
|
-
seg->a = a;
|
389
|
-
seg->b = b;
|
390
|
-
seg->n = cpvperp(cpvnormalize(cpvsub(b, a)));
|
391
|
-
}
|
392
|
-
|
393
|
-
void
|
394
|
-
cpSegmentShapeSetRadius(cpShape *shape, cpFloat radius)
|
395
|
-
{
|
396
|
-
cpAssert(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
|
397
|
-
cpSegmentShape *seg = (cpSegmentShape *)shape;
|
398
|
-
|
399
|
-
seg->r = radius;
|
400
|
-
}
|
@@ -1,99 +0,0 @@
|
|
1
|
-
/* Copyright (c) 2007 Scott Lembcke
|
2
|
-
*
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
* of this software and associated documentation files (the "Software"), to deal
|
5
|
-
* in the Software without restriction, including without limitation the rights
|
6
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
* copies of the Software, and to permit persons to whom the Software is
|
8
|
-
* furnished to do so, subject to the following conditions:
|
9
|
-
*
|
10
|
-
* The above copyright notice and this permission notice shall be included in
|
11
|
-
* all copies or substantial portions of the Software.
|
12
|
-
*
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
-
* SOFTWARE.
|
20
|
-
*/
|
21
|
-
|
22
|
-
#include <stdlib.h>
|
23
|
-
|
24
|
-
#include "chipmunk.h"
|
25
|
-
#include "constraints/util.h"
|
26
|
-
|
27
|
-
static void
|
28
|
-
preStep(cpSimpleMotor *joint, cpFloat dt, cpFloat dt_inv)
|
29
|
-
{
|
30
|
-
cpBody *a = joint->constraint.a;
|
31
|
-
cpBody *b = joint->constraint.b;
|
32
|
-
|
33
|
-
// calculate moment of inertia coefficient.
|
34
|
-
joint->iSum = 1.0f/(a->i_inv + b->i_inv);
|
35
|
-
|
36
|
-
// compute max impulse
|
37
|
-
joint->jMax = J_MAX(joint, dt);
|
38
|
-
|
39
|
-
// apply joint torque
|
40
|
-
a->w -= joint->jAcc*a->i_inv;
|
41
|
-
b->w += joint->jAcc*b->i_inv;
|
42
|
-
}
|
43
|
-
|
44
|
-
static void
|
45
|
-
applyImpulse(cpSimpleMotor *joint)
|
46
|
-
{
|
47
|
-
cpBody *a = joint->constraint.a;
|
48
|
-
cpBody *b = joint->constraint.b;
|
49
|
-
|
50
|
-
// compute relative rotational velocity
|
51
|
-
cpFloat wr = b->w - a->w + joint->rate;
|
52
|
-
|
53
|
-
// compute normal impulse
|
54
|
-
cpFloat j = -wr*joint->iSum;
|
55
|
-
cpFloat jOld = joint->jAcc;
|
56
|
-
joint->jAcc = cpfclamp(jOld + j, -joint->jMax, joint->jMax);
|
57
|
-
j = joint->jAcc - jOld;
|
58
|
-
|
59
|
-
// apply impulse
|
60
|
-
a->w -= j*a->i_inv;
|
61
|
-
b->w += j*b->i_inv;
|
62
|
-
}
|
63
|
-
|
64
|
-
static cpFloat
|
65
|
-
getImpulse(cpSimpleMotor *joint)
|
66
|
-
{
|
67
|
-
return cpfabs(joint->jAcc);
|
68
|
-
}
|
69
|
-
|
70
|
-
static const cpConstraintClass klass = {
|
71
|
-
(cpConstraintPreStepFunction)preStep,
|
72
|
-
(cpConstraintApplyImpulseFunction)applyImpulse,
|
73
|
-
(cpConstraintGetImpulseFunction)getImpulse,
|
74
|
-
};
|
75
|
-
CP_DefineClassGetter(cpSimpleMotor)
|
76
|
-
|
77
|
-
cpSimpleMotor *
|
78
|
-
cpSimpleMotorAlloc(void)
|
79
|
-
{
|
80
|
-
return (cpSimpleMotor *)cpmalloc(sizeof(cpSimpleMotor));
|
81
|
-
}
|
82
|
-
|
83
|
-
cpSimpleMotor *
|
84
|
-
cpSimpleMotorInit(cpSimpleMotor *joint, cpBody *a, cpBody *b, cpFloat rate)
|
85
|
-
{
|
86
|
-
cpConstraintInit((cpConstraint *)joint, &klass, a, b);
|
87
|
-
|
88
|
-
joint->rate = rate;
|
89
|
-
|
90
|
-
joint->jAcc = 0.0f;
|
91
|
-
|
92
|
-
return joint;
|
93
|
-
}
|
94
|
-
|
95
|
-
cpConstraint *
|
96
|
-
cpSimpleMotorNew(cpBody *a, cpBody *b, cpFloat rate)
|
97
|
-
{
|
98
|
-
return (cpConstraint *)cpSimpleMotorInit(cpSimpleMotorAlloc(), a, b, rate);
|
99
|
-
}
|