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,114 @@
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(cpPivotJoint *joint, cpFloat dt, cpFloat dt_inv)
29
+ {
30
+ CONSTRAINT_BEGIN(joint, a, b);
31
+
32
+ joint->r1 = cpvrotate(joint->anchr1, a->rot);
33
+ joint->r2 = cpvrotate(joint->anchr2, b->rot);
34
+
35
+ // Calculate mass tensor
36
+ k_tensor(a, b, joint->r1, joint->r2, &joint->k1, &joint->k2);
37
+
38
+ // compute max impulse
39
+ joint->jMaxLen = J_MAX(joint, dt);
40
+
41
+ // calculate bias velocity
42
+ cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
43
+ joint->bias = cpvclamp(cpvmult(delta, -joint->constraint.biasCoef*dt_inv), joint->constraint.maxBias);
44
+
45
+ // apply accumulated impulse
46
+ apply_impulses(a, b, joint->r1, joint->r2, joint->jAcc);
47
+ }
48
+
49
+ static void
50
+ applyImpulse(cpPivotJoint *joint)
51
+ {
52
+ CONSTRAINT_BEGIN(joint, a, b);
53
+
54
+ cpVect r1 = joint->r1;
55
+ cpVect r2 = joint->r2;
56
+
57
+ // compute relative velocity
58
+ cpVect vr = relative_velocity(a, b, r1, r2);
59
+
60
+ // compute normal impulse
61
+ cpVect j = mult_k(cpvsub(joint->bias, vr), joint->k1, joint->k2);
62
+ cpVect jOld = joint->jAcc;
63
+ joint->jAcc = cpvclamp(cpvadd(joint->jAcc, j), joint->jMaxLen);
64
+ j = cpvsub(joint->jAcc, jOld);
65
+
66
+ // apply impulse
67
+ apply_impulses(a, b, joint->r1, joint->r2, j);
68
+ }
69
+
70
+ static cpFloat
71
+ getImpulse(cpConstraint *joint)
72
+ {
73
+ return cpvlength(((cpPivotJoint *)joint)->jAcc);
74
+ }
75
+
76
+ static const cpConstraintClass klass = {
77
+ (cpConstraintPreStepFunction)preStep,
78
+ (cpConstraintApplyImpulseFunction)applyImpulse,
79
+ (cpConstraintGetImpulseFunction)getImpulse,
80
+ };
81
+ CP_DefineClassGetter(cpPivotJoint)
82
+
83
+ cpPivotJoint *
84
+ cpPivotJointAlloc(void)
85
+ {
86
+ return (cpPivotJoint *)cpmalloc(sizeof(cpPivotJoint));
87
+ }
88
+
89
+ cpPivotJoint *
90
+ cpPivotJointInit(cpPivotJoint *joint, cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2)
91
+ {
92
+ cpConstraintInit((cpConstraint *)joint, &klass, a, b);
93
+
94
+ joint->anchr1 = anchr1;
95
+ joint->anchr2 = anchr2;
96
+
97
+ joint->jAcc = cpvzero;
98
+
99
+ return joint;
100
+ }
101
+
102
+ cpConstraint *
103
+ cpPivotJointNew2(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2)
104
+ {
105
+ return (cpConstraint *)cpPivotJointInit(cpPivotJointAlloc(), a, b, anchr1, anchr2);
106
+ }
107
+
108
+ cpConstraint *
109
+ cpPivotJointNew(cpBody *a, cpBody *b, cpVect pivot)
110
+ {
111
+ cpVect anchr1 = (a ? cpBodyWorld2Local(a, pivot) : pivot);
112
+ cpVect anchr2 = (b ? cpBodyWorld2Local(b, pivot) : pivot);
113
+ return cpPivotJointNew2(a, b, anchr1, anchr2);
114
+ }
@@ -0,0 +1,126 @@
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(cpRatchetJoint *joint, cpFloat dt, cpFloat dt_inv)
30
+ {
31
+ CONSTRAINT_BEGIN(joint, a, b);
32
+
33
+ cpFloat angle = joint->angle;
34
+ cpFloat phase = joint->phase;
35
+ cpFloat ratchet = joint->ratchet;
36
+
37
+ cpFloat delta = b->a - a->a;
38
+ cpFloat diff = angle - delta;
39
+ cpFloat pdist = 0.0f;
40
+
41
+ if(diff*ratchet > 0.0f){
42
+ pdist = diff;
43
+ } else {
44
+ joint->angle = cpffloor((delta - phase)/ratchet)*ratchet + phase;
45
+ }
46
+
47
+ // calculate moment of inertia coefficient.
48
+ joint->iSum = 1.0f/(a->i_inv + b->i_inv);
49
+
50
+ // calculate bias velocity
51
+ cpFloat maxBias = joint->constraint.maxBias;
52
+ joint->bias = cpfclamp(-joint->constraint.biasCoef*dt_inv*pdist, -maxBias, maxBias);
53
+
54
+ // compute max impulse
55
+ joint->jMax = J_MAX(joint, dt);
56
+
57
+ // If the bias is 0, the joint is not at a limit. Reset the impulse.
58
+ if(!joint->bias)
59
+ joint->jAcc = 0.0f;
60
+
61
+ // apply joint torque
62
+ a->w -= joint->jAcc*a->i_inv;
63
+ b->w += joint->jAcc*b->i_inv;
64
+ }
65
+
66
+ static void
67
+ applyImpulse(cpRatchetJoint *joint)
68
+ {
69
+ if(!joint->bias) return; // early exit
70
+
71
+ CONSTRAINT_BEGIN(joint, a, b);
72
+
73
+ // compute relative rotational velocity
74
+ cpFloat wr = b->w - a->w;
75
+ cpFloat ratchet = joint->ratchet;
76
+
77
+ // compute normal impulse
78
+ cpFloat j = -(joint->bias + wr)*joint->iSum;
79
+ cpFloat jOld = joint->jAcc;
80
+ joint->jAcc = cpfclamp((jOld + j)*ratchet, 0.0f, joint->jMax*cpfabs(ratchet))/ratchet;
81
+ j = joint->jAcc - jOld;
82
+
83
+ // apply impulse
84
+ a->w -= j*a->i_inv;
85
+ b->w += j*b->i_inv;
86
+ }
87
+
88
+ static cpFloat
89
+ getImpulse(cpRatchetJoint *joint)
90
+ {
91
+ return cpfabs(joint->jAcc);
92
+ }
93
+
94
+ static const cpConstraintClass klass = {
95
+ (cpConstraintPreStepFunction)preStep,
96
+ (cpConstraintApplyImpulseFunction)applyImpulse,
97
+ (cpConstraintGetImpulseFunction)getImpulse,
98
+ };
99
+ CP_DefineClassGetter(cpRatchetJoint)
100
+
101
+ cpRatchetJoint *
102
+ cpRatchetJointAlloc(void)
103
+ {
104
+ return (cpRatchetJoint *)cpmalloc(sizeof(cpRatchetJoint));
105
+ }
106
+
107
+ cpRatchetJoint *
108
+ cpRatchetJointInit(cpRatchetJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
109
+ {
110
+ cpConstraintInit((cpConstraint *)joint, &klass, a, b);
111
+
112
+ joint->angle = 0.0f;
113
+ joint->phase = phase;
114
+ joint->ratchet = ratchet;
115
+
116
+ // STATIC_BODY_CHECK
117
+ joint->angle = (b ? b->a : 0.0f) - (a ? a->a : 0.0f);
118
+
119
+ return joint;
120
+ }
121
+
122
+ cpConstraint *
123
+ cpRatchetJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
124
+ {
125
+ return (cpConstraint *)cpRatchetJointInit(cpRatchetJointAlloc(), a, b, phase, ratchet);
126
+ }
@@ -0,0 +1,120 @@
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(cpRotaryLimitJoint *joint, cpFloat dt, cpFloat dt_inv)
29
+ {
30
+ CONSTRAINT_BEGIN(joint, a, b);
31
+
32
+ cpFloat dist = b->a - a->a;
33
+ cpFloat pdist = 0.0f;
34
+ if(dist > joint->max) {
35
+ pdist = joint->max - dist;
36
+ } else if(dist < joint->min) {
37
+ pdist = joint->min - dist;
38
+ }
39
+
40
+ // calculate moment of inertia coefficient.
41
+ joint->iSum = 1.0f/(a->i_inv + b->i_inv);
42
+
43
+ // calculate bias velocity
44
+ cpFloat maxBias = joint->constraint.maxBias;
45
+ joint->bias = cpfclamp(-joint->constraint.biasCoef*dt_inv*(pdist), -maxBias, maxBias);
46
+
47
+ // compute max impulse
48
+ joint->jMax = J_MAX(joint, dt);
49
+
50
+ // If the bias is 0, the joint is not at a limit. Reset the impulse.
51
+ if(!joint->bias)
52
+ joint->jAcc = 0.0f;
53
+
54
+ // apply joint torque
55
+ a->w -= joint->jAcc*a->i_inv;
56
+ b->w += joint->jAcc*b->i_inv;
57
+ }
58
+
59
+ static void
60
+ applyImpulse(cpRotaryLimitJoint *joint)
61
+ {
62
+ if(!joint->bias) return; // early exit
63
+
64
+ CONSTRAINT_BEGIN(joint, a, b);
65
+
66
+ // compute relative rotational velocity
67
+ cpFloat wr = b->w - a->w;
68
+
69
+ // compute normal impulse
70
+ cpFloat j = -(joint->bias + wr)*joint->iSum;
71
+ cpFloat jOld = joint->jAcc;
72
+ if(joint->bias < 0.0f){
73
+ joint->jAcc = cpfclamp(jOld + j, 0.0f, joint->jMax);
74
+ } else {
75
+ joint->jAcc = cpfclamp(jOld + j, -joint->jMax, 0.0f);
76
+ }
77
+ j = joint->jAcc - jOld;
78
+
79
+ // apply impulse
80
+ a->w -= j*a->i_inv;
81
+ b->w += j*b->i_inv;
82
+ }
83
+
84
+ static cpFloat
85
+ getImpulse(cpRotaryLimitJoint *joint)
86
+ {
87
+ return cpfabs(joint->jAcc);
88
+ }
89
+
90
+ static const cpConstraintClass klass = {
91
+ (cpConstraintPreStepFunction)preStep,
92
+ (cpConstraintApplyImpulseFunction)applyImpulse,
93
+ (cpConstraintGetImpulseFunction)getImpulse,
94
+ };
95
+ CP_DefineClassGetter(cpRotaryLimitJoint)
96
+
97
+ cpRotaryLimitJoint *
98
+ cpRotaryLimitJointAlloc(void)
99
+ {
100
+ return (cpRotaryLimitJoint *)cpmalloc(sizeof(cpRotaryLimitJoint));
101
+ }
102
+
103
+ cpRotaryLimitJoint *
104
+ cpRotaryLimitJointInit(cpRotaryLimitJoint *joint, cpBody *a, cpBody *b, cpFloat min, cpFloat max)
105
+ {
106
+ cpConstraintInit((cpConstraint *)joint, &klass, a, b);
107
+
108
+ joint->min = min;
109
+ joint->max = max;
110
+
111
+ joint->jAcc = 0.0f;
112
+
113
+ return joint;
114
+ }
115
+
116
+ cpConstraint *
117
+ cpRotaryLimitJointNew(cpBody *a, cpBody *b, cpFloat min, cpFloat max)
118
+ {
119
+ return (cpConstraint *)cpRotaryLimitJointInit(cpRotaryLimitJointAlloc(), a, b, min, max);
120
+ }
@@ -0,0 +1,97 @@
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(cpSimpleMotor *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 + b->i_inv);
34
+
35
+ // compute max impulse
36
+ joint->jMax = J_MAX(joint, dt);
37
+
38
+ // apply joint torque
39
+ a->w -= joint->jAcc*a->i_inv;
40
+ b->w += joint->jAcc*b->i_inv;
41
+ }
42
+
43
+ static void
44
+ applyImpulse(cpSimpleMotor *joint)
45
+ {
46
+ CONSTRAINT_BEGIN(joint, a, b);
47
+
48
+ // compute relative rotational velocity
49
+ cpFloat wr = b->w - a->w + joint->rate;
50
+
51
+ // compute normal impulse
52
+ cpFloat j = -wr*joint->iSum;
53
+ cpFloat jOld = joint->jAcc;
54
+ joint->jAcc = cpfclamp(jOld + j, -joint->jMax, joint->jMax);
55
+ j = joint->jAcc - jOld;
56
+
57
+ // apply impulse
58
+ a->w -= j*a->i_inv;
59
+ b->w += j*b->i_inv;
60
+ }
61
+
62
+ static cpFloat
63
+ getImpulse(cpSimpleMotor *joint)
64
+ {
65
+ return cpfabs(joint->jAcc);
66
+ }
67
+
68
+ static const cpConstraintClass klass = {
69
+ (cpConstraintPreStepFunction)preStep,
70
+ (cpConstraintApplyImpulseFunction)applyImpulse,
71
+ (cpConstraintGetImpulseFunction)getImpulse,
72
+ };
73
+ CP_DefineClassGetter(cpSimpleMotor)
74
+
75
+ cpSimpleMotor *
76
+ cpSimpleMotorAlloc(void)
77
+ {
78
+ return (cpSimpleMotor *)cpmalloc(sizeof(cpSimpleMotor));
79
+ }
80
+
81
+ cpSimpleMotor *
82
+ cpSimpleMotorInit(cpSimpleMotor *joint, cpBody *a, cpBody *b, cpFloat rate)
83
+ {
84
+ cpConstraintInit((cpConstraint *)joint, &klass, a, b);
85
+
86
+ joint->rate = rate;
87
+
88
+ joint->jAcc = 0.0f;
89
+
90
+ return joint;
91
+ }
92
+
93
+ cpConstraint *
94
+ cpSimpleMotorNew(cpBody *a, cpBody *b, cpFloat rate)
95
+ {
96
+ return (cpConstraint *)cpSimpleMotorInit(cpSimpleMotorAlloc(), a, b, rate);
97
+ }