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/cpPolyShape.c
DELETED
@@ -1,241 +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
|
-
|
25
|
-
#include "chipmunk.h"
|
26
|
-
#include "chipmunk_unsafe.h"
|
27
|
-
|
28
|
-
cpPolyShape *
|
29
|
-
cpPolyShapeAlloc(void)
|
30
|
-
{
|
31
|
-
return (cpPolyShape *)cpcalloc(1, sizeof(cpPolyShape));
|
32
|
-
}
|
33
|
-
|
34
|
-
static void
|
35
|
-
cpPolyShapeTransformVerts(cpPolyShape *poly, cpVect p, cpVect rot)
|
36
|
-
{
|
37
|
-
cpVect *src = poly->verts;
|
38
|
-
cpVect *dst = poly->tVerts;
|
39
|
-
|
40
|
-
for(int i=0; i<poly->numVerts; i++)
|
41
|
-
dst[i] = cpvadd(p, cpvrotate(src[i], rot));
|
42
|
-
}
|
43
|
-
|
44
|
-
static void
|
45
|
-
cpPolyShapeTransformAxes(cpPolyShape *poly, cpVect p, cpVect rot)
|
46
|
-
{
|
47
|
-
cpPolyShapeAxis *src = poly->axes;
|
48
|
-
cpPolyShapeAxis *dst = poly->tAxes;
|
49
|
-
|
50
|
-
for(int i=0; i<poly->numVerts; i++){
|
51
|
-
cpVect n = cpvrotate(src[i].n, rot);
|
52
|
-
dst[i].n = n;
|
53
|
-
dst[i].d = cpvdot(p, n) + src[i].d;
|
54
|
-
}
|
55
|
-
}
|
56
|
-
|
57
|
-
static cpBB
|
58
|
-
cpPolyShapeCacheData(cpShape *shape, cpVect p, cpVect rot)
|
59
|
-
{
|
60
|
-
cpPolyShape *poly = (cpPolyShape *)shape;
|
61
|
-
|
62
|
-
cpFloat l, b, r, t;
|
63
|
-
|
64
|
-
cpPolyShapeTransformAxes(poly, p, rot);
|
65
|
-
cpPolyShapeTransformVerts(poly, p, rot);
|
66
|
-
|
67
|
-
cpVect *verts = poly->tVerts;
|
68
|
-
l = r = verts[0].x;
|
69
|
-
b = t = verts[0].y;
|
70
|
-
|
71
|
-
// TODO do as part of cpPolyShapeTransformVerts?
|
72
|
-
for(int i=1; i<poly->numVerts; i++){
|
73
|
-
cpVect v = verts[i];
|
74
|
-
|
75
|
-
l = cpfmin(l, v.x);
|
76
|
-
r = cpfmax(r, v.x);
|
77
|
-
|
78
|
-
b = cpfmin(b, v.y);
|
79
|
-
t = cpfmax(t, v.y);
|
80
|
-
}
|
81
|
-
|
82
|
-
return cpBBNew(l, b, r, t);
|
83
|
-
}
|
84
|
-
|
85
|
-
static void
|
86
|
-
cpPolyShapeDestroy(cpShape *shape)
|
87
|
-
{
|
88
|
-
cpPolyShape *poly = (cpPolyShape *)shape;
|
89
|
-
|
90
|
-
cpfree(poly->verts);
|
91
|
-
cpfree(poly->tVerts);
|
92
|
-
|
93
|
-
cpfree(poly->axes);
|
94
|
-
cpfree(poly->tAxes);
|
95
|
-
}
|
96
|
-
|
97
|
-
static int
|
98
|
-
cpPolyShapePointQuery(cpShape *shape, cpVect p){
|
99
|
-
return cpBBcontainsVect(shape->bb, p) && cpPolyShapeContainsVert((cpPolyShape *)shape, p);
|
100
|
-
}
|
101
|
-
|
102
|
-
static void
|
103
|
-
cpPolyShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info)
|
104
|
-
{
|
105
|
-
cpPolyShape *poly = (cpPolyShape *)shape;
|
106
|
-
cpPolyShapeAxis *axes = poly->tAxes;
|
107
|
-
cpVect *verts = poly->tVerts;
|
108
|
-
int numVerts = poly->numVerts;
|
109
|
-
|
110
|
-
for(int i=0; i<numVerts; i++){
|
111
|
-
cpVect n = axes[i].n;
|
112
|
-
cpFloat an = cpvdot(a, n);
|
113
|
-
if(axes[i].d > an) continue;
|
114
|
-
|
115
|
-
cpFloat bn = cpvdot(b, n);
|
116
|
-
cpFloat t = (axes[i].d - an)/(bn - an);
|
117
|
-
if(t < 0.0f || 1.0f < t) continue;
|
118
|
-
|
119
|
-
cpVect point = cpvlerp(a, b, t);
|
120
|
-
cpFloat dt = -cpvcross(n, point);
|
121
|
-
cpFloat dtMin = -cpvcross(n, verts[i]);
|
122
|
-
cpFloat dtMax = -cpvcross(n, verts[(i+1)%numVerts]);
|
123
|
-
|
124
|
-
if(dtMin <= dt && dt <= dtMax){
|
125
|
-
info->shape = shape;
|
126
|
-
info->t = t;
|
127
|
-
info->n = n;
|
128
|
-
}
|
129
|
-
}
|
130
|
-
}
|
131
|
-
|
132
|
-
static const cpShapeClass polyClass = {
|
133
|
-
CP_POLY_SHAPE,
|
134
|
-
cpPolyShapeCacheData,
|
135
|
-
cpPolyShapeDestroy,
|
136
|
-
cpPolyShapePointQuery,
|
137
|
-
cpPolyShapeSegmentQuery,
|
138
|
-
};
|
139
|
-
|
140
|
-
int
|
141
|
-
cpPolyValidate(cpVect *verts, int numVerts)
|
142
|
-
{
|
143
|
-
for(int i=0; i<numVerts; i++){
|
144
|
-
cpVect a = verts[i];
|
145
|
-
cpVect b = verts[(i+1)%numVerts];
|
146
|
-
cpVect c = verts[(i+2)%numVerts];
|
147
|
-
|
148
|
-
if(cpvcross(cpvsub(b, a), cpvsub(c, b)) > 0.0f)
|
149
|
-
return 0;
|
150
|
-
}
|
151
|
-
|
152
|
-
return 1;
|
153
|
-
}
|
154
|
-
|
155
|
-
int
|
156
|
-
cpPolyShapeGetNumVerts(cpShape *shape)
|
157
|
-
{
|
158
|
-
cpAssert(shape->klass == &polyClass, "Shape is not a poly shape.");
|
159
|
-
return ((cpPolyShape *)shape)->numVerts;
|
160
|
-
}
|
161
|
-
|
162
|
-
cpVect
|
163
|
-
cpPolyShapeGetVert(cpShape *shape, int idx)
|
164
|
-
{
|
165
|
-
cpAssert(shape->klass == &polyClass, "Shape is not a poly shape.");
|
166
|
-
cpAssert(0 <= idx && idx < cpPolyShapeGetNumVerts(shape), "Index out of range.");
|
167
|
-
|
168
|
-
return ((cpPolyShape *)shape)->verts[idx];
|
169
|
-
}
|
170
|
-
|
171
|
-
|
172
|
-
static void
|
173
|
-
setUpVerts(cpPolyShape *poly, int numVerts, cpVect *verts, cpVect offset)
|
174
|
-
{
|
175
|
-
poly->numVerts = numVerts;
|
176
|
-
|
177
|
-
poly->verts = (cpVect *)cpcalloc(numVerts, sizeof(cpVect));
|
178
|
-
poly->tVerts = (cpVect *)cpcalloc(numVerts, sizeof(cpVect));
|
179
|
-
poly->axes = (cpPolyShapeAxis *)cpcalloc(numVerts, sizeof(cpPolyShapeAxis));
|
180
|
-
poly->tAxes = (cpPolyShapeAxis *)cpcalloc(numVerts, sizeof(cpPolyShapeAxis));
|
181
|
-
|
182
|
-
for(int i=0; i<numVerts; i++){
|
183
|
-
cpVect a = cpvadd(offset, verts[i]);
|
184
|
-
cpVect b = cpvadd(offset, verts[(i+1)%numVerts]);
|
185
|
-
cpVect n = cpvnormalize(cpvperp(cpvsub(b, a)));
|
186
|
-
|
187
|
-
poly->verts[i] = a;
|
188
|
-
poly->axes[i].n = n;
|
189
|
-
poly->axes[i].d = cpvdot(n, a);
|
190
|
-
}
|
191
|
-
}
|
192
|
-
|
193
|
-
cpPolyShape *
|
194
|
-
cpPolyShapeInit(cpPolyShape *poly, cpBody *body, int numVerts, cpVect *verts, cpVect offset)
|
195
|
-
{
|
196
|
-
// Fail if the user attempts to pass a concave poly, or a bad winding.
|
197
|
-
cpAssert(cpPolyValidate(verts, numVerts), "Polygon is concave or has a reversed winding.");
|
198
|
-
|
199
|
-
setUpVerts(poly, numVerts, verts, offset);
|
200
|
-
cpShapeInit((cpShape *)poly, &polyClass, body);
|
201
|
-
|
202
|
-
return poly;
|
203
|
-
}
|
204
|
-
|
205
|
-
cpShape *
|
206
|
-
cpPolyShapeNew(cpBody *body, int numVerts, cpVect *verts, cpVect offset)
|
207
|
-
{
|
208
|
-
return (cpShape *)cpPolyShapeInit(cpPolyShapeAlloc(), body, numVerts, verts, offset);
|
209
|
-
}
|
210
|
-
|
211
|
-
cpPolyShape *
|
212
|
-
cpBoxShapeInit(cpPolyShape *poly, cpBody *body, cpFloat width, cpFloat height)
|
213
|
-
{
|
214
|
-
cpFloat hw = width/2.0;
|
215
|
-
cpFloat hh = height/2.0;
|
216
|
-
|
217
|
-
cpVect verts[] = {
|
218
|
-
cpv(-hw,-hh),
|
219
|
-
cpv(-hw, hh),
|
220
|
-
cpv( hw, hh),
|
221
|
-
cpv( hw,-hh),
|
222
|
-
};
|
223
|
-
|
224
|
-
return cpPolyShapeInit(poly, body, 4, verts, cpvzero);
|
225
|
-
}
|
226
|
-
|
227
|
-
cpShape *
|
228
|
-
cpBoxShapeNew(cpBody *body, cpFloat width, cpFloat height)
|
229
|
-
{
|
230
|
-
return (cpShape *)cpBoxShapeInit(cpPolyShapeAlloc(), body, width, height);
|
231
|
-
}
|
232
|
-
|
233
|
-
// Unsafe API (chipmunk_unsafe.h)
|
234
|
-
|
235
|
-
void
|
236
|
-
cpPolyShapeSetVerts(cpShape *shape, int numVerts, cpVect *verts, cpVect offset)
|
237
|
-
{
|
238
|
-
cpAssert(shape->klass == &polyClass, "Shape is not a poly shape.");
|
239
|
-
cpPolyShapeDestroy(shape);
|
240
|
-
setUpVerts((cpPolyShape *)shape, numVerts, verts, offset);
|
241
|
-
}
|
@@ -1,128 +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 <stdio.h>
|
23
|
-
#include <stdlib.h>
|
24
|
-
#include <math.h>
|
25
|
-
|
26
|
-
#include "chipmunk.h"
|
27
|
-
#include "constraints/util.h"
|
28
|
-
|
29
|
-
static void
|
30
|
-
preStep(cpRatchetJoint *joint, cpFloat dt, cpFloat dt_inv)
|
31
|
-
{
|
32
|
-
cpBody *a = joint->constraint.a;
|
33
|
-
cpBody *b = joint->constraint.b;
|
34
|
-
|
35
|
-
cpFloat angle = joint->angle;
|
36
|
-
cpFloat phase = joint->phase;
|
37
|
-
cpFloat ratchet = joint->ratchet;
|
38
|
-
|
39
|
-
cpFloat delta = b->a - a->a;
|
40
|
-
cpFloat diff = angle - delta;
|
41
|
-
cpFloat pdist = 0.0f;
|
42
|
-
|
43
|
-
if(diff*ratchet > 0.0f){
|
44
|
-
pdist = diff;
|
45
|
-
} else {
|
46
|
-
joint->angle = cpffloor((delta - phase)/ratchet)*ratchet + phase;
|
47
|
-
}
|
48
|
-
|
49
|
-
// calculate moment of inertia coefficient.
|
50
|
-
joint->iSum = 1.0f/(a->i_inv + b->i_inv);
|
51
|
-
|
52
|
-
// calculate bias velocity
|
53
|
-
cpFloat maxBias = joint->constraint.maxBias;
|
54
|
-
joint->bias = cpfclamp(-joint->constraint.biasCoef*dt_inv*pdist, -maxBias, maxBias);
|
55
|
-
|
56
|
-
// compute max impulse
|
57
|
-
joint->jMax = J_MAX(joint, dt);
|
58
|
-
|
59
|
-
// If the bias is 0, the joint is not at a limit. Reset the impulse.
|
60
|
-
if(!joint->bias)
|
61
|
-
joint->jAcc = 0.0f;
|
62
|
-
|
63
|
-
// apply joint torque
|
64
|
-
a->w -= joint->jAcc*a->i_inv;
|
65
|
-
b->w += joint->jAcc*b->i_inv;
|
66
|
-
}
|
67
|
-
|
68
|
-
static void
|
69
|
-
applyImpulse(cpRatchetJoint *joint)
|
70
|
-
{
|
71
|
-
if(!joint->bias) return; // early exit
|
72
|
-
|
73
|
-
cpBody *a = joint->constraint.a;
|
74
|
-
cpBody *b = joint->constraint.b;
|
75
|
-
|
76
|
-
// compute relative rotational velocity
|
77
|
-
cpFloat wr = b->w - a->w;
|
78
|
-
cpFloat ratchet = joint->ratchet;
|
79
|
-
|
80
|
-
// compute normal impulse
|
81
|
-
cpFloat j = -(joint->bias + wr)*joint->iSum;
|
82
|
-
cpFloat jOld = joint->jAcc;
|
83
|
-
joint->jAcc = cpfclamp((jOld + j)*ratchet, 0.0f, joint->jMax*cpfabs(ratchet))/ratchet;
|
84
|
-
j = joint->jAcc - jOld;
|
85
|
-
|
86
|
-
// apply impulse
|
87
|
-
a->w -= j*a->i_inv;
|
88
|
-
b->w += j*b->i_inv;
|
89
|
-
}
|
90
|
-
|
91
|
-
static cpFloat
|
92
|
-
getImpulse(cpRatchetJoint *joint)
|
93
|
-
{
|
94
|
-
return cpfabs(joint->jAcc);
|
95
|
-
}
|
96
|
-
|
97
|
-
static const cpConstraintClass klass = {
|
98
|
-
(cpConstraintPreStepFunction)preStep,
|
99
|
-
(cpConstraintApplyImpulseFunction)applyImpulse,
|
100
|
-
(cpConstraintGetImpulseFunction)getImpulse,
|
101
|
-
};
|
102
|
-
CP_DefineClassGetter(cpRatchetJoint)
|
103
|
-
|
104
|
-
cpRatchetJoint *
|
105
|
-
cpRatchetJointAlloc(void)
|
106
|
-
{
|
107
|
-
return (cpRatchetJoint *)cpmalloc(sizeof(cpRatchetJoint));
|
108
|
-
}
|
109
|
-
|
110
|
-
cpRatchetJoint *
|
111
|
-
cpRatchetJointInit(cpRatchetJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
|
112
|
-
{
|
113
|
-
cpConstraintInit((cpConstraint *)joint, &klass, a, b);
|
114
|
-
|
115
|
-
joint->angle = 0.0f;
|
116
|
-
joint->phase = phase;
|
117
|
-
joint->ratchet = ratchet;
|
118
|
-
|
119
|
-
joint->angle = b->a - a->a;
|
120
|
-
|
121
|
-
return joint;
|
122
|
-
}
|
123
|
-
|
124
|
-
cpConstraint *
|
125
|
-
cpRatchetJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
|
126
|
-
{
|
127
|
-
return (cpConstraint *)cpRatchetJointInit(cpRatchetJointAlloc(), a, b, phase, ratchet);
|
128
|
-
}
|
@@ -1,122 +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(cpRotaryLimitJoint *joint, cpFloat dt, cpFloat dt_inv)
|
29
|
-
{
|
30
|
-
cpBody *a = joint->constraint.a;
|
31
|
-
cpBody *b = joint->constraint.b;
|
32
|
-
|
33
|
-
cpFloat dist = b->a - a->a;
|
34
|
-
cpFloat pdist = 0.0f;
|
35
|
-
if(dist > joint->max) {
|
36
|
-
pdist = joint->max - dist;
|
37
|
-
} else if(dist < joint->min) {
|
38
|
-
pdist = joint->min - dist;
|
39
|
-
}
|
40
|
-
|
41
|
-
// calculate moment of inertia coefficient.
|
42
|
-
joint->iSum = 1.0f/(a->i_inv + b->i_inv);
|
43
|
-
|
44
|
-
// calculate bias velocity
|
45
|
-
cpFloat maxBias = joint->constraint.maxBias;
|
46
|
-
joint->bias = cpfclamp(-joint->constraint.biasCoef*dt_inv*(pdist), -maxBias, maxBias);
|
47
|
-
|
48
|
-
// compute max impulse
|
49
|
-
joint->jMax = J_MAX(joint, dt);
|
50
|
-
|
51
|
-
// If the bias is 0, the joint is not at a limit. Reset the impulse.
|
52
|
-
if(!joint->bias)
|
53
|
-
joint->jAcc = 0.0f;
|
54
|
-
|
55
|
-
// apply joint torque
|
56
|
-
a->w -= joint->jAcc*a->i_inv;
|
57
|
-
b->w += joint->jAcc*b->i_inv;
|
58
|
-
}
|
59
|
-
|
60
|
-
static void
|
61
|
-
applyImpulse(cpRotaryLimitJoint *joint)
|
62
|
-
{
|
63
|
-
if(!joint->bias) return; // early exit
|
64
|
-
|
65
|
-
cpBody *a = joint->constraint.a;
|
66
|
-
cpBody *b = joint->constraint.b;
|
67
|
-
|
68
|
-
// compute relative rotational velocity
|
69
|
-
cpFloat wr = b->w - a->w;
|
70
|
-
|
71
|
-
// compute normal impulse
|
72
|
-
cpFloat j = -(joint->bias + wr)*joint->iSum;
|
73
|
-
cpFloat jOld = joint->jAcc;
|
74
|
-
if(joint->bias < 0.0f){
|
75
|
-
joint->jAcc = cpfclamp(jOld + j, 0.0f, joint->jMax);
|
76
|
-
} else {
|
77
|
-
joint->jAcc = cpfclamp(jOld + j, -joint->jMax, 0.0f);
|
78
|
-
}
|
79
|
-
j = joint->jAcc - jOld;
|
80
|
-
|
81
|
-
// apply impulse
|
82
|
-
a->w -= j*a->i_inv;
|
83
|
-
b->w += j*b->i_inv;
|
84
|
-
}
|
85
|
-
|
86
|
-
static cpFloat
|
87
|
-
getImpulse(cpRotaryLimitJoint *joint)
|
88
|
-
{
|
89
|
-
return cpfabs(joint->jAcc);
|
90
|
-
}
|
91
|
-
|
92
|
-
static const cpConstraintClass klass = {
|
93
|
-
(cpConstraintPreStepFunction)preStep,
|
94
|
-
(cpConstraintApplyImpulseFunction)applyImpulse,
|
95
|
-
(cpConstraintGetImpulseFunction)getImpulse,
|
96
|
-
};
|
97
|
-
CP_DefineClassGetter(cpRotaryLimitJoint)
|
98
|
-
|
99
|
-
cpRotaryLimitJoint *
|
100
|
-
cpRotaryLimitJointAlloc(void)
|
101
|
-
{
|
102
|
-
return (cpRotaryLimitJoint *)cpmalloc(sizeof(cpRotaryLimitJoint));
|
103
|
-
}
|
104
|
-
|
105
|
-
cpRotaryLimitJoint *
|
106
|
-
cpRotaryLimitJointInit(cpRotaryLimitJoint *joint, cpBody *a, cpBody *b, cpFloat min, cpFloat max)
|
107
|
-
{
|
108
|
-
cpConstraintInit((cpConstraint *)joint, &klass, a, b);
|
109
|
-
|
110
|
-
joint->min = min;
|
111
|
-
joint->max = max;
|
112
|
-
|
113
|
-
joint->jAcc = 0.0f;
|
114
|
-
|
115
|
-
return joint;
|
116
|
-
}
|
117
|
-
|
118
|
-
cpConstraint *
|
119
|
-
cpRotaryLimitJointNew(cpBody *a, cpBody *b, cpFloat min, cpFloat max)
|
120
|
-
{
|
121
|
-
return (cpConstraint *)cpRotaryLimitJointInit(cpRotaryLimitJointAlloc(), a, b, min, max);
|
122
|
-
}
|