box2d-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +1 -0
  6. data/box2d-rails.gemspec +20 -0
  7. data/lib/box2d-rails.rb +8 -0
  8. data/lib/box2d-rails/version.rb +5 -0
  9. data/vendor/assets/javascripts/box2d/collision/ClipVertex.js +35 -0
  10. data/vendor/assets/javascripts/box2d/collision/Features.js +61 -0
  11. data/vendor/assets/javascripts/box2d/collision/b2AABB.js +45 -0
  12. data/vendor/assets/javascripts/box2d/collision/b2Bound.js +43 -0
  13. data/vendor/assets/javascripts/box2d/collision/b2BoundValues.js +31 -0
  14. data/vendor/assets/javascripts/box2d/collision/b2BroadPhase.js +898 -0
  15. data/vendor/assets/javascripts/box2d/collision/b2BufferedPair.js +26 -0
  16. data/vendor/assets/javascripts/box2d/collision/b2Collision.js +738 -0
  17. data/vendor/assets/javascripts/box2d/collision/b2ContactID.js +52 -0
  18. data/vendor/assets/javascripts/box2d/collision/b2ContactPoint.js +35 -0
  19. data/vendor/assets/javascripts/box2d/collision/b2Distance.js +333 -0
  20. data/vendor/assets/javascripts/box2d/collision/b2Manifold.js +34 -0
  21. data/vendor/assets/javascripts/box2d/collision/b2OBB.js +34 -0
  22. data/vendor/assets/javascripts/box2d/collision/b2Pair.js +60 -0
  23. data/vendor/assets/javascripts/box2d/collision/b2PairCallback.js +34 -0
  24. data/vendor/assets/javascripts/box2d/collision/b2PairManager.js +386 -0
  25. data/vendor/assets/javascripts/box2d/collision/b2Proxy.js +40 -0
  26. data/vendor/assets/javascripts/box2d/collision/shapes/b2BoxDef.js +49 -0
  27. data/vendor/assets/javascripts/box2d/collision/shapes/b2CircleDef.js +49 -0
  28. data/vendor/assets/javascripts/box2d/collision/shapes/b2CircleShape.js +198 -0
  29. data/vendor/assets/javascripts/box2d/collision/shapes/b2MassData.js +36 -0
  30. data/vendor/assets/javascripts/box2d/collision/shapes/b2PolyDef.js +58 -0
  31. data/vendor/assets/javascripts/box2d/collision/shapes/b2PolyShape.js +492 -0
  32. data/vendor/assets/javascripts/box2d/collision/shapes/b2Shape.js +339 -0
  33. data/vendor/assets/javascripts/box2d/collision/shapes/b2ShapeDef.js +109 -0
  34. data/vendor/assets/javascripts/box2d/common/b2Settings.js +72 -0
  35. data/vendor/assets/javascripts/box2d/common/math/b2Mat22.js +130 -0
  36. data/vendor/assets/javascripts/box2d/common/math/b2Math.js +218 -0
  37. data/vendor/assets/javascripts/box2d/common/math/b2Vec2.js +131 -0
  38. data/vendor/assets/javascripts/box2d/dynamics/b2Body.js +469 -0
  39. data/vendor/assets/javascripts/box2d/dynamics/b2BodyDef.js +69 -0
  40. data/vendor/assets/javascripts/box2d/dynamics/b2CollisionFilter.js +42 -0
  41. data/vendor/assets/javascripts/box2d/dynamics/b2ContactManager.js +337 -0
  42. data/vendor/assets/javascripts/box2d/dynamics/b2Island.js +331 -0
  43. data/vendor/assets/javascripts/box2d/dynamics/b2TimeStep.js +27 -0
  44. data/vendor/assets/javascripts/box2d/dynamics/b2World.js +522 -0
  45. data/vendor/assets/javascripts/box2d/dynamics/b2WorldListener.js +52 -0
  46. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2CircleContact.js +102 -0
  47. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2Conservative.js +228 -0
  48. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2Contact.js +201 -0
  49. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactConstraint.js +45 -0
  50. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactConstraintPoint.js +40 -0
  51. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactNode.js +33 -0
  52. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactRegister.js +30 -0
  53. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactSolver.js +537 -0
  54. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2NullContact.js +65 -0
  55. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2PolyAndCircleContact.js +103 -0
  56. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2PolyContact.js +163 -0
  57. data/vendor/assets/javascripts/box2d/dynamics/joints/b2DistanceJoint.js +264 -0
  58. data/vendor/assets/javascripts/box2d/dynamics/joints/b2DistanceJointDef.js +49 -0
  59. data/vendor/assets/javascripts/box2d/dynamics/joints/b2GearJoint.js +307 -0
  60. data/vendor/assets/javascripts/box2d/dynamics/joints/b2GearJointDef.js +50 -0
  61. data/vendor/assets/javascripts/box2d/dynamics/joints/b2Jacobian.js +49 -0
  62. data/vendor/assets/javascripts/box2d/dynamics/joints/b2Joint.js +200 -0
  63. data/vendor/assets/javascripts/box2d/dynamics/joints/b2JointDef.js +40 -0
  64. data/vendor/assets/javascripts/box2d/dynamics/joints/b2JointNode.js +33 -0
  65. data/vendor/assets/javascripts/box2d/dynamics/joints/b2MouseJoint.js +234 -0
  66. data/vendor/assets/javascripts/box2d/dynamics/joints/b2MouseJointDef.js +53 -0
  67. data/vendor/assets/javascripts/box2d/dynamics/joints/b2PrismaticJoint.js +676 -0
  68. data/vendor/assets/javascripts/box2d/dynamics/joints/b2PrismaticJointDef.js +56 -0
  69. data/vendor/assets/javascripts/box2d/dynamics/joints/b2PulleyJoint.js +618 -0
  70. data/vendor/assets/javascripts/box2d/dynamics/joints/b2PulleyJointDef.js +70 -0
  71. data/vendor/assets/javascripts/box2d/dynamics/joints/b2RevoluteJoint.js +491 -0
  72. data/vendor/assets/javascripts/box2d/dynamics/joints/b2RevoluteJointDef.js +55 -0
  73. metadata +133 -0
@@ -0,0 +1,469 @@
1
+ /*
2
+ * Copyright (c) 2006-2007 Erin Catto http:
3
+ *
4
+ * This software is provided 'as-is', without any express or implied
5
+ * warranty. In no event will the authors be held liable for any damages
6
+ * arising from the use of this software.
7
+ * Permission is granted to anyone to use this software for any purpose,
8
+ * including commercial applications, and to alter it and redistribute it
9
+ * freely, subject to the following restrictions:
10
+ * 1. The origin of this software must not be misrepresented; you must not
11
+ * claim that you wrote the original software. If you use this software
12
+ * in a product, an acknowledgment in the product documentation would be
13
+ * appreciated but is not required.
14
+ * 2. Altered source versions must be plainly marked, and must not be
15
+ * misrepresented the original software.
16
+ * 3. This notice may not be removed or altered from any source distribution.
17
+ */
18
+
19
+
20
+
21
+
22
+
23
+
24
+ // A rigid body. Internal computation are done in terms
25
+ // of the center of mass position. The center of mass may
26
+ // be offset from the body's origin.
27
+ var b2Body = Class.create();
28
+ b2Body.prototype =
29
+ {
30
+ // Set the position of the body's origin and rotation (radians).
31
+ // This breaks any contacts and wakes the other bodies.
32
+ SetOriginPosition: function(position, rotation){
33
+ if (this.IsFrozen())
34
+ {
35
+ return;
36
+ }
37
+
38
+ this.m_rotation = rotation;
39
+ this.m_R.Set(this.m_rotation);
40
+ this.m_position = b2Math.AddVV(position , b2Math.b2MulMV(this.m_R, this.m_center));
41
+
42
+ this.m_position0.SetV(this.m_position);
43
+ this.m_rotation0 = this.m_rotation;
44
+
45
+ for (var s = this.m_shapeList; s != null; s = s.m_next)
46
+ {
47
+ s.Synchronize(this.m_position, this.m_R, this.m_position, this.m_R);
48
+ }
49
+
50
+ this.m_world.m_broadPhase.Commit();
51
+ },
52
+
53
+ // Get the position of the body's origin. The body's origin does not
54
+ // necessarily coincide with the center of mass. It depends on how the
55
+ // shapes are created.
56
+ GetOriginPosition: function(){
57
+ return b2Math.SubtractVV(this.m_position, b2Math.b2MulMV(this.m_R, this.m_center));
58
+ },
59
+
60
+ // Set the position of the body's center of mass and rotation (radians).
61
+ // This breaks any contacts and wakes the other bodies.
62
+ SetCenterPosition: function(position, rotation){
63
+ if (this.IsFrozen())
64
+ {
65
+ return;
66
+ }
67
+
68
+ this.m_rotation = rotation;
69
+ this.m_R.Set(this.m_rotation);
70
+ this.m_position.SetV( position );
71
+
72
+ this.m_position0.SetV(this.m_position);
73
+ this.m_rotation0 = this.m_rotation;
74
+
75
+ for (var s = this.m_shapeList; s != null; s = s.m_next)
76
+ {
77
+ s.Synchronize(this.m_position, this.m_R, this.m_position, this.m_R);
78
+ }
79
+
80
+ this.m_world.m_broadPhase.Commit();
81
+ },
82
+
83
+ // Get the position of the body's center of mass. The body's center of mass
84
+ // does not necessarily coincide with the body's origin. It depends on how the
85
+ // shapes are created.
86
+ GetCenterPosition: function(){
87
+ return this.m_position;
88
+ },
89
+
90
+ // Get the rotation in radians.
91
+ GetRotation: function(){
92
+ return this.m_rotation;
93
+ },
94
+
95
+ GetRotationMatrix: function(){
96
+ return this.m_R;
97
+ },
98
+
99
+ // Set/Get the linear velocity of the center of mass.
100
+ SetLinearVelocity: function(v){
101
+ this.m_linearVelocity.SetV(v);
102
+ },
103
+ GetLinearVelocity: function(){
104
+ return this.m_linearVelocity;
105
+ },
106
+
107
+ // Set/Get the angular velocity.
108
+ SetAngularVelocity: function(w){
109
+ this.m_angularVelocity = w;
110
+ },
111
+ GetAngularVelocity: function(){
112
+ return this.m_angularVelocity;
113
+ },
114
+
115
+ // Apply a force at a world point. Additive.
116
+ ApplyForce: function(force, point)
117
+ {
118
+ if (this.IsSleeping() == false)
119
+ {
120
+ this.m_force.Add( force );
121
+ this.m_torque += b2Math.b2CrossVV(b2Math.SubtractVV(point, this.m_position), force);
122
+ }
123
+ },
124
+
125
+ // Apply a torque. Additive.
126
+ ApplyTorque: function(torque)
127
+ {
128
+ if (this.IsSleeping() == false)
129
+ {
130
+ this.m_torque += torque;
131
+ }
132
+ },
133
+
134
+ // Apply an impulse at a point. This immediately modifies the velocity.
135
+ ApplyImpulse: function(impulse, point)
136
+ {
137
+ if (this.IsSleeping() == false)
138
+ {
139
+ this.m_linearVelocity.Add( b2Math.MulFV(this.m_invMass, impulse) );
140
+ this.m_angularVelocity += ( this.m_invI * b2Math.b2CrossVV( b2Math.SubtractVV(point, this.m_position), impulse) );
141
+ }
142
+ },
143
+
144
+ GetMass: function(){
145
+ return this.m_mass;
146
+ },
147
+
148
+ GetInertia: function(){
149
+ return this.m_I;
150
+ },
151
+
152
+ // Get the world coordinates of a point give the local coordinates
153
+ // relative to the body's center of mass.
154
+ GetWorldPoint: function(localPoint){
155
+ return b2Math.AddVV(this.m_position , b2Math.b2MulMV(this.m_R, localPoint));
156
+ },
157
+
158
+ // Get the world coordinates of a vector given the local coordinates.
159
+ GetWorldVector: function(localVector){
160
+ return b2Math.b2MulMV(this.m_R, localVector);
161
+ },
162
+
163
+ // Returns a local point relative to the center of mass given a world point.
164
+ GetLocalPoint: function(worldPoint){
165
+ return b2Math.b2MulTMV(this.m_R, b2Math.SubtractVV(worldPoint, this.m_position));
166
+ },
167
+
168
+ // Returns a local vector given a world vector.
169
+ GetLocalVector: function(worldVector){
170
+ return b2Math.b2MulTMV(this.m_R, worldVector);
171
+ },
172
+
173
+ // Is this body static (immovable)?
174
+ IsStatic: function(){
175
+ return (this.m_flags & b2Body.e_staticFlag) == b2Body.e_staticFlag;
176
+ },
177
+
178
+ IsFrozen: function()
179
+ {
180
+ return (this.m_flags & b2Body.e_frozenFlag) == b2Body.e_frozenFlag;
181
+ },
182
+
183
+ // Is this body sleeping (not simulating).
184
+ IsSleeping: function(){
185
+ return (this.m_flags & b2Body.e_sleepFlag) == b2Body.e_sleepFlag;
186
+ },
187
+
188
+ // You can disable sleeping on this particular body.
189
+ AllowSleeping: function(flag)
190
+ {
191
+ if (flag)
192
+ {
193
+ this.m_flags |= b2Body.e_allowSleepFlag;
194
+ }
195
+ else
196
+ {
197
+ this.m_flags &= ~b2Body.e_allowSleepFlag;
198
+ this.WakeUp();
199
+ }
200
+ },
201
+
202
+ // Wake up this body so it will begin simulating.
203
+ WakeUp: function(){
204
+ this.m_flags &= ~b2Body.e_sleepFlag;
205
+ this.m_sleepTime = 0.0;
206
+ },
207
+
208
+ // Get the list of all shapes attached to this body.
209
+ GetShapeList: function(){
210
+ return this.m_shapeList;
211
+ },
212
+
213
+ GetContactList: function()
214
+ {
215
+ return this.m_contactList;
216
+ },
217
+
218
+ GetJointList: function()
219
+ {
220
+ return this.m_jointList;
221
+ },
222
+
223
+ // Get the next body in the world's body list.
224
+ GetNext: function(){
225
+ return this.m_next;
226
+ },
227
+
228
+ GetUserData: function(){
229
+ return this.m_userData;
230
+ },
231
+
232
+ //--------------- Internals Below -------------------
233
+
234
+ initialize: function(bd, world){
235
+ // initialize instance variables for references
236
+ this.sMat0 = new b2Mat22();
237
+ this.m_position = new b2Vec2();
238
+ this.m_R = new b2Mat22(0);
239
+ this.m_position0 = new b2Vec2();
240
+ //
241
+
242
+ var i = 0;
243
+ var sd;
244
+ var massData;
245
+
246
+ this.m_flags = 0;
247
+ this.m_position.SetV( bd.position );
248
+ this.m_rotation = bd.rotation;
249
+ this.m_R.Set(this.m_rotation);
250
+ this.m_position0.SetV(this.m_position);
251
+ this.m_rotation0 = this.m_rotation;
252
+ this.m_world = world;
253
+
254
+ this.m_linearDamping = b2Math.b2Clamp(1.0 - bd.linearDamping, 0.0, 1.0);
255
+ this.m_angularDamping = b2Math.b2Clamp(1.0 - bd.angularDamping, 0.0, 1.0);
256
+
257
+ this.m_force = new b2Vec2(0.0, 0.0);
258
+ this.m_torque = 0.0;
259
+
260
+ this.m_mass = 0.0;
261
+
262
+ var massDatas = new Array(b2Settings.b2_maxShapesPerBody);
263
+ for (i = 0; i < b2Settings.b2_maxShapesPerBody; i++){
264
+ massDatas[i] = new b2MassData();
265
+ }
266
+
267
+ // Compute the shape mass properties, the bodies total mass and COM.
268
+ this.m_shapeCount = 0;
269
+ this.m_center = new b2Vec2(0.0, 0.0);
270
+ for (i = 0; i < b2Settings.b2_maxShapesPerBody; ++i)
271
+ {
272
+ sd = bd.shapes[i];
273
+ if (sd == null) break;
274
+ massData = massDatas[ i ];
275
+ sd.ComputeMass(massData);
276
+ this.m_mass += massData.mass;
277
+ //this.m_center += massData->mass * (sd->localPosition + massData->center);
278
+ this.m_center.x += massData.mass * (sd.localPosition.x + massData.center.x);
279
+ this.m_center.y += massData.mass * (sd.localPosition.y + massData.center.y);
280
+ ++this.m_shapeCount;
281
+ }
282
+
283
+ // Compute center of mass, and shift the origin to the COM.
284
+ if (this.m_mass > 0.0)
285
+ {
286
+ this.m_center.Multiply( 1.0 / this.m_mass );
287
+ this.m_position.Add( b2Math.b2MulMV(this.m_R, this.m_center) );
288
+ }
289
+ else
290
+ {
291
+ this.m_flags |= b2Body.e_staticFlag;
292
+ }
293
+
294
+ // Compute the moment of inertia.
295
+ this.m_I = 0.0;
296
+ for (i = 0; i < this.m_shapeCount; ++i)
297
+ {
298
+ sd = bd.shapes[i];
299
+ massData = massDatas[ i ];
300
+ this.m_I += massData.I;
301
+ var r = b2Math.SubtractVV( b2Math.AddVV(sd.localPosition, massData.center), this.m_center );
302
+ this.m_I += massData.mass * b2Math.b2Dot(r, r);
303
+ }
304
+
305
+ if (this.m_mass > 0.0)
306
+ {
307
+ this.m_invMass = 1.0 / this.m_mass;
308
+ }
309
+ else
310
+ {
311
+ this.m_invMass = 0.0;
312
+ }
313
+
314
+ if (this.m_I > 0.0 && bd.preventRotation == false)
315
+ {
316
+ this.m_invI = 1.0 / this.m_I;
317
+ }
318
+ else
319
+ {
320
+ this.m_I = 0.0;
321
+ this.m_invI = 0.0;
322
+ }
323
+
324
+ // Compute the center of mass velocity.
325
+ this.m_linearVelocity = b2Math.AddVV(bd.linearVelocity, b2Math.b2CrossFV(bd.angularVelocity, this.m_center));
326
+ this.m_angularVelocity = bd.angularVelocity;
327
+
328
+ this.m_jointList = null;
329
+ this.m_contactList = null;
330
+ this.m_prev = null;
331
+ this.m_next = null;
332
+
333
+ // Create the shapes.
334
+ this.m_shapeList = null;
335
+ for (i = 0; i < this.m_shapeCount; ++i)
336
+ {
337
+ sd = bd.shapes[i];
338
+ var shape = b2Shape.Create(sd, this, this.m_center);
339
+ shape.m_next = this.m_shapeList;
340
+ this.m_shapeList = shape;
341
+ }
342
+
343
+ this.m_sleepTime = 0.0;
344
+ if (bd.allowSleep)
345
+ {
346
+ this.m_flags |= b2Body.e_allowSleepFlag;
347
+ }
348
+ if (bd.isSleeping)
349
+ {
350
+ this.m_flags |= b2Body.e_sleepFlag;
351
+ }
352
+
353
+ if ((this.m_flags & b2Body.e_sleepFlag) || this.m_invMass == 0.0)
354
+ {
355
+ this.m_linearVelocity.Set(0.0, 0.0);
356
+ this.m_angularVelocity = 0.0;
357
+ }
358
+
359
+ this.m_userData = bd.userData;
360
+ },
361
+ // does not support destructors
362
+ /*~b2Body(){
363
+ b2Shape* s = this.m_shapeList;
364
+ while (s)
365
+ {
366
+ b2Shape* s0 = s;
367
+ s = s->this.m_next;
368
+
369
+ b2Shape::this.Destroy(s0);
370
+ }
371
+ }*/
372
+
373
+ Destroy: function(){
374
+ var s = this.m_shapeList;
375
+ while (s)
376
+ {
377
+ var s0 = s;
378
+ s = s.m_next;
379
+
380
+ b2Shape.Destroy(s0);
381
+ }
382
+ },
383
+
384
+ // Temp mat
385
+ sMat0: new b2Mat22(),
386
+ SynchronizeShapes: function(){
387
+ //b2Mat22 R0(this.m_rotation0);
388
+ this.sMat0.Set(this.m_rotation0);
389
+ for (var s = this.m_shapeList; s != null; s = s.m_next)
390
+ {
391
+ s.Synchronize(this.m_position0, this.sMat0, this.m_position, this.m_R);
392
+ }
393
+ },
394
+
395
+ QuickSyncShapes: function(){
396
+ for (var s = this.m_shapeList; s != null; s = s.m_next)
397
+ {
398
+ s.QuickSync(this.m_position, this.m_R);
399
+ }
400
+ },
401
+
402
+ // This is used to prevent connected bodies from colliding.
403
+ // It may lie, depending on the collideConnected flag.
404
+ IsConnected: function(other){
405
+ for (var jn = this.m_jointList; jn != null; jn = jn.next)
406
+ {
407
+ if (jn.other == other)
408
+ return jn.joint.m_collideConnected == false;
409
+ }
410
+
411
+ return false;
412
+ },
413
+
414
+ Freeze: function(){
415
+ this.m_flags |= b2Body.e_frozenFlag;
416
+ this.m_linearVelocity.SetZero();
417
+ this.m_angularVelocity = 0.0;
418
+
419
+ for (var s = this.m_shapeList; s != null; s = s.m_next)
420
+ {
421
+ s.DestroyProxy();
422
+ }
423
+ },
424
+
425
+ m_flags: 0,
426
+
427
+ m_position: new b2Vec2(),
428
+ m_rotation: null,
429
+ m_R: new b2Mat22(0),
430
+
431
+ // Conservative advancement data.
432
+ m_position0: new b2Vec2(),
433
+ m_rotation0: null,
434
+
435
+ m_linearVelocity: null,
436
+ m_angularVelocity: null,
437
+
438
+ m_force: null,
439
+ m_torque: null,
440
+
441
+ m_center: null,
442
+
443
+ m_world: null,
444
+ m_prev: null,
445
+ m_next: null,
446
+
447
+ m_shapeList: null,
448
+ m_shapeCount: 0,
449
+
450
+ m_jointList: null,
451
+ m_contactList: null,
452
+
453
+ m_mass: null,
454
+ m_invMass: null,
455
+ m_I: null,
456
+ m_invI: null,
457
+
458
+ m_linearDamping: null,
459
+ m_angularDamping: null,
460
+
461
+ m_sleepTime: null,
462
+
463
+ m_userData: null};
464
+ b2Body.e_staticFlag = 0x0001;
465
+ b2Body.e_frozenFlag = 0x0002;
466
+ b2Body.e_islandFlag = 0x0004;
467
+ b2Body.e_sleepFlag = 0x0008;
468
+ b2Body.e_allowSleepFlag = 0x0010;
469
+ b2Body.e_destroyFlag = 0x0020;