chipmunk 5.3.4.0-x86-mingw32 → 5.3.4.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/README +33 -13
  2. data/Rakefile +20 -7
  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 +219 -241
  9. data/ext/chipmunk/rb_cpConstraint.c +173 -185
  10. data/ext/chipmunk/rb_cpShape.c +347 -251
  11. data/ext/chipmunk/rb_cpSpace.c +376 -408
  12. data/ext/chipmunk/rb_cpVect.c +132 -170
  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/1.9/chipmunk.so +0 -0
  69. data/lib/chipmunk.rb +19 -8
  70. metadata +84 -42
  71. data/lib/1.8/chipmunk.so +0 -0
@@ -0,0 +1,115 @@
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
+ defaultSpringForce(cpDampedSpring *spring, cpFloat dist){
30
+ return (spring->restLength - dist)*spring->stiffness;
31
+ }
32
+
33
+ static void
34
+ preStep(cpDampedSpring *spring, cpFloat dt, cpFloat dt_inv)
35
+ {
36
+ CONSTRAINT_BEGIN(spring, a, b);
37
+
38
+ spring->r1 = cpvrotate(spring->anchr1, a->rot);
39
+ spring->r2 = cpvrotate(spring->anchr2, b->rot);
40
+
41
+ cpVect delta = cpvsub(cpvadd(b->p, spring->r2), cpvadd(a->p, spring->r1));
42
+ cpFloat dist = cpvlength(delta);
43
+ spring->n = cpvmult(delta, 1.0f/(dist ? dist : INFINITY));
44
+
45
+ cpFloat k = k_scalar(a, b, spring->r1, spring->r2, spring->n);
46
+ spring->nMass = 1.0f/k;
47
+
48
+ spring->target_vrn = 0.0f;
49
+ spring->v_coef = 1.0f - cpfexp(-spring->damping*dt*k);
50
+
51
+ // apply spring force
52
+ cpFloat f_spring = spring->springForceFunc((cpConstraint *)spring, dist);
53
+ apply_impulses(a, b, spring->r1, spring->r2, cpvmult(spring->n, f_spring*dt));
54
+ }
55
+
56
+ static void
57
+ applyImpulse(cpDampedSpring *spring)
58
+ {
59
+ CONSTRAINT_BEGIN(spring, a, b);
60
+
61
+ cpVect n = spring->n;
62
+ cpVect r1 = spring->r1;
63
+ cpVect r2 = spring->r2;
64
+
65
+ // compute relative velocity
66
+ cpFloat vrn = normal_relative_velocity(a, b, r1, r2, n) - spring->target_vrn;
67
+
68
+ // compute velocity loss from drag
69
+ // not 100% certain this is derived correctly, though it makes sense
70
+ cpFloat v_damp = -vrn*spring->v_coef;
71
+ spring->target_vrn = vrn + v_damp;
72
+
73
+ apply_impulses(a, b, spring->r1, spring->r2, cpvmult(spring->n, v_damp*spring->nMass));
74
+ }
75
+
76
+ static cpFloat
77
+ getImpulse(cpConstraint *constraint)
78
+ {
79
+ return 0.0f;
80
+ }
81
+
82
+ static const cpConstraintClass klass = {
83
+ (cpConstraintPreStepFunction)preStep,
84
+ (cpConstraintApplyImpulseFunction)applyImpulse,
85
+ (cpConstraintGetImpulseFunction)getImpulse,
86
+ };
87
+ CP_DefineClassGetter(cpDampedSpring)
88
+
89
+ cpDampedSpring *
90
+ cpDampedSpringAlloc(void)
91
+ {
92
+ return (cpDampedSpring *)cpmalloc(sizeof(cpDampedSpring));
93
+ }
94
+
95
+ cpDampedSpring *
96
+ cpDampedSpringInit(cpDampedSpring *spring, cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat restLength, cpFloat stiffness, cpFloat damping)
97
+ {
98
+ cpConstraintInit((cpConstraint *)spring, cpDampedSpringGetClass(), a, b);
99
+
100
+ spring->anchr1 = anchr1;
101
+ spring->anchr2 = anchr2;
102
+
103
+ spring->restLength = restLength;
104
+ spring->stiffness = stiffness;
105
+ spring->damping = damping;
106
+ spring->springForceFunc = (cpDampedSpringForceFunc)defaultSpringForce;
107
+
108
+ return spring;
109
+ }
110
+
111
+ cpConstraint *
112
+ cpDampedSpringNew(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat restLength, cpFloat stiffness, cpFloat damping)
113
+ {
114
+ return (cpConstraint *)cpDampedSpringInit(cpDampedSpringAlloc(), a, b, anchr1, anchr2, restLength, stiffness, damping);
115
+ }
@@ -0,0 +1,113 @@
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
+ static void
28
+ preStep(cpGearJoint *joint, cpFloat dt, cpFloat dt_inv)
29
+ {
30
+ CONSTRAINT_BEGIN(joint, a, b);
31
+
32
+ // calculate moment of inertia coefficient.
33
+ joint->iSum = 1.0f/(a->i_inv*joint->ratio_inv + joint->ratio*b->i_inv);
34
+
35
+ // calculate bias velocity
36
+ cpFloat maxBias = joint->constraint.maxBias;
37
+ joint->bias = cpfclamp(-joint->constraint.biasCoef*dt_inv*(b->a*joint->ratio - a->a - joint->phase), -maxBias, maxBias);
38
+
39
+ // compute max impulse
40
+ joint->jMax = J_MAX(joint, dt);
41
+
42
+ // apply joint torque
43
+ cpFloat j = joint->jAcc;
44
+ a->w -= j*a->i_inv*joint->ratio_inv;
45
+ b->w += j*b->i_inv;
46
+ }
47
+
48
+ static void
49
+ applyImpulse(cpGearJoint *joint)
50
+ {
51
+ CONSTRAINT_BEGIN(joint, a, b);
52
+
53
+ // compute relative rotational velocity
54
+ cpFloat wr = b->w*joint->ratio - a->w;
55
+
56
+ // compute normal impulse
57
+ cpFloat j = (joint->bias - wr)*joint->iSum;
58
+ cpFloat jOld = joint->jAcc;
59
+ joint->jAcc = cpfclamp(jOld + j, -joint->jMax, joint->jMax);
60
+ j = joint->jAcc - jOld;
61
+
62
+ // apply impulse
63
+ a->w -= j*a->i_inv*joint->ratio_inv;
64
+ b->w += j*b->i_inv;
65
+ }
66
+
67
+ static cpFloat
68
+ getImpulse(cpGearJoint *joint)
69
+ {
70
+ return cpfabs(joint->jAcc);
71
+ }
72
+
73
+ static const cpConstraintClass klass = {
74
+ (cpConstraintPreStepFunction)preStep,
75
+ (cpConstraintApplyImpulseFunction)applyImpulse,
76
+ (cpConstraintGetImpulseFunction)getImpulse,
77
+ };
78
+ CP_DefineClassGetter(cpGearJoint)
79
+
80
+ cpGearJoint *
81
+ cpGearJointAlloc(void)
82
+ {
83
+ return (cpGearJoint *)cpmalloc(sizeof(cpGearJoint));
84
+ }
85
+
86
+ cpGearJoint *
87
+ cpGearJointInit(cpGearJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratio)
88
+ {
89
+ cpConstraintInit((cpConstraint *)joint, &klass, a, b);
90
+
91
+ joint->phase = phase;
92
+ joint->ratio = ratio;
93
+ joint->ratio_inv = 1.0f/ratio;
94
+
95
+ joint->jAcc = 0.0f;
96
+
97
+ return joint;
98
+ }
99
+
100
+ cpConstraint *
101
+ cpGearJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratio)
102
+ {
103
+ return (cpConstraint *)cpGearJointInit(cpGearJointAlloc(), a, b, phase, ratio);
104
+ }
105
+
106
+ void
107
+ cpGearJointSetRatio(cpConstraint *constraint, cpFloat value)
108
+ {
109
+ cpConstraintCheckCast(constraint, cpGearJoint);
110
+ ((cpGearJoint *)constraint)->ratio = value;
111
+ ((cpGearJoint *)constraint)->ratio_inv = 1.0f/value;
112
+ cpConstraintActivateBodies(constraint);
113
+ }
@@ -0,0 +1,161 @@
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
+ static void
28
+ preStep(cpGrooveJoint *joint, cpFloat dt, cpFloat dt_inv)
29
+ {
30
+ CONSTRAINT_BEGIN(joint, a, b);
31
+
32
+ // calculate endpoints in worldspace
33
+ cpVect ta = cpBodyLocal2World(a, joint->grv_a);
34
+ cpVect tb = cpBodyLocal2World(a, joint->grv_b);
35
+
36
+ // calculate axis
37
+ cpVect n = cpvrotate(joint->grv_n, a->rot);
38
+ cpFloat d = cpvdot(ta, n);
39
+
40
+ joint->grv_tn = n;
41
+ joint->r2 = cpvrotate(joint->anchr2, b->rot);
42
+
43
+ // calculate tangential distance along the axis of r2
44
+ cpFloat td = cpvcross(cpvadd(b->p, joint->r2), n);
45
+ // calculate clamping factor and r2
46
+ if(td <= cpvcross(ta, n)){
47
+ joint->clamp = 1.0f;
48
+ joint->r1 = cpvsub(ta, a->p);
49
+ } else if(td >= cpvcross(tb, n)){
50
+ joint->clamp = -1.0f;
51
+ joint->r1 = cpvsub(tb, a->p);
52
+ } else {
53
+ joint->clamp = 0.0f;
54
+ joint->r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a->p);
55
+ }
56
+
57
+ // Calculate mass tensor
58
+ k_tensor(a, b, joint->r1, joint->r2, &joint->k1, &joint->k2);
59
+
60
+ // compute max impulse
61
+ joint->jMaxLen = J_MAX(joint, dt);
62
+
63
+ // calculate bias velocity
64
+ cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
65
+ joint->bias = cpvclamp(cpvmult(delta, -joint->constraint.biasCoef*dt_inv), joint->constraint.maxBias);
66
+
67
+ // apply accumulated impulse
68
+ apply_impulses(a, b, joint->r1, joint->r2, joint->jAcc);
69
+ }
70
+
71
+ static inline cpVect
72
+ grooveConstrain(cpGrooveJoint *joint, cpVect j){
73
+ cpVect n = joint->grv_tn;
74
+ cpVect jClamp = (joint->clamp*cpvcross(j, n) > 0.0f) ? j : cpvproject(j, n);
75
+ return cpvclamp(jClamp, joint->jMaxLen);
76
+ }
77
+
78
+ static void
79
+ applyImpulse(cpGrooveJoint *joint)
80
+ {
81
+ CONSTRAINT_BEGIN(joint, a, b);
82
+
83
+ cpVect r1 = joint->r1;
84
+ cpVect r2 = joint->r2;
85
+
86
+ // compute impulse
87
+ cpVect vr = relative_velocity(a, b, r1, r2);
88
+
89
+ cpVect j = mult_k(cpvsub(joint->bias, vr), joint->k1, joint->k2);
90
+ cpVect jOld = joint->jAcc;
91
+ joint->jAcc = grooveConstrain(joint, cpvadd(jOld, j));
92
+ j = cpvsub(joint->jAcc, jOld);
93
+
94
+ // apply impulse
95
+ apply_impulses(a, b, joint->r1, joint->r2, j);
96
+ }
97
+
98
+ static cpFloat
99
+ getImpulse(cpGrooveJoint *joint)
100
+ {
101
+ return cpvlength(joint->jAcc);
102
+ }
103
+
104
+ static const cpConstraintClass klass = {
105
+ (cpConstraintPreStepFunction)preStep,
106
+ (cpConstraintApplyImpulseFunction)applyImpulse,
107
+ (cpConstraintGetImpulseFunction)getImpulse,
108
+ };
109
+ CP_DefineClassGetter(cpGrooveJoint)
110
+
111
+ cpGrooveJoint *
112
+ cpGrooveJointAlloc(void)
113
+ {
114
+ return (cpGrooveJoint *)cpmalloc(sizeof(cpGrooveJoint));
115
+ }
116
+
117
+ cpGrooveJoint *
118
+ cpGrooveJointInit(cpGrooveJoint *joint, cpBody *a, cpBody *b, cpVect groove_a, cpVect groove_b, cpVect anchr2)
119
+ {
120
+ cpConstraintInit((cpConstraint *)joint, &klass, a, b);
121
+
122
+ joint->grv_a = groove_a;
123
+ joint->grv_b = groove_b;
124
+ joint->grv_n = cpvperp(cpvnormalize(cpvsub(groove_b, groove_a)));
125
+ joint->anchr2 = anchr2;
126
+
127
+ joint->jAcc = cpvzero;
128
+
129
+ return joint;
130
+ }
131
+
132
+ cpConstraint *
133
+ cpGrooveJointNew(cpBody *a, cpBody *b, cpVect groove_a, cpVect groove_b, cpVect anchr2)
134
+ {
135
+ return (cpConstraint *)cpGrooveJointInit(cpGrooveJointAlloc(), a, b, groove_a, groove_b, anchr2);
136
+ }
137
+
138
+ void
139
+ cpGrooveJointSetGrooveA(cpConstraint *constraint, cpVect value)
140
+ {
141
+ cpGrooveJoint *g = (cpGrooveJoint *)constraint;
142
+ cpConstraintCheckCast(constraint, cpGrooveJoint);
143
+
144
+ g->grv_a = value;
145
+ g->grv_n = cpvperp(cpvnormalize(cpvsub(g->grv_b, value)));
146
+
147
+ cpConstraintActivateBodies(constraint);
148
+ }
149
+
150
+ void
151
+ cpGrooveJointSetGrooveB(cpConstraint *constraint, cpVect value)
152
+ {
153
+ cpGrooveJoint *g = (cpGrooveJoint *)constraint;
154
+ cpConstraintCheckCast(constraint, cpGrooveJoint);
155
+
156
+ g->grv_b = value;
157
+ g->grv_n = cpvperp(cpvnormalize(cpvsub(value, g->grv_a)));
158
+
159
+ cpConstraintActivateBodies(constraint);
160
+ }
161
+
@@ -0,0 +1,116 @@
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 void
29
+ preStep(cpPinJoint *joint, cpFloat dt, cpFloat dt_inv)
30
+ {
31
+ CONSTRAINT_BEGIN(joint, a, b);
32
+
33
+ joint->r1 = cpvrotate(joint->anchr1, a->rot);
34
+ joint->r2 = cpvrotate(joint->anchr2, b->rot);
35
+
36
+ cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
37
+ cpFloat dist = cpvlength(delta);
38
+ joint->n = cpvmult(delta, 1.0f/(dist ? dist : (cpFloat)INFINITY));
39
+
40
+ // calculate mass normal
41
+ joint->nMass = 1.0f/k_scalar(a, b, joint->r1, joint->r2, joint->n);
42
+
43
+ // calculate bias velocity
44
+ cpFloat maxBias = joint->constraint.maxBias;
45
+ joint->bias = cpfclamp(-joint->constraint.biasCoef*dt_inv*(dist - joint->dist), -maxBias, maxBias);
46
+
47
+ // compute max impulse
48
+ joint->jnMax = J_MAX(joint, dt);
49
+
50
+ // apply accumulated impulse
51
+ cpVect j = cpvmult(joint->n, joint->jnAcc);
52
+ apply_impulses(a, b, joint->r1, joint->r2, j);
53
+ }
54
+
55
+ static void
56
+ applyImpulse(cpPinJoint *joint)
57
+ {
58
+ CONSTRAINT_BEGIN(joint, a, b);
59
+ cpVect n = joint->n;
60
+
61
+ // compute relative velocity
62
+ cpFloat vrn = normal_relative_velocity(a, b, joint->r1, joint->r2, n);
63
+
64
+ // compute normal impulse
65
+ cpFloat jn = (joint->bias - vrn)*joint->nMass;
66
+ cpFloat jnOld = joint->jnAcc;
67
+ joint->jnAcc = cpfclamp(jnOld + jn, -joint->jnMax, joint->jnMax);
68
+ jn = joint->jnAcc - jnOld;
69
+
70
+ // apply impulse
71
+ apply_impulses(a, b, joint->r1, joint->r2, cpvmult(n, jn));
72
+ }
73
+
74
+ static cpFloat
75
+ getImpulse(cpPinJoint *joint)
76
+ {
77
+ return cpfabs(joint->jnAcc);
78
+ }
79
+
80
+ static const cpConstraintClass klass = {
81
+ (cpConstraintPreStepFunction)preStep,
82
+ (cpConstraintApplyImpulseFunction)applyImpulse,
83
+ (cpConstraintGetImpulseFunction)getImpulse,
84
+ };
85
+ CP_DefineClassGetter(cpPinJoint);
86
+
87
+
88
+ cpPinJoint *
89
+ cpPinJointAlloc(void)
90
+ {
91
+ return (cpPinJoint *)cpmalloc(sizeof(cpPinJoint));
92
+ }
93
+
94
+ cpPinJoint *
95
+ cpPinJointInit(cpPinJoint *joint, cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2)
96
+ {
97
+ cpConstraintInit((cpConstraint *)joint, &klass, a, b);
98
+
99
+ joint->anchr1 = anchr1;
100
+ joint->anchr2 = anchr2;
101
+
102
+ // STATIC_BODY_CHECK
103
+ cpVect p1 = (a ? cpvadd(a->p, cpvrotate(anchr1, a->rot)) : anchr1);
104
+ cpVect p2 = (b ? cpvadd(b->p, cpvrotate(anchr2, b->rot)) : anchr2);
105
+ joint->dist = cpvlength(cpvsub(p2, p1));
106
+
107
+ joint->jnAcc = 0.0f;
108
+
109
+ return joint;
110
+ }
111
+
112
+ cpConstraint *
113
+ cpPinJointNew(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2)
114
+ {
115
+ return (cpConstraint *)cpPinJointInit(cpPinJointAlloc(), a, b, anchr1, anchr2);
116
+ }