chipmunk 5.2.2 → 5.3.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/LICENSE +21 -19
  2. data/README +67 -60
  3. data/Rakefile +85 -50
  4. data/ext/chipmunk/extconf.rb +42 -35
  5. data/ext/chipmunk/rb_chipmunk.c +250 -125
  6. data/ext/chipmunk/rb_chipmunk.h +105 -96
  7. data/ext/chipmunk/rb_cpArbiter.c +253 -225
  8. data/ext/chipmunk/rb_cpBB.c +210 -174
  9. data/ext/chipmunk/rb_cpBody.c +504 -347
  10. data/ext/chipmunk/rb_cpConstraint.c +336 -346
  11. data/ext/chipmunk/rb_cpShape.c +433 -455
  12. data/ext/chipmunk/rb_cpSpace.c +711 -544
  13. data/ext/chipmunk/rb_cpVect.c +346 -321
  14. data/lib/chipmunk.rb +183 -30
  15. metadata +12 -66
  16. data/ext/chipmunk/chipmunk.c +0 -105
  17. data/ext/chipmunk/cpArbiter.c +0 -274
  18. data/ext/chipmunk/cpArray.c +0 -128
  19. data/ext/chipmunk/cpBB.c +0 -47
  20. data/ext/chipmunk/cpBody.c +0 -188
  21. data/ext/chipmunk/cpCollision.c +0 -391
  22. data/ext/chipmunk/cpConstraint.c +0 -54
  23. data/ext/chipmunk/cpDampedRotarySpring.c +0 -106
  24. data/ext/chipmunk/cpDampedSpring.c +0 -117
  25. data/ext/chipmunk/cpGearJoint.c +0 -114
  26. data/ext/chipmunk/cpGrooveJoint.c +0 -138
  27. data/ext/chipmunk/cpHashSet.c +0 -253
  28. data/ext/chipmunk/cpPinJoint.c +0 -117
  29. data/ext/chipmunk/cpPivotJoint.c +0 -114
  30. data/ext/chipmunk/cpPolyShape.c +0 -241
  31. data/ext/chipmunk/cpRatchetJoint.c +0 -128
  32. data/ext/chipmunk/cpRotaryLimitJoint.c +0 -122
  33. data/ext/chipmunk/cpShape.c +0 -400
  34. data/ext/chipmunk/cpSimpleMotor.c +0 -99
  35. data/ext/chipmunk/cpSlideJoint.c +0 -131
  36. data/ext/chipmunk/cpSpace.c +0 -899
  37. data/ext/chipmunk/cpSpaceHash.c +0 -541
  38. data/ext/chipmunk/cpVect.c +0 -71
  39. data/ext/chipmunk/include/chipmunk/chipmunk.h +0 -148
  40. data/ext/chipmunk/include/chipmunk/chipmunk_ffi.h +0 -42
  41. data/ext/chipmunk/include/chipmunk/chipmunk_types.h +0 -80
  42. data/ext/chipmunk/include/chipmunk/chipmunk_unsafe.h +0 -54
  43. data/ext/chipmunk/include/chipmunk/constraints/cpConstraint.h +0 -92
  44. data/ext/chipmunk/include/chipmunk/constraints/cpDampedRotarySpring.h +0 -46
  45. data/ext/chipmunk/include/chipmunk/constraints/cpDampedSpring.h +0 -53
  46. data/ext/chipmunk/include/chipmunk/constraints/cpGearJoint.h +0 -41
  47. data/ext/chipmunk/include/chipmunk/constraints/cpGrooveJoint.h +0 -44
  48. data/ext/chipmunk/include/chipmunk/constraints/cpPinJoint.h +0 -43
  49. data/ext/chipmunk/include/chipmunk/constraints/cpPivotJoint.h +0 -42
  50. data/ext/chipmunk/include/chipmunk/constraints/cpRatchetJoint.h +0 -40
  51. data/ext/chipmunk/include/chipmunk/constraints/cpRotaryLimitJoint.h +0 -39
  52. data/ext/chipmunk/include/chipmunk/constraints/cpSimpleMotor.h +0 -37
  53. data/ext/chipmunk/include/chipmunk/constraints/cpSlideJoint.h +0 -44
  54. data/ext/chipmunk/include/chipmunk/constraints/util.h +0 -116
  55. data/ext/chipmunk/include/chipmunk/cpArbiter.h +0 -136
  56. data/ext/chipmunk/include/chipmunk/cpArray.h +0 -46
  57. data/ext/chipmunk/include/chipmunk/cpBB.h +0 -74
  58. data/ext/chipmunk/include/chipmunk/cpBody.h +0 -160
  59. data/ext/chipmunk/include/chipmunk/cpCollision.h +0 -23
  60. data/ext/chipmunk/include/chipmunk/cpHashSet.h +0 -82
  61. data/ext/chipmunk/include/chipmunk/cpPolyShape.h +0 -103
  62. data/ext/chipmunk/include/chipmunk/cpShape.h +0 -174
  63. data/ext/chipmunk/include/chipmunk/cpSpace.h +0 -180
  64. data/ext/chipmunk/include/chipmunk/cpSpaceHash.h +0 -109
  65. data/ext/chipmunk/include/chipmunk/cpVect.h +0 -157
  66. data/ext/chipmunk/prime.h +0 -68
  67. data/lib/chipmunk/version.rb +0 -3
data/lib/chipmunk.rb CHANGED
@@ -1,30 +1,183 @@
1
- # this redirection script by John Mair (banisterfiend)
2
-
3
- direc = File.dirname(__FILE__)
4
-
5
- require 'rbconfig'
6
- require "#{direc}/chipmunk/version"
7
-
8
- dlext = Config::CONFIG['DLEXT']
9
- begin
10
- if RUBY_VERSION && RUBY_VERSION =~ /1.9/
11
- require "#{direc}/1.9/chipmunk.#{dlext}"
12
- else
13
- require "#{direc}/1.8/chipmunk.#{dlext}"
14
- end
15
- rescue LoadError => e
16
- require "#{direc}/chipmunk.#{dlext}"
17
- end
18
-
19
- # Add some constants to CP here, so we don't need
20
- # to do it in the C code.
21
- # Let's cheat a bit here.. :p
22
-
23
- module CP
24
- ZERO_VEC_2 = Vec2.new(0,0).freeze
25
- ALL_ONES = Vec2.new(1,1).freeze
26
- end
27
-
28
-
29
-
30
-
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
+ VERSION = '5.4.3'
23
+ ZERO_VEC_2 = Vec2.new(0,0).freeze
24
+ ALL_ONES = Vec2.new(1,1).freeze
25
+ end
26
+
27
+ # Extra functionality added by Slembkce and Beoran.
28
+
29
+ module CP
30
+ # Chipmunk Object
31
+ # Makes it easier to manage complex objects that reference many primitive Chipmunk objects such as bodies shapes and constraints.
32
+ # New composite objects simply need to include CP::Object and call #init_chipmunk_object(*objects) with the
33
+ # composite and primitive Chipmunk objects that make up itself.
34
+ module Object
35
+ # Returns the list of primitive Chipmunk objects (bodies, shapes and constraints)
36
+ # that this composite object references directly and indirectly.
37
+ def chipmunk_objects
38
+ if @chipmunk_objects
39
+ return @chipmunk_objects
40
+ else
41
+ raise "This CP::Object (#{self.class}) did not call #init_chipmunk_object."
42
+ end
43
+ end
44
+
45
+ private
46
+ # Should be called during initialization of a CP::Object to set what primitive
47
+ # and composite Chipmunk objects this object references.
48
+ def init_chipmunk_object(*objs)
49
+ bad_objs = objs.reject{|obj| obj.is_a?(CP::Object)}
50
+ raise(ArgumentError, "The following objects: #{bad_objs.inspect} are not CP::Objects") unless bad_objs.empty?
51
+
52
+ @chipmunk_objects = objs.inject([]){|sum, obj| sum + obj.chipmunk_objects}.uniq
53
+ end
54
+ end
55
+
56
+ class Body
57
+ include CP::Object
58
+
59
+ def chipmunk_objects
60
+ [self]
61
+ end
62
+
63
+ def add_to_space(space)
64
+ space.add_body(self)
65
+ end
66
+
67
+ def remove_from_space(space)
68
+ space.remove_body(self)
69
+ end
70
+
71
+ end
72
+
73
+ module Shape
74
+ include CP::Object
75
+
76
+ def chipmunk_objects
77
+ [self]
78
+ end
79
+
80
+ def add_to_space(space)
81
+ space.add_shape(self)
82
+ end
83
+
84
+ def remove_from_space(space)
85
+ space.remove_shape(self)
86
+ end
87
+ end
88
+
89
+ module Constraint
90
+ include CP::Object
91
+
92
+ def chipmunk_objects
93
+ [self]
94
+ end
95
+
96
+ def add_to_space(space)
97
+ space.add_constraint(self)
98
+ end
99
+
100
+ def remove_from_space(space)
101
+ space.remove_constraint(self)
102
+ end
103
+ end
104
+
105
+ class Space
106
+ def add_object(obj)
107
+ obj.chipmunk_objects.each{|elt| elt.add_to_space(self)}
108
+ end
109
+
110
+ def add_objects(*objs)
111
+ objs.each{|obj| add_object(obj)}
112
+ end
113
+
114
+ def remove_object(obj)
115
+ obj.chipmunk_objects.each{|elt| elt.remove_from_space(self)}
116
+ end
117
+
118
+ def remove_objects(*objs)
119
+ objs.each{|obj| remove_object(obj)}
120
+ end
121
+
122
+ end
123
+
124
+ class Vec2
125
+ ZERO = Vec2.new(0,0).freeze
126
+ end
127
+
128
+ # define the helpers here( easier than in the extension.
129
+ class SegmentQueryInfo
130
+ def hit_point(start, stop)
131
+ return start.lerp(stop, self.t)
132
+ end
133
+
134
+ def hit_dist(start, stop)
135
+ return start.dist(stop) * self.t
136
+ end
137
+ end
138
+
139
+
140
+ end
141
+
142
+
143
+ # Create derived static objects that know to add themselves as static.
144
+ module CP
145
+ class StaticBody < Body
146
+ def initialize
147
+ super(Float::INFINITY, Float::INFINITY)
148
+ end
149
+
150
+ def chipmunk_objects
151
+ # return [] instead of [self] so the static body will not be added.
152
+ []
153
+ end
154
+ end
155
+
156
+ module StaticShape
157
+ include Shape
158
+
159
+ class Circle < Shape::Circle
160
+ include StaticShape
161
+ end
162
+
163
+ class Segment < Shape::Segment
164
+ include StaticShape
165
+ end
166
+
167
+ class Poly < Shape::Poly
168
+ include StaticShape
169
+ end
170
+
171
+ def add_to_space(space)
172
+ space.add_static_shape(self)
173
+ end
174
+
175
+ def remove_from_space(space)
176
+ space.remove_static_shape(self)
177
+ end
178
+ end
179
+ end
180
+
181
+
182
+
183
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chipmunk
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
5
4
  prerelease: false
6
5
  segments:
7
6
  - 5
8
- - 2
9
- - 2
10
- version: 5.2.2
7
+ - 3
8
+ - 4
9
+ - 0
10
+ version: 5.3.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Scott Lembcke, Beoran, John Mair (banisterfiend)
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-25 00:00:00 +12:00
18
+ date: 2011-05-01 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: ruby bindings for the chipmunk 5.1.0 physics engine
22
+ description: "Enhanced ruby bindings for the chipmunk 5.3.4 game physics engine. "
23
23
  email: beoran@rubyforge.com
24
24
  executables: []
25
25
 
@@ -32,70 +32,18 @@ files:
32
32
  - README
33
33
  - LICENSE
34
34
  - lib/chipmunk.rb
35
- - lib/chipmunk/version.rb
36
35
  - 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
64
- - ext/chipmunk/prime.h
65
36
  - ext/chipmunk/rb_chipmunk.h
66
- - ext/chipmunk/chipmunk.c
67
- - ext/chipmunk/cpArbiter.c
68
- - ext/chipmunk/cpArray.c
69
- - ext/chipmunk/cpBB.c
70
- - ext/chipmunk/cpBody.c
71
- - ext/chipmunk/cpCollision.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
77
- - ext/chipmunk/cpHashSet.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
83
- - ext/chipmunk/cpShape.c
84
- - ext/chipmunk/cpSimpleMotor.c
85
- - ext/chipmunk/cpSlideJoint.c
86
- - ext/chipmunk/cpSpace.c
87
- - ext/chipmunk/cpSpaceHash.c
88
- - ext/chipmunk/cpVect.c
89
37
  - ext/chipmunk/rb_chipmunk.c
90
- - ext/chipmunk/rb_cpArbiter.c
91
- - ext/chipmunk/rb_cpBB.c
92
38
  - ext/chipmunk/rb_cpBody.c
93
- - ext/chipmunk/rb_cpConstraint.c
39
+ - ext/chipmunk/rb_cpBB.c
94
40
  - ext/chipmunk/rb_cpShape.c
95
- - ext/chipmunk/rb_cpSpace.c
41
+ - ext/chipmunk/rb_cpConstraint.c
96
42
  - ext/chipmunk/rb_cpVect.c
43
+ - ext/chipmunk/rb_cpArbiter.c
44
+ - ext/chipmunk/rb_cpSpace.c
97
45
  has_rdoc: true
98
- homepage: http://code.google.com/p/chipmunk-physics/
46
+ homepage: https://github.com/beoran/chipmunk
99
47
  licenses: []
100
48
 
101
49
  post_install_message:
@@ -108,7 +56,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
56
  requirements:
109
57
  - - ">="
110
58
  - !ruby/object:Gem::Version
111
- hash: 3
112
59
  segments:
113
60
  - 0
114
61
  version: "0"
@@ -117,7 +64,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
64
  requirements:
118
65
  - - ">="
119
66
  - !ruby/object:Gem::Version
120
- hash: 3
121
67
  segments:
122
68
  - 0
123
69
  version: "0"
@@ -127,6 +73,6 @@ rubyforge_project:
127
73
  rubygems_version: 1.3.7
128
74
  signing_key:
129
75
  specification_version: 3
130
- summary: ruby bindings for the chipmunk 5.1.0 physics engine
76
+ summary: Enhanced ruby bindings for the chipmunk 5.3.4 game physics engine.
131
77
  test_files: []
132
78
 
@@ -1,105 +0,0 @@
1
- /* Copyright (c) 2007 Scott Lembcke
2
- *
3
- * Permission is hereby granted, free of charge, to any person obtaining a copy
4
- * of this software and associated documentation files (the "Software"), to deal
5
- * in the Software without restriction, including without limitation the rights
6
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- * copies of the Software, and to permit persons to whom the Software is
8
- * furnished to do so, subject to the following conditions:
9
- *
10
- * The above copyright notice and this permission notice shall be included in
11
- * all copies or substantial portions of the Software.
12
- *
13
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- * SOFTWARE.
20
- */
21
-
22
- #include <stdlib.h>
23
- #include <stdio.h>
24
-
25
- #include "chipmunk.h"
26
-
27
- #ifdef __cplusplus
28
- extern "C" {
29
- #endif
30
- void cpInitCollisionFuncs(void);
31
- #ifdef __cplusplus
32
- }
33
- #endif
34
-
35
- void
36
- cpMessage(char *message, char *condition, char *file, int line, int isError)
37
- {
38
- fprintf(stderr, (isError ? "Aborting due to Chipmunk error: %s\n" : "Chipmunk warning: %s\n"), message);
39
- fprintf(stderr, "\tFailed condition: %s\n", condition);
40
- fprintf(stderr, "\tSource:%s:%d\n", file, line);
41
-
42
- if(isError) abort();
43
- }
44
-
45
-
46
- char *cpVersionString = "5.x.x";
47
-
48
- void
49
- cpInitChipmunk(void)
50
- {
51
- #ifndef NDEBUG
52
- printf("Initializing Chipmunk v%s (Debug Enabled)\n", cpVersionString);
53
- printf("Compile with -DNDEBUG defined to disable debug mode and runtime assertion checks\n");
54
- #endif
55
-
56
- cpInitCollisionFuncs();
57
- }
58
-
59
- cpFloat
60
- cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset)
61
- {
62
- return (1.0f/2.0f)*m*(r1*r1 + r2*r2) + m*cpvdot(offset, offset);
63
- }
64
-
65
- cpFloat
66
- cpMomentForSegment(cpFloat m, cpVect a, cpVect b)
67
- {
68
- cpFloat length = cpvlength(cpvsub(b, a));
69
- cpVect offset = cpvmult(cpvadd(a, b), 1.0f/2.0f);
70
-
71
- return m*length*length/12.0f + m*cpvdot(offset, offset);
72
- }
73
-
74
- cpFloat
75
- cpMomentForPoly(cpFloat m, const int numVerts, cpVect *verts, cpVect offset)
76
- {
77
- cpVect *tVerts = (cpVect *)cpcalloc(numVerts, sizeof(cpVect));
78
- for(int i=0; i<numVerts; i++)
79
- tVerts[i] = cpvadd(verts[i], offset);
80
-
81
- cpFloat sum1 = 0.0f;
82
- cpFloat sum2 = 0.0f;
83
- for(int i=0; i<numVerts; i++){
84
- cpVect v1 = tVerts[i];
85
- cpVect v2 = tVerts[(i+1)%numVerts];
86
-
87
- cpFloat a = cpvcross(v2, v1);
88
- cpFloat b = cpvdot(v1, v1) + cpvdot(v1, v2) + cpvdot(v2, v2);
89
-
90
- sum1 += a*b;
91
- sum2 += a;
92
- }
93
-
94
- cpfree(tVerts);
95
- return (m*sum1)/(6.0f*sum2);
96
- }
97
-
98
- cpFloat
99
- cpMomentForBox(cpFloat m, cpFloat width, cpFloat height)
100
- {
101
- return m*(width*width + height*height)/12.0;
102
- }
103
-
104
-
105
- #include "chipmunk_ffi.h"
@@ -1,274 +0,0 @@
1
- /* Copyright (c) 2007 Scott Lembcke
2
- *
3
- * Permission is hereby granted, free of charge, to any person obtaining a copy
4
- * of this software and associated documentation files (the "Software"), to deal
5
- * in the Software without restriction, including without limitation the rights
6
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- * copies of the Software, and to permit persons to whom the Software is
8
- * furnished to do so, subject to the following conditions:
9
- *
10
- * The above copyright notice and this permission notice shall be included in
11
- * all copies or substantial portions of the Software.
12
- *
13
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- * SOFTWARE.
20
- */
21
-
22
- #include <stdlib.h>
23
-
24
- #include "chipmunk.h"
25
- #include "constraints/util.h"
26
-
27
- cpFloat cp_bias_coef = 0.1f;
28
- cpFloat cp_collision_slop = 0.1f;
29
-
30
- cpContact*
31
- cpContactInit(cpContact *con, cpVect p, cpVect n, cpFloat dist, cpHashValue hash)
32
- {
33
- con->p = p;
34
- con->n = n;
35
- con->dist = dist;
36
-
37
- con->jnAcc = 0.0f;
38
- con->jtAcc = 0.0f;
39
- con->jBias = 0.0f;
40
-
41
- con->hash = hash;
42
-
43
- return con;
44
- }
45
-
46
- cpVect
47
- cpArbiterTotalImpulse(cpArbiter *arb)
48
- {
49
- cpContact *contacts = arb->contacts;
50
- cpVect sum = cpvzero;
51
-
52
- for(int i=0, count=arb->numContacts; i<count; i++){
53
- cpContact *con = &contacts[i];
54
- sum = cpvadd(sum, cpvmult(con->n, con->jnAcc));
55
- }
56
-
57
- return sum;
58
- }
59
-
60
- cpVect
61
- cpArbiterTotalImpulseWithFriction(cpArbiter *arb)
62
- {
63
- cpContact *contacts = arb->contacts;
64
- cpVect sum = cpvzero;
65
-
66
- for(int i=0, count=arb->numContacts; i<count; i++){
67
- cpContact *con = &contacts[i];
68
- sum = cpvadd(sum, cpvrotate(con->n, cpv(con->jnAcc, con->jtAcc)));
69
- }
70
-
71
- return sum;
72
- }
73
-
74
- cpFloat
75
- cpContactsEstimateCrushingImpulse(cpContact *contacts, int numContacts)
76
- {
77
- cpFloat fsum = 0.0f;
78
- cpVect vsum = cpvzero;
79
-
80
- for(int i=0; i<numContacts; i++){
81
- cpContact *con = &contacts[i];
82
- cpVect j = cpvrotate(con->n, cpv(con->jnAcc, con->jtAcc));
83
-
84
- fsum += cpvlength(j);
85
- vsum = cpvadd(vsum, j);
86
- }
87
-
88
- cpFloat vmag = cpvlength(vsum);
89
- return (1.0f - vmag/fsum);
90
- }
91
-
92
- void
93
- cpArbiterIgnore(cpArbiter *arb)
94
- {
95
- arb->state = cpArbiterStateIgnore ;
96
- }
97
-
98
- cpArbiter*
99
- cpArbiterAlloc(void)
100
- {
101
- return (cpArbiter *)cpcalloc(1, sizeof(cpArbiter));
102
- }
103
-
104
- cpArbiter*
105
- cpArbiterInit(cpArbiter *arb, cpShape *a, cpShape *b)
106
- {
107
- arb->numContacts = 0;
108
- arb->contacts = NULL;
109
-
110
- arb->private_a = a;
111
- arb->private_b = b;
112
-
113
- arb->stamp = -1;
114
- arb->state = cpArbiterStateFirstColl;
115
-
116
- return arb;
117
- }
118
-
119
- cpArbiter*
120
- cpArbiterNew(cpShape *a, cpShape *b)
121
- {
122
- return cpArbiterInit(cpArbiterAlloc(), a, b);
123
- }
124
-
125
- void
126
- cpArbiterDestroy(cpArbiter *arb)
127
- {
128
- // if(arb->contacts) cpfree(arb->contacts);
129
- }
130
-
131
- void
132
- cpArbiterFree(cpArbiter *arb)
133
- {
134
- if(arb){
135
- cpArbiterDestroy(arb);
136
- cpfree(arb);
137
- }
138
- }
139
-
140
- void
141
- cpArbiterUpdate(cpArbiter *arb, cpContact *contacts, int numContacts, cpCollisionHandler *handler, cpShape *a, cpShape *b)
142
- {
143
- // Arbiters without contact data may exist if a collision function rejected the collision.
144
- if(arb->contacts){
145
- // Iterate over the possible pairs to look for hash value matches.
146
- for(int i=0; i<arb->numContacts; i++){
147
- cpContact *old = &arb->contacts[i];
148
-
149
- for(int j=0; j<numContacts; j++){
150
- cpContact *new_contact = &contacts[j];
151
-
152
- // This could trigger false positives, but is fairly unlikely nor serious if it does.
153
- if(new_contact->hash == old->hash){
154
- // Copy the persistant contact information.
155
- new_contact->jnAcc = old->jnAcc;
156
- new_contact->jtAcc = old->jtAcc;
157
- }
158
- }
159
- }
160
-
161
- // cpfree(arb->contacts);
162
- }
163
-
164
- arb->contacts = contacts;
165
- arb->numContacts = numContacts;
166
-
167
- arb->handler = handler;
168
- arb->swappedColl = (a->collision_type != handler->a);
169
-
170
- arb->e = a->e * b->e;
171
- arb->u = a->u * b->u;
172
- arb->surface_vr = cpvsub(a->surface_v, b->surface_v);
173
-
174
- // For collisions between two similar primitive types, the order could have been swapped.
175
- arb->private_a = a; arb->private_b = b;
176
- }
177
-
178
- void
179
- cpArbiterPreStep(cpArbiter *arb, cpFloat dt_inv)
180
- {
181
- cpShape *shapea = arb->private_a;
182
- cpShape *shapeb = arb->private_b;
183
-
184
- cpBody *a = shapea->body;
185
- cpBody *b = shapeb->body;
186
-
187
- for(int i=0; i<arb->numContacts; i++){
188
- cpContact *con = &arb->contacts[i];
189
-
190
- // Calculate the offsets.
191
- con->r1 = cpvsub(con->p, a->p);
192
- con->r2 = cpvsub(con->p, b->p);
193
-
194
- // Calculate the mass normal and mass tangent.
195
- con->nMass = 1.0f/k_scalar(a, b, con->r1, con->r2, con->n);
196
- con->tMass = 1.0f/k_scalar(a, b, con->r1, con->r2, cpvperp(con->n));
197
-
198
- // Calculate the target bias velocity.
199
- con->bias = -cp_bias_coef*dt_inv*cpfmin(0.0f, con->dist + cp_collision_slop);
200
- con->jBias = 0.0f;
201
-
202
- // Calculate the target bounce velocity.
203
- con->bounce = normal_relative_velocity(a, b, con->r1, con->r2, con->n)*arb->e;//cpvdot(con->n, cpvsub(v2, v1))*e;
204
- }
205
- }
206
-
207
- void
208
- cpArbiterApplyCachedImpulse(cpArbiter *arb)
209
- {
210
- cpShape *shapea = arb->private_a;
211
- cpShape *shapeb = arb->private_b;
212
-
213
- arb->u = shapea->u * shapeb->u;
214
- arb->surface_vr = cpvsub(shapeb->surface_v, shapea->surface_v);
215
-
216
- cpBody *a = shapea->body;
217
- cpBody *b = shapeb->body;
218
-
219
- for(int i=0; i<arb->numContacts; i++){
220
- cpContact *con = &arb->contacts[i];
221
- apply_impulses(a, b, con->r1, con->r2, cpvrotate(con->n, cpv(con->jnAcc, con->jtAcc)));
222
- }
223
- }
224
-
225
- void
226
- cpArbiterApplyImpulse(cpArbiter *arb, cpFloat eCoef)
227
- {
228
- cpBody *a = arb->private_a->body;
229
- cpBody *b = arb->private_b->body;
230
-
231
- for(int i=0; i<arb->numContacts; i++){
232
- cpContact *con = &arb->contacts[i];
233
- cpVect n = con->n;
234
- cpVect r1 = con->r1;
235
- cpVect r2 = con->r2;
236
-
237
- // Calculate the relative bias velocities.
238
- cpVect vb1 = cpvadd(a->v_bias, cpvmult(cpvperp(r1), a->w_bias));
239
- cpVect vb2 = cpvadd(b->v_bias, cpvmult(cpvperp(r2), b->w_bias));
240
- cpFloat vbn = cpvdot(cpvsub(vb2, vb1), n);
241
-
242
- // Calculate and clamp the bias impulse.
243
- cpFloat jbn = (con->bias - vbn)*con->nMass;
244
- cpFloat jbnOld = con->jBias;
245
- con->jBias = cpfmax(jbnOld + jbn, 0.0f);
246
- jbn = con->jBias - jbnOld;
247
-
248
- // Apply the bias impulse.
249
- apply_bias_impulses(a, b, r1, r2, cpvmult(n, jbn));
250
-
251
- // Calculate the relative velocity.
252
- cpVect vr = relative_velocity(a, b, r1, r2);
253
- cpFloat vrn = cpvdot(vr, n);
254
-
255
- // Calculate and clamp the normal impulse.
256
- cpFloat jn = -(con->bounce*eCoef + vrn)*con->nMass;
257
- cpFloat jnOld = con->jnAcc;
258
- con->jnAcc = cpfmax(jnOld + jn, 0.0f);
259
- jn = con->jnAcc - jnOld;
260
-
261
- // Calculate the relative tangent velocity.
262
- cpFloat vrt = cpvdot(cpvadd(vr, arb->surface_vr), cpvperp(n));
263
-
264
- // Calculate and clamp the friction impulse.
265
- cpFloat jtMax = arb->u*con->jnAcc;
266
- cpFloat jt = -vrt*con->tMass;
267
- cpFloat jtOld = con->jtAcc;
268
- con->jtAcc = cpfclamp(jtOld + jt, -jtMax, jtMax);
269
- jt = con->jtAcc - jtOld;
270
-
271
- // Apply the final impulse.
272
- apply_impulses(a, b, r1, r2, cpvrotate(n, cpv(jn, jt)));
273
- }
274
- }