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
@@ -0,0 +1,180 @@
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
+ struct cpSpace;
23
+
24
+ // Number of frames that contact information should persist.
25
+ extern int cp_contact_persistence;
26
+
27
+ // User collision handler function types.
28
+ typedef int (*cpCollisionBeginFunc)(cpArbiter *arb, struct cpSpace *space, void *data);
29
+ typedef int (*cpCollisionPreSolveFunc)(cpArbiter *arb, struct cpSpace *space, void *data);
30
+ typedef void (*cpCollisionPostSolveFunc)(cpArbiter *arb, struct cpSpace *space, void *data);
31
+ typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, struct cpSpace *space, void *data);
32
+
33
+ // Structure for holding collision pair function information.
34
+ // Used internally.
35
+ typedef struct cpCollisionHandler {
36
+ cpCollisionType a;
37
+ cpCollisionType b;
38
+ cpCollisionBeginFunc begin;
39
+ cpCollisionPreSolveFunc preSolve;
40
+ cpCollisionPostSolveFunc postSolve;
41
+ cpCollisionSeparateFunc separate;
42
+ void *data;
43
+ } cpCollisionHandler;
44
+
45
+ #define CP_MAX_CONTACTS_PER_ARBITER 6
46
+ typedef struct cpContactBufferHeader {
47
+ int stamp;
48
+ struct cpContactBufferHeader *next;
49
+ unsigned int numContacts;
50
+ } cpContactBufferHeader;
51
+
52
+ typedef struct cpSpace{
53
+ // *** User definable fields
54
+
55
+ // Number of iterations to use in the impulse solver to solve contacts.
56
+ int iterations;
57
+
58
+ // Number of iterations to use in the impulse solver to solve elastic collisions.
59
+ int elasticIterations;
60
+
61
+ // Default gravity to supply when integrating rigid body motions.
62
+ cpVect gravity;
63
+
64
+ // Default damping to supply when integrating rigid body motions.
65
+ cpFloat damping;
66
+
67
+ // *** Internally Used Fields
68
+
69
+ // When the space is locked, you should not add or remove objects;
70
+ int locked;
71
+
72
+ // Time stamp. Is incremented on every call to cpSpaceStep().
73
+ int stamp;
74
+
75
+ // The static and active shape spatial hashes.
76
+ cpSpaceHash *staticShapes;
77
+ cpSpaceHash *activeShapes;
78
+
79
+ // List of bodies in the system.
80
+ cpArray *bodies;
81
+
82
+ // List of active arbiters for the impulse solver.
83
+ cpArray *arbiters, *pooledArbiters;
84
+
85
+ // Linked list ring of contact buffers.
86
+ // Head is the current buffer. Tail is the oldest buffer.
87
+ // The list points in the direction of tail->head.
88
+ cpContactBufferHeader *contactBuffersHead, *contactBuffersTail;
89
+
90
+ // List of buffers to be free()ed when destroying the space.
91
+ cpArray *allocatedBuffers;
92
+
93
+ // Persistant contact set.
94
+ cpHashSet *contactSet;
95
+
96
+ // List of constraints in the system.
97
+ cpArray *constraints;
98
+
99
+ // Set of collisionpair functions.
100
+ cpHashSet *collFuncSet;
101
+ // Default collision handler.
102
+ cpCollisionHandler defaultHandler;
103
+
104
+ cpHashSet *postStepCallbacks;
105
+ } cpSpace;
106
+
107
+ // Basic allocation/destruction functions.
108
+ cpSpace* cpSpaceAlloc(void);
109
+ cpSpace* cpSpaceInit(cpSpace *space);
110
+ cpSpace* cpSpaceNew(void);
111
+
112
+ void cpSpaceDestroy(cpSpace *space);
113
+ void cpSpaceFree(cpSpace *space);
114
+
115
+ // Convenience function. Frees all referenced entities. (bodies, shapes and constraints)
116
+ void cpSpaceFreeChildren(cpSpace *space);
117
+
118
+ // Collision handler management functions.
119
+ void cpSpaceSetDefaultCollisionHandler(
120
+ cpSpace *space,
121
+ cpCollisionBeginFunc begin,
122
+ cpCollisionPreSolveFunc preSolve,
123
+ cpCollisionPostSolveFunc postSolve,
124
+ cpCollisionSeparateFunc separate,
125
+ void *data
126
+ );
127
+ void cpSpaceAddCollisionHandler(
128
+ cpSpace *space,
129
+ cpCollisionType a, cpCollisionType b,
130
+ cpCollisionBeginFunc begin,
131
+ cpCollisionPreSolveFunc preSolve,
132
+ cpCollisionPostSolveFunc postSolve,
133
+ cpCollisionSeparateFunc separate,
134
+ void *data
135
+ );
136
+ void cpSpaceRemoveCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b);
137
+
138
+ // Add and remove entities from the system.
139
+ cpShape *cpSpaceAddShape(cpSpace *space, cpShape *shape);
140
+ cpShape *cpSpaceAddStaticShape(cpSpace *space, cpShape *shape);
141
+ cpBody *cpSpaceAddBody(cpSpace *space, cpBody *body);
142
+ cpConstraint *cpSpaceAddConstraint(cpSpace *space, cpConstraint *constraint);
143
+
144
+ void cpSpaceRemoveShape(cpSpace *space, cpShape *shape);
145
+ void cpSpaceRemoveStaticShape(cpSpace *space, cpShape *shape);
146
+ void cpSpaceRemoveBody(cpSpace *space, cpBody *body);
147
+ void cpSpaceRemoveConstraint(cpSpace *space, cpConstraint *constraint);
148
+
149
+ // Post Step function definition
150
+ typedef void (*cpPostStepFunc)(cpSpace *space, void *obj, void *data);
151
+ // Register a post step function to be called after cpSpaceStep() has finished.
152
+ // obj is used a key, you can only register one callback per unique value for obj
153
+ void cpSpaceAddPostStepCallback(cpSpace *space, cpPostStepFunc func, void *obj, void *data);
154
+
155
+ // Point query callback function
156
+ typedef void (*cpSpacePointQueryFunc)(cpShape *shape, void *data);
157
+ void cpSpacePointQuery(cpSpace *space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, void *data);
158
+ cpShape *cpSpacePointQueryFirst(cpSpace *space, cpVect point, cpLayers layers, cpGroup group);
159
+
160
+ // Segment query callback function
161
+ typedef void (*cpSpaceSegmentQueryFunc)(cpShape *shape, cpFloat t, cpVect n, void *data);
162
+ int cpSpaceSegmentQuery(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSpaceSegmentQueryFunc func, void *data);
163
+ cpShape *cpSpaceSegmentQueryFirst(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSegmentQueryInfo *out);
164
+
165
+ // BB query callback function
166
+ typedef void (*cpSpaceBBQueryFunc)(cpShape *shape, void *data);
167
+ void cpSpaceBBQuery(cpSpace *space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryFunc func, void *data);
168
+
169
+
170
+ // Iterator function for iterating the bodies in a space.
171
+ typedef void (*cpSpaceBodyIterator)(cpBody *body, void *data);
172
+ void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIterator func, void *data);
173
+
174
+ // Spatial hash management functions.
175
+ void cpSpaceResizeStaticHash(cpSpace *space, cpFloat dim, int count);
176
+ void cpSpaceResizeActiveHash(cpSpace *space, cpFloat dim, int count);
177
+ void cpSpaceRehashStatic(cpSpace *space);
178
+
179
+ // Update the space.
180
+ void cpSpaceStep(cpSpace *space, cpFloat dt);
@@ -51,13 +51,16 @@ typedef struct cpSpaceHash{
51
51
  // BBox callback.
52
52
  cpSpaceHashBBFunc bbfunc;
53
53
 
54
- // Hashset of all the handles.
54
+ // Hashset of the handles and the recycled ones.
55
55
  cpHashSet *handleSet;
56
+ cpArray *pooledHandles;
57
+
58
+ // The table and the recycled bins.
59
+ cpSpaceHashBin **table, *pooledBins;
60
+
61
+ // list of buffers to free on destruction.
62
+ cpArray *allocatedBuffers;
56
63
 
57
- cpSpaceHashBin **table;
58
- // List of recycled bins.
59
- cpSpaceHashBin *bins;
60
-
61
64
  // Incremented on each query. See cpHandle.stamp.
62
65
  int stamp;
63
66
  } cpSpaceHash;
@@ -74,9 +77,9 @@ void cpSpaceHashFree(cpSpaceHash *hash);
74
77
  void cpSpaceHashResize(cpSpaceHash *hash, cpFloat celldim, int numcells);
75
78
 
76
79
  // Add an object to the hash.
77
- void cpSpaceHashInsert(cpSpaceHash *hash, void *obj, unsigned int id, cpBB bb);
80
+ void cpSpaceHashInsert(cpSpaceHash *hash, void *obj, cpHashValue id, cpBB bb);
78
81
  // Remove an object from the hash.
79
- void cpSpaceHashRemove(cpSpaceHash *hash, void *obj, unsigned int id);
82
+ void cpSpaceHashRemove(cpSpaceHash *hash, void *obj, cpHashValue id);
80
83
 
81
84
  // Iterator function
82
85
  typedef void (*cpSpaceHashIterator)(void *obj, void *data);
@@ -86,10 +89,10 @@ void cpSpaceHashEach(cpSpaceHash *hash, cpSpaceHashIterator func, void *data);
86
89
  // Rehash the contents of the hash.
87
90
  void cpSpaceHashRehash(cpSpaceHash *hash);
88
91
  // Rehash only a specific object.
89
- void cpSpaceHashRehashObject(cpSpaceHash *hash, void *obj, unsigned int id);
92
+ void cpSpaceHashRehashObject(cpSpaceHash *hash, void *obj, cpHashValue id);
90
93
 
91
94
  // Query callback.
92
- typedef int (*cpSpaceHashQueryFunc)(void *obj1, void *obj2, void *data);
95
+ typedef void (*cpSpaceHashQueryFunc)(void *obj1, void *obj2, void *data);
93
96
  // Point query the hash. A reference to the query point is passed as obj1 to the query callback.
94
97
  void cpSpaceHashPointQuery(cpSpaceHash *hash, cpVect point, cpSpaceHashQueryFunc func, void *data);
95
98
  // Query the hash for a given BBox.
@@ -98,3 +101,9 @@ void cpSpaceHashQuery(cpSpaceHash *hash, void *obj, cpBB bb, cpSpaceHashQueryFun
98
101
  void cpSpaceHashQueryInsert(cpSpaceHash *hash, void *obj, cpBB bb, cpSpaceHashQueryFunc func, void *data);
99
102
  // Rehashes while querying for each object. (Optimized case)
100
103
  void cpSpaceHashQueryRehash(cpSpaceHash *hash, cpSpaceHashQueryFunc func, void *data);
104
+
105
+ // Segment Query callback.
106
+ // Return value is uesd for early exits of the query.
107
+ // If while traversing the grid, the raytrace function detects that an entire grid cell is beyond the hit point, it will stop the trace.
108
+ typedef cpFloat (*cpSpaceHashSegmentQueryFunc)(void *obj1, void *obj2, void *data);
109
+ void cpSpaceHashSegmentQuery(cpSpaceHash *hash, void *obj, cpVect a, cpVect b, cpFloat t_exit, cpSpaceHashSegmentQueryFunc func, void *data);
@@ -18,10 +18,6 @@
18
18
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
19
  * SOFTWARE.
20
20
  */
21
-
22
- typedef struct cpVect{
23
- cpFloat x,y;
24
- } cpVect;
25
21
 
26
22
  static const cpVect cpvzero={0.0f,0.0f};
27
23
 
@@ -32,6 +28,14 @@ cpv(const cpFloat x, const cpFloat y)
32
28
  return v;
33
29
  }
34
30
 
31
+ // non-inlined functions
32
+ cpFloat cpvlength(const cpVect v);
33
+ cpVect cpvslerp(const cpVect v1, const cpVect v2, const cpFloat t);
34
+ cpVect cpvslerpconst(const cpVect v1, const cpVect v2, const cpFloat a);
35
+ cpVect cpvforangle(const cpFloat a); // convert radians to a normalized vector
36
+ cpFloat cpvtoangle(const cpVect v); // convert a vector to radians
37
+ char *cpvstr(const cpVect v); // get a string representation of a vector
38
+
35
39
  static inline cpVect
36
40
  cpvadd(const cpVect v1, const cpVect v2)
37
41
  {
@@ -98,9 +102,56 @@ cpvunrotate(const cpVect v1, const cpVect v2)
98
102
  return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
99
103
  }
100
104
 
101
- cpFloat cpvlength(const cpVect v);
102
- cpFloat cpvlengthsq(const cpVect v); // no sqrt() call
103
- cpVect cpvnormalize(const cpVect v);
104
- cpVect cpvforangle(const cpFloat a); // convert radians to a normalized vector
105
- cpFloat cpvtoangle(const cpVect v); // convert a vector to radians
106
- char *cpvstr(const cpVect v); // get a string representation of a vector
105
+ static inline cpFloat
106
+ cpvlengthsq(const cpVect v)
107
+ {
108
+ return cpvdot(v, v);
109
+ }
110
+
111
+ static inline cpVect
112
+ cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t)
113
+ {
114
+ return cpvadd(cpvmult(v1, 1.0f - t), cpvmult(v2, t));
115
+ }
116
+
117
+ static inline cpVect
118
+ cpvnormalize(const cpVect v)
119
+ {
120
+ return cpvmult(v, 1.0f/cpvlength(v));
121
+ }
122
+
123
+ static inline cpVect
124
+ cpvnormalize_safe(const cpVect v)
125
+ {
126
+ return (v.x == 0.0f && v.y == 0.0f ? cpvzero : cpvnormalize(v));
127
+ }
128
+
129
+ static inline cpVect
130
+ cpvclamp(const cpVect v, const cpFloat len)
131
+ {
132
+ return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v;
133
+ }
134
+
135
+ static inline cpVect
136
+ cpvlerpconst(cpVect v1, cpVect v2, cpFloat d)
137
+ {
138
+ return cpvadd(v1, cpvclamp(cpvsub(v2, v1), d));
139
+ }
140
+
141
+ static inline cpFloat
142
+ cpvdist(const cpVect v1, const cpVect v2)
143
+ {
144
+ return cpvlength(cpvsub(v1, v2));
145
+ }
146
+
147
+ static inline cpFloat
148
+ cpvdistsq(const cpVect v1, const cpVect v2)
149
+ {
150
+ return cpvlengthsq(cpvsub(v1, v2));
151
+ }
152
+
153
+ static inline int
154
+ cpvnear(const cpVect v1, const cpVect v2, const cpFloat dist)
155
+ {
156
+ return cpvdistsq(v1, v2) < dist*dist;
157
+ }
data/ext/chipmunk/prime.h CHANGED
@@ -21,47 +21,47 @@
21
21
 
22
22
  // Used for resizing hash tables.
23
23
  // Values approximately double.
24
-
24
+ // http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
25
25
  static int primes[] = {
26
- 5, //2^2 + 1
27
- 11, //2^3 + 3
28
- 17, //2^4 + 1
29
- 37, //2^5 + 5
30
- 67, //2^6 + 3
31
- 131, //2^7 + 3
32
- 257, //2^8 + 1
33
- 521, //2^9 + 9
34
- 1031, //2^10 + 7
35
- 2053, //2^11 + 5
36
- 4099, //2^12 + 3
37
- 8209, //2^13 + 17
38
- 16411, //2^14 + 27
39
- 32771, //2^15 + 3
40
- 65537, //2^16 + 1
41
- 131101, //2^17 + 29
42
- 262147, //2^18 + 3
43
- 524309, //2^19 + 21
44
- 1048583, //2^20 + 7
45
- 2097169, //2^21 + 17
46
- 4194319, //2^22 + 15
47
- 8388617, //2^23 + 9
48
- 16777259, //2^24 + 43
49
- 33554467, //2^25 + 35
50
- 67108879, //2^26 + 15
51
- 134217757, //2^27 + 29
52
- 268435459, //2^28 + 3
53
- 536870923, //2^29 + 11
54
- 1073741827, //2^30 + 3
26
+ 5,
27
+ 13,
28
+ 23,
29
+ 47,
30
+ 97,
31
+ 193,
32
+ 389,
33
+ 769,
34
+ 1543,
35
+ 3079,
36
+ 6151,
37
+ 12289,
38
+ 24593,
39
+ 49157,
40
+ 98317,
41
+ 196613,
42
+ 393241,
43
+ 786433,
44
+ 1572869,
45
+ 3145739,
46
+ 6291469,
47
+ 12582917,
48
+ 25165843,
49
+ 50331653,
50
+ 100663319,
51
+ 201326611,
52
+ 402653189,
53
+ 805306457,
54
+ 1610612741,
55
55
  0,
56
56
  };
57
57
 
58
- static int
58
+ static inline int
59
59
  next_prime(int n)
60
60
  {
61
61
  int i = 0;
62
62
  while(n > primes[i]){
63
63
  i++;
64
- assert(primes[i]); // realistically this should never happen
64
+ cpAssert(primes[i], "Tried to resize a hash table to a size greater than 1610612741 O_o"); // realistically this should never happen
65
65
  }
66
66
 
67
67
  return primes[i];
@@ -1,109 +1,125 @@
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 "chipmunk.h"
23
-
24
- #include "ruby.h"
25
- #include "rb_chipmunk.h"
26
-
27
- VALUE m_Chipmunk;
28
-
29
- ID id_parent;
30
-
31
- static VALUE
32
- rb_get_cp_bias_coef(VALUE self)
33
- {
34
- return rb_float_new(cp_bias_coef);
35
- }
36
-
37
- static VALUE
38
- rb_set_cp_bias_coef(VALUE self, VALUE num)
39
- {
40
- cp_bias_coef = NUM2DBL(num);
41
- return num;
42
- }
43
-
44
- static VALUE
45
- rb_get_cp_collision_slop(VALUE self)
46
- {
47
- return rb_float_new(cp_collision_slop);
48
- }
49
-
50
- static VALUE
51
- rb_set_cp_collision_slop(VALUE self, VALUE num)
52
- {
53
- cp_collision_slop = NUM2DBL(num);
54
- return num;
55
- }
56
-
57
- static VALUE
58
- rb_momentForCircle(VALUE self, VALUE m, VALUE r1, VALUE r2, VALUE offset)
59
- {
60
- cpFloat i = cpMomentForCircle(NUM2DBL(m), NUM2DBL(r1), NUM2DBL(r2), *VGET(offset));
61
- return rb_float_new(i);
62
- }
63
-
64
- static VALUE
65
- rb_momentForPoly(VALUE self, VALUE m, VALUE arr, VALUE offset)
66
- {
67
- Check_Type(arr, T_ARRAY);
68
- int numVerts = RARRAY_LEN(arr);
69
- cpVect verts[numVerts];
70
-
71
- for(int i=0; i<numVerts; i++)
72
- verts[i] = *VGET(RARRAY_PTR(arr)[i]);
73
-
74
- cpFloat inertia = cpMomentForPoly(NUM2DBL(m), numVerts, verts, *VGET(offset));
75
- return rb_float_new(inertia);
76
- }
77
-
78
- static VALUE
79
- rb_dampedSpring(VALUE self, VALUE a, VALUE b, VALUE r1, VALUE r2, VALUE len, VALUE k, VALUE dmp, VALUE dt)
80
- {
81
- cpDampedSpring(BODY(a), BODY(b), *VGET(r1), *VGET(r2), NUM2DBL(len), NUM2DBL(k), NUM2DBL(dmp), NUM2DBL(dt));
82
- return Qnil;
83
- }
84
-
85
- void
86
- Init_chipmunk(void)
87
- {
88
- id_parent = rb_intern("parent");
89
-
90
- cpInitChipmunk();
91
-
92
- m_Chipmunk = rb_define_module("CP");
93
- rb_define_module_function(m_Chipmunk, "bias_coef", rb_get_cp_bias_coef, 0);
94
- rb_define_module_function(m_Chipmunk, "bias_coef=", rb_set_cp_bias_coef, 1);
95
- rb_define_module_function(m_Chipmunk, "collision_slop", rb_get_cp_collision_slop, 0);
96
- rb_define_module_function(m_Chipmunk, "collision_slop=", rb_set_cp_collision_slop, 1);
97
-
98
- rb_define_module_function(m_Chipmunk, "moment_for_circle", rb_momentForCircle, 4);
99
- rb_define_module_function(m_Chipmunk, "moment_for_poly", rb_momentForPoly, 3);
100
-
101
- rb_define_module_function(m_Chipmunk, "damped_spring", rb_dampedSpring, 8);
102
-
103
- Init_cpVect();
104
- Init_cpBB();
105
- Init_cpBody();
106
- Init_cpShape();
107
- Init_cpJoint();
108
- Init_cpSpace();
109
- }
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
+
26
+ #include "ruby.h"
27
+ #include "rb_chipmunk.h"
28
+
29
+ VALUE m_Chipmunk;
30
+
31
+ ID id_parent;
32
+
33
+ static VALUE
34
+ rb_get_cp_bias_coef(VALUE self)
35
+ {
36
+ return rb_float_new(cp_bias_coef);
37
+ }
38
+
39
+ static VALUE
40
+ rb_set_cp_bias_coef(VALUE self, VALUE num)
41
+ {
42
+ cp_bias_coef = NUM2DBL(num);
43
+ return num;
44
+ }
45
+
46
+ static VALUE
47
+ rb_get_cp_collision_slop(VALUE self)
48
+ {
49
+ return rb_float_new(cp_collision_slop);
50
+ }
51
+
52
+ static VALUE
53
+ rb_set_cp_collision_slop(VALUE self, VALUE num)
54
+ {
55
+ cp_collision_slop = NUM2DBL(num);
56
+ return num;
57
+ }
58
+
59
+ static VALUE
60
+ rb_momentForCircle(VALUE self, VALUE m, VALUE r1, VALUE r2, VALUE offset)
61
+ {
62
+ cpFloat i = cpMomentForCircle(NUM2DBL(m), NUM2DBL(r1), NUM2DBL(r2), *VGET(offset));
63
+ return rb_float_new(i);
64
+ }
65
+
66
+ static VALUE
67
+ rb_momentForPoly(VALUE self, VALUE m, VALUE arr, VALUE offset)
68
+ {
69
+ Check_Type(arr, T_ARRAY);
70
+ int numVerts = RARRAY_LEN(arr);
71
+ VALUE *ary_ptr = RARRAY_PTR(arr);
72
+ cpVect verts[numVerts];
73
+
74
+ for(int i=0; i<numVerts; i++)
75
+ verts[i] = *VGET(ary_ptr[i]);
76
+
77
+ cpFloat inertia = cpMomentForPoly(NUM2DBL(m), numVerts, verts, *VGET(offset));
78
+ return rb_float_new(inertia);
79
+ }
80
+
81
+ static VALUE
82
+ rb_momentForSegment(VALUE self, VALUE m, VALUE a, VALUE b)
83
+ {
84
+ cpFloat i = cpMomentForSegment(NUM2DBL(m), *VGET(a), *VGET(b));
85
+ return rb_float_new(i);
86
+ }
87
+
88
+
89
+ void
90
+ Init_chipmunk(void)
91
+ {
92
+ id_parent = rb_intern("parent");
93
+
94
+ cpInitChipmunk();
95
+
96
+ rb_eval_string("Float::INFINITY = 1.0/0.0");
97
+
98
+ m_Chipmunk = rb_define_module("CP");
99
+ rb_define_module_function(m_Chipmunk, "bias_coef",
100
+ rb_get_cp_bias_coef, 0);
101
+ rb_define_module_function(m_Chipmunk, "bias_coef=",
102
+ rb_set_cp_bias_coef, 1);
103
+ rb_define_module_function(m_Chipmunk, "collision_slop",
104
+ rb_get_cp_collision_slop, 0);
105
+ rb_define_module_function(m_Chipmunk, "collision_slop=",
106
+ rb_set_cp_collision_slop, 1);
107
+ rb_define_module_function(m_Chipmunk, "moment_for_circle",
108
+ rb_momentForCircle, 4);
109
+ rb_define_module_function(m_Chipmunk, "moment_for_poly",
110
+ rb_momentForPoly, 3);
111
+ rb_define_module_function(m_Chipmunk, "moment_for_segment",
112
+ rb_momentForSegment, 3);
113
+ rb_define_const(m_Chipmunk, "INFINITY", rb_float_new(INFINITY));
114
+ rb_define_const(m_Chipmunk, "VERSION" ,
115
+ rb_str_new2(cpVersionString));
116
+
117
+ Init_cpVect();
118
+ Init_cpArbiter();
119
+ Init_cpBB();
120
+ Init_cpBody();
121
+ Init_cpShape();
122
+ Init_cpConstraint();
123
+ Init_cpSpace();
124
+
125
+ }