chipmunk 4.1.0 → 5.2.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.
Files changed (71) hide show
  1. data/LICENSE +20 -0
  2. data/README +60 -0
  3. data/Rakefile +47 -40
  4. data/ext/chipmunk/chipmunk.c +39 -3
  5. data/ext/chipmunk/cpArbiter.c +91 -80
  6. data/ext/chipmunk/cpArray.c +24 -10
  7. data/ext/chipmunk/cpBB.c +5 -4
  8. data/ext/chipmunk/cpBody.c +30 -22
  9. data/ext/chipmunk/cpCollision.c +54 -53
  10. data/ext/chipmunk/cpConstraint.c +54 -0
  11. data/ext/chipmunk/cpDampedRotarySpring.c +106 -0
  12. data/ext/chipmunk/cpDampedSpring.c +117 -0
  13. data/ext/chipmunk/cpGearJoint.c +114 -0
  14. data/ext/chipmunk/cpGrooveJoint.c +138 -0
  15. data/ext/chipmunk/cpHashSet.c +74 -40
  16. data/ext/chipmunk/cpPinJoint.c +117 -0
  17. data/ext/chipmunk/cpPivotJoint.c +114 -0
  18. data/ext/chipmunk/cpPolyShape.c +117 -15
  19. data/ext/chipmunk/cpRatchetJoint.c +128 -0
  20. data/ext/chipmunk/cpRotaryLimitJoint.c +122 -0
  21. data/ext/chipmunk/cpShape.c +174 -18
  22. data/ext/chipmunk/cpSimpleMotor.c +99 -0
  23. data/ext/chipmunk/cpSlideJoint.c +131 -0
  24. data/ext/chipmunk/cpSpace.c +584 -215
  25. data/ext/chipmunk/cpSpaceHash.c +191 -105
  26. data/ext/chipmunk/cpVect.c +18 -10
  27. data/ext/chipmunk/extconf.rb +34 -4
  28. data/ext/chipmunk/{chipmunk.h → include/chipmunk/chipmunk.h} +63 -6
  29. data/ext/chipmunk/include/chipmunk/chipmunk_ffi.h +42 -0
  30. data/ext/chipmunk/include/chipmunk/chipmunk_types.h +80 -0
  31. data/ext/chipmunk/include/chipmunk/chipmunk_unsafe.h +54 -0
  32. data/ext/chipmunk/include/chipmunk/constraints/cpConstraint.h +92 -0
  33. data/ext/chipmunk/include/chipmunk/constraints/cpDampedRotarySpring.h +46 -0
  34. data/ext/chipmunk/include/chipmunk/constraints/cpDampedSpring.h +53 -0
  35. data/ext/chipmunk/include/chipmunk/constraints/cpGearJoint.h +41 -0
  36. data/ext/chipmunk/include/chipmunk/constraints/cpGrooveJoint.h +44 -0
  37. data/ext/chipmunk/include/chipmunk/constraints/cpPinJoint.h +43 -0
  38. data/ext/chipmunk/include/chipmunk/constraints/cpPivotJoint.h +42 -0
  39. data/ext/chipmunk/include/chipmunk/constraints/cpRatchetJoint.h +40 -0
  40. data/ext/chipmunk/include/chipmunk/constraints/cpRotaryLimitJoint.h +39 -0
  41. data/ext/chipmunk/include/chipmunk/constraints/cpSimpleMotor.h +37 -0
  42. data/ext/chipmunk/include/chipmunk/constraints/cpSlideJoint.h +44 -0
  43. data/ext/chipmunk/include/chipmunk/constraints/util.h +116 -0
  44. data/ext/chipmunk/{cpArbiter.h → include/chipmunk/cpArbiter.h} +66 -15
  45. data/ext/chipmunk/{cpArray.h → include/chipmunk/cpArray.h} +2 -1
  46. data/ext/chipmunk/{cpBB.h → include/chipmunk/cpBB.h} +21 -0
  47. data/ext/chipmunk/{cpBody.h → include/chipmunk/cpBody.h} +37 -9
  48. data/ext/chipmunk/{cpCollision.h → include/chipmunk/cpCollision.h} +1 -1
  49. data/ext/chipmunk/{cpHashSet.h → include/chipmunk/cpHashSet.h} +12 -9
  50. data/ext/chipmunk/{cpPolyShape.h → include/chipmunk/cpPolyShape.h} +13 -2
  51. data/ext/chipmunk/{cpShape.h → include/chipmunk/cpShape.h} +51 -18
  52. data/ext/chipmunk/include/chipmunk/cpSpace.h +180 -0
  53. data/ext/chipmunk/{cpSpaceHash.h → include/chipmunk/cpSpaceHash.h} +18 -9
  54. data/ext/chipmunk/{cpVect.h → include/chipmunk/cpVect.h} +61 -10
  55. data/ext/chipmunk/prime.h +32 -32
  56. data/ext/chipmunk/rb_chipmunk.c +125 -109
  57. data/ext/chipmunk/rb_chipmunk.h +96 -77
  58. data/ext/chipmunk/rb_cpArbiter.c +225 -0
  59. data/ext/chipmunk/rb_cpBB.c +174 -154
  60. data/ext/chipmunk/rb_cpBody.c +347 -239
  61. data/ext/chipmunk/rb_cpConstraint.c +346 -0
  62. data/ext/chipmunk/rb_cpShape.c +455 -292
  63. data/ext/chipmunk/rb_cpSpace.c +544 -330
  64. data/ext/chipmunk/rb_cpVect.c +321 -250
  65. data/lib/chipmunk.rb +28 -15
  66. data/lib/chipmunk/version.rb +3 -0
  67. metadata +74 -34
  68. data/ext/chipmunk/cpJoint.c +0 -553
  69. data/ext/chipmunk/cpJoint.h +0 -122
  70. data/ext/chipmunk/cpSpace.h +0 -120
  71. data/ext/chipmunk/rb_cpJoint.c +0 -136
data/lib/chipmunk.rb CHANGED
@@ -1,15 +1,28 @@
1
- # this redirection script by John Mair (banisterfiend)
2
-
3
- require 'rbconfig'
4
-
5
- direc = File.dirname(__FILE__)
6
- dlext = Config::CONFIG['DLEXT']
7
- begin
8
- if RUBY_VERSION && RUBY_VERSION =~ /1.9/
9
- require "#{direc}/1.9/chipmunk.#{dlext}"
10
- else
11
- require "#{direc}/1.8/chipmunk.#{dlext}"
12
- end
13
- rescue LoadError => e
14
- require "#{direc}/chipmunk.#{dlext}"
15
- end
1
+ # this redirection script by John Mair (banisterfiend)
2
+
3
+ require 'rbconfig'
4
+
5
+ direc = File.dirname(__FILE__)
6
+ dlext = Config::CONFIG['DLEXT']
7
+ begin
8
+ if RUBY_VERSION && RUBY_VERSION =~ /1.9/
9
+ require "#{direc}/1.9/chipmunk.#{dlext}"
10
+ else
11
+ require "#{direc}/1.8/chipmunk.#{dlext}"
12
+ end
13
+ rescue LoadError => e
14
+ require "#{direc}/chipmunk.#{dlext}"
15
+ end
16
+
17
+ # Add some constants to CP here, so we don't need
18
+ # to do it in the C code.
19
+ # Let's cheat a bit here.. :p
20
+
21
+ module CP
22
+ ZERO_VEC_2 = Vec2.new(0,0).freeze
23
+ ALL_ONES = Vec2.new(1,1).freeze
24
+ end
25
+
26
+
27
+
28
+
@@ -0,0 +1,3 @@
1
+ module Chipmunk
2
+ VERSION = "5.2.0"
3
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chipmunk
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ hash: 63
5
+ prerelease: false
6
+ segments:
7
+ - 5
8
+ - 2
9
+ - 0
10
+ version: 5.2.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Scott Lembcke, Beoran, John Mair (banisterfiend)
@@ -9,12 +15,12 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-10-01 00:00:00 +13:00
18
+ date: 2010-07-25 00:00:00 +12:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
16
- description: ruby bindings for the chipmunk physics engine
17
- email: jrmair@gmail.com
22
+ description: ruby bindings for the chipmunk 5.1.0 physics engine
23
+ email: beoran@rubyforge.com
18
24
  executables: []
19
25
 
20
26
  extensions:
@@ -23,43 +29,71 @@ extra_rdoc_files: []
23
29
 
24
30
  files:
25
31
  - Rakefile
32
+ - README
33
+ - LICENSE
26
34
  - lib/chipmunk.rb
35
+ - lib/chipmunk/version.rb
27
36
  - ext/chipmunk/extconf.rb
37
+ - ext/chipmunk/include/chipmunk/chipmunk.h
38
+ - ext/chipmunk/include/chipmunk/chipmunk_ffi.h
39
+ - ext/chipmunk/include/chipmunk/chipmunk_types.h
40
+ - ext/chipmunk/include/chipmunk/chipmunk_unsafe.h
41
+ - ext/chipmunk/include/chipmunk/constraints/cpConstraint.h
42
+ - ext/chipmunk/include/chipmunk/constraints/cpDampedRotarySpring.h
43
+ - ext/chipmunk/include/chipmunk/constraints/cpDampedSpring.h
44
+ - ext/chipmunk/include/chipmunk/constraints/cpGearJoint.h
45
+ - ext/chipmunk/include/chipmunk/constraints/cpGrooveJoint.h
46
+ - ext/chipmunk/include/chipmunk/constraints/cpPinJoint.h
47
+ - ext/chipmunk/include/chipmunk/constraints/cpPivotJoint.h
48
+ - ext/chipmunk/include/chipmunk/constraints/cpRatchetJoint.h
49
+ - ext/chipmunk/include/chipmunk/constraints/cpRotaryLimitJoint.h
50
+ - ext/chipmunk/include/chipmunk/constraints/cpSimpleMotor.h
51
+ - ext/chipmunk/include/chipmunk/constraints/cpSlideJoint.h
52
+ - ext/chipmunk/include/chipmunk/constraints/util.h
53
+ - ext/chipmunk/include/chipmunk/cpArbiter.h
54
+ - ext/chipmunk/include/chipmunk/cpArray.h
55
+ - ext/chipmunk/include/chipmunk/cpBB.h
56
+ - ext/chipmunk/include/chipmunk/cpBody.h
57
+ - ext/chipmunk/include/chipmunk/cpCollision.h
58
+ - ext/chipmunk/include/chipmunk/cpHashSet.h
59
+ - ext/chipmunk/include/chipmunk/cpPolyShape.h
60
+ - ext/chipmunk/include/chipmunk/cpShape.h
61
+ - ext/chipmunk/include/chipmunk/cpSpace.h
62
+ - ext/chipmunk/include/chipmunk/cpSpaceHash.h
63
+ - ext/chipmunk/include/chipmunk/cpVect.h
28
64
  - ext/chipmunk/prime.h
29
- - ext/chipmunk/cpVect.h
30
- - ext/chipmunk/cpJoint.h
31
- - ext/chipmunk/cpBB.h
32
65
  - ext/chipmunk/rb_chipmunk.h
33
- - ext/chipmunk/cpBody.h
34
- - ext/chipmunk/chipmunk.h
35
- - ext/chipmunk/cpHashSet.h
36
- - ext/chipmunk/cpShape.h
37
- - ext/chipmunk/cpSpace.h
38
- - ext/chipmunk/cpPolyShape.h
39
- - ext/chipmunk/cpCollision.h
40
- - ext/chipmunk/cpArray.h
41
- - ext/chipmunk/cpSpaceHash.h
42
- - ext/chipmunk/cpArbiter.h
66
+ - ext/chipmunk/chipmunk.c
67
+ - ext/chipmunk/cpArbiter.c
68
+ - ext/chipmunk/cpArray.c
43
69
  - ext/chipmunk/cpBB.c
70
+ - ext/chipmunk/cpBody.c
44
71
  - ext/chipmunk/cpCollision.c
45
- - ext/chipmunk/rb_cpBody.c
46
- - ext/chipmunk/cpArbiter.c
47
- - ext/chipmunk/cpJoint.c
48
- - ext/chipmunk/chipmunk.c
72
+ - ext/chipmunk/cpConstraint.c
73
+ - ext/chipmunk/cpDampedRotarySpring.c
74
+ - ext/chipmunk/cpDampedSpring.c
75
+ - ext/chipmunk/cpGearJoint.c
76
+ - ext/chipmunk/cpGrooveJoint.c
49
77
  - ext/chipmunk/cpHashSet.c
50
- - ext/chipmunk/cpSpace.c
78
+ - ext/chipmunk/cpPinJoint.c
79
+ - ext/chipmunk/cpPivotJoint.c
80
+ - ext/chipmunk/cpPolyShape.c
81
+ - ext/chipmunk/cpRatchetJoint.c
82
+ - ext/chipmunk/cpRotaryLimitJoint.c
51
83
  - ext/chipmunk/cpShape.c
52
- - ext/chipmunk/cpBody.c
53
- - ext/chipmunk/rb_cpBB.c
54
- - ext/chipmunk/cpArray.c
55
- - ext/chipmunk/rb_cpJoint.c
56
- - ext/chipmunk/rb_cpVect.c
57
- - ext/chipmunk/rb_cpSpace.c
84
+ - ext/chipmunk/cpSimpleMotor.c
85
+ - ext/chipmunk/cpSlideJoint.c
86
+ - ext/chipmunk/cpSpace.c
58
87
  - ext/chipmunk/cpSpaceHash.c
59
88
  - ext/chipmunk/cpVect.c
60
- - ext/chipmunk/rb_cpShape.c
61
89
  - ext/chipmunk/rb_chipmunk.c
62
- - ext/chipmunk/cpPolyShape.c
90
+ - ext/chipmunk/rb_cpArbiter.c
91
+ - ext/chipmunk/rb_cpBB.c
92
+ - ext/chipmunk/rb_cpBody.c
93
+ - ext/chipmunk/rb_cpConstraint.c
94
+ - ext/chipmunk/rb_cpShape.c
95
+ - ext/chipmunk/rb_cpSpace.c
96
+ - ext/chipmunk/rb_cpVect.c
63
97
  has_rdoc: true
64
98
  homepage: http://code.google.com/p/chipmunk-physics/
65
99
  licenses: []
@@ -70,23 +104,29 @@ rdoc_options: []
70
104
  require_paths:
71
105
  - lib
72
106
  required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
73
108
  requirements:
74
109
  - - ">="
75
110
  - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
76
114
  version: "0"
77
- version:
78
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
79
117
  requirements:
80
118
  - - ">="
81
119
  - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
82
123
  version: "0"
83
- version:
84
124
  requirements: []
85
125
 
86
126
  rubyforge_project:
87
- rubygems_version: 1.3.5
127
+ rubygems_version: 1.3.7
88
128
  signing_key:
89
129
  specification_version: 3
90
- summary: ruby bindings for the chipmunk physics engine
130
+ summary: ruby bindings for the chipmunk 5.1.0 physics engine
91
131
  test_files: []
92
132
 
@@ -1,553 +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 <math.h>
24
-
25
- #include "chipmunk.h"
26
-
27
- // TODO: Comment me!
28
-
29
- cpFloat cp_joint_bias_coef = 0.1f;
30
-
31
- void cpJointDestroy(cpJoint *joint){}
32
-
33
- void
34
- cpJointFree(cpJoint *joint)
35
- {
36
- if(joint) cpJointDestroy(joint);
37
- free(joint);
38
- }
39
-
40
- static void
41
- cpJointInit(cpJoint *joint, const cpJointClass *klass, cpBody *a, cpBody *b)
42
- {
43
- joint->klass = klass;
44
- joint->a = a;
45
- joint->b = b;
46
- }
47
-
48
-
49
- static inline cpVect
50
- relative_velocity(cpVect r1, cpVect v1, cpFloat w1, cpVect r2, cpVect v2, cpFloat w2){
51
- cpVect v1_sum = cpvadd(v1, cpvmult(cpvperp(r1), w1));
52
- cpVect v2_sum = cpvadd(v2, cpvmult(cpvperp(r2), w2));
53
-
54
- return cpvsub(v2_sum, v1_sum);
55
- }
56
-
57
- static inline cpFloat
58
- scalar_k(cpBody *a, cpBody *b, cpVect r1, cpVect r2, cpVect n)
59
- {
60
- cpFloat mass_sum = a->m_inv + b->m_inv;
61
- cpFloat r1cn = cpvcross(r1, n);
62
- cpFloat r2cn = cpvcross(r2, n);
63
-
64
- return mass_sum + a->i_inv*r1cn*r1cn + b->i_inv*r2cn*r2cn;
65
- }
66
-
67
- static inline void
68
- apply_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
69
- {
70
- cpBodyApplyImpulse(a, cpvneg(j), r1);
71
- cpBodyApplyImpulse(b, j, r2);
72
- }
73
-
74
- static inline void
75
- apply_bias_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
76
- {
77
- cpBodyApplyBiasImpulse(a, cpvneg(j), r1);
78
- cpBodyApplyBiasImpulse(b, j, r2);
79
- }
80
-
81
-
82
- static void
83
- pinJointPreStep(cpJoint *joint, cpFloat dt_inv)
84
- {
85
- cpBody *a = joint->a;
86
- cpBody *b = joint->b;
87
- cpPinJoint *jnt = (cpPinJoint *)joint;
88
-
89
- jnt->r1 = cpvrotate(jnt->anchr1, a->rot);
90
- jnt->r2 = cpvrotate(jnt->anchr2, b->rot);
91
-
92
- cpVect delta = cpvsub(cpvadd(b->p, jnt->r2), cpvadd(a->p, jnt->r1));
93
- cpFloat dist = cpvlength(delta);
94
- jnt->n = cpvmult(delta, 1.0f/(dist ? dist : INFINITY));
95
-
96
- // calculate mass normal
97
- jnt->nMass = 1.0f/scalar_k(a, b, jnt->r1, jnt->r2, jnt->n);
98
-
99
- // calculate bias velocity
100
- jnt->bias = -cp_joint_bias_coef*dt_inv*(dist - jnt->dist);
101
- jnt->jBias = 0.0f;
102
-
103
- // apply accumulated impulse
104
- cpVect j = cpvmult(jnt->n, jnt->jnAcc);
105
- apply_impulses(a, b, jnt->r1, jnt->r2, j);
106
- }
107
-
108
- static void
109
- pinJointApplyImpulse(cpJoint *joint)
110
- {
111
- cpBody *a = joint->a;
112
- cpBody *b = joint->b;
113
-
114
- cpPinJoint *jnt = (cpPinJoint *)joint;
115
- cpVect n = jnt->n;
116
- cpVect r1 = jnt->r1;
117
- cpVect r2 = jnt->r2;
118
-
119
- //calculate bias impulse
120
- cpVect vbr = relative_velocity(r1, a->v_bias, a->w_bias, r2, b->v_bias, b->w_bias);
121
- cpFloat vbn = cpvdot(vbr, n);
122
-
123
- cpFloat jbn = (jnt->bias - vbn)*jnt->nMass;
124
- jnt->jBias += jbn;
125
-
126
- cpVect jb = cpvmult(n, jbn);
127
- apply_bias_impulses(a, b, jnt->r1, jnt->r2, jb);
128
-
129
- // compute relative velocity
130
- cpVect vr = relative_velocity(r1, a->v, a->w, r2, b->v, b->w);
131
- cpFloat vrn = cpvdot(vr, n);
132
-
133
- // compute normal impulse
134
- cpFloat jn = -vrn*jnt->nMass;
135
- jnt->jnAcc =+ jn;
136
-
137
- // apply impulse
138
- cpVect j = cpvmult(n, jn);
139
- apply_impulses(a, b, jnt->r1, jnt->r2, j);
140
- }
141
-
142
- static const cpJointClass pinJointClass = {
143
- CP_PIN_JOINT,
144
- pinJointPreStep,
145
- pinJointApplyImpulse,
146
- };
147
-
148
- cpPinJoint *
149
- cpPinJointAlloc(void)
150
- {
151
- return (cpPinJoint *)malloc(sizeof(cpPinJoint));
152
- }
153
-
154
- cpPinJoint *
155
- cpPinJointInit(cpPinJoint *joint, cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2)
156
- {
157
- cpJointInit((cpJoint *)joint, &pinJointClass, a, b);
158
-
159
- joint->anchr1 = anchr1;
160
- joint->anchr2 = anchr2;
161
-
162
- cpVect p1 = cpvadd(a->p, cpvrotate(anchr1, a->rot));
163
- cpVect p2 = cpvadd(b->p, cpvrotate(anchr2, b->rot));
164
- joint->dist = cpvlength(cpvsub(p2, p1));
165
-
166
- joint->jnAcc = 0.0;
167
-
168
- return joint;
169
- }
170
-
171
- cpJoint *
172
- cpPinJointNew(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2)
173
- {
174
- return (cpJoint *)cpPinJointInit(cpPinJointAlloc(), a, b, anchr1, anchr2);
175
- }
176
-
177
-
178
-
179
-
180
- static void
181
- slideJointPreStep(cpJoint *joint, cpFloat dt_inv)
182
- {
183
- cpBody *a = joint->a;
184
- cpBody *b = joint->b;
185
- cpSlideJoint *jnt = (cpSlideJoint *)joint;
186
-
187
- jnt->r1 = cpvrotate(jnt->anchr1, a->rot);
188
- jnt->r2 = cpvrotate(jnt->anchr2, b->rot);
189
-
190
- cpVect delta = cpvsub(cpvadd(b->p, jnt->r2), cpvadd(a->p, jnt->r1));
191
- cpFloat dist = cpvlength(delta);
192
- cpFloat pdist = 0.0;
193
- if(dist > jnt->max) {
194
- pdist = dist - jnt->max;
195
- } else if(dist < jnt->min) {
196
- pdist = jnt->min - dist;
197
- dist = -dist;
198
- }
199
- jnt->n = cpvmult(delta, 1.0f/(dist ? dist : INFINITY));
200
-
201
- // calculate mass normal
202
- jnt->nMass = 1.0f/scalar_k(a, b, jnt->r1, jnt->r2, jnt->n);
203
-
204
- // calculate bias velocity
205
- jnt->bias = -cp_joint_bias_coef*dt_inv*(pdist);
206
- jnt->jBias = 0.0f;
207
-
208
- // apply accumulated impulse
209
- if(!jnt->bias) //{
210
- // if bias is 0, then the joint is not at a limit.
211
- jnt->jnAcc = 0.0f;
212
- // } else {
213
- cpVect j = cpvmult(jnt->n, jnt->jnAcc);
214
- apply_impulses(a, b, jnt->r1, jnt->r2, j);
215
- // }
216
- }
217
-
218
- static void
219
- slideJointApplyImpulse(cpJoint *joint)
220
- {
221
- cpSlideJoint *jnt = (cpSlideJoint *)joint;
222
- if(!jnt->bias) return; // early exit
223
-
224
- cpBody *a = joint->a;
225
- cpBody *b = joint->b;
226
-
227
- cpVect n = jnt->n;
228
- cpVect r1 = jnt->r1;
229
- cpVect r2 = jnt->r2;
230
-
231
- //calculate bias impulse
232
- cpVect vbr = relative_velocity(r1, a->v_bias, a->w_bias, r2, b->v_bias, b->w_bias);
233
- cpFloat vbn = cpvdot(vbr, n);
234
-
235
- cpFloat jbn = (jnt->bias - vbn)*jnt->nMass;
236
- cpFloat jbnOld = jnt->jBias;
237
- jnt->jBias = cpfmin(jbnOld + jbn, 0.0f);
238
- jbn = jnt->jBias - jbnOld;
239
-
240
- cpVect jb = cpvmult(n, jbn);
241
- apply_bias_impulses(a, b, jnt->r1, jnt->r2, jb);
242
-
243
- // compute relative velocity
244
- cpVect vr = relative_velocity(r1, a->v, a->w, r2, b->v, b->w);
245
- cpFloat vrn = cpvdot(vr, n);
246
-
247
- // compute normal impulse
248
- cpFloat jn = -vrn*jnt->nMass;
249
- cpFloat jnOld = jnt->jnAcc;
250
- jnt->jnAcc = cpfmin(jnOld + jn, 0.0f);
251
- jn = jnt->jnAcc - jnOld;
252
-
253
- // apply impulse
254
- cpVect j = cpvmult(n, jn);
255
- apply_impulses(a, b, jnt->r1, jnt->r2, j);
256
- }
257
-
258
- static const cpJointClass slideJointClass = {
259
- CP_SLIDE_JOINT,
260
- slideJointPreStep,
261
- slideJointApplyImpulse,
262
- };
263
-
264
- cpSlideJoint *
265
- cpSlideJointAlloc(void)
266
- {
267
- return (cpSlideJoint *)malloc(sizeof(cpSlideJoint));
268
- }
269
-
270
- cpSlideJoint *
271
- cpSlideJointInit(cpSlideJoint *joint, cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat min, cpFloat max)
272
- {
273
- cpJointInit((cpJoint *)joint, &slideJointClass, a, b);
274
-
275
- joint->anchr1 = anchr1;
276
- joint->anchr2 = anchr2;
277
- joint->min = min;
278
- joint->max = max;
279
-
280
- joint->jnAcc = 0.0;
281
-
282
- return joint;
283
- }
284
-
285
- cpJoint *
286
- cpSlideJointNew(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat min, cpFloat max)
287
- {
288
- return (cpJoint *)cpSlideJointInit(cpSlideJointAlloc(), a, b, anchr1, anchr2, min, max);
289
- }
290
-
291
-
292
-
293
-
294
- static void
295
- pivotJointPreStep(cpJoint *joint, cpFloat dt_inv)
296
- {
297
- cpBody *a = joint->a;
298
- cpBody *b = joint->b;
299
- cpPivotJoint *jnt = (cpPivotJoint *)joint;
300
-
301
- jnt->r1 = cpvrotate(jnt->anchr1, a->rot);
302
- jnt->r2 = cpvrotate(jnt->anchr2, b->rot);
303
-
304
- // calculate mass matrix
305
- // If I wasn't lazy, this wouldn't be so gross...
306
- cpFloat k11, k12, k21, k22;
307
-
308
- cpFloat m_sum = a->m_inv + b->m_inv;
309
- k11 = m_sum; k12 = 0.0f;
310
- k21 = 0.0f; k22 = m_sum;
311
-
312
- cpFloat r1xsq = jnt->r1.x * jnt->r1.x * a->i_inv;
313
- cpFloat r1ysq = jnt->r1.y * jnt->r1.y * a->i_inv;
314
- cpFloat r1nxy = -jnt->r1.x * jnt->r1.y * a->i_inv;
315
- k11 += r1ysq; k12 += r1nxy;
316
- k21 += r1nxy; k22 += r1xsq;
317
-
318
- cpFloat r2xsq = jnt->r2.x * jnt->r2.x * b->i_inv;
319
- cpFloat r2ysq = jnt->r2.y * jnt->r2.y * b->i_inv;
320
- cpFloat r2nxy = -jnt->r2.x * jnt->r2.y * b->i_inv;
321
- k11 += r2ysq; k12 += r2nxy;
322
- k21 += r2nxy; k22 += r2xsq;
323
-
324
- cpFloat det_inv = 1.0f/(k11*k22 - k12*k21);
325
- jnt->k1 = cpv( k22*det_inv, -k12*det_inv);
326
- jnt->k2 = cpv(-k21*det_inv, k11*det_inv);
327
-
328
-
329
- // calculate bias velocity
330
- cpVect delta = cpvsub(cpvadd(b->p, jnt->r2), cpvadd(a->p, jnt->r1));
331
- jnt->bias = cpvmult(delta, -cp_joint_bias_coef*dt_inv);
332
- jnt->jBias = cpvzero;
333
-
334
- // apply accumulated impulse
335
- apply_impulses(a, b, jnt->r1, jnt->r2, jnt->jAcc);
336
- }
337
-
338
- static void
339
- pivotJointApplyImpulse(cpJoint *joint)
340
- {
341
- cpBody *a = joint->a;
342
- cpBody *b = joint->b;
343
-
344
- cpPivotJoint *jnt = (cpPivotJoint *)joint;
345
- cpVect r1 = jnt->r1;
346
- cpVect r2 = jnt->r2;
347
- cpVect k1 = jnt->k1;
348
- cpVect k2 = jnt->k2;
349
-
350
- //calculate bias impulse
351
- cpVect vbr = relative_velocity(r1, a->v_bias, a->w_bias, r2, b->v_bias, b->w_bias);
352
- vbr = cpvsub(jnt->bias, vbr);
353
-
354
- cpVect jb = cpv(cpvdot(vbr, k1), cpvdot(vbr, k2));
355
- jnt->jBias = cpvadd(jnt->jBias, jb);
356
-
357
- apply_bias_impulses(a, b, jnt->r1, jnt->r2, jb);
358
-
359
- // compute relative velocity
360
- cpVect vr = relative_velocity(r1, a->v, a->w, r2, b->v, b->w);
361
-
362
- // compute normal impulse
363
- cpVect j = cpv(-cpvdot(vr, k1), -cpvdot(vr, k2));
364
- jnt->jAcc = cpvadd(jnt->jAcc, j);
365
-
366
- // apply impulse
367
- apply_impulses(a, b, jnt->r1, jnt->r2, j);
368
- }
369
-
370
- static const cpJointClass pivotJointClass = {
371
- CP_PIVOT_JOINT,
372
- pivotJointPreStep,
373
- pivotJointApplyImpulse,
374
- };
375
-
376
- cpPivotJoint *
377
- cpPivotJointAlloc(void)
378
- {
379
- return (cpPivotJoint *)malloc(sizeof(cpPivotJoint));
380
- }
381
-
382
- cpPivotJoint *
383
- cpPivotJointInit(cpPivotJoint *joint, cpBody *a, cpBody *b, cpVect pivot)
384
- {
385
- cpJointInit((cpJoint *)joint, &pivotJointClass, a, b);
386
-
387
- joint->anchr1 = cpvunrotate(cpvsub(pivot, a->p), a->rot);
388
- joint->anchr2 = cpvunrotate(cpvsub(pivot, b->p), b->rot);
389
-
390
- joint->jAcc = cpvzero;
391
-
392
- return joint;
393
- }
394
-
395
- cpJoint *
396
- cpPivotJointNew(cpBody *a, cpBody *b, cpVect pivot)
397
- {
398
- return (cpJoint *)cpPivotJointInit(cpPivotJointAlloc(), a, b, pivot);
399
- }
400
-
401
-
402
-
403
-
404
- static void
405
- grooveJointPreStep(cpJoint *joint, cpFloat dt_inv)
406
- {
407
- cpBody *a = joint->a;
408
- cpBody *b = joint->b;
409
- cpGrooveJoint *jnt = (cpGrooveJoint *)joint;
410
-
411
- // calculate endpoints in worldspace
412
- cpVect ta = cpBodyLocal2World(a, jnt->grv_a);
413
- cpVect tb = cpBodyLocal2World(a, jnt->grv_b);
414
-
415
- // calculate axis
416
- cpVect n = cpvrotate(jnt->grv_n, a->rot);
417
- cpFloat d = cpvdot(ta, n);
418
-
419
- jnt->grv_tn = n;
420
- jnt->r2 = cpvrotate(jnt->anchr2, b->rot);
421
-
422
- // calculate tangential distance along the axis of r2
423
- cpFloat td = cpvcross(cpvadd(b->p, jnt->r2), n);
424
- // calculate clamping factor and r2
425
- if(td <= cpvcross(ta, n)){
426
- jnt->clamp = 1.0f;
427
- jnt->r1 = cpvsub(ta, a->p);
428
- } else if(td >= cpvcross(tb, n)){
429
- jnt->clamp = -1.0f;
430
- jnt->r1 = cpvsub(tb, a->p);
431
- } else {
432
- jnt->clamp = 0.0f;
433
- jnt->r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a->p);
434
- }
435
-
436
- // calculate mass matrix
437
- // If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
438
- cpFloat k11, k12, k21, k22;
439
- cpFloat m_sum = a->m_inv + b->m_inv;
440
-
441
- // start with I*m_sum
442
- k11 = m_sum; k12 = 0.0f;
443
- k21 = 0.0f; k22 = m_sum;
444
-
445
- // add the influence from r1
446
- cpFloat r1xsq = jnt->r1.x * jnt->r1.x * a->i_inv;
447
- cpFloat r1ysq = jnt->r1.y * jnt->r1.y * a->i_inv;
448
- cpFloat r1nxy = -jnt->r1.x * jnt->r1.y * a->i_inv;
449
- k11 += r1ysq; k12 += r1nxy;
450
- k21 += r1nxy; k22 += r1xsq;
451
-
452
- // add the influnce from r2
453
- cpFloat r2xsq = jnt->r2.x * jnt->r2.x * b->i_inv;
454
- cpFloat r2ysq = jnt->r2.y * jnt->r2.y * b->i_inv;
455
- cpFloat r2nxy = -jnt->r2.x * jnt->r2.y * b->i_inv;
456
- k11 += r2ysq; k12 += r2nxy;
457
- k21 += r2nxy; k22 += r2xsq;
458
-
459
- // invert
460
- cpFloat det_inv = 1.0f/(k11*k22 - k12*k21);
461
- jnt->k1 = cpv( k22*det_inv, -k12*det_inv);
462
- jnt->k2 = cpv(-k21*det_inv, k11*det_inv);
463
-
464
-
465
- // calculate bias velocity
466
- cpVect delta = cpvsub(cpvadd(b->p, jnt->r2), cpvadd(a->p, jnt->r1));
467
- jnt->bias = cpvmult(delta, -cp_joint_bias_coef*dt_inv);
468
- jnt->jBias = cpvzero;
469
-
470
- // apply accumulated impulse
471
- apply_impulses(a, b, jnt->r1, jnt->r2, jnt->jAcc);
472
- }
473
-
474
- static inline cpVect
475
- grooveConstrain(cpGrooveJoint *jnt, cpVect j){
476
- cpVect n = jnt->grv_tn;
477
- cpVect jn = cpvmult(n, cpvdot(j, n));
478
-
479
- cpVect t = cpvperp(n);
480
- cpFloat coef = (jnt->clamp*cpvcross(j, n) > 0.0f) ? 1.0f : 0.0f;
481
- cpVect jt = cpvmult(t, cpvdot(j, t)*coef);
482
-
483
- return cpvadd(jn, jt);
484
- }
485
-
486
- static void
487
- grooveJointApplyImpulse(cpJoint *joint)
488
- {
489
- cpBody *a = joint->a;
490
- cpBody *b = joint->b;
491
-
492
- cpGrooveJoint *jnt = (cpGrooveJoint *)joint;
493
- cpVect r1 = jnt->r1;
494
- cpVect r2 = jnt->r2;
495
- cpVect k1 = jnt->k1;
496
- cpVect k2 = jnt->k2;
497
-
498
- //calculate bias impulse
499
- cpVect vbr = relative_velocity(r1, a->v_bias, a->w_bias, r2, b->v_bias, b->w_bias);
500
- vbr = cpvsub(jnt->bias, vbr);
501
-
502
- cpVect jb = cpv(cpvdot(vbr, k1), cpvdot(vbr, k2));
503
- cpVect jbOld = jnt->jBias;
504
- jnt->jBias = grooveConstrain(jnt, cpvadd(jbOld, jb));
505
- jb = cpvsub(jnt->jBias, jbOld);
506
-
507
- apply_bias_impulses(a, b, jnt->r1, jnt->r2, jb);
508
-
509
- // compute impulse
510
- cpVect vr = relative_velocity(r1, a->v, a->w, r2, b->v, b->w);
511
-
512
- cpVect j = cpv(-cpvdot(vr, k1), -cpvdot(vr, k2));
513
- cpVect jOld = jnt->jAcc;
514
- jnt->jAcc = grooveConstrain(jnt, cpvadd(jOld, j));
515
- j = cpvsub(jnt->jAcc, jOld);
516
-
517
- // apply impulse
518
- apply_impulses(a, b, jnt->r1, jnt->r2, j);
519
- }
520
-
521
- static const cpJointClass grooveJointClass = {
522
- CP_GROOVE_JOINT,
523
- grooveJointPreStep,
524
- grooveJointApplyImpulse,
525
- };
526
-
527
- cpGrooveJoint *
528
- cpGrooveJointAlloc(void)
529
- {
530
- return (cpGrooveJoint *)malloc(sizeof(cpGrooveJoint));
531
- }
532
-
533
- cpGrooveJoint *
534
- cpGrooveJointInit(cpGrooveJoint *joint, cpBody *a, cpBody *b, cpVect groove_a, cpVect groove_b, cpVect anchr2)
535
- {
536
- cpJointInit((cpJoint *)joint, &grooveJointClass, a, b);
537
-
538
- joint->grv_a = groove_a;
539
- joint->grv_b = groove_b;
540
- joint->grv_n = cpvperp(cpvnormalize(cpvsub(groove_b, groove_a)));
541
- joint->anchr2 = anchr2;
542
-
543
- joint->jAcc = cpvzero;
544
-
545
- return joint;
546
- }
547
-
548
- cpJoint *
549
- cpGrooveJointNew(cpBody *a, cpBody *b, cpVect groove_a, cpVect groove_b, cpVect anchr2)
550
- {
551
- return (cpJoint *)cpGrooveJointInit(cpGrooveJointAlloc(), a, b, groove_a, groove_b, anchr2);
552
- }
553
-