chipmunk 5.3.4.0 → 5.3.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/README +36 -13
  2. data/Rakefile +23 -9
  3. data/ext/chipmunk/extconf.rb +55 -12
  4. data/ext/chipmunk/rb_chipmunk.c +92 -98
  5. data/ext/chipmunk/rb_chipmunk.h +44 -34
  6. data/ext/chipmunk/rb_cpArbiter.c +135 -98
  7. data/ext/chipmunk/rb_cpBB.c +84 -101
  8. data/ext/chipmunk/rb_cpBody.c +221 -243
  9. data/ext/chipmunk/rb_cpConstraint.c +173 -185
  10. data/ext/chipmunk/rb_cpShape.c +353 -240
  11. data/ext/chipmunk/rb_cpSpace.c +376 -408
  12. data/ext/chipmunk/rb_cpVect.c +135 -173
  13. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/chipmunk.h +163 -0
  14. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/chipmunk_ffi.h +59 -0
  15. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/chipmunk_private.h +49 -0
  16. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/chipmunk_types.h +151 -0
  17. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/chipmunk_unsafe.h +54 -0
  18. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpConstraint.h +105 -0
  19. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpDampedRotarySpring.h +46 -0
  20. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpDampedSpring.h +53 -0
  21. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpGearJoint.h +41 -0
  22. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpGrooveJoint.h +48 -0
  23. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpPinJoint.h +43 -0
  24. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpPivotJoint.h +42 -0
  25. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpRatchetJoint.h +40 -0
  26. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpRotaryLimitJoint.h +39 -0
  27. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpSimpleMotor.h +37 -0
  28. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/cpSlideJoint.h +44 -0
  29. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/constraints/util.h +134 -0
  30. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpArbiter.h +188 -0
  31. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpArray.h +49 -0
  32. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpBB.h +74 -0
  33. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpBody.h +219 -0
  34. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpCollision.h +28 -0
  35. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpHashSet.h +82 -0
  36. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpPolyShape.h +103 -0
  37. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpShape.h +177 -0
  38. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpSpace.h +206 -0
  39. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpSpaceHash.h +110 -0
  40. data/ext/chipmunk/vendor/chipmunk-5.3.4/include/chipmunk/cpVect.h +207 -0
  41. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/chipmunk.c +151 -0
  42. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpConstraint.c +54 -0
  43. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpDampedRotarySpring.c +105 -0
  44. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpDampedSpring.c +115 -0
  45. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpGearJoint.c +113 -0
  46. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpGrooveJoint.c +161 -0
  47. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpPinJoint.c +116 -0
  48. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpPivotJoint.c +114 -0
  49. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpRatchetJoint.c +126 -0
  50. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpRotaryLimitJoint.c +120 -0
  51. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpSimpleMotor.c +97 -0
  52. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/constraints/cpSlideJoint.c +129 -0
  53. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpArbiter.c +280 -0
  54. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpArray.c +143 -0
  55. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpBB.c +47 -0
  56. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpBody.c +192 -0
  57. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpCollision.c +411 -0
  58. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpHashSet.c +253 -0
  59. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpPolyShape.c +240 -0
  60. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpShape.c +405 -0
  61. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpSpace.c +499 -0
  62. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpSpaceComponent.c +279 -0
  63. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpSpaceHash.c +534 -0
  64. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpSpaceQuery.c +246 -0
  65. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpSpaceStep.c +398 -0
  66. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/cpVect.c +71 -0
  67. data/ext/chipmunk/vendor/chipmunk-5.3.4/src/prime.h +68 -0
  68. data/lib/chipmunk.rb +19 -8
  69. metadata +85 -38
@@ -0,0 +1,207 @@
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
+ /// Constant for the zero vector.
23
+ static const cpVect cpvzero = {0.0f,0.0f};
24
+
25
+ /// Convenience constructor for cpVect structs.
26
+ static inline cpVect
27
+ cpv(const cpFloat x, const cpFloat y)
28
+ {
29
+ cpVect v = {x, y};
30
+ return v;
31
+ }
32
+
33
+ // non-inlined functions
34
+
35
+ /// Returns the length of v.
36
+ cpFloat cpvlength(const cpVect v);
37
+
38
+ /// Spherical linearly interpolate between v1 and v2.
39
+ cpVect cpvslerp(const cpVect v1, const cpVect v2, const cpFloat t);
40
+
41
+ /// Spherical linearly interpolate between v1 towards v2 by no more than angle a radians
42
+ cpVect cpvslerpconst(const cpVect v1, const cpVect v2, const cpFloat a);
43
+
44
+ /// Returns the unit length vector for the given angle (in radians).
45
+ cpVect cpvforangle(const cpFloat a);
46
+
47
+ /// Returns the angular direction v is pointing in (in radians).
48
+ cpFloat cpvtoangle(const cpVect v);
49
+
50
+ /**
51
+ Returns a string representation of v. Intended mostly for debugging purposes and not production use.
52
+
53
+ @attention The string points to a static local and is reset every time the function is called.
54
+ If you want to print more than one vector you will have to split up your printing onto separate lines.
55
+ */
56
+ char *cpvstr(const cpVect v);
57
+
58
+ /// Check if two vectors are equal. (Be careful when comparing floating point numbers!)
59
+ static inline cpBool
60
+ cpveql(const cpVect v1, const cpVect v2)
61
+ {
62
+ return (v1.x == v2.x && v1.y == v2.y);
63
+ }
64
+
65
+ /// Add two vectors
66
+ static inline cpVect
67
+ cpvadd(const cpVect v1, const cpVect v2)
68
+ {
69
+ return cpv(v1.x + v2.x, v1.y + v2.y);
70
+ }
71
+
72
+ /// Negate a vector.
73
+ static inline cpVect
74
+ cpvneg(const cpVect v)
75
+ {
76
+ return cpv(-v.x, -v.y);
77
+ }
78
+
79
+ /// Subtract two vectors.
80
+ static inline cpVect
81
+ cpvsub(const cpVect v1, const cpVect v2)
82
+ {
83
+ return cpv(v1.x - v2.x, v1.y - v2.y);
84
+ }
85
+
86
+ /// Scalar multiplication.
87
+ static inline cpVect
88
+ cpvmult(const cpVect v, const cpFloat s)
89
+ {
90
+ return cpv(v.x*s, v.y*s);
91
+ }
92
+
93
+ /// Vector dot product.
94
+ static inline cpFloat
95
+ cpvdot(const cpVect v1, const cpVect v2)
96
+ {
97
+ return v1.x*v2.x + v1.y*v2.y;
98
+ }
99
+
100
+ /**
101
+ 2D vector cross product analog.
102
+ The cross product of 2D vectors results in a 3D vector with only a z component.
103
+ This function returns the magnitude of the z value.
104
+ */
105
+ static inline cpFloat
106
+ cpvcross(const cpVect v1, const cpVect v2)
107
+ {
108
+ return v1.x*v2.y - v1.y*v2.x;
109
+ }
110
+
111
+ /// Returns a perpendicular vector. (90 degree rotation)
112
+ static inline cpVect
113
+ cpvperp(const cpVect v)
114
+ {
115
+ return cpv(-v.y, v.x);
116
+ }
117
+
118
+ /// Returns a perpendicular vector. (-90 degree rotation)
119
+ static inline cpVect
120
+ cpvrperp(const cpVect v)
121
+ {
122
+ return cpv(v.y, -v.x);
123
+ }
124
+
125
+ /// Returns the vector projection of v1 onto v2.
126
+ static inline cpVect
127
+ cpvproject(const cpVect v1, const cpVect v2)
128
+ {
129
+ return cpvmult(v2, cpvdot(v1, v2)/cpvdot(v2, v2));
130
+ }
131
+
132
+ /// Uses complex number multiplication to rotate v1 by v2. Scaling will occur if v1 is not a unit vector.
133
+ static inline cpVect
134
+ cpvrotate(const cpVect v1, const cpVect v2)
135
+ {
136
+ return cpv(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x);
137
+ }
138
+
139
+ /// Inverse of cpvrotate().
140
+ static inline cpVect
141
+ cpvunrotate(const cpVect v1, const cpVect v2)
142
+ {
143
+ return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
144
+ }
145
+
146
+ /// Returns the squared length of v. Faster than cpvlength() when you only need to compare lengths.
147
+ static inline cpFloat
148
+ cpvlengthsq(const cpVect v)
149
+ {
150
+ return cpvdot(v, v);
151
+ }
152
+
153
+ /// Linearly interpolate between v1 and v2.
154
+ static inline cpVect
155
+ cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t)
156
+ {
157
+ return cpvadd(cpvmult(v1, 1.0f - t), cpvmult(v2, t));
158
+ }
159
+
160
+ /// Returns a normalized copy of v.
161
+ static inline cpVect
162
+ cpvnormalize(const cpVect v)
163
+ {
164
+ return cpvmult(v, 1.0f/cpvlength(v));
165
+ }
166
+
167
+ /// Returns a normalized copy of v or cpvzero if v was already cpvzero. Protects against divide by zero errors.
168
+ static inline cpVect
169
+ cpvnormalize_safe(const cpVect v)
170
+ {
171
+ return (v.x == 0.0f && v.y == 0.0f ? cpvzero : cpvnormalize(v));
172
+ }
173
+
174
+ /// Clamp v to length len.
175
+ static inline cpVect
176
+ cpvclamp(const cpVect v, const cpFloat len)
177
+ {
178
+ return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v;
179
+ }
180
+
181
+ /// Linearly interpolate between v1 towards v2 by distance d.
182
+ static inline cpVect
183
+ cpvlerpconst(cpVect v1, cpVect v2, cpFloat d)
184
+ {
185
+ return cpvadd(v1, cpvclamp(cpvsub(v2, v1), d));
186
+ }
187
+
188
+ /// Returns the distance between v1 and v2.
189
+ static inline cpFloat
190
+ cpvdist(const cpVect v1, const cpVect v2)
191
+ {
192
+ return cpvlength(cpvsub(v1, v2));
193
+ }
194
+
195
+ /// Returns the squared distance between v1 and v2. Faster than cpvdist() when you only need to compare distances.
196
+ static inline cpFloat
197
+ cpvdistsq(const cpVect v1, const cpVect v2)
198
+ {
199
+ return cpvlengthsq(cpvsub(v1, v2));
200
+ }
201
+
202
+ /// Returns true if the distance between v1 and v2 is less than dist.
203
+ static inline cpBool
204
+ cpvnear(const cpVect v1, const cpVect v2, const cpFloat dist)
205
+ {
206
+ return cpvdistsq(v1, v2) < dist*dist;
207
+ }
@@ -0,0 +1,151 @@
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
+ #define _USE_MATH_DEFINES
25
+ #include <math.h>
26
+
27
+ #include "chipmunk.h"
28
+
29
+ #ifdef __cplusplus
30
+ extern "C" {
31
+ #endif
32
+ void cpInitCollisionFuncs(void);
33
+ #ifdef __cplusplus
34
+ }
35
+ #endif
36
+
37
+ void
38
+ cpMessage(const char *message, const char *condition, const char *file, int line, int isError)
39
+ {
40
+ fprintf(stderr, (isError ? "Aborting due to Chipmunk error: %s\n" : "Chipmunk warning: %s\n"), message);
41
+ fprintf(stderr, "\tFailed condition: %s\n", condition);
42
+ fprintf(stderr, "\tSource:%s:%d\n", file, line);
43
+
44
+ if(isError) abort();
45
+ }
46
+
47
+
48
+ const char *cpVersionString = "5.3.4";
49
+
50
+ void
51
+ cpInitChipmunk(void)
52
+ {
53
+ #ifndef NDEBUG
54
+ printf("Initializing Chipmunk v%s (Debug Enabled)\n", cpVersionString);
55
+ printf("Compile with -DNDEBUG defined to disable debug mode and runtime assertion checks\n");
56
+ #endif
57
+
58
+ cpInitCollisionFuncs();
59
+ }
60
+
61
+ cpFloat
62
+ cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset)
63
+ {
64
+ return m*(0.5f*(r1*r1 + r2*r2) + cpvlengthsq(offset));
65
+ }
66
+
67
+ cpFloat
68
+ cpAreaForCircle(cpFloat r1, cpFloat r2)
69
+ {
70
+ return 2.0f*(cpFloat)M_PI*cpfabs(r1*r1 - r2*r2);
71
+ }
72
+
73
+ cpFloat
74
+ cpMomentForSegment(cpFloat m, cpVect a, cpVect b)
75
+ {
76
+ cpFloat length = cpvlength(cpvsub(b, a));
77
+ cpVect offset = cpvmult(cpvadd(a, b), 1.0f/2.0f);
78
+
79
+ return m*(length*length/12.0f + cpvlengthsq(offset));
80
+ }
81
+
82
+ cpFloat
83
+ cpAreaForSegment(cpVect a, cpVect b, cpFloat r)
84
+ {
85
+ return 2.0f*r*((cpFloat)M_PI*r + cpvdist(a, b));
86
+ }
87
+
88
+ cpFloat
89
+ cpMomentForPoly(cpFloat m, const int numVerts, const cpVect *verts, cpVect offset)
90
+ {
91
+ cpFloat sum1 = 0.0f;
92
+ cpFloat sum2 = 0.0f;
93
+ for(int i=0; i<numVerts; i++){
94
+ cpVect v1 = cpvadd(verts[i], offset);
95
+ cpVect v2 = cpvadd(verts[(i+1)%numVerts], offset);
96
+
97
+ cpFloat a = cpvcross(v2, v1);
98
+ cpFloat b = cpvdot(v1, v1) + cpvdot(v1, v2) + cpvdot(v2, v2);
99
+
100
+ sum1 += a*b;
101
+ sum2 += a;
102
+ }
103
+
104
+ return (m*sum1)/(6.0f*sum2);
105
+ }
106
+
107
+ cpFloat
108
+ cpAreaForPoly(const int numVerts, const cpVect *verts)
109
+ {
110
+ cpFloat area = 0.0f;
111
+ for(int i=0; i<numVerts; i++){
112
+ area += cpvcross(verts[i], verts[(i+1)%numVerts]);
113
+ }
114
+
115
+ return area/2.0f;
116
+ }
117
+
118
+ cpVect
119
+ cpCentroidForPoly(const int numVerts, const cpVect *verts)
120
+ {
121
+ cpFloat sum = 0.0f;
122
+ cpVect vsum = cpvzero;
123
+
124
+ for(int i=0; i<numVerts; i++){
125
+ cpVect v1 = verts[i];
126
+ cpVect v2 = verts[(i+1)%numVerts];
127
+ cpFloat cross = cpvcross(v1, v2);
128
+
129
+ sum += cross;
130
+ vsum = cpvadd(vsum, cpvmult(cpvadd(v1, v2), cross));
131
+ }
132
+
133
+ return cpvmult(vsum, 1.0f/(3.0f*sum));
134
+ }
135
+
136
+ void
137
+ cpRecenterPoly(const int numVerts, cpVect *verts){
138
+ cpVect centroid = cpCentroidForPoly(numVerts, verts);
139
+
140
+ for(int i=0; i<numVerts; i++){
141
+ verts[i] = cpvsub(verts[i], centroid);
142
+ }
143
+ }
144
+
145
+ cpFloat
146
+ cpMomentForBox(cpFloat m, cpFloat width, cpFloat height)
147
+ {
148
+ return m*(width*width + height*height)/12.0f;
149
+ }
150
+
151
+ #include "chipmunk_ffi.h"
@@ -0,0 +1,54 @@
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_private.h"
25
+ #include "constraints/util.h"
26
+
27
+ // TODO: Comment me!
28
+
29
+ cpFloat cp_constraint_bias_coef = 0.1f;
30
+
31
+ void cpConstraintDestroy(cpConstraint *constraint){}
32
+
33
+ void
34
+ cpConstraintFree(cpConstraint *constraint)
35
+ {
36
+ if(constraint){
37
+ cpConstraintDestroy(constraint);
38
+ cpfree(constraint);
39
+ }
40
+ }
41
+
42
+ // *** defined in util.h
43
+
44
+ void
45
+ cpConstraintInit(cpConstraint *constraint, const cpConstraintClass *klass, cpBody *a, cpBody *b)
46
+ {
47
+ constraint->klass = klass;
48
+ constraint->a = a;
49
+ constraint->b = b;
50
+
51
+ constraint->maxForce = (cpFloat)INFINITY;
52
+ constraint->biasCoef = cp_constraint_bias_coef;
53
+ constraint->maxBias = (cpFloat)INFINITY;
54
+ }
@@ -0,0 +1,105 @@
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 <math.h>
24
+
25
+ #include "chipmunk_private.h"
26
+ #include "constraints/util.h"
27
+
28
+ static cpFloat
29
+ defaultSpringTorque(cpDampedRotarySpring *spring, cpFloat relativeAngle){
30
+ return (relativeAngle - spring->restAngle)*spring->stiffness;
31
+ }
32
+
33
+ static void
34
+ preStep(cpDampedRotarySpring *spring, cpFloat dt, cpFloat dt_inv)
35
+ {
36
+ CONSTRAINT_BEGIN(spring, a, b);
37
+
38
+ cpFloat moment = a->i_inv + b->i_inv;
39
+ spring->iSum = 1.0f/moment;
40
+
41
+ spring->w_coef = 1.0f - cpfexp(-spring->damping*dt*moment);
42
+ spring->target_wrn = 0.0f;
43
+
44
+ // apply spring torque
45
+ cpFloat j_spring = spring->springTorqueFunc((cpConstraint *)spring, a->a - b->a)*dt;
46
+ a->w -= j_spring*a->i_inv;
47
+ b->w += j_spring*b->i_inv;
48
+ }
49
+
50
+ static void
51
+ applyImpulse(cpDampedRotarySpring *spring)
52
+ {
53
+ CONSTRAINT_BEGIN(spring, a, b);
54
+
55
+ // compute relative velocity
56
+ cpFloat wrn = a->w - b->w;//normal_relative_velocity(a, b, r1, r2, n) - spring->target_vrn;
57
+
58
+ // compute velocity loss from drag
59
+ // not 100% certain this is derived correctly, though it makes sense
60
+ cpFloat w_damp = wrn*spring->w_coef;
61
+ spring->target_wrn = wrn - w_damp;
62
+
63
+ //apply_impulses(a, b, spring->r1, spring->r2, cpvmult(spring->n, v_damp*spring->nMass));
64
+ cpFloat j_damp = w_damp*spring->iSum;
65
+ a->w -= j_damp*a->i_inv;
66
+ b->w += j_damp*b->i_inv;
67
+ }
68
+
69
+ static cpFloat
70
+ getImpulse(cpConstraint *constraint)
71
+ {
72
+ return 0.0f;
73
+ }
74
+
75
+ static const cpConstraintClass klass = {
76
+ (cpConstraintPreStepFunction)preStep,
77
+ (cpConstraintApplyImpulseFunction)applyImpulse,
78
+ (cpConstraintGetImpulseFunction)getImpulse,
79
+ };
80
+ CP_DefineClassGetter(cpDampedRotarySpring)
81
+
82
+ cpDampedRotarySpring *
83
+ cpDampedRotarySpringAlloc(void)
84
+ {
85
+ return (cpDampedRotarySpring *)cpmalloc(sizeof(cpDampedRotarySpring));
86
+ }
87
+
88
+ cpDampedRotarySpring *
89
+ cpDampedRotarySpringInit(cpDampedRotarySpring *spring, cpBody *a, cpBody *b, cpFloat restAngle, cpFloat stiffness, cpFloat damping)
90
+ {
91
+ cpConstraintInit((cpConstraint *)spring, &klass, a, b);
92
+
93
+ spring->restAngle = restAngle;
94
+ spring->stiffness = stiffness;
95
+ spring->damping = damping;
96
+ spring->springTorqueFunc = (cpDampedRotarySpringTorqueFunc)defaultSpringTorque;
97
+
98
+ return spring;
99
+ }
100
+
101
+ cpConstraint *
102
+ cpDampedRotarySpringNew(cpBody *a, cpBody *b, cpFloat restAngle, cpFloat stiffness, cpFloat damping)
103
+ {
104
+ return (cpConstraint *)cpDampedRotarySpringInit(cpDampedRotarySpringAlloc(), a, b, restAngle, stiffness, damping);
105
+ }