supernets.org

- https://supernets.org
git clone git://git.acid.vegas/supernets.org.git
Log | Files | Refs | Archive

Box2dWeb-2.1.a.3.js (429888B)

      1 /*
      2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
      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 as such, and must not be
     15 * misrepresented as being the original software.
     16 * 3. This notice may not be removed or altered from any source distribution.
     17 */
     18 var Box2D = {};
     19 
     20 (function (a2j, undefined) {
     21 
     22    if(!(Object.prototype.defineProperty instanceof Function)
     23       && Object.prototype.__defineGetter__ instanceof Function
     24       && Object.prototype.__defineSetter__ instanceof Function)
     25    {
     26       Object.defineProperty = function(obj, p, cfg) {
     27          if(cfg.get instanceof Function)
     28             obj.__defineGetter__(p, cfg.get);
     29          if(cfg.set instanceof Function)
     30             obj.__defineSetter__(p, cfg.set);
     31       }
     32    }
     33    
     34    function emptyFn() {};
     35    a2j.inherit = function(cls, base) {
     36       var tmpCtr = cls;
     37       emptyFn.prototype = base.prototype;
     38       cls.prototype = new emptyFn;
     39       cls.prototype.constructor = tmpCtr;
     40    };
     41    
     42    a2j.generateCallback = function generateCallback(context, cb) {
     43       return function () {
     44          cb.apply(context, arguments);
     45       };
     46    };
     47    
     48    a2j.NVector = function NVector(length) {
     49       if (length === undefined) length = 0;
     50       var tmp = new Array(length || 0);
     51       for (var i = 0; i < length; ++i)
     52       tmp[i] = 0;
     53       return tmp;
     54    };
     55    
     56    a2j.is = function is(o1, o2) {
     57       if (o1 === null) return false;
     58       if ((o2 instanceof Function) && (o1 instanceof o2)) return true;
     59       if ((o1.constructor.__implements != undefined) && (o1.constructor.__implements[o2])) return true;
     60       return false;
     61    };
     62    
     63    a2j.parseUInt = function(v) {
     64       return Math.abs(parseInt(v));
     65    }
     66    
     67 })(Box2D);
     68 
     69 //#TODO remove assignments from global namespace
     70 var Vector = Array;
     71 var Vector_a2j_Number = Box2D.NVector;
     72 //package structure
     73 if (typeof(Box2D) === "undefined") Box2D = {};
     74 if (typeof(Box2D.Collision) === "undefined") Box2D.Collision = {};
     75 if (typeof(Box2D.Collision.Shapes) === "undefined") Box2D.Collision.Shapes = {};
     76 if (typeof(Box2D.Common) === "undefined") Box2D.Common = {};
     77 if (typeof(Box2D.Common.Math) === "undefined") Box2D.Common.Math = {};
     78 if (typeof(Box2D.Dynamics) === "undefined") Box2D.Dynamics = {};
     79 if (typeof(Box2D.Dynamics.Contacts) === "undefined") Box2D.Dynamics.Contacts = {};
     80 if (typeof(Box2D.Dynamics.Controllers) === "undefined") Box2D.Dynamics.Controllers = {};
     81 if (typeof(Box2D.Dynamics.Joints) === "undefined") Box2D.Dynamics.Joints = {};
     82 //pre-definitions
     83 (function () {
     84    Box2D.Collision.IBroadPhase = 'Box2D.Collision.IBroadPhase';
     85 
     86    function b2AABB() {
     87       b2AABB.b2AABB.apply(this, arguments);
     88    };
     89    Box2D.Collision.b2AABB = b2AABB;
     90 
     91    function b2Bound() {
     92       b2Bound.b2Bound.apply(this, arguments);
     93    };
     94    Box2D.Collision.b2Bound = b2Bound;
     95 
     96    function b2BoundValues() {
     97       b2BoundValues.b2BoundValues.apply(this, arguments);
     98       if (this.constructor === b2BoundValues) this.b2BoundValues.apply(this, arguments);
     99    };
    100    Box2D.Collision.b2BoundValues = b2BoundValues;
    101 
    102    function b2Collision() {
    103       b2Collision.b2Collision.apply(this, arguments);
    104    };
    105    Box2D.Collision.b2Collision = b2Collision;
    106 
    107    function b2ContactID() {
    108       b2ContactID.b2ContactID.apply(this, arguments);
    109       if (this.constructor === b2ContactID) this.b2ContactID.apply(this, arguments);
    110    };
    111    Box2D.Collision.b2ContactID = b2ContactID;
    112 
    113    function b2ContactPoint() {
    114       b2ContactPoint.b2ContactPoint.apply(this, arguments);
    115    };
    116    Box2D.Collision.b2ContactPoint = b2ContactPoint;
    117 
    118    function b2Distance() {
    119       b2Distance.b2Distance.apply(this, arguments);
    120    };
    121    Box2D.Collision.b2Distance = b2Distance;
    122 
    123    function b2DistanceInput() {
    124       b2DistanceInput.b2DistanceInput.apply(this, arguments);
    125    };
    126    Box2D.Collision.b2DistanceInput = b2DistanceInput;
    127 
    128    function b2DistanceOutput() {
    129       b2DistanceOutput.b2DistanceOutput.apply(this, arguments);
    130    };
    131    Box2D.Collision.b2DistanceOutput = b2DistanceOutput;
    132 
    133    function b2DistanceProxy() {
    134       b2DistanceProxy.b2DistanceProxy.apply(this, arguments);
    135    };
    136    Box2D.Collision.b2DistanceProxy = b2DistanceProxy;
    137 
    138    function b2DynamicTree() {
    139       b2DynamicTree.b2DynamicTree.apply(this, arguments);
    140       if (this.constructor === b2DynamicTree) this.b2DynamicTree.apply(this, arguments);
    141    };
    142    Box2D.Collision.b2DynamicTree = b2DynamicTree;
    143 
    144    function b2DynamicTreeBroadPhase() {
    145       b2DynamicTreeBroadPhase.b2DynamicTreeBroadPhase.apply(this, arguments);
    146    };
    147    Box2D.Collision.b2DynamicTreeBroadPhase = b2DynamicTreeBroadPhase;
    148 
    149    function b2DynamicTreeNode() {
    150       b2DynamicTreeNode.b2DynamicTreeNode.apply(this, arguments);
    151    };
    152    Box2D.Collision.b2DynamicTreeNode = b2DynamicTreeNode;
    153 
    154    function b2DynamicTreePair() {
    155       b2DynamicTreePair.b2DynamicTreePair.apply(this, arguments);
    156    };
    157    Box2D.Collision.b2DynamicTreePair = b2DynamicTreePair;
    158 
    159    function b2Manifold() {
    160       b2Manifold.b2Manifold.apply(this, arguments);
    161       if (this.constructor === b2Manifold) this.b2Manifold.apply(this, arguments);
    162    };
    163    Box2D.Collision.b2Manifold = b2Manifold;
    164 
    165    function b2ManifoldPoint() {
    166       b2ManifoldPoint.b2ManifoldPoint.apply(this, arguments);
    167       if (this.constructor === b2ManifoldPoint) this.b2ManifoldPoint.apply(this, arguments);
    168    };
    169    Box2D.Collision.b2ManifoldPoint = b2ManifoldPoint;
    170 
    171    function b2Point() {
    172       b2Point.b2Point.apply(this, arguments);
    173    };
    174    Box2D.Collision.b2Point = b2Point;
    175 
    176    function b2RayCastInput() {
    177       b2RayCastInput.b2RayCastInput.apply(this, arguments);
    178       if (this.constructor === b2RayCastInput) this.b2RayCastInput.apply(this, arguments);
    179    };
    180    Box2D.Collision.b2RayCastInput = b2RayCastInput;
    181 
    182    function b2RayCastOutput() {
    183       b2RayCastOutput.b2RayCastOutput.apply(this, arguments);
    184    };
    185    Box2D.Collision.b2RayCastOutput = b2RayCastOutput;
    186 
    187    function b2Segment() {
    188       b2Segment.b2Segment.apply(this, arguments);
    189    };
    190    Box2D.Collision.b2Segment = b2Segment;
    191 
    192    function b2SeparationFunction() {
    193       b2SeparationFunction.b2SeparationFunction.apply(this, arguments);
    194    };
    195    Box2D.Collision.b2SeparationFunction = b2SeparationFunction;
    196 
    197    function b2Simplex() {
    198       b2Simplex.b2Simplex.apply(this, arguments);
    199       if (this.constructor === b2Simplex) this.b2Simplex.apply(this, arguments);
    200    };
    201    Box2D.Collision.b2Simplex = b2Simplex;
    202 
    203    function b2SimplexCache() {
    204       b2SimplexCache.b2SimplexCache.apply(this, arguments);
    205    };
    206    Box2D.Collision.b2SimplexCache = b2SimplexCache;
    207 
    208    function b2SimplexVertex() {
    209       b2SimplexVertex.b2SimplexVertex.apply(this, arguments);
    210    };
    211    Box2D.Collision.b2SimplexVertex = b2SimplexVertex;
    212 
    213    function b2TimeOfImpact() {
    214       b2TimeOfImpact.b2TimeOfImpact.apply(this, arguments);
    215    };
    216    Box2D.Collision.b2TimeOfImpact = b2TimeOfImpact;
    217 
    218    function b2TOIInput() {
    219       b2TOIInput.b2TOIInput.apply(this, arguments);
    220    };
    221    Box2D.Collision.b2TOIInput = b2TOIInput;
    222 
    223    function b2WorldManifold() {
    224       b2WorldManifold.b2WorldManifold.apply(this, arguments);
    225       if (this.constructor === b2WorldManifold) this.b2WorldManifold.apply(this, arguments);
    226    };
    227    Box2D.Collision.b2WorldManifold = b2WorldManifold;
    228 
    229    function ClipVertex() {
    230       ClipVertex.ClipVertex.apply(this, arguments);
    231    };
    232    Box2D.Collision.ClipVertex = ClipVertex;
    233 
    234    function Features() {
    235       Features.Features.apply(this, arguments);
    236    };
    237    Box2D.Collision.Features = Features;
    238 
    239    function b2CircleShape() {
    240       b2CircleShape.b2CircleShape.apply(this, arguments);
    241       if (this.constructor === b2CircleShape) this.b2CircleShape.apply(this, arguments);
    242    };
    243    Box2D.Collision.Shapes.b2CircleShape = b2CircleShape;
    244 
    245    function b2EdgeChainDef() {
    246       b2EdgeChainDef.b2EdgeChainDef.apply(this, arguments);
    247       if (this.constructor === b2EdgeChainDef) this.b2EdgeChainDef.apply(this, arguments);
    248    };
    249    Box2D.Collision.Shapes.b2EdgeChainDef = b2EdgeChainDef;
    250 
    251    function b2EdgeShape() {
    252       b2EdgeShape.b2EdgeShape.apply(this, arguments);
    253       if (this.constructor === b2EdgeShape) this.b2EdgeShape.apply(this, arguments);
    254    };
    255    Box2D.Collision.Shapes.b2EdgeShape = b2EdgeShape;
    256 
    257    function b2MassData() {
    258       b2MassData.b2MassData.apply(this, arguments);
    259    };
    260    Box2D.Collision.Shapes.b2MassData = b2MassData;
    261 
    262    function b2PolygonShape() {
    263       b2PolygonShape.b2PolygonShape.apply(this, arguments);
    264       if (this.constructor === b2PolygonShape) this.b2PolygonShape.apply(this, arguments);
    265    };
    266    Box2D.Collision.Shapes.b2PolygonShape = b2PolygonShape;
    267 
    268    function b2Shape() {
    269       b2Shape.b2Shape.apply(this, arguments);
    270       if (this.constructor === b2Shape) this.b2Shape.apply(this, arguments);
    271    };
    272    Box2D.Collision.Shapes.b2Shape = b2Shape;
    273    Box2D.Common.b2internal = 'Box2D.Common.b2internal';
    274 
    275    function b2Color() {
    276       b2Color.b2Color.apply(this, arguments);
    277       if (this.constructor === b2Color) this.b2Color.apply(this, arguments);
    278    };
    279    Box2D.Common.b2Color = b2Color;
    280 
    281    function b2Settings() {
    282       b2Settings.b2Settings.apply(this, arguments);
    283    };
    284    Box2D.Common.b2Settings = b2Settings;
    285 
    286    function b2Mat22() {
    287       b2Mat22.b2Mat22.apply(this, arguments);
    288       if (this.constructor === b2Mat22) this.b2Mat22.apply(this, arguments);
    289    };
    290    Box2D.Common.Math.b2Mat22 = b2Mat22;
    291 
    292    function b2Mat33() {
    293       b2Mat33.b2Mat33.apply(this, arguments);
    294       if (this.constructor === b2Mat33) this.b2Mat33.apply(this, arguments);
    295    };
    296    Box2D.Common.Math.b2Mat33 = b2Mat33;
    297 
    298    function b2Math() {
    299       b2Math.b2Math.apply(this, arguments);
    300    };
    301    Box2D.Common.Math.b2Math = b2Math;
    302 
    303    function b2Sweep() {
    304       b2Sweep.b2Sweep.apply(this, arguments);
    305    };
    306    Box2D.Common.Math.b2Sweep = b2Sweep;
    307 
    308    function b2Transform() {
    309       b2Transform.b2Transform.apply(this, arguments);
    310       if (this.constructor === b2Transform) this.b2Transform.apply(this, arguments);
    311    };
    312    Box2D.Common.Math.b2Transform = b2Transform;
    313 
    314    function b2Vec2() {
    315       b2Vec2.b2Vec2.apply(this, arguments);
    316       if (this.constructor === b2Vec2) this.b2Vec2.apply(this, arguments);
    317    };
    318    Box2D.Common.Math.b2Vec2 = b2Vec2;
    319 
    320    function b2Vec3() {
    321       b2Vec3.b2Vec3.apply(this, arguments);
    322       if (this.constructor === b2Vec3) this.b2Vec3.apply(this, arguments);
    323    };
    324    Box2D.Common.Math.b2Vec3 = b2Vec3;
    325 
    326    function b2Body() {
    327       b2Body.b2Body.apply(this, arguments);
    328       if (this.constructor === b2Body) this.b2Body.apply(this, arguments);
    329    };
    330    Box2D.Dynamics.b2Body = b2Body;
    331 
    332    function b2BodyDef() {
    333       b2BodyDef.b2BodyDef.apply(this, arguments);
    334       if (this.constructor === b2BodyDef) this.b2BodyDef.apply(this, arguments);
    335    };
    336    Box2D.Dynamics.b2BodyDef = b2BodyDef;
    337 
    338    function b2ContactFilter() {
    339       b2ContactFilter.b2ContactFilter.apply(this, arguments);
    340    };
    341    Box2D.Dynamics.b2ContactFilter = b2ContactFilter;
    342 
    343    function b2ContactImpulse() {
    344       b2ContactImpulse.b2ContactImpulse.apply(this, arguments);
    345    };
    346    Box2D.Dynamics.b2ContactImpulse = b2ContactImpulse;
    347 
    348    function b2ContactListener() {
    349       b2ContactListener.b2ContactListener.apply(this, arguments);
    350    };
    351    Box2D.Dynamics.b2ContactListener = b2ContactListener;
    352 
    353    function b2ContactManager() {
    354       b2ContactManager.b2ContactManager.apply(this, arguments);
    355       if (this.constructor === b2ContactManager) this.b2ContactManager.apply(this, arguments);
    356    };
    357    Box2D.Dynamics.b2ContactManager = b2ContactManager;
    358 
    359    function b2DebugDraw() {
    360       b2DebugDraw.b2DebugDraw.apply(this, arguments);
    361       if (this.constructor === b2DebugDraw) this.b2DebugDraw.apply(this, arguments);
    362    };
    363    Box2D.Dynamics.b2DebugDraw = b2DebugDraw;
    364 
    365    function b2DestructionListener() {
    366       b2DestructionListener.b2DestructionListener.apply(this, arguments);
    367    };
    368    Box2D.Dynamics.b2DestructionListener = b2DestructionListener;
    369 
    370    function b2FilterData() {
    371       b2FilterData.b2FilterData.apply(this, arguments);
    372    };
    373    Box2D.Dynamics.b2FilterData = b2FilterData;
    374 
    375    function b2Fixture() {
    376       b2Fixture.b2Fixture.apply(this, arguments);
    377       if (this.constructor === b2Fixture) this.b2Fixture.apply(this, arguments);
    378    };
    379    Box2D.Dynamics.b2Fixture = b2Fixture;
    380 
    381    function b2FixtureDef() {
    382       b2FixtureDef.b2FixtureDef.apply(this, arguments);
    383       if (this.constructor === b2FixtureDef) this.b2FixtureDef.apply(this, arguments);
    384    };
    385    Box2D.Dynamics.b2FixtureDef = b2FixtureDef;
    386 
    387    function b2Island() {
    388       b2Island.b2Island.apply(this, arguments);
    389       if (this.constructor === b2Island) this.b2Island.apply(this, arguments);
    390    };
    391    Box2D.Dynamics.b2Island = b2Island;
    392 
    393    function b2TimeStep() {
    394       b2TimeStep.b2TimeStep.apply(this, arguments);
    395    };
    396    Box2D.Dynamics.b2TimeStep = b2TimeStep;
    397 
    398    function b2World() {
    399       b2World.b2World.apply(this, arguments);
    400       if (this.constructor === b2World) this.b2World.apply(this, arguments);
    401    };
    402    Box2D.Dynamics.b2World = b2World;
    403 
    404    function b2CircleContact() {
    405       b2CircleContact.b2CircleContact.apply(this, arguments);
    406    };
    407    Box2D.Dynamics.Contacts.b2CircleContact = b2CircleContact;
    408 
    409    function b2Contact() {
    410       b2Contact.b2Contact.apply(this, arguments);
    411       if (this.constructor === b2Contact) this.b2Contact.apply(this, arguments);
    412    };
    413    Box2D.Dynamics.Contacts.b2Contact = b2Contact;
    414 
    415    function b2ContactConstraint() {
    416       b2ContactConstraint.b2ContactConstraint.apply(this, arguments);
    417       if (this.constructor === b2ContactConstraint) this.b2ContactConstraint.apply(this, arguments);
    418    };
    419    Box2D.Dynamics.Contacts.b2ContactConstraint = b2ContactConstraint;
    420 
    421    function b2ContactConstraintPoint() {
    422       b2ContactConstraintPoint.b2ContactConstraintPoint.apply(this, arguments);
    423    };
    424    Box2D.Dynamics.Contacts.b2ContactConstraintPoint = b2ContactConstraintPoint;
    425 
    426    function b2ContactEdge() {
    427       b2ContactEdge.b2ContactEdge.apply(this, arguments);
    428    };
    429    Box2D.Dynamics.Contacts.b2ContactEdge = b2ContactEdge;
    430 
    431    function b2ContactFactory() {
    432       b2ContactFactory.b2ContactFactory.apply(this, arguments);
    433       if (this.constructor === b2ContactFactory) this.b2ContactFactory.apply(this, arguments);
    434    };
    435    Box2D.Dynamics.Contacts.b2ContactFactory = b2ContactFactory;
    436 
    437    function b2ContactRegister() {
    438       b2ContactRegister.b2ContactRegister.apply(this, arguments);
    439    };
    440    Box2D.Dynamics.Contacts.b2ContactRegister = b2ContactRegister;
    441 
    442    function b2ContactResult() {
    443       b2ContactResult.b2ContactResult.apply(this, arguments);
    444    };
    445    Box2D.Dynamics.Contacts.b2ContactResult = b2ContactResult;
    446 
    447    function b2ContactSolver() {
    448       b2ContactSolver.b2ContactSolver.apply(this, arguments);
    449       if (this.constructor === b2ContactSolver) this.b2ContactSolver.apply(this, arguments);
    450    };
    451    Box2D.Dynamics.Contacts.b2ContactSolver = b2ContactSolver;
    452 
    453    function b2EdgeAndCircleContact() {
    454       b2EdgeAndCircleContact.b2EdgeAndCircleContact.apply(this, arguments);
    455    };
    456    Box2D.Dynamics.Contacts.b2EdgeAndCircleContact = b2EdgeAndCircleContact;
    457 
    458    function b2NullContact() {
    459       b2NullContact.b2NullContact.apply(this, arguments);
    460       if (this.constructor === b2NullContact) this.b2NullContact.apply(this, arguments);
    461    };
    462    Box2D.Dynamics.Contacts.b2NullContact = b2NullContact;
    463 
    464    function b2PolyAndCircleContact() {
    465       b2PolyAndCircleContact.b2PolyAndCircleContact.apply(this, arguments);
    466    };
    467    Box2D.Dynamics.Contacts.b2PolyAndCircleContact = b2PolyAndCircleContact;
    468 
    469    function b2PolyAndEdgeContact() {
    470       b2PolyAndEdgeContact.b2PolyAndEdgeContact.apply(this, arguments);
    471    };
    472    Box2D.Dynamics.Contacts.b2PolyAndEdgeContact = b2PolyAndEdgeContact;
    473 
    474    function b2PolygonContact() {
    475       b2PolygonContact.b2PolygonContact.apply(this, arguments);
    476    };
    477    Box2D.Dynamics.Contacts.b2PolygonContact = b2PolygonContact;
    478 
    479    function b2PositionSolverManifold() {
    480       b2PositionSolverManifold.b2PositionSolverManifold.apply(this, arguments);
    481       if (this.constructor === b2PositionSolverManifold) this.b2PositionSolverManifold.apply(this, arguments);
    482    };
    483    Box2D.Dynamics.Contacts.b2PositionSolverManifold = b2PositionSolverManifold;
    484 
    485    function b2BuoyancyController() {
    486       b2BuoyancyController.b2BuoyancyController.apply(this, arguments);
    487    };
    488    Box2D.Dynamics.Controllers.b2BuoyancyController = b2BuoyancyController;
    489 
    490    function b2ConstantAccelController() {
    491       b2ConstantAccelController.b2ConstantAccelController.apply(this, arguments);
    492    };
    493    Box2D.Dynamics.Controllers.b2ConstantAccelController = b2ConstantAccelController;
    494 
    495    function b2ConstantForceController() {
    496       b2ConstantForceController.b2ConstantForceController.apply(this, arguments);
    497    };
    498    Box2D.Dynamics.Controllers.b2ConstantForceController = b2ConstantForceController;
    499 
    500    function b2Controller() {
    501       b2Controller.b2Controller.apply(this, arguments);
    502    };
    503    Box2D.Dynamics.Controllers.b2Controller = b2Controller;
    504 
    505    function b2ControllerEdge() {
    506       b2ControllerEdge.b2ControllerEdge.apply(this, arguments);
    507    };
    508    Box2D.Dynamics.Controllers.b2ControllerEdge = b2ControllerEdge;
    509 
    510    function b2GravityController() {
    511       b2GravityController.b2GravityController.apply(this, arguments);
    512    };
    513    Box2D.Dynamics.Controllers.b2GravityController = b2GravityController;
    514 
    515    function b2TensorDampingController() {
    516       b2TensorDampingController.b2TensorDampingController.apply(this, arguments);
    517    };
    518    Box2D.Dynamics.Controllers.b2TensorDampingController = b2TensorDampingController;
    519 
    520    function b2DistanceJoint() {
    521       b2DistanceJoint.b2DistanceJoint.apply(this, arguments);
    522       if (this.constructor === b2DistanceJoint) this.b2DistanceJoint.apply(this, arguments);
    523    };
    524    Box2D.Dynamics.Joints.b2DistanceJoint = b2DistanceJoint;
    525 
    526    function b2DistanceJointDef() {
    527       b2DistanceJointDef.b2DistanceJointDef.apply(this, arguments);
    528       if (this.constructor === b2DistanceJointDef) this.b2DistanceJointDef.apply(this, arguments);
    529    };
    530    Box2D.Dynamics.Joints.b2DistanceJointDef = b2DistanceJointDef;
    531 
    532    function b2FrictionJoint() {
    533       b2FrictionJoint.b2FrictionJoint.apply(this, arguments);
    534       if (this.constructor === b2FrictionJoint) this.b2FrictionJoint.apply(this, arguments);
    535    };
    536    Box2D.Dynamics.Joints.b2FrictionJoint = b2FrictionJoint;
    537 
    538    function b2FrictionJointDef() {
    539       b2FrictionJointDef.b2FrictionJointDef.apply(this, arguments);
    540       if (this.constructor === b2FrictionJointDef) this.b2FrictionJointDef.apply(this, arguments);
    541    };
    542    Box2D.Dynamics.Joints.b2FrictionJointDef = b2FrictionJointDef;
    543 
    544    function b2GearJoint() {
    545       b2GearJoint.b2GearJoint.apply(this, arguments);
    546       if (this.constructor === b2GearJoint) this.b2GearJoint.apply(this, arguments);
    547    };
    548    Box2D.Dynamics.Joints.b2GearJoint = b2GearJoint;
    549 
    550    function b2GearJointDef() {
    551       b2GearJointDef.b2GearJointDef.apply(this, arguments);
    552       if (this.constructor === b2GearJointDef) this.b2GearJointDef.apply(this, arguments);
    553    };
    554    Box2D.Dynamics.Joints.b2GearJointDef = b2GearJointDef;
    555 
    556    function b2Jacobian() {
    557       b2Jacobian.b2Jacobian.apply(this, arguments);
    558    };
    559    Box2D.Dynamics.Joints.b2Jacobian = b2Jacobian;
    560 
    561    function b2Joint() {
    562       b2Joint.b2Joint.apply(this, arguments);
    563       if (this.constructor === b2Joint) this.b2Joint.apply(this, arguments);
    564    };
    565    Box2D.Dynamics.Joints.b2Joint = b2Joint;
    566 
    567    function b2JointDef() {
    568       b2JointDef.b2JointDef.apply(this, arguments);
    569       if (this.constructor === b2JointDef) this.b2JointDef.apply(this, arguments);
    570    };
    571    Box2D.Dynamics.Joints.b2JointDef = b2JointDef;
    572 
    573    function b2JointEdge() {
    574       b2JointEdge.b2JointEdge.apply(this, arguments);
    575    };
    576    Box2D.Dynamics.Joints.b2JointEdge = b2JointEdge;
    577 
    578    function b2LineJoint() {
    579       b2LineJoint.b2LineJoint.apply(this, arguments);
    580       if (this.constructor === b2LineJoint) this.b2LineJoint.apply(this, arguments);
    581    };
    582    Box2D.Dynamics.Joints.b2LineJoint = b2LineJoint;
    583 
    584    function b2LineJointDef() {
    585       b2LineJointDef.b2LineJointDef.apply(this, arguments);
    586       if (this.constructor === b2LineJointDef) this.b2LineJointDef.apply(this, arguments);
    587    };
    588    Box2D.Dynamics.Joints.b2LineJointDef = b2LineJointDef;
    589 
    590    function b2MouseJoint() {
    591       b2MouseJoint.b2MouseJoint.apply(this, arguments);
    592       if (this.constructor === b2MouseJoint) this.b2MouseJoint.apply(this, arguments);
    593    };
    594    Box2D.Dynamics.Joints.b2MouseJoint = b2MouseJoint;
    595 
    596    function b2MouseJointDef() {
    597       b2MouseJointDef.b2MouseJointDef.apply(this, arguments);
    598       if (this.constructor === b2MouseJointDef) this.b2MouseJointDef.apply(this, arguments);
    599    };
    600    Box2D.Dynamics.Joints.b2MouseJointDef = b2MouseJointDef;
    601 
    602    function b2PrismaticJoint() {
    603       b2PrismaticJoint.b2PrismaticJoint.apply(this, arguments);
    604       if (this.constructor === b2PrismaticJoint) this.b2PrismaticJoint.apply(this, arguments);
    605    };
    606    Box2D.Dynamics.Joints.b2PrismaticJoint = b2PrismaticJoint;
    607 
    608    function b2PrismaticJointDef() {
    609       b2PrismaticJointDef.b2PrismaticJointDef.apply(this, arguments);
    610       if (this.constructor === b2PrismaticJointDef) this.b2PrismaticJointDef.apply(this, arguments);
    611    };
    612    Box2D.Dynamics.Joints.b2PrismaticJointDef = b2PrismaticJointDef;
    613 
    614    function b2PulleyJoint() {
    615       b2PulleyJoint.b2PulleyJoint.apply(this, arguments);
    616       if (this.constructor === b2PulleyJoint) this.b2PulleyJoint.apply(this, arguments);
    617    };
    618    Box2D.Dynamics.Joints.b2PulleyJoint = b2PulleyJoint;
    619 
    620    function b2PulleyJointDef() {
    621       b2PulleyJointDef.b2PulleyJointDef.apply(this, arguments);
    622       if (this.constructor === b2PulleyJointDef) this.b2PulleyJointDef.apply(this, arguments);
    623    };
    624    Box2D.Dynamics.Joints.b2PulleyJointDef = b2PulleyJointDef;
    625 
    626    function b2RevoluteJoint() {
    627       b2RevoluteJoint.b2RevoluteJoint.apply(this, arguments);
    628       if (this.constructor === b2RevoluteJoint) this.b2RevoluteJoint.apply(this, arguments);
    629    };
    630    Box2D.Dynamics.Joints.b2RevoluteJoint = b2RevoluteJoint;
    631 
    632    function b2RevoluteJointDef() {
    633       b2RevoluteJointDef.b2RevoluteJointDef.apply(this, arguments);
    634       if (this.constructor === b2RevoluteJointDef) this.b2RevoluteJointDef.apply(this, arguments);
    635    };
    636    Box2D.Dynamics.Joints.b2RevoluteJointDef = b2RevoluteJointDef;
    637 
    638    function b2WeldJoint() {
    639       b2WeldJoint.b2WeldJoint.apply(this, arguments);
    640       if (this.constructor === b2WeldJoint) this.b2WeldJoint.apply(this, arguments);
    641    };
    642    Box2D.Dynamics.Joints.b2WeldJoint = b2WeldJoint;
    643 
    644    function b2WeldJointDef() {
    645       b2WeldJointDef.b2WeldJointDef.apply(this, arguments);
    646       if (this.constructor === b2WeldJointDef) this.b2WeldJointDef.apply(this, arguments);
    647    };
    648    Box2D.Dynamics.Joints.b2WeldJointDef = b2WeldJointDef;
    649 })(); //definitions
    650 Box2D.postDefs = [];
    651 (function () {
    652    var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
    653       b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
    654       b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
    655       b2MassData = Box2D.Collision.Shapes.b2MassData,
    656       b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
    657       b2Shape = Box2D.Collision.Shapes.b2Shape,
    658       b2Color = Box2D.Common.b2Color,
    659       b2internal = Box2D.Common.b2internal,
    660       b2Settings = Box2D.Common.b2Settings,
    661       b2Mat22 = Box2D.Common.Math.b2Mat22,
    662       b2Mat33 = Box2D.Common.Math.b2Mat33,
    663       b2Math = Box2D.Common.Math.b2Math,
    664       b2Sweep = Box2D.Common.Math.b2Sweep,
    665       b2Transform = Box2D.Common.Math.b2Transform,
    666       b2Vec2 = Box2D.Common.Math.b2Vec2,
    667       b2Vec3 = Box2D.Common.Math.b2Vec3,
    668       b2AABB = Box2D.Collision.b2AABB,
    669       b2Bound = Box2D.Collision.b2Bound,
    670       b2BoundValues = Box2D.Collision.b2BoundValues,
    671       b2Collision = Box2D.Collision.b2Collision,
    672       b2ContactID = Box2D.Collision.b2ContactID,
    673       b2ContactPoint = Box2D.Collision.b2ContactPoint,
    674       b2Distance = Box2D.Collision.b2Distance,
    675       b2DistanceInput = Box2D.Collision.b2DistanceInput,
    676       b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
    677       b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
    678       b2DynamicTree = Box2D.Collision.b2DynamicTree,
    679       b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
    680       b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
    681       b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
    682       b2Manifold = Box2D.Collision.b2Manifold,
    683       b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
    684       b2Point = Box2D.Collision.b2Point,
    685       b2RayCastInput = Box2D.Collision.b2RayCastInput,
    686       b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
    687       b2Segment = Box2D.Collision.b2Segment,
    688       b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
    689       b2Simplex = Box2D.Collision.b2Simplex,
    690       b2SimplexCache = Box2D.Collision.b2SimplexCache,
    691       b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
    692       b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
    693       b2TOIInput = Box2D.Collision.b2TOIInput,
    694       b2WorldManifold = Box2D.Collision.b2WorldManifold,
    695       ClipVertex = Box2D.Collision.ClipVertex,
    696       Features = Box2D.Collision.Features,
    697       IBroadPhase = Box2D.Collision.IBroadPhase;
    698 
    699    b2AABB.b2AABB = function () {
    700       this.lowerBound = new b2Vec2();
    701       this.upperBound = new b2Vec2();
    702    };
    703    b2AABB.prototype.IsValid = function () {
    704       var dX = this.upperBound.x - this.lowerBound.x;
    705       var dY = this.upperBound.y - this.lowerBound.y;
    706       var valid = dX >= 0.0 && dY >= 0.0;
    707       valid = valid && this.lowerBound.IsValid() && this.upperBound.IsValid();
    708       return valid;
    709    }
    710    b2AABB.prototype.GetCenter = function () {
    711       return new b2Vec2((this.lowerBound.x + this.upperBound.x) / 2, (this.lowerBound.y + this.upperBound.y) / 2);
    712    }
    713    b2AABB.prototype.GetExtents = function () {
    714       return new b2Vec2((this.upperBound.x - this.lowerBound.x) / 2, (this.upperBound.y - this.lowerBound.y) / 2);
    715    }
    716    b2AABB.prototype.Contains = function (aabb) {
    717       var result = true;
    718       result = result && this.lowerBound.x <= aabb.lowerBound.x;
    719       result = result && this.lowerBound.y <= aabb.lowerBound.y;
    720       result = result && aabb.upperBound.x <= this.upperBound.x;
    721       result = result && aabb.upperBound.y <= this.upperBound.y;
    722       return result;
    723    }
    724    b2AABB.prototype.RayCast = function (output, input) {
    725       var tmin = (-Number.MAX_VALUE);
    726       var tmax = Number.MAX_VALUE;
    727       var pX = input.p1.x;
    728       var pY = input.p1.y;
    729       var dX = input.p2.x - input.p1.x;
    730       var dY = input.p2.y - input.p1.y;
    731       var absDX = Math.abs(dX);
    732       var absDY = Math.abs(dY);
    733       var normal = output.normal;
    734       var inv_d = 0;
    735       var t1 = 0;
    736       var t2 = 0;
    737       var t3 = 0;
    738       var s = 0; {
    739          if (absDX < Number.MIN_VALUE) {
    740             if (pX < this.lowerBound.x || this.upperBound.x < pX) return false;
    741          }
    742          else {
    743             inv_d = 1.0 / dX;
    744             t1 = (this.lowerBound.x - pX) * inv_d;
    745             t2 = (this.upperBound.x - pX) * inv_d;
    746             s = (-1.0);
    747             if (t1 > t2) {
    748                t3 = t1;
    749                t1 = t2;
    750                t2 = t3;
    751                s = 1.0;
    752             }
    753             if (t1 > tmin) {
    754                normal.x = s;
    755                normal.y = 0;
    756                tmin = t1;
    757             }
    758             tmax = Math.min(tmax, t2);
    759             if (tmin > tmax) return false;
    760          }
    761       } {
    762          if (absDY < Number.MIN_VALUE) {
    763             if (pY < this.lowerBound.y || this.upperBound.y < pY) return false;
    764          }
    765          else {
    766             inv_d = 1.0 / dY;
    767             t1 = (this.lowerBound.y - pY) * inv_d;
    768             t2 = (this.upperBound.y - pY) * inv_d;
    769             s = (-1.0);
    770             if (t1 > t2) {
    771                t3 = t1;
    772                t1 = t2;
    773                t2 = t3;
    774                s = 1.0;
    775             }
    776             if (t1 > tmin) {
    777                normal.y = s;
    778                normal.x = 0;
    779                tmin = t1;
    780             }
    781             tmax = Math.min(tmax, t2);
    782             if (tmin > tmax) return false;
    783          }
    784       }
    785       output.fraction = tmin;
    786       return true;
    787    }
    788    b2AABB.prototype.TestOverlap = function (other) {
    789       var d1X = other.lowerBound.x - this.upperBound.x;
    790       var d1Y = other.lowerBound.y - this.upperBound.y;
    791       var d2X = this.lowerBound.x - other.upperBound.x;
    792       var d2Y = this.lowerBound.y - other.upperBound.y;
    793       if (d1X > 0.0 || d1Y > 0.0) return false;
    794       if (d2X > 0.0 || d2Y > 0.0) return false;
    795       return true;
    796    }
    797    b2AABB.Combine = function (aabb1, aabb2) {
    798       var aabb = new b2AABB();
    799       aabb.Combine(aabb1, aabb2);
    800       return aabb;
    801    }
    802    b2AABB.prototype.Combine = function (aabb1, aabb2) {
    803       this.lowerBound.x = Math.min(aabb1.lowerBound.x, aabb2.lowerBound.x);
    804       this.lowerBound.y = Math.min(aabb1.lowerBound.y, aabb2.lowerBound.y);
    805       this.upperBound.x = Math.max(aabb1.upperBound.x, aabb2.upperBound.x);
    806       this.upperBound.y = Math.max(aabb1.upperBound.y, aabb2.upperBound.y);
    807    }
    808    b2Bound.b2Bound = function () {};
    809    b2Bound.prototype.IsLower = function () {
    810       return (this.value & 1) == 0;
    811    }
    812    b2Bound.prototype.IsUpper = function () {
    813       return (this.value & 1) == 1;
    814    }
    815    b2Bound.prototype.Swap = function (b) {
    816       var tempValue = this.value;
    817       var tempProxy = this.proxy;
    818       var tempStabbingCount = this.stabbingCount;
    819       this.value = b.value;
    820       this.proxy = b.proxy;
    821       this.stabbingCount = b.stabbingCount;
    822       b.value = tempValue;
    823       b.proxy = tempProxy;
    824       b.stabbingCount = tempStabbingCount;
    825    }
    826    b2BoundValues.b2BoundValues = function () {};
    827    b2BoundValues.prototype.b2BoundValues = function () {
    828       this.lowerValues = new Vector_a2j_Number();
    829       this.lowerValues[0] = 0.0;
    830       this.lowerValues[1] = 0.0;
    831       this.upperValues = new Vector_a2j_Number();
    832       this.upperValues[0] = 0.0;
    833       this.upperValues[1] = 0.0;
    834    }
    835    b2Collision.b2Collision = function () {};
    836    b2Collision.ClipSegmentToLine = function (vOut, vIn, normal, offset) {
    837       if (offset === undefined) offset = 0;
    838       var cv;
    839       var numOut = 0;
    840       cv = vIn[0];
    841       var vIn0 = cv.v;
    842       cv = vIn[1];
    843       var vIn1 = cv.v;
    844       var distance0 = normal.x * vIn0.x + normal.y * vIn0.y - offset;
    845       var distance1 = normal.x * vIn1.x + normal.y * vIn1.y - offset;
    846       if (distance0 <= 0.0) vOut[numOut++].Set(vIn[0]);
    847       if (distance1 <= 0.0) vOut[numOut++].Set(vIn[1]);
    848       if (distance0 * distance1 < 0.0) {
    849          var interp = distance0 / (distance0 - distance1);
    850          cv = vOut[numOut];
    851          var tVec = cv.v;
    852          tVec.x = vIn0.x + interp * (vIn1.x - vIn0.x);
    853          tVec.y = vIn0.y + interp * (vIn1.y - vIn0.y);
    854          cv = vOut[numOut];
    855          var cv2;
    856          if (distance0 > 0.0) {
    857             cv2 = vIn[0];
    858             cv.id = cv2.id;
    859          }
    860          else {
    861             cv2 = vIn[1];
    862             cv.id = cv2.id;
    863          }++numOut;
    864       }
    865       return numOut;
    866    }
    867    b2Collision.EdgeSeparation = function (poly1, xf1, edge1, poly2, xf2) {
    868       if (edge1 === undefined) edge1 = 0;
    869       var count1 = parseInt(poly1.m_vertexCount);
    870       var vertices1 = poly1.m_vertices;
    871       var normals1 = poly1.m_normals;
    872       var count2 = parseInt(poly2.m_vertexCount);
    873       var vertices2 = poly2.m_vertices;
    874       var tMat;
    875       var tVec;
    876       tMat = xf1.R;
    877       tVec = normals1[edge1];
    878       var normal1WorldX = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
    879       var normal1WorldY = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
    880       tMat = xf2.R;
    881       var normal1X = (tMat.col1.x * normal1WorldX + tMat.col1.y * normal1WorldY);
    882       var normal1Y = (tMat.col2.x * normal1WorldX + tMat.col2.y * normal1WorldY);
    883       var index = 0;
    884       var minDot = Number.MAX_VALUE;
    885       for (var i = 0; i < count2; ++i) {
    886          tVec = vertices2[i];
    887          var dot = tVec.x * normal1X + tVec.y * normal1Y;
    888          if (dot < minDot) {
    889             minDot = dot;
    890             index = i;
    891          }
    892       }
    893       tVec = vertices1[edge1];
    894       tMat = xf1.R;
    895       var v1X = xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
    896       var v1Y = xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
    897       tVec = vertices2[index];
    898       tMat = xf2.R;
    899       var v2X = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
    900       var v2Y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
    901       v2X -= v1X;
    902       v2Y -= v1Y;
    903       var separation = v2X * normal1WorldX + v2Y * normal1WorldY;
    904       return separation;
    905    }
    906    b2Collision.FindMaxSeparation = function (edgeIndex, poly1, xf1, poly2, xf2) {
    907       var count1 = parseInt(poly1.m_vertexCount);
    908       var normals1 = poly1.m_normals;
    909       var tVec;
    910       var tMat;
    911       tMat = xf2.R;
    912       tVec = poly2.m_centroid;
    913       var dX = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
    914       var dY = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
    915       tMat = xf1.R;
    916       tVec = poly1.m_centroid;
    917       dX -= xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
    918       dY -= xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
    919       var dLocal1X = (dX * xf1.R.col1.x + dY * xf1.R.col1.y);
    920       var dLocal1Y = (dX * xf1.R.col2.x + dY * xf1.R.col2.y);
    921       var edge = 0;
    922       var maxDot = (-Number.MAX_VALUE);
    923       for (var i = 0; i < count1; ++i) {
    924          tVec = normals1[i];
    925          var dot = (tVec.x * dLocal1X + tVec.y * dLocal1Y);
    926          if (dot > maxDot) {
    927             maxDot = dot;
    928             edge = i;
    929          }
    930       }
    931       var s = b2Collision.EdgeSeparation(poly1, xf1, edge, poly2, xf2);
    932       var prevEdge = parseInt(edge - 1 >= 0 ? edge - 1 : count1 - 1);
    933       var sPrev = b2Collision.EdgeSeparation(poly1, xf1, prevEdge, poly2, xf2);
    934       var nextEdge = parseInt(edge + 1 < count1 ? edge + 1 : 0);
    935       var sNext = b2Collision.EdgeSeparation(poly1, xf1, nextEdge, poly2, xf2);
    936       var bestEdge = 0;
    937       var bestSeparation = 0;
    938       var increment = 0;
    939       if (sPrev > s && sPrev > sNext) {
    940          increment = (-1);
    941          bestEdge = prevEdge;
    942          bestSeparation = sPrev;
    943       }
    944       else if (sNext > s) {
    945          increment = 1;
    946          bestEdge = nextEdge;
    947          bestSeparation = sNext;
    948       }
    949       else {
    950          edgeIndex[0] = edge;
    951          return s;
    952       }
    953       while (true) {
    954          if (increment == (-1)) edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1;
    955          else edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0;s = b2Collision.EdgeSeparation(poly1, xf1, edge, poly2, xf2);
    956          if (s > bestSeparation) {
    957             bestEdge = edge;
    958             bestSeparation = s;
    959          }
    960          else {
    961             break;
    962          }
    963       }
    964       edgeIndex[0] = bestEdge;
    965       return bestSeparation;
    966    }
    967    b2Collision.FindIncidentEdge = function (c, poly1, xf1, edge1, poly2, xf2) {
    968       if (edge1 === undefined) edge1 = 0;
    969       var count1 = parseInt(poly1.m_vertexCount);
    970       var normals1 = poly1.m_normals;
    971       var count2 = parseInt(poly2.m_vertexCount);
    972       var vertices2 = poly2.m_vertices;
    973       var normals2 = poly2.m_normals;
    974       var tMat;
    975       var tVec;
    976       tMat = xf1.R;
    977       tVec = normals1[edge1];
    978       var normal1X = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
    979       var normal1Y = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
    980       tMat = xf2.R;
    981       var tX = (tMat.col1.x * normal1X + tMat.col1.y * normal1Y);
    982       normal1Y = (tMat.col2.x * normal1X + tMat.col2.y * normal1Y);
    983       normal1X = tX;
    984       var index = 0;
    985       var minDot = Number.MAX_VALUE;
    986       for (var i = 0; i < count2; ++i) {
    987          tVec = normals2[i];
    988          var dot = (normal1X * tVec.x + normal1Y * tVec.y);
    989          if (dot < minDot) {
    990             minDot = dot;
    991             index = i;
    992          }
    993       }
    994       var tClip;
    995       var i1 = parseInt(index);
    996       var i2 = parseInt(i1 + 1 < count2 ? i1 + 1 : 0);
    997       tClip = c[0];
    998       tVec = vertices2[i1];
    999       tMat = xf2.R;
   1000       tClip.v.x = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   1001       tClip.v.y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   1002       tClip.id.features.referenceEdge = edge1;
   1003       tClip.id.features.incidentEdge = i1;
   1004       tClip.id.features.incidentVertex = 0;
   1005       tClip = c[1];
   1006       tVec = vertices2[i2];
   1007       tMat = xf2.R;
   1008       tClip.v.x = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   1009       tClip.v.y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   1010       tClip.id.features.referenceEdge = edge1;
   1011       tClip.id.features.incidentEdge = i2;
   1012       tClip.id.features.incidentVertex = 1;
   1013    }
   1014    b2Collision.MakeClipPointVector = function () {
   1015       var r = new Vector(2);
   1016       r[0] = new ClipVertex();
   1017       r[1] = new ClipVertex();
   1018       return r;
   1019    }
   1020    b2Collision.CollidePolygons = function (manifold, polyA, xfA, polyB, xfB) {
   1021       var cv;
   1022       manifold.m_pointCount = 0;
   1023       var totalRadius = polyA.m_radius + polyB.m_radius;
   1024       var edgeA = 0;
   1025       b2Collision.s_edgeAO[0] = edgeA;
   1026       var separationA = b2Collision.FindMaxSeparation(b2Collision.s_edgeAO, polyA, xfA, polyB, xfB);
   1027       edgeA = b2Collision.s_edgeAO[0];
   1028       if (separationA > totalRadius) return;
   1029       var edgeB = 0;
   1030       b2Collision.s_edgeBO[0] = edgeB;
   1031       var separationB = b2Collision.FindMaxSeparation(b2Collision.s_edgeBO, polyB, xfB, polyA, xfA);
   1032       edgeB = b2Collision.s_edgeBO[0];
   1033       if (separationB > totalRadius) return;
   1034       var poly1;
   1035       var poly2;
   1036       var xf1;
   1037       var xf2;
   1038       var edge1 = 0;
   1039       var flip = 0;
   1040       var k_relativeTol = 0.98;
   1041       var k_absoluteTol = 0.001;
   1042       var tMat;
   1043       if (separationB > k_relativeTol * separationA + k_absoluteTol) {
   1044          poly1 = polyB;
   1045          poly2 = polyA;
   1046          xf1 = xfB;
   1047          xf2 = xfA;
   1048          edge1 = edgeB;
   1049          manifold.m_type = b2Manifold.e_faceB;
   1050          flip = 1;
   1051       }
   1052       else {
   1053          poly1 = polyA;
   1054          poly2 = polyB;
   1055          xf1 = xfA;
   1056          xf2 = xfB;
   1057          edge1 = edgeA;
   1058          manifold.m_type = b2Manifold.e_faceA;
   1059          flip = 0;
   1060       }
   1061       var incidentEdge = b2Collision.s_incidentEdge;
   1062       b2Collision.FindIncidentEdge(incidentEdge, poly1, xf1, edge1, poly2, xf2);
   1063       var count1 = parseInt(poly1.m_vertexCount);
   1064       var vertices1 = poly1.m_vertices;
   1065       var local_v11 = vertices1[edge1];
   1066       var local_v12;
   1067       if (edge1 + 1 < count1) {
   1068          local_v12 = vertices1[parseInt(edge1 + 1)];
   1069       }
   1070       else {
   1071          local_v12 = vertices1[0];
   1072       }
   1073       var localTangent = b2Collision.s_localTangent;
   1074       localTangent.Set(local_v12.x - local_v11.x, local_v12.y - local_v11.y);
   1075       localTangent.Normalize();
   1076       var localNormal = b2Collision.s_localNormal;
   1077       localNormal.x = localTangent.y;
   1078       localNormal.y = (-localTangent.x);
   1079       var planePoint = b2Collision.s_planePoint;
   1080       planePoint.Set(0.5 * (local_v11.x + local_v12.x), 0.5 * (local_v11.y + local_v12.y));
   1081       var tangent = b2Collision.s_tangent;
   1082       tMat = xf1.R;
   1083       tangent.x = (tMat.col1.x * localTangent.x + tMat.col2.x * localTangent.y);
   1084       tangent.y = (tMat.col1.y * localTangent.x + tMat.col2.y * localTangent.y);
   1085       var tangent2 = b2Collision.s_tangent2;
   1086       tangent2.x = (-tangent.x);
   1087       tangent2.y = (-tangent.y);
   1088       var normal = b2Collision.s_normal;
   1089       normal.x = tangent.y;
   1090       normal.y = (-tangent.x);
   1091       var v11 = b2Collision.s_v11;
   1092       var v12 = b2Collision.s_v12;
   1093       v11.x = xf1.position.x + (tMat.col1.x * local_v11.x + tMat.col2.x * local_v11.y);
   1094       v11.y = xf1.position.y + (tMat.col1.y * local_v11.x + tMat.col2.y * local_v11.y);
   1095       v12.x = xf1.position.x + (tMat.col1.x * local_v12.x + tMat.col2.x * local_v12.y);
   1096       v12.y = xf1.position.y + (tMat.col1.y * local_v12.x + tMat.col2.y * local_v12.y);
   1097       var frontOffset = normal.x * v11.x + normal.y * v11.y;
   1098       var sideOffset1 = (-tangent.x * v11.x) - tangent.y * v11.y + totalRadius;
   1099       var sideOffset2 = tangent.x * v12.x + tangent.y * v12.y + totalRadius;
   1100       var clipPoints1 = b2Collision.s_clipPoints1;
   1101       var clipPoints2 = b2Collision.s_clipPoints2;
   1102       var np = 0;
   1103       np = b2Collision.ClipSegmentToLine(clipPoints1, incidentEdge, tangent2, sideOffset1);
   1104       if (np < 2) return;
   1105       np = b2Collision.ClipSegmentToLine(clipPoints2, clipPoints1, tangent, sideOffset2);
   1106       if (np < 2) return;
   1107       manifold.m_localPlaneNormal.SetV(localNormal);
   1108       manifold.m_localPoint.SetV(planePoint);
   1109       var pointCount = 0;
   1110       for (var i = 0; i < b2Settings.b2_maxManifoldPoints; ++i) {
   1111          cv = clipPoints2[i];
   1112          var separation = normal.x * cv.v.x + normal.y * cv.v.y - frontOffset;
   1113          if (separation <= totalRadius) {
   1114             var cp = manifold.m_points[pointCount];
   1115             tMat = xf2.R;
   1116             var tX = cv.v.x - xf2.position.x;
   1117             var tY = cv.v.y - xf2.position.y;
   1118             cp.m_localPoint.x = (tX * tMat.col1.x + tY * tMat.col1.y);
   1119             cp.m_localPoint.y = (tX * tMat.col2.x + tY * tMat.col2.y);
   1120             cp.m_id.Set(cv.id);
   1121             cp.m_id.features.flip = flip;
   1122             ++pointCount;
   1123          }
   1124       }
   1125       manifold.m_pointCount = pointCount;
   1126    }
   1127    b2Collision.CollideCircles = function (manifold, circle1, xf1, circle2, xf2) {
   1128       manifold.m_pointCount = 0;
   1129       var tMat;
   1130       var tVec;
   1131       tMat = xf1.R;
   1132       tVec = circle1.m_p;
   1133       var p1X = xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   1134       var p1Y = xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   1135       tMat = xf2.R;
   1136       tVec = circle2.m_p;
   1137       var p2X = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   1138       var p2Y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   1139       var dX = p2X - p1X;
   1140       var dY = p2Y - p1Y;
   1141       var distSqr = dX * dX + dY * dY;
   1142       var radius = circle1.m_radius + circle2.m_radius;
   1143       if (distSqr > radius * radius) {
   1144          return;
   1145       }
   1146       manifold.m_type = b2Manifold.e_circles;
   1147       manifold.m_localPoint.SetV(circle1.m_p);
   1148       manifold.m_localPlaneNormal.SetZero();
   1149       manifold.m_pointCount = 1;
   1150       manifold.m_points[0].m_localPoint.SetV(circle2.m_p);
   1151       manifold.m_points[0].m_id.key = 0;
   1152    }
   1153    b2Collision.CollidePolygonAndCircle = function (manifold, polygon, xf1, circle, xf2) {
   1154       manifold.m_pointCount = 0;
   1155       var tPoint;
   1156       var dX = 0;
   1157       var dY = 0;
   1158       var positionX = 0;
   1159       var positionY = 0;
   1160       var tVec;
   1161       var tMat;
   1162       tMat = xf2.R;
   1163       tVec = circle.m_p;
   1164       var cX = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   1165       var cY = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   1166       dX = cX - xf1.position.x;
   1167       dY = cY - xf1.position.y;
   1168       tMat = xf1.R;
   1169       var cLocalX = (dX * tMat.col1.x + dY * tMat.col1.y);
   1170       var cLocalY = (dX * tMat.col2.x + dY * tMat.col2.y);
   1171       var dist = 0;
   1172       var normalIndex = 0;
   1173       var separation = (-Number.MAX_VALUE);
   1174       var radius = polygon.m_radius + circle.m_radius;
   1175       var vertexCount = parseInt(polygon.m_vertexCount);
   1176       var vertices = polygon.m_vertices;
   1177       var normals = polygon.m_normals;
   1178       for (var i = 0; i < vertexCount; ++i) {
   1179          tVec = vertices[i];
   1180          dX = cLocalX - tVec.x;
   1181          dY = cLocalY - tVec.y;
   1182          tVec = normals[i];
   1183          var s = tVec.x * dX + tVec.y * dY;
   1184          if (s > radius) {
   1185             return;
   1186          }
   1187          if (s > separation) {
   1188             separation = s;
   1189             normalIndex = i;
   1190          }
   1191       }
   1192       var vertIndex1 = parseInt(normalIndex);
   1193       var vertIndex2 = parseInt(vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0);
   1194       var v1 = vertices[vertIndex1];
   1195       var v2 = vertices[vertIndex2];
   1196       if (separation < Number.MIN_VALUE) {
   1197          manifold.m_pointCount = 1;
   1198          manifold.m_type = b2Manifold.e_faceA;
   1199          manifold.m_localPlaneNormal.SetV(normals[normalIndex]);
   1200          manifold.m_localPoint.x = 0.5 * (v1.x + v2.x);
   1201          manifold.m_localPoint.y = 0.5 * (v1.y + v2.y);
   1202          manifold.m_points[0].m_localPoint.SetV(circle.m_p);
   1203          manifold.m_points[0].m_id.key = 0;
   1204          return;
   1205       }
   1206       var u1 = (cLocalX - v1.x) * (v2.x - v1.x) + (cLocalY - v1.y) * (v2.y - v1.y);
   1207       var u2 = (cLocalX - v2.x) * (v1.x - v2.x) + (cLocalY - v2.y) * (v1.y - v2.y);
   1208       if (u1 <= 0.0) {
   1209          if ((cLocalX - v1.x) * (cLocalX - v1.x) + (cLocalY - v1.y) * (cLocalY - v1.y) > radius * radius) return;
   1210          manifold.m_pointCount = 1;
   1211          manifold.m_type = b2Manifold.e_faceA;
   1212          manifold.m_localPlaneNormal.x = cLocalX - v1.x;
   1213          manifold.m_localPlaneNormal.y = cLocalY - v1.y;
   1214          manifold.m_localPlaneNormal.Normalize();
   1215          manifold.m_localPoint.SetV(v1);
   1216          manifold.m_points[0].m_localPoint.SetV(circle.m_p);
   1217          manifold.m_points[0].m_id.key = 0;
   1218       }
   1219       else if (u2 <= 0) {
   1220          if ((cLocalX - v2.x) * (cLocalX - v2.x) + (cLocalY - v2.y) * (cLocalY - v2.y) > radius * radius) return;
   1221          manifold.m_pointCount = 1;
   1222          manifold.m_type = b2Manifold.e_faceA;
   1223          manifold.m_localPlaneNormal.x = cLocalX - v2.x;
   1224          manifold.m_localPlaneNormal.y = cLocalY - v2.y;
   1225          manifold.m_localPlaneNormal.Normalize();
   1226          manifold.m_localPoint.SetV(v2);
   1227          manifold.m_points[0].m_localPoint.SetV(circle.m_p);
   1228          manifold.m_points[0].m_id.key = 0;
   1229       }
   1230       else {
   1231          var faceCenterX = 0.5 * (v1.x + v2.x);
   1232          var faceCenterY = 0.5 * (v1.y + v2.y);
   1233          separation = (cLocalX - faceCenterX) * normals[vertIndex1].x + (cLocalY - faceCenterY) * normals[vertIndex1].y;
   1234          if (separation > radius) return;
   1235          manifold.m_pointCount = 1;
   1236          manifold.m_type = b2Manifold.e_faceA;
   1237          manifold.m_localPlaneNormal.x = normals[vertIndex1].x;
   1238          manifold.m_localPlaneNormal.y = normals[vertIndex1].y;
   1239          manifold.m_localPlaneNormal.Normalize();
   1240          manifold.m_localPoint.Set(faceCenterX, faceCenterY);
   1241          manifold.m_points[0].m_localPoint.SetV(circle.m_p);
   1242          manifold.m_points[0].m_id.key = 0;
   1243       }
   1244    }
   1245    b2Collision.TestOverlap = function (a, b) {
   1246       var t1 = b.lowerBound;
   1247       var t2 = a.upperBound;
   1248       var d1X = t1.x - t2.x;
   1249       var d1Y = t1.y - t2.y;
   1250       t1 = a.lowerBound;
   1251       t2 = b.upperBound;
   1252       var d2X = t1.x - t2.x;
   1253       var d2Y = t1.y - t2.y;
   1254       if (d1X > 0.0 || d1Y > 0.0) return false;
   1255       if (d2X > 0.0 || d2Y > 0.0) return false;
   1256       return true;
   1257    }
   1258    Box2D.postDefs.push(function () {
   1259       Box2D.Collision.b2Collision.s_incidentEdge = b2Collision.MakeClipPointVector();
   1260       Box2D.Collision.b2Collision.s_clipPoints1 = b2Collision.MakeClipPointVector();
   1261       Box2D.Collision.b2Collision.s_clipPoints2 = b2Collision.MakeClipPointVector();
   1262       Box2D.Collision.b2Collision.s_edgeAO = new Vector_a2j_Number(1);
   1263       Box2D.Collision.b2Collision.s_edgeBO = new Vector_a2j_Number(1);
   1264       Box2D.Collision.b2Collision.s_localTangent = new b2Vec2();
   1265       Box2D.Collision.b2Collision.s_localNormal = new b2Vec2();
   1266       Box2D.Collision.b2Collision.s_planePoint = new b2Vec2();
   1267       Box2D.Collision.b2Collision.s_normal = new b2Vec2();
   1268       Box2D.Collision.b2Collision.s_tangent = new b2Vec2();
   1269       Box2D.Collision.b2Collision.s_tangent2 = new b2Vec2();
   1270       Box2D.Collision.b2Collision.s_v11 = new b2Vec2();
   1271       Box2D.Collision.b2Collision.s_v12 = new b2Vec2();
   1272       Box2D.Collision.b2Collision.b2CollidePolyTempVec = new b2Vec2();
   1273       Box2D.Collision.b2Collision.b2_nullFeature = 0x000000ff;
   1274    });
   1275    b2ContactID.b2ContactID = function () {
   1276       this.features = new Features();
   1277    };
   1278    b2ContactID.prototype.b2ContactID = function () {
   1279       this.features._m_id = this;
   1280    }
   1281    b2ContactID.prototype.Set = function (id) {
   1282       this.key = id._key;
   1283    }
   1284    b2ContactID.prototype.Copy = function () {
   1285       var id = new b2ContactID();
   1286       id.key = this.key;
   1287       return id;
   1288    }
   1289    Object.defineProperty(b2ContactID.prototype, 'key', {
   1290       enumerable: false,
   1291       configurable: true,
   1292       get: function () {
   1293          return this._key;
   1294       }
   1295    });
   1296    Object.defineProperty(b2ContactID.prototype, 'key', {
   1297       enumerable: false,
   1298       configurable: true,
   1299       set: function (value) {
   1300          if (value === undefined) value = 0;
   1301          this._key = value;
   1302          this.features._referenceEdge = this._key & 0x000000ff;
   1303          this.features._incidentEdge = ((this._key & 0x0000ff00) >> 8) & 0x000000ff;
   1304          this.features._incidentVertex = ((this._key & 0x00ff0000) >> 16) & 0x000000ff;
   1305          this.features._flip = ((this._key & 0xff000000) >> 24) & 0x000000ff;
   1306       }
   1307    });
   1308    b2ContactPoint.b2ContactPoint = function () {
   1309       this.position = new b2Vec2();
   1310       this.velocity = new b2Vec2();
   1311       this.normal = new b2Vec2();
   1312       this.id = new b2ContactID();
   1313    };
   1314    b2Distance.b2Distance = function () {};
   1315    b2Distance.Distance = function (output, cache, input) {
   1316       ++b2Distance.b2_gjkCalls;
   1317       var proxyA = input.proxyA;
   1318       var proxyB = input.proxyB;
   1319       var transformA = input.transformA;
   1320       var transformB = input.transformB;
   1321       var simplex = b2Distance.s_simplex;
   1322       simplex.ReadCache(cache, proxyA, transformA, proxyB, transformB);
   1323       var vertices = simplex.m_vertices;
   1324       var k_maxIters = 20;
   1325       var saveA = b2Distance.s_saveA;
   1326       var saveB = b2Distance.s_saveB;
   1327       var saveCount = 0;
   1328       var closestPoint = simplex.GetClosestPoint();
   1329       var distanceSqr1 = closestPoint.LengthSquared();
   1330       var distanceSqr2 = distanceSqr1;
   1331       var i = 0;
   1332       var p;
   1333       var iter = 0;
   1334       while (iter < k_maxIters) {
   1335          saveCount = simplex.m_count;
   1336          for (i = 0;
   1337          i < saveCount; i++) {
   1338             saveA[i] = vertices[i].indexA;
   1339             saveB[i] = vertices[i].indexB;
   1340          }
   1341          switch (simplex.m_count) {
   1342          case 1:
   1343             break;
   1344          case 2:
   1345             simplex.Solve2();
   1346             break;
   1347          case 3:
   1348             simplex.Solve3();
   1349             break;
   1350          default:
   1351             b2Settings.b2Assert(false);
   1352          }
   1353          if (simplex.m_count == 3) {
   1354             break;
   1355          }
   1356          p = simplex.GetClosestPoint();
   1357          distanceSqr2 = p.LengthSquared();
   1358          if (distanceSqr2 > distanceSqr1) {}
   1359          distanceSqr1 = distanceSqr2;
   1360          var d = simplex.GetSearchDirection();
   1361          if (d.LengthSquared() < Number.MIN_VALUE * Number.MIN_VALUE) {
   1362             break;
   1363          }
   1364          var vertex = vertices[simplex.m_count];
   1365          vertex.indexA = proxyA.GetSupport(b2Math.MulTMV(transformA.R, d.GetNegative()));
   1366          vertex.wA = b2Math.MulX(transformA, proxyA.GetVertex(vertex.indexA));
   1367          vertex.indexB = proxyB.GetSupport(b2Math.MulTMV(transformB.R, d));
   1368          vertex.wB = b2Math.MulX(transformB, proxyB.GetVertex(vertex.indexB));
   1369          vertex.w = b2Math.SubtractVV(vertex.wB, vertex.wA);
   1370          ++iter;
   1371          ++b2Distance.b2_gjkIters;
   1372          var duplicate = false;
   1373          for (i = 0;
   1374          i < saveCount; i++) {
   1375             if (vertex.indexA == saveA[i] && vertex.indexB == saveB[i]) {
   1376                duplicate = true;
   1377                break;
   1378             }
   1379          }
   1380          if (duplicate) {
   1381             break;
   1382          }++simplex.m_count;
   1383       }
   1384       b2Distance.b2_gjkMaxIters = b2Math.Max(b2Distance.b2_gjkMaxIters, iter);
   1385       simplex.GetWitnessPoints(output.pointA, output.pointB);
   1386       output.distance = b2Math.SubtractVV(output.pointA, output.pointB).Length();
   1387       output.iterations = iter;
   1388       simplex.WriteCache(cache);
   1389       if (input.useRadii) {
   1390          var rA = proxyA.m_radius;
   1391          var rB = proxyB.m_radius;
   1392          if (output.distance > rA + rB && output.distance > Number.MIN_VALUE) {
   1393             output.distance -= rA + rB;
   1394             var normal = b2Math.SubtractVV(output.pointB, output.pointA);
   1395             normal.Normalize();
   1396             output.pointA.x += rA * normal.x;
   1397             output.pointA.y += rA * normal.y;
   1398             output.pointB.x -= rB * normal.x;
   1399             output.pointB.y -= rB * normal.y;
   1400          }
   1401          else {
   1402             p = new b2Vec2();
   1403             p.x = .5 * (output.pointA.x + output.pointB.x);
   1404             p.y = .5 * (output.pointA.y + output.pointB.y);
   1405             output.pointA.x = output.pointB.x = p.x;
   1406             output.pointA.y = output.pointB.y = p.y;
   1407             output.distance = 0.0;
   1408          }
   1409       }
   1410    }
   1411    Box2D.postDefs.push(function () {
   1412       Box2D.Collision.b2Distance.s_simplex = new b2Simplex();
   1413       Box2D.Collision.b2Distance.s_saveA = new Vector_a2j_Number(3);
   1414       Box2D.Collision.b2Distance.s_saveB = new Vector_a2j_Number(3);
   1415    });
   1416    b2DistanceInput.b2DistanceInput = function () {};
   1417    b2DistanceOutput.b2DistanceOutput = function () {
   1418       this.pointA = new b2Vec2();
   1419       this.pointB = new b2Vec2();
   1420    };
   1421    b2DistanceProxy.b2DistanceProxy = function () {};
   1422    b2DistanceProxy.prototype.Set = function (shape) {
   1423       switch (shape.GetType()) {
   1424       case b2Shape.e_circleShape:
   1425          {
   1426             var circle = (shape instanceof b2CircleShape ? shape : null);
   1427             this.m_vertices = new Vector(1, true);
   1428             this.m_vertices[0] = circle.m_p;
   1429             this.m_count = 1;
   1430             this.m_radius = circle.m_radius;
   1431          }
   1432          break;
   1433       case b2Shape.e_polygonShape:
   1434          {
   1435             var polygon = (shape instanceof b2PolygonShape ? shape : null);
   1436             this.m_vertices = polygon.m_vertices;
   1437             this.m_count = polygon.m_vertexCount;
   1438             this.m_radius = polygon.m_radius;
   1439          }
   1440          break;
   1441       default:
   1442          b2Settings.b2Assert(false);
   1443       }
   1444    }
   1445    b2DistanceProxy.prototype.GetSupport = function (d) {
   1446       var bestIndex = 0;
   1447       var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
   1448       for (var i = 1; i < this.m_count; ++i) {
   1449          var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
   1450          if (value > bestValue) {
   1451             bestIndex = i;
   1452             bestValue = value;
   1453          }
   1454       }
   1455       return bestIndex;
   1456    }
   1457    b2DistanceProxy.prototype.GetSupportVertex = function (d) {
   1458       var bestIndex = 0;
   1459       var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
   1460       for (var i = 1; i < this.m_count; ++i) {
   1461          var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
   1462          if (value > bestValue) {
   1463             bestIndex = i;
   1464             bestValue = value;
   1465          }
   1466       }
   1467       return this.m_vertices[bestIndex];
   1468    }
   1469    b2DistanceProxy.prototype.GetVertexCount = function () {
   1470       return this.m_count;
   1471    }
   1472    b2DistanceProxy.prototype.GetVertex = function (index) {
   1473       if (index === undefined) index = 0;
   1474       b2Settings.b2Assert(0 <= index && index < this.m_count);
   1475       return this.m_vertices[index];
   1476    }
   1477    b2DynamicTree.b2DynamicTree = function () {};
   1478    b2DynamicTree.prototype.b2DynamicTree = function () {
   1479       this.m_root = null;
   1480       this.m_freeList = null;
   1481       this.m_path = 0;
   1482       this.m_insertionCount = 0;
   1483    }
   1484    b2DynamicTree.prototype.CreateProxy = function (aabb, userData) {
   1485       var node = this.AllocateNode();
   1486       var extendX = b2Settings.b2_aabbExtension;
   1487       var extendY = b2Settings.b2_aabbExtension;
   1488       node.aabb.lowerBound.x = aabb.lowerBound.x - extendX;
   1489       node.aabb.lowerBound.y = aabb.lowerBound.y - extendY;
   1490       node.aabb.upperBound.x = aabb.upperBound.x + extendX;
   1491       node.aabb.upperBound.y = aabb.upperBound.y + extendY;
   1492       node.userData = userData;
   1493       this.InsertLeaf(node);
   1494       return node;
   1495    }
   1496    b2DynamicTree.prototype.DestroyProxy = function (proxy) {
   1497       this.RemoveLeaf(proxy);
   1498       this.FreeNode(proxy);
   1499    }
   1500    b2DynamicTree.prototype.MoveProxy = function (proxy, aabb, displacement) {
   1501       b2Settings.b2Assert(proxy.IsLeaf());
   1502       if (proxy.aabb.Contains(aabb)) {
   1503          return false;
   1504       }
   1505       this.RemoveLeaf(proxy);
   1506       var extendX = b2Settings.b2_aabbExtension + b2Settings.b2_aabbMultiplier * (displacement.x > 0 ? displacement.x : (-displacement.x));
   1507       var extendY = b2Settings.b2_aabbExtension + b2Settings.b2_aabbMultiplier * (displacement.y > 0 ? displacement.y : (-displacement.y));
   1508       proxy.aabb.lowerBound.x = aabb.lowerBound.x - extendX;
   1509       proxy.aabb.lowerBound.y = aabb.lowerBound.y - extendY;
   1510       proxy.aabb.upperBound.x = aabb.upperBound.x + extendX;
   1511       proxy.aabb.upperBound.y = aabb.upperBound.y + extendY;
   1512       this.InsertLeaf(proxy);
   1513       return true;
   1514    }
   1515    b2DynamicTree.prototype.Rebalance = function (iterations) {
   1516       if (iterations === undefined) iterations = 0;
   1517       if (this.m_root == null) return;
   1518       for (var i = 0; i < iterations; i++) {
   1519          var node = this.m_root;
   1520          var bit = 0;
   1521          while (node.IsLeaf() == false) {
   1522             node = (this.m_path >> bit) & 1 ? node.child2 : node.child1;
   1523             bit = (bit + 1) & 31;
   1524          }++this.m_path;
   1525          this.RemoveLeaf(node);
   1526          this.InsertLeaf(node);
   1527       }
   1528    }
   1529    b2DynamicTree.prototype.GetFatAABB = function (proxy) {
   1530       return proxy.aabb;
   1531    }
   1532    b2DynamicTree.prototype.GetUserData = function (proxy) {
   1533       return proxy.userData;
   1534    }
   1535    b2DynamicTree.prototype.Query = function (callback, aabb) {
   1536       if (this.m_root == null) return;
   1537       var stack = new Vector();
   1538       var count = 0;
   1539       stack[count++] = this.m_root;
   1540       while (count > 0) {
   1541          var node = stack[--count];
   1542          if (node.aabb.TestOverlap(aabb)) {
   1543             if (node.IsLeaf()) {
   1544                var proceed = callback(node);
   1545                if (!proceed) return;
   1546             }
   1547             else {
   1548                stack[count++] = node.child1;
   1549                stack[count++] = node.child2;
   1550             }
   1551          }
   1552       }
   1553    }
   1554    b2DynamicTree.prototype.RayCast = function (callback, input) {
   1555       if (this.m_root == null) return;
   1556       var p1 = input.p1;
   1557       var p2 = input.p2;
   1558       var r = b2Math.SubtractVV(p1, p2);
   1559       r.Normalize();
   1560       var v = b2Math.CrossFV(1.0, r);
   1561       var abs_v = b2Math.AbsV(v);
   1562       var maxFraction = input.maxFraction;
   1563       var segmentAABB = new b2AABB();
   1564       var tX = 0;
   1565       var tY = 0; {
   1566          tX = p1.x + maxFraction * (p2.x - p1.x);
   1567          tY = p1.y + maxFraction * (p2.y - p1.y);
   1568          segmentAABB.lowerBound.x = Math.min(p1.x, tX);
   1569          segmentAABB.lowerBound.y = Math.min(p1.y, tY);
   1570          segmentAABB.upperBound.x = Math.max(p1.x, tX);
   1571          segmentAABB.upperBound.y = Math.max(p1.y, tY);
   1572       }
   1573       var stack = new Vector();
   1574       var count = 0;
   1575       stack[count++] = this.m_root;
   1576       while (count > 0) {
   1577          var node = stack[--count];
   1578          if (node.aabb.TestOverlap(segmentAABB) == false) {
   1579             continue;
   1580          }
   1581          var c = node.aabb.GetCenter();
   1582          var h = node.aabb.GetExtents();
   1583          var separation = Math.abs(v.x * (p1.x - c.x) + v.y * (p1.y - c.y)) - abs_v.x * h.x - abs_v.y * h.y;
   1584          if (separation > 0.0) continue;
   1585          if (node.IsLeaf()) {
   1586             var subInput = new b2RayCastInput();
   1587             subInput.p1 = input.p1;
   1588             subInput.p2 = input.p2;
   1589             subInput.maxFraction = input.maxFraction;
   1590             maxFraction = callback(subInput, node);
   1591             if (maxFraction == 0.0) return;
   1592             if (maxFraction > 0.0) {
   1593                tX = p1.x + maxFraction * (p2.x - p1.x);
   1594                tY = p1.y + maxFraction * (p2.y - p1.y);
   1595                segmentAABB.lowerBound.x = Math.min(p1.x, tX);
   1596                segmentAABB.lowerBound.y = Math.min(p1.y, tY);
   1597                segmentAABB.upperBound.x = Math.max(p1.x, tX);
   1598                segmentAABB.upperBound.y = Math.max(p1.y, tY);
   1599             }
   1600          }
   1601          else {
   1602             stack[count++] = node.child1;
   1603             stack[count++] = node.child2;
   1604          }
   1605       }
   1606    }
   1607    b2DynamicTree.prototype.AllocateNode = function () {
   1608       if (this.m_freeList) {
   1609          var node = this.m_freeList;
   1610          this.m_freeList = node.parent;
   1611          node.parent = null;
   1612          node.child1 = null;
   1613          node.child2 = null;
   1614          return node;
   1615       }
   1616       return new b2DynamicTreeNode();
   1617    }
   1618    b2DynamicTree.prototype.FreeNode = function (node) {
   1619       node.parent = this.m_freeList;
   1620       this.m_freeList = node;
   1621    }
   1622    b2DynamicTree.prototype.InsertLeaf = function (leaf) {
   1623       ++this.m_insertionCount;
   1624       if (this.m_root == null) {
   1625          this.m_root = leaf;
   1626          this.m_root.parent = null;
   1627          return;
   1628       }
   1629       var center = leaf.aabb.GetCenter();
   1630       var sibling = this.m_root;
   1631       if (sibling.IsLeaf() == false) {
   1632          do {
   1633             var child1 = sibling.child1;
   1634             var child2 = sibling.child2;
   1635             var norm1 = Math.abs((child1.aabb.lowerBound.x + child1.aabb.upperBound.x) / 2 - center.x) + Math.abs((child1.aabb.lowerBound.y + child1.aabb.upperBound.y) / 2 - center.y);
   1636             var norm2 = Math.abs((child2.aabb.lowerBound.x + child2.aabb.upperBound.x) / 2 - center.x) + Math.abs((child2.aabb.lowerBound.y + child2.aabb.upperBound.y) / 2 - center.y);
   1637             if (norm1 < norm2) {
   1638                sibling = child1;
   1639             }
   1640             else {
   1641                sibling = child2;
   1642             }
   1643          }
   1644          while (sibling.IsLeaf() == false)
   1645       }
   1646       var node1 = sibling.parent;
   1647       var node2 = this.AllocateNode();
   1648       node2.parent = node1;
   1649       node2.userData = null;
   1650       node2.aabb.Combine(leaf.aabb, sibling.aabb);
   1651       if (node1) {
   1652          if (sibling.parent.child1 == sibling) {
   1653             node1.child1 = node2;
   1654          }
   1655          else {
   1656             node1.child2 = node2;
   1657          }
   1658          node2.child1 = sibling;
   1659          node2.child2 = leaf;
   1660          sibling.parent = node2;
   1661          leaf.parent = node2;
   1662          do {
   1663             if (node1.aabb.Contains(node2.aabb)) break;
   1664             node1.aabb.Combine(node1.child1.aabb, node1.child2.aabb);
   1665             node2 = node1;
   1666             node1 = node1.parent;
   1667          }
   1668          while (node1)
   1669       }
   1670       else {
   1671          node2.child1 = sibling;
   1672          node2.child2 = leaf;
   1673          sibling.parent = node2;
   1674          leaf.parent = node2;
   1675          this.m_root = node2;
   1676       }
   1677    }
   1678    b2DynamicTree.prototype.RemoveLeaf = function (leaf) {
   1679       if (leaf == this.m_root) {
   1680          this.m_root = null;
   1681          return;
   1682       }
   1683       var node2 = leaf.parent;
   1684       var node1 = node2.parent;
   1685       var sibling;
   1686       if (node2.child1 == leaf) {
   1687          sibling = node2.child2;
   1688       }
   1689       else {
   1690          sibling = node2.child1;
   1691       }
   1692       if (node1) {
   1693          if (node1.child1 == node2) {
   1694             node1.child1 = sibling;
   1695          }
   1696          else {
   1697             node1.child2 = sibling;
   1698          }
   1699          sibling.parent = node1;
   1700          this.FreeNode(node2);
   1701          while (node1) {
   1702             var oldAABB = node1.aabb;
   1703             node1.aabb = b2AABB.Combine(node1.child1.aabb, node1.child2.aabb);
   1704             if (oldAABB.Contains(node1.aabb)) break;
   1705             node1 = node1.parent;
   1706          }
   1707       }
   1708       else {
   1709          this.m_root = sibling;
   1710          sibling.parent = null;
   1711          this.FreeNode(node2);
   1712       }
   1713    }
   1714    b2DynamicTreeBroadPhase.b2DynamicTreeBroadPhase = function () {
   1715       this.m_tree = new b2DynamicTree();
   1716       this.m_moveBuffer = new Vector();
   1717       this.m_pairBuffer = new Vector();
   1718       this.m_pairCount = 0;
   1719    };
   1720    b2DynamicTreeBroadPhase.prototype.CreateProxy = function (aabb, userData) {
   1721       var proxy = this.m_tree.CreateProxy(aabb, userData);
   1722       ++this.m_proxyCount;
   1723       this.BufferMove(proxy);
   1724       return proxy;
   1725    }
   1726    b2DynamicTreeBroadPhase.prototype.DestroyProxy = function (proxy) {
   1727       this.UnBufferMove(proxy);
   1728       --this.m_proxyCount;
   1729       this.m_tree.DestroyProxy(proxy);
   1730    }
   1731    b2DynamicTreeBroadPhase.prototype.MoveProxy = function (proxy, aabb, displacement) {
   1732       var buffer = this.m_tree.MoveProxy(proxy, aabb, displacement);
   1733       if (buffer) {
   1734          this.BufferMove(proxy);
   1735       }
   1736    }
   1737    b2DynamicTreeBroadPhase.prototype.TestOverlap = function (proxyA, proxyB) {
   1738       var aabbA = this.m_tree.GetFatAABB(proxyA);
   1739       var aabbB = this.m_tree.GetFatAABB(proxyB);
   1740       return aabbA.TestOverlap(aabbB);
   1741    }
   1742    b2DynamicTreeBroadPhase.prototype.GetUserData = function (proxy) {
   1743       return this.m_tree.GetUserData(proxy);
   1744    }
   1745    b2DynamicTreeBroadPhase.prototype.GetFatAABB = function (proxy) {
   1746       return this.m_tree.GetFatAABB(proxy);
   1747    }
   1748    b2DynamicTreeBroadPhase.prototype.GetProxyCount = function () {
   1749       return this.m_proxyCount;
   1750    }
   1751    b2DynamicTreeBroadPhase.prototype.UpdatePairs = function (callback) {
   1752       var __this = this;
   1753       __this.m_pairCount = 0;
   1754       var i = 0,
   1755          queryProxy;
   1756       for (i = 0;
   1757       i < __this.m_moveBuffer.length; ++i) {
   1758          queryProxy = __this.m_moveBuffer[i];
   1759 
   1760          function QueryCallback(proxy) {
   1761             if (proxy == queryProxy) return true;
   1762             if (__this.m_pairCount == __this.m_pairBuffer.length) {
   1763                __this.m_pairBuffer[__this.m_pairCount] = new b2DynamicTreePair();
   1764             }
   1765             var pair = __this.m_pairBuffer[__this.m_pairCount];
   1766             pair.proxyA = proxy < queryProxy ? proxy : queryProxy;
   1767             pair.proxyB = proxy >= queryProxy ? proxy : queryProxy;++__this.m_pairCount;
   1768             return true;
   1769          };
   1770          var fatAABB = __this.m_tree.GetFatAABB(queryProxy);
   1771          __this.m_tree.Query(QueryCallback, fatAABB);
   1772       }
   1773       __this.m_moveBuffer.length = 0;
   1774       for (var i = 0; i < __this.m_pairCount;) {
   1775          var primaryPair = __this.m_pairBuffer[i];
   1776          var userDataA = __this.m_tree.GetUserData(primaryPair.proxyA);
   1777          var userDataB = __this.m_tree.GetUserData(primaryPair.proxyB);
   1778          callback(userDataA, userDataB);
   1779          ++i;
   1780          while (i < __this.m_pairCount) {
   1781             var pair = __this.m_pairBuffer[i];
   1782             if (pair.proxyA != primaryPair.proxyA || pair.proxyB != primaryPair.proxyB) {
   1783                break;
   1784             }++i;
   1785          }
   1786       }
   1787    }
   1788    b2DynamicTreeBroadPhase.prototype.Query = function (callback, aabb) {
   1789       this.m_tree.Query(callback, aabb);
   1790    }
   1791    b2DynamicTreeBroadPhase.prototype.RayCast = function (callback, input) {
   1792       this.m_tree.RayCast(callback, input);
   1793    }
   1794    b2DynamicTreeBroadPhase.prototype.Validate = function () {}
   1795    b2DynamicTreeBroadPhase.prototype.Rebalance = function (iterations) {
   1796       if (iterations === undefined) iterations = 0;
   1797       this.m_tree.Rebalance(iterations);
   1798    }
   1799    b2DynamicTreeBroadPhase.prototype.BufferMove = function (proxy) {
   1800       this.m_moveBuffer[this.m_moveBuffer.length] = proxy;
   1801    }
   1802    b2DynamicTreeBroadPhase.prototype.UnBufferMove = function (proxy) {
   1803       var i = parseInt(this.m_moveBuffer.indexOf(proxy));
   1804       this.m_moveBuffer.splice(i, 1);
   1805    }
   1806    b2DynamicTreeBroadPhase.prototype.ComparePairs = function (pair1, pair2) {
   1807       return 0;
   1808    }
   1809    b2DynamicTreeBroadPhase.__implements = {};
   1810    b2DynamicTreeBroadPhase.__implements[IBroadPhase] = true;
   1811    b2DynamicTreeNode.b2DynamicTreeNode = function () {
   1812       this.aabb = new b2AABB();
   1813    };
   1814    b2DynamicTreeNode.prototype.IsLeaf = function () {
   1815       return this.child1 == null;
   1816    }
   1817    b2DynamicTreePair.b2DynamicTreePair = function () {};
   1818    b2Manifold.b2Manifold = function () {
   1819       this.m_pointCount = 0;
   1820    };
   1821    b2Manifold.prototype.b2Manifold = function () {
   1822       this.m_points = new Vector(b2Settings.b2_maxManifoldPoints);
   1823       for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
   1824          this.m_points[i] = new b2ManifoldPoint();
   1825       }
   1826       this.m_localPlaneNormal = new b2Vec2();
   1827       this.m_localPoint = new b2Vec2();
   1828    }
   1829    b2Manifold.prototype.Reset = function () {
   1830       for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
   1831          ((this.m_points[i] instanceof b2ManifoldPoint ? this.m_points[i] : null)).Reset();
   1832       }
   1833       this.m_localPlaneNormal.SetZero();
   1834       this.m_localPoint.SetZero();
   1835       this.m_type = 0;
   1836       this.m_pointCount = 0;
   1837    }
   1838    b2Manifold.prototype.Set = function (m) {
   1839       this.m_pointCount = m.m_pointCount;
   1840       for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
   1841          ((this.m_points[i] instanceof b2ManifoldPoint ? this.m_points[i] : null)).Set(m.m_points[i]);
   1842       }
   1843       this.m_localPlaneNormal.SetV(m.m_localPlaneNormal);
   1844       this.m_localPoint.SetV(m.m_localPoint);
   1845       this.m_type = m.m_type;
   1846    }
   1847    b2Manifold.prototype.Copy = function () {
   1848       var copy = new b2Manifold();
   1849       copy.Set(this);
   1850       return copy;
   1851    }
   1852    Box2D.postDefs.push(function () {
   1853       Box2D.Collision.b2Manifold.e_circles = 0x0001;
   1854       Box2D.Collision.b2Manifold.e_faceA = 0x0002;
   1855       Box2D.Collision.b2Manifold.e_faceB = 0x0004;
   1856    });
   1857    b2ManifoldPoint.b2ManifoldPoint = function () {
   1858       this.m_localPoint = new b2Vec2();
   1859       this.m_id = new b2ContactID();
   1860    };
   1861    b2ManifoldPoint.prototype.b2ManifoldPoint = function () {
   1862       this.Reset();
   1863    }
   1864    b2ManifoldPoint.prototype.Reset = function () {
   1865       this.m_localPoint.SetZero();
   1866       this.m_normalImpulse = 0.0;
   1867       this.m_tangentImpulse = 0.0;
   1868       this.m_id.key = 0;
   1869    }
   1870    b2ManifoldPoint.prototype.Set = function (m) {
   1871       this.m_localPoint.SetV(m.m_localPoint);
   1872       this.m_normalImpulse = m.m_normalImpulse;
   1873       this.m_tangentImpulse = m.m_tangentImpulse;
   1874       this.m_id.Set(m.m_id);
   1875    }
   1876    b2Point.b2Point = function () {
   1877       this.p = new b2Vec2();
   1878    };
   1879    b2Point.prototype.Support = function (xf, vX, vY) {
   1880       if (vX === undefined) vX = 0;
   1881       if (vY === undefined) vY = 0;
   1882       return this.p;
   1883    }
   1884    b2Point.prototype.GetFirstVertex = function (xf) {
   1885       return this.p;
   1886    }
   1887    b2RayCastInput.b2RayCastInput = function () {
   1888       this.p1 = new b2Vec2();
   1889       this.p2 = new b2Vec2();
   1890    };
   1891    b2RayCastInput.prototype.b2RayCastInput = function (p1, p2, maxFraction) {
   1892       if (p1 === undefined) p1 = null;
   1893       if (p2 === undefined) p2 = null;
   1894       if (maxFraction === undefined) maxFraction = 1;
   1895       if (p1) this.p1.SetV(p1);
   1896       if (p2) this.p2.SetV(p2);
   1897       this.maxFraction = maxFraction;
   1898    }
   1899    b2RayCastOutput.b2RayCastOutput = function () {
   1900       this.normal = new b2Vec2();
   1901    };
   1902    b2Segment.b2Segment = function () {
   1903       this.p1 = new b2Vec2();
   1904       this.p2 = new b2Vec2();
   1905    };
   1906    b2Segment.prototype.TestSegment = function (lambda, normal, segment, maxLambda) {
   1907       if (maxLambda === undefined) maxLambda = 0;
   1908       var s = segment.p1;
   1909       var rX = segment.p2.x - s.x;
   1910       var rY = segment.p2.y - s.y;
   1911       var dX = this.p2.x - this.p1.x;
   1912       var dY = this.p2.y - this.p1.y;
   1913       var nX = dY;
   1914       var nY = (-dX);
   1915       var k_slop = 100.0 * Number.MIN_VALUE;
   1916       var denom = (-(rX * nX + rY * nY));
   1917       if (denom > k_slop) {
   1918          var bX = s.x - this.p1.x;
   1919          var bY = s.y - this.p1.y;
   1920          var a = (bX * nX + bY * nY);
   1921          if (0.0 <= a && a <= maxLambda * denom) {
   1922             var mu2 = (-rX * bY) + rY * bX;
   1923             if ((-k_slop * denom) <= mu2 && mu2 <= denom * (1.0 + k_slop)) {
   1924                a /= denom;
   1925                var nLen = Math.sqrt(nX * nX + nY * nY);
   1926                nX /= nLen;
   1927                nY /= nLen;
   1928                lambda[0] = a;
   1929                normal.Set(nX, nY);
   1930                return true;
   1931             }
   1932          }
   1933       }
   1934       return false;
   1935    }
   1936    b2Segment.prototype.Extend = function (aabb) {
   1937       this.ExtendForward(aabb);
   1938       this.ExtendBackward(aabb);
   1939    }
   1940    b2Segment.prototype.ExtendForward = function (aabb) {
   1941       var dX = this.p2.x - this.p1.x;
   1942       var dY = this.p2.y - this.p1.y;
   1943       var lambda = Math.min(dX > 0 ? (aabb.upperBound.x - this.p1.x) / dX : dX < 0 ? (aabb.lowerBound.x - this.p1.x) / dX : Number.POSITIVE_INFINITY,
   1944       dY > 0 ? (aabb.upperBound.y - this.p1.y) / dY : dY < 0 ? (aabb.lowerBound.y - this.p1.y) / dY : Number.POSITIVE_INFINITY);
   1945       this.p2.x = this.p1.x + dX * lambda;
   1946       this.p2.y = this.p1.y + dY * lambda;
   1947    }
   1948    b2Segment.prototype.ExtendBackward = function (aabb) {
   1949       var dX = (-this.p2.x) + this.p1.x;
   1950       var dY = (-this.p2.y) + this.p1.y;
   1951       var lambda = Math.min(dX > 0 ? (aabb.upperBound.x - this.p2.x) / dX : dX < 0 ? (aabb.lowerBound.x - this.p2.x) / dX : Number.POSITIVE_INFINITY,
   1952       dY > 0 ? (aabb.upperBound.y - this.p2.y) / dY : dY < 0 ? (aabb.lowerBound.y - this.p2.y) / dY : Number.POSITIVE_INFINITY);
   1953       this.p1.x = this.p2.x + dX * lambda;
   1954       this.p1.y = this.p2.y + dY * lambda;
   1955    }
   1956    b2SeparationFunction.b2SeparationFunction = function () {
   1957       this.m_localPoint = new b2Vec2();
   1958       this.m_axis = new b2Vec2();
   1959    };
   1960    b2SeparationFunction.prototype.Initialize = function (cache, proxyA, transformA, proxyB, transformB) {
   1961       this.m_proxyA = proxyA;
   1962       this.m_proxyB = proxyB;
   1963       var count = parseInt(cache.count);
   1964       b2Settings.b2Assert(0 < count && count < 3);
   1965       var localPointA;
   1966       var localPointA1;
   1967       var localPointA2;
   1968       var localPointB;
   1969       var localPointB1;
   1970       var localPointB2;
   1971       var pointAX = 0;
   1972       var pointAY = 0;
   1973       var pointBX = 0;
   1974       var pointBY = 0;
   1975       var normalX = 0;
   1976       var normalY = 0;
   1977       var tMat;
   1978       var tVec;
   1979       var s = 0;
   1980       var sgn = 0;
   1981       if (count == 1) {
   1982          this.m_type = b2SeparationFunction.e_points;
   1983          localPointA = this.m_proxyA.GetVertex(cache.indexA[0]);
   1984          localPointB = this.m_proxyB.GetVertex(cache.indexB[0]);
   1985          tVec = localPointA;
   1986          tMat = transformA.R;
   1987          pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   1988          pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   1989          tVec = localPointB;
   1990          tMat = transformB.R;
   1991          pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   1992          pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   1993          this.m_axis.x = pointBX - pointAX;
   1994          this.m_axis.y = pointBY - pointAY;
   1995          this.m_axis.Normalize();
   1996       }
   1997       else if (cache.indexB[0] == cache.indexB[1]) {
   1998          this.m_type = b2SeparationFunction.e_faceA;
   1999          localPointA1 = this.m_proxyA.GetVertex(cache.indexA[0]);
   2000          localPointA2 = this.m_proxyA.GetVertex(cache.indexA[1]);
   2001          localPointB = this.m_proxyB.GetVertex(cache.indexB[0]);
   2002          this.m_localPoint.x = 0.5 * (localPointA1.x + localPointA2.x);
   2003          this.m_localPoint.y = 0.5 * (localPointA1.y + localPointA2.y);
   2004          this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointA2, localPointA1), 1.0);
   2005          this.m_axis.Normalize();
   2006          tVec = this.m_axis;
   2007          tMat = transformA.R;
   2008          normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2009          normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2010          tVec = this.m_localPoint;
   2011          tMat = transformA.R;
   2012          pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   2013          pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   2014          tVec = localPointB;
   2015          tMat = transformB.R;
   2016          pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   2017          pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   2018          s = (pointBX - pointAX) * normalX + (pointBY - pointAY) * normalY;
   2019          if (s < 0.0) {
   2020             this.m_axis.NegativeSelf();
   2021          }
   2022       }
   2023       else if (cache.indexA[0] == cache.indexA[0]) {
   2024          this.m_type = b2SeparationFunction.e_faceB;
   2025          localPointB1 = this.m_proxyB.GetVertex(cache.indexB[0]);
   2026          localPointB2 = this.m_proxyB.GetVertex(cache.indexB[1]);
   2027          localPointA = this.m_proxyA.GetVertex(cache.indexA[0]);
   2028          this.m_localPoint.x = 0.5 * (localPointB1.x + localPointB2.x);
   2029          this.m_localPoint.y = 0.5 * (localPointB1.y + localPointB2.y);
   2030          this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointB2, localPointB1), 1.0);
   2031          this.m_axis.Normalize();
   2032          tVec = this.m_axis;
   2033          tMat = transformB.R;
   2034          normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2035          normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2036          tVec = this.m_localPoint;
   2037          tMat = transformB.R;
   2038          pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   2039          pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   2040          tVec = localPointA;
   2041          tMat = transformA.R;
   2042          pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   2043          pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   2044          s = (pointAX - pointBX) * normalX + (pointAY - pointBY) * normalY;
   2045          if (s < 0.0) {
   2046             this.m_axis.NegativeSelf();
   2047          }
   2048       }
   2049       else {
   2050          localPointA1 = this.m_proxyA.GetVertex(cache.indexA[0]);
   2051          localPointA2 = this.m_proxyA.GetVertex(cache.indexA[1]);
   2052          localPointB1 = this.m_proxyB.GetVertex(cache.indexB[0]);
   2053          localPointB2 = this.m_proxyB.GetVertex(cache.indexB[1]);
   2054          var pA = b2Math.MulX(transformA, localPointA);
   2055          var dA = b2Math.MulMV(transformA.R, b2Math.SubtractVV(localPointA2, localPointA1));
   2056          var pB = b2Math.MulX(transformB, localPointB);
   2057          var dB = b2Math.MulMV(transformB.R, b2Math.SubtractVV(localPointB2, localPointB1));
   2058          var a = dA.x * dA.x + dA.y * dA.y;
   2059          var e = dB.x * dB.x + dB.y * dB.y;
   2060          var r = b2Math.SubtractVV(dB, dA);
   2061          var c = dA.x * r.x + dA.y * r.y;
   2062          var f = dB.x * r.x + dB.y * r.y;
   2063          var b = dA.x * dB.x + dA.y * dB.y;
   2064          var denom = a * e - b * b;
   2065          s = 0.0;
   2066          if (denom != 0.0) {
   2067             s = b2Math.Clamp((b * f - c * e) / denom, 0.0, 1.0);
   2068          }
   2069          var t = (b * s + f) / e;
   2070          if (t < 0.0) {
   2071             t = 0.0;
   2072             s = b2Math.Clamp((b - c) / a, 0.0, 1.0);
   2073          }
   2074          localPointA = new b2Vec2();
   2075          localPointA.x = localPointA1.x + s * (localPointA2.x - localPointA1.x);
   2076          localPointA.y = localPointA1.y + s * (localPointA2.y - localPointA1.y);
   2077          localPointB = new b2Vec2();
   2078          localPointB.x = localPointB1.x + s * (localPointB2.x - localPointB1.x);
   2079          localPointB.y = localPointB1.y + s * (localPointB2.y - localPointB1.y);
   2080          if (s == 0.0 || s == 1.0) {
   2081             this.m_type = b2SeparationFunction.e_faceB;
   2082             this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointB2, localPointB1), 1.0);
   2083             this.m_axis.Normalize();
   2084             this.m_localPoint = localPointB;
   2085             tVec = this.m_axis;
   2086             tMat = transformB.R;
   2087             normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2088             normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2089             tVec = this.m_localPoint;
   2090             tMat = transformB.R;
   2091             pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   2092             pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   2093             tVec = localPointA;
   2094             tMat = transformA.R;
   2095             pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   2096             pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   2097             sgn = (pointAX - pointBX) * normalX + (pointAY - pointBY) * normalY;
   2098             if (s < 0.0) {
   2099                this.m_axis.NegativeSelf();
   2100             }
   2101          }
   2102          else {
   2103             this.m_type = b2SeparationFunction.e_faceA;
   2104             this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointA2, localPointA1), 1.0);
   2105             this.m_localPoint = localPointA;
   2106             tVec = this.m_axis;
   2107             tMat = transformA.R;
   2108             normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2109             normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2110             tVec = this.m_localPoint;
   2111             tMat = transformA.R;
   2112             pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   2113             pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   2114             tVec = localPointB;
   2115             tMat = transformB.R;
   2116             pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   2117             pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   2118             sgn = (pointBX - pointAX) * normalX + (pointBY - pointAY) * normalY;
   2119             if (s < 0.0) {
   2120                this.m_axis.NegativeSelf();
   2121             }
   2122          }
   2123       }
   2124    }
   2125    b2SeparationFunction.prototype.Evaluate = function (transformA, transformB) {
   2126       var axisA;
   2127       var axisB;
   2128       var localPointA;
   2129       var localPointB;
   2130       var pointA;
   2131       var pointB;
   2132       var seperation = 0;
   2133       var normal;
   2134       switch (this.m_type) {
   2135       case b2SeparationFunction.e_points:
   2136          {
   2137             axisA = b2Math.MulTMV(transformA.R, this.m_axis);
   2138             axisB = b2Math.MulTMV(transformB.R, this.m_axis.GetNegative());
   2139             localPointA = this.m_proxyA.GetSupportVertex(axisA);
   2140             localPointB = this.m_proxyB.GetSupportVertex(axisB);
   2141             pointA = b2Math.MulX(transformA, localPointA);
   2142             pointB = b2Math.MulX(transformB, localPointB);
   2143             seperation = (pointB.x - pointA.x) * this.m_axis.x + (pointB.y - pointA.y) * this.m_axis.y;
   2144             return seperation;
   2145          }
   2146       case b2SeparationFunction.e_faceA:
   2147          {
   2148             normal = b2Math.MulMV(transformA.R, this.m_axis);
   2149             pointA = b2Math.MulX(transformA, this.m_localPoint);
   2150             axisB = b2Math.MulTMV(transformB.R, normal.GetNegative());
   2151             localPointB = this.m_proxyB.GetSupportVertex(axisB);
   2152             pointB = b2Math.MulX(transformB, localPointB);
   2153             seperation = (pointB.x - pointA.x) * normal.x + (pointB.y - pointA.y) * normal.y;
   2154             return seperation;
   2155          }
   2156       case b2SeparationFunction.e_faceB:
   2157          {
   2158             normal = b2Math.MulMV(transformB.R, this.m_axis);
   2159             pointB = b2Math.MulX(transformB, this.m_localPoint);
   2160             axisA = b2Math.MulTMV(transformA.R, normal.GetNegative());
   2161             localPointA = this.m_proxyA.GetSupportVertex(axisA);
   2162             pointA = b2Math.MulX(transformA, localPointA);
   2163             seperation = (pointA.x - pointB.x) * normal.x + (pointA.y - pointB.y) * normal.y;
   2164             return seperation;
   2165          }
   2166       default:
   2167          b2Settings.b2Assert(false);
   2168          return 0.0;
   2169       }
   2170    }
   2171    Box2D.postDefs.push(function () {
   2172       Box2D.Collision.b2SeparationFunction.e_points = 0x01;
   2173       Box2D.Collision.b2SeparationFunction.e_faceA = 0x02;
   2174       Box2D.Collision.b2SeparationFunction.e_faceB = 0x04;
   2175    });
   2176    b2Simplex.b2Simplex = function () {
   2177       this.m_v1 = new b2SimplexVertex();
   2178       this.m_v2 = new b2SimplexVertex();
   2179       this.m_v3 = new b2SimplexVertex();
   2180       this.m_vertices = new Vector(3);
   2181    };
   2182    b2Simplex.prototype.b2Simplex = function () {
   2183       this.m_vertices[0] = this.m_v1;
   2184       this.m_vertices[1] = this.m_v2;
   2185       this.m_vertices[2] = this.m_v3;
   2186    }
   2187    b2Simplex.prototype.ReadCache = function (cache, proxyA, transformA, proxyB, transformB) {
   2188       b2Settings.b2Assert(0 <= cache.count && cache.count <= 3);
   2189       var wALocal;
   2190       var wBLocal;
   2191       this.m_count = cache.count;
   2192       var vertices = this.m_vertices;
   2193       for (var i = 0; i < this.m_count; i++) {
   2194          var v = vertices[i];
   2195          v.indexA = cache.indexA[i];
   2196          v.indexB = cache.indexB[i];
   2197          wALocal = proxyA.GetVertex(v.indexA);
   2198          wBLocal = proxyB.GetVertex(v.indexB);
   2199          v.wA = b2Math.MulX(transformA, wALocal);
   2200          v.wB = b2Math.MulX(transformB, wBLocal);
   2201          v.w = b2Math.SubtractVV(v.wB, v.wA);
   2202          v.a = 0;
   2203       }
   2204       if (this.m_count > 1) {
   2205          var metric1 = cache.metric;
   2206          var metric2 = this.GetMetric();
   2207          if (metric2 < .5 * metric1 || 2.0 * metric1 < metric2 || metric2 < Number.MIN_VALUE) {
   2208             this.m_count = 0;
   2209          }
   2210       }
   2211       if (this.m_count == 0) {
   2212          v = vertices[0];
   2213          v.indexA = 0;
   2214          v.indexB = 0;
   2215          wALocal = proxyA.GetVertex(0);
   2216          wBLocal = proxyB.GetVertex(0);
   2217          v.wA = b2Math.MulX(transformA, wALocal);
   2218          v.wB = b2Math.MulX(transformB, wBLocal);
   2219          v.w = b2Math.SubtractVV(v.wB, v.wA);
   2220          this.m_count = 1;
   2221       }
   2222    }
   2223    b2Simplex.prototype.WriteCache = function (cache) {
   2224       cache.metric = this.GetMetric();
   2225       cache.count = Box2D.parseUInt(this.m_count);
   2226       var vertices = this.m_vertices;
   2227       for (var i = 0; i < this.m_count; i++) {
   2228          cache.indexA[i] = Box2D.parseUInt(vertices[i].indexA);
   2229          cache.indexB[i] = Box2D.parseUInt(vertices[i].indexB);
   2230       }
   2231    }
   2232    b2Simplex.prototype.GetSearchDirection = function () {
   2233       switch (this.m_count) {
   2234       case 1:
   2235          return this.m_v1.w.GetNegative();
   2236       case 2:
   2237          {
   2238             var e12 = b2Math.SubtractVV(this.m_v2.w, this.m_v1.w);
   2239             var sgn = b2Math.CrossVV(e12, this.m_v1.w.GetNegative());
   2240             if (sgn > 0.0) {
   2241                return b2Math.CrossFV(1.0, e12);
   2242             }
   2243             else {
   2244                return b2Math.CrossVF(e12, 1.0);
   2245             }
   2246          }
   2247       default:
   2248          b2Settings.b2Assert(false);
   2249          return new b2Vec2();
   2250       }
   2251    }
   2252    b2Simplex.prototype.GetClosestPoint = function () {
   2253       switch (this.m_count) {
   2254       case 0:
   2255          b2Settings.b2Assert(false);
   2256          return new b2Vec2();
   2257       case 1:
   2258          return this.m_v1.w;
   2259       case 2:
   2260          return new b2Vec2(this.m_v1.a * this.m_v1.w.x + this.m_v2.a * this.m_v2.w.x, this.m_v1.a * this.m_v1.w.y + this.m_v2.a * this.m_v2.w.y);
   2261       default:
   2262          b2Settings.b2Assert(false);
   2263          return new b2Vec2();
   2264       }
   2265    }
   2266    b2Simplex.prototype.GetWitnessPoints = function (pA, pB) {
   2267       switch (this.m_count) {
   2268       case 0:
   2269          b2Settings.b2Assert(false);
   2270          break;
   2271       case 1:
   2272          pA.SetV(this.m_v1.wA);
   2273          pB.SetV(this.m_v1.wB);
   2274          break;
   2275       case 2:
   2276          pA.x = this.m_v1.a * this.m_v1.wA.x + this.m_v2.a * this.m_v2.wA.x;
   2277          pA.y = this.m_v1.a * this.m_v1.wA.y + this.m_v2.a * this.m_v2.wA.y;
   2278          pB.x = this.m_v1.a * this.m_v1.wB.x + this.m_v2.a * this.m_v2.wB.x;
   2279          pB.y = this.m_v1.a * this.m_v1.wB.y + this.m_v2.a * this.m_v2.wB.y;
   2280          break;
   2281       case 3:
   2282          pB.x = pA.x = this.m_v1.a * this.m_v1.wA.x + this.m_v2.a * this.m_v2.wA.x + this.m_v3.a * this.m_v3.wA.x;
   2283          pB.y = pA.y = this.m_v1.a * this.m_v1.wA.y + this.m_v2.a * this.m_v2.wA.y + this.m_v3.a * this.m_v3.wA.y;
   2284          break;
   2285       default:
   2286          b2Settings.b2Assert(false);
   2287          break;
   2288       }
   2289    }
   2290    b2Simplex.prototype.GetMetric = function () {
   2291       switch (this.m_count) {
   2292       case 0:
   2293          b2Settings.b2Assert(false);
   2294          return 0.0;
   2295       case 1:
   2296          return 0.0;
   2297       case 2:
   2298          return b2Math.SubtractVV(this.m_v1.w, this.m_v2.w).Length();
   2299       case 3:
   2300          return b2Math.CrossVV(b2Math.SubtractVV(this.m_v2.w, this.m_v1.w), b2Math.SubtractVV(this.m_v3.w, this.m_v1.w));
   2301       default:
   2302          b2Settings.b2Assert(false);
   2303          return 0.0;
   2304       }
   2305    }
   2306    b2Simplex.prototype.Solve2 = function () {
   2307       var w1 = this.m_v1.w;
   2308       var w2 = this.m_v2.w;
   2309       var e12 = b2Math.SubtractVV(w2, w1);
   2310       var d12_2 = (-(w1.x * e12.x + w1.y * e12.y));
   2311       if (d12_2 <= 0.0) {
   2312          this.m_v1.a = 1.0;
   2313          this.m_count = 1;
   2314          return;
   2315       }
   2316       var d12_1 = (w2.x * e12.x + w2.y * e12.y);
   2317       if (d12_1 <= 0.0) {
   2318          this.m_v2.a = 1.0;
   2319          this.m_count = 1;
   2320          this.m_v1.Set(this.m_v2);
   2321          return;
   2322       }
   2323       var inv_d12 = 1.0 / (d12_1 + d12_2);
   2324       this.m_v1.a = d12_1 * inv_d12;
   2325       this.m_v2.a = d12_2 * inv_d12;
   2326       this.m_count = 2;
   2327    }
   2328    b2Simplex.prototype.Solve3 = function () {
   2329       var w1 = this.m_v1.w;
   2330       var w2 = this.m_v2.w;
   2331       var w3 = this.m_v3.w;
   2332       var e12 = b2Math.SubtractVV(w2, w1);
   2333       var w1e12 = b2Math.Dot(w1, e12);
   2334       var w2e12 = b2Math.Dot(w2, e12);
   2335       var d12_1 = w2e12;
   2336       var d12_2 = (-w1e12);
   2337       var e13 = b2Math.SubtractVV(w3, w1);
   2338       var w1e13 = b2Math.Dot(w1, e13);
   2339       var w3e13 = b2Math.Dot(w3, e13);
   2340       var d13_1 = w3e13;
   2341       var d13_2 = (-w1e13);
   2342       var e23 = b2Math.SubtractVV(w3, w2);
   2343       var w2e23 = b2Math.Dot(w2, e23);
   2344       var w3e23 = b2Math.Dot(w3, e23);
   2345       var d23_1 = w3e23;
   2346       var d23_2 = (-w2e23);
   2347       var n123 = b2Math.CrossVV(e12, e13);
   2348       var d123_1 = n123 * b2Math.CrossVV(w2, w3);
   2349       var d123_2 = n123 * b2Math.CrossVV(w3, w1);
   2350       var d123_3 = n123 * b2Math.CrossVV(w1, w2);
   2351       if (d12_2 <= 0.0 && d13_2 <= 0.0) {
   2352          this.m_v1.a = 1.0;
   2353          this.m_count = 1;
   2354          return;
   2355       }
   2356       if (d12_1 > 0.0 && d12_2 > 0.0 && d123_3 <= 0.0) {
   2357          var inv_d12 = 1.0 / (d12_1 + d12_2);
   2358          this.m_v1.a = d12_1 * inv_d12;
   2359          this.m_v2.a = d12_2 * inv_d12;
   2360          this.m_count = 2;
   2361          return;
   2362       }
   2363       if (d13_1 > 0.0 && d13_2 > 0.0 && d123_2 <= 0.0) {
   2364          var inv_d13 = 1.0 / (d13_1 + d13_2);
   2365          this.m_v1.a = d13_1 * inv_d13;
   2366          this.m_v3.a = d13_2 * inv_d13;
   2367          this.m_count = 2;
   2368          this.m_v2.Set(this.m_v3);
   2369          return;
   2370       }
   2371       if (d12_1 <= 0.0 && d23_2 <= 0.0) {
   2372          this.m_v2.a = 1.0;
   2373          this.m_count = 1;
   2374          this.m_v1.Set(this.m_v2);
   2375          return;
   2376       }
   2377       if (d13_1 <= 0.0 && d23_1 <= 0.0) {
   2378          this.m_v3.a = 1.0;
   2379          this.m_count = 1;
   2380          this.m_v1.Set(this.m_v3);
   2381          return;
   2382       }
   2383       if (d23_1 > 0.0 && d23_2 > 0.0 && d123_1 <= 0.0) {
   2384          var inv_d23 = 1.0 / (d23_1 + d23_2);
   2385          this.m_v2.a = d23_1 * inv_d23;
   2386          this.m_v3.a = d23_2 * inv_d23;
   2387          this.m_count = 2;
   2388          this.m_v1.Set(this.m_v3);
   2389          return;
   2390       }
   2391       var inv_d123 = 1.0 / (d123_1 + d123_2 + d123_3);
   2392       this.m_v1.a = d123_1 * inv_d123;
   2393       this.m_v2.a = d123_2 * inv_d123;
   2394       this.m_v3.a = d123_3 * inv_d123;
   2395       this.m_count = 3;
   2396    }
   2397    b2SimplexCache.b2SimplexCache = function () {
   2398       this.indexA = new Vector_a2j_Number(3);
   2399       this.indexB = new Vector_a2j_Number(3);
   2400    };
   2401    b2SimplexVertex.b2SimplexVertex = function () {};
   2402    b2SimplexVertex.prototype.Set = function (other) {
   2403       this.wA.SetV(other.wA);
   2404       this.wB.SetV(other.wB);
   2405       this.w.SetV(other.w);
   2406       this.a = other.a;
   2407       this.indexA = other.indexA;
   2408       this.indexB = other.indexB;
   2409    }
   2410    b2TimeOfImpact.b2TimeOfImpact = function () {};
   2411    b2TimeOfImpact.TimeOfImpact = function (input) {
   2412       ++b2TimeOfImpact.b2_toiCalls;
   2413       var proxyA = input.proxyA;
   2414       var proxyB = input.proxyB;
   2415       var sweepA = input.sweepA;
   2416       var sweepB = input.sweepB;
   2417       b2Settings.b2Assert(sweepA.t0 == sweepB.t0);
   2418       b2Settings.b2Assert(1.0 - sweepA.t0 > Number.MIN_VALUE);
   2419       var radius = proxyA.m_radius + proxyB.m_radius;
   2420       var tolerance = input.tolerance;
   2421       var alpha = 0.0;
   2422       var k_maxIterations = 1000;
   2423       var iter = 0;
   2424       var target = 0.0;
   2425       b2TimeOfImpact.s_cache.count = 0;
   2426       b2TimeOfImpact.s_distanceInput.useRadii = false;
   2427       for (;;) {
   2428          sweepA.GetTransform(b2TimeOfImpact.s_xfA, alpha);
   2429          sweepB.GetTransform(b2TimeOfImpact.s_xfB, alpha);
   2430          b2TimeOfImpact.s_distanceInput.proxyA = proxyA;
   2431          b2TimeOfImpact.s_distanceInput.proxyB = proxyB;
   2432          b2TimeOfImpact.s_distanceInput.transformA = b2TimeOfImpact.s_xfA;
   2433          b2TimeOfImpact.s_distanceInput.transformB = b2TimeOfImpact.s_xfB;
   2434          b2Distance.Distance(b2TimeOfImpact.s_distanceOutput, b2TimeOfImpact.s_cache, b2TimeOfImpact.s_distanceInput);
   2435          if (b2TimeOfImpact.s_distanceOutput.distance <= 0.0) {
   2436             alpha = 1.0;
   2437             break;
   2438          }
   2439          b2TimeOfImpact.s_fcn.Initialize(b2TimeOfImpact.s_cache, proxyA, b2TimeOfImpact.s_xfA, proxyB, b2TimeOfImpact.s_xfB);
   2440          var separation = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
   2441          if (separation <= 0.0) {
   2442             alpha = 1.0;
   2443             break;
   2444          }
   2445          if (iter == 0) {
   2446             if (separation > radius) {
   2447                target = b2Math.Max(radius - tolerance, 0.75 * radius);
   2448             }
   2449             else {
   2450                target = b2Math.Max(separation - tolerance, 0.02 * radius);
   2451             }
   2452          }
   2453          if (separation - target < 0.5 * tolerance) {
   2454             if (iter == 0) {
   2455                alpha = 1.0;
   2456                break;
   2457             }
   2458             break;
   2459          }
   2460          var newAlpha = alpha; {
   2461             var x1 = alpha;
   2462             var x2 = 1.0;
   2463             var f1 = separation;
   2464             sweepA.GetTransform(b2TimeOfImpact.s_xfA, x2);
   2465             sweepB.GetTransform(b2TimeOfImpact.s_xfB, x2);
   2466             var f2 = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
   2467             if (f2 >= target) {
   2468                alpha = 1.0;
   2469                break;
   2470             }
   2471             var rootIterCount = 0;
   2472             for (;;) {
   2473                var x = 0;
   2474                if (rootIterCount & 1) {
   2475                   x = x1 + (target - f1) * (x2 - x1) / (f2 - f1);
   2476                }
   2477                else {
   2478                   x = 0.5 * (x1 + x2);
   2479                }
   2480                sweepA.GetTransform(b2TimeOfImpact.s_xfA, x);
   2481                sweepB.GetTransform(b2TimeOfImpact.s_xfB, x);
   2482                var f = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
   2483                if (b2Math.Abs(f - target) < 0.025 * tolerance) {
   2484                   newAlpha = x;
   2485                   break;
   2486                }
   2487                if (f > target) {
   2488                   x1 = x;
   2489                   f1 = f;
   2490                }
   2491                else {
   2492                   x2 = x;
   2493                   f2 = f;
   2494                }++rootIterCount;
   2495                ++b2TimeOfImpact.b2_toiRootIters;
   2496                if (rootIterCount == 50) {
   2497                   break;
   2498                }
   2499             }
   2500             b2TimeOfImpact.b2_toiMaxRootIters = b2Math.Max(b2TimeOfImpact.b2_toiMaxRootIters, rootIterCount);
   2501          }
   2502          if (newAlpha < (1.0 + 100.0 * Number.MIN_VALUE) * alpha) {
   2503             break;
   2504          }
   2505          alpha = newAlpha;
   2506          iter++;
   2507          ++b2TimeOfImpact.b2_toiIters;
   2508          if (iter == k_maxIterations) {
   2509             break;
   2510          }
   2511       }
   2512       b2TimeOfImpact.b2_toiMaxIters = b2Math.Max(b2TimeOfImpact.b2_toiMaxIters, iter);
   2513       return alpha;
   2514    }
   2515    Box2D.postDefs.push(function () {
   2516       Box2D.Collision.b2TimeOfImpact.b2_toiCalls = 0;
   2517       Box2D.Collision.b2TimeOfImpact.b2_toiIters = 0;
   2518       Box2D.Collision.b2TimeOfImpact.b2_toiMaxIters = 0;
   2519       Box2D.Collision.b2TimeOfImpact.b2_toiRootIters = 0;
   2520       Box2D.Collision.b2TimeOfImpact.b2_toiMaxRootIters = 0;
   2521       Box2D.Collision.b2TimeOfImpact.s_cache = new b2SimplexCache();
   2522       Box2D.Collision.b2TimeOfImpact.s_distanceInput = new b2DistanceInput();
   2523       Box2D.Collision.b2TimeOfImpact.s_xfA = new b2Transform();
   2524       Box2D.Collision.b2TimeOfImpact.s_xfB = new b2Transform();
   2525       Box2D.Collision.b2TimeOfImpact.s_fcn = new b2SeparationFunction();
   2526       Box2D.Collision.b2TimeOfImpact.s_distanceOutput = new b2DistanceOutput();
   2527    });
   2528    b2TOIInput.b2TOIInput = function () {
   2529       this.proxyA = new b2DistanceProxy();
   2530       this.proxyB = new b2DistanceProxy();
   2531       this.sweepA = new b2Sweep();
   2532       this.sweepB = new b2Sweep();
   2533    };
   2534    b2WorldManifold.b2WorldManifold = function () {
   2535       this.m_normal = new b2Vec2();
   2536    };
   2537    b2WorldManifold.prototype.b2WorldManifold = function () {
   2538       this.m_points = new Vector(b2Settings.b2_maxManifoldPoints);
   2539       for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
   2540          this.m_points[i] = new b2Vec2();
   2541       }
   2542    }
   2543    b2WorldManifold.prototype.Initialize = function (manifold, xfA, radiusA, xfB, radiusB) {
   2544       if (radiusA === undefined) radiusA = 0;
   2545       if (radiusB === undefined) radiusB = 0;
   2546       if (manifold.m_pointCount == 0) {
   2547          return;
   2548       }
   2549       var i = 0;
   2550       var tVec;
   2551       var tMat;
   2552       var normalX = 0;
   2553       var normalY = 0;
   2554       var planePointX = 0;
   2555       var planePointY = 0;
   2556       var clipPointX = 0;
   2557       var clipPointY = 0;
   2558       switch (manifold.m_type) {
   2559       case b2Manifold.e_circles:
   2560          {
   2561             tMat = xfA.R;
   2562             tVec = manifold.m_localPoint;
   2563             var pointAX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2564             var pointAY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2565             tMat = xfB.R;
   2566             tVec = manifold.m_points[0].m_localPoint;
   2567             var pointBX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2568             var pointBY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2569             var dX = pointBX - pointAX;
   2570             var dY = pointBY - pointAY;
   2571             var d2 = dX * dX + dY * dY;
   2572             if (d2 > Number.MIN_VALUE * Number.MIN_VALUE) {
   2573                var d = Math.sqrt(d2);
   2574                this.m_normal.x = dX / d;
   2575                this.m_normal.y = dY / d;
   2576             }
   2577             else {
   2578                this.m_normal.x = 1;
   2579                this.m_normal.y = 0;
   2580             }
   2581             var cAX = pointAX + radiusA * this.m_normal.x;
   2582             var cAY = pointAY + radiusA * this.m_normal.y;
   2583             var cBX = pointBX - radiusB * this.m_normal.x;
   2584             var cBY = pointBY - radiusB * this.m_normal.y;
   2585             this.m_points[0].x = 0.5 * (cAX + cBX);
   2586             this.m_points[0].y = 0.5 * (cAY + cBY);
   2587          }
   2588          break;
   2589       case b2Manifold.e_faceA:
   2590          {
   2591             tMat = xfA.R;
   2592             tVec = manifold.m_localPlaneNormal;
   2593             normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2594             normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2595             tMat = xfA.R;
   2596             tVec = manifold.m_localPoint;
   2597             planePointX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2598             planePointY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2599             this.m_normal.x = normalX;
   2600             this.m_normal.y = normalY;
   2601             for (i = 0;
   2602             i < manifold.m_pointCount; i++) {
   2603                tMat = xfB.R;
   2604                tVec = manifold.m_points[i].m_localPoint;
   2605                clipPointX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2606                clipPointY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2607                this.m_points[i].x = clipPointX + 0.5 * (radiusA - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusB) * normalX;
   2608                this.m_points[i].y = clipPointY + 0.5 * (radiusA - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusB) * normalY;
   2609             }
   2610          }
   2611          break;
   2612       case b2Manifold.e_faceB:
   2613          {
   2614             tMat = xfB.R;
   2615             tVec = manifold.m_localPlaneNormal;
   2616             normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2617             normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2618             tMat = xfB.R;
   2619             tVec = manifold.m_localPoint;
   2620             planePointX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2621             planePointY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2622             this.m_normal.x = (-normalX);
   2623             this.m_normal.y = (-normalY);
   2624             for (i = 0;
   2625             i < manifold.m_pointCount; i++) {
   2626                tMat = xfA.R;
   2627                tVec = manifold.m_points[i].m_localPoint;
   2628                clipPointX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   2629                clipPointY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   2630                this.m_points[i].x = clipPointX + 0.5 * (radiusB - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusA) * normalX;
   2631                this.m_points[i].y = clipPointY + 0.5 * (radiusB - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusA) * normalY;
   2632             }
   2633          }
   2634          break;
   2635       }
   2636    }
   2637    ClipVertex.ClipVertex = function () {
   2638       this.v = new b2Vec2();
   2639       this.id = new b2ContactID();
   2640    };
   2641    ClipVertex.prototype.Set = function (other) {
   2642       this.v.SetV(other.v);
   2643       this.id.Set(other.id);
   2644    }
   2645    Features.Features = function () {};
   2646    Object.defineProperty(Features.prototype, 'referenceEdge', {
   2647       enumerable: false,
   2648       configurable: true,
   2649       get: function () {
   2650          return this._referenceEdge;
   2651       }
   2652    });
   2653    Object.defineProperty(Features.prototype, 'referenceEdge', {
   2654       enumerable: false,
   2655       configurable: true,
   2656       set: function (value) {
   2657          if (value === undefined) value = 0;
   2658          this._referenceEdge = value;
   2659          this._m_id._key = (this._m_id._key & 0xffffff00) | (this._referenceEdge & 0x000000ff);
   2660       }
   2661    });
   2662    Object.defineProperty(Features.prototype, 'incidentEdge', {
   2663       enumerable: false,
   2664       configurable: true,
   2665       get: function () {
   2666          return this._incidentEdge;
   2667       }
   2668    });
   2669    Object.defineProperty(Features.prototype, 'incidentEdge', {
   2670       enumerable: false,
   2671       configurable: true,
   2672       set: function (value) {
   2673          if (value === undefined) value = 0;
   2674          this._incidentEdge = value;
   2675          this._m_id._key = (this._m_id._key & 0xffff00ff) | ((this._incidentEdge << 8) & 0x0000ff00);
   2676       }
   2677    });
   2678    Object.defineProperty(Features.prototype, 'incidentVertex', {
   2679       enumerable: false,
   2680       configurable: true,
   2681       get: function () {
   2682          return this._incidentVertex;
   2683       }
   2684    });
   2685    Object.defineProperty(Features.prototype, 'incidentVertex', {
   2686       enumerable: false,
   2687       configurable: true,
   2688       set: function (value) {
   2689          if (value === undefined) value = 0;
   2690          this._incidentVertex = value;
   2691          this._m_id._key = (this._m_id._key & 0xff00ffff) | ((this._incidentVertex << 16) & 0x00ff0000);
   2692       }
   2693    });
   2694    Object.defineProperty(Features.prototype, 'flip', {
   2695       enumerable: false,
   2696       configurable: true,
   2697       get: function () {
   2698          return this._flip;
   2699       }
   2700    });
   2701    Object.defineProperty(Features.prototype, 'flip', {
   2702       enumerable: false,
   2703       configurable: true,
   2704       set: function (value) {
   2705          if (value === undefined) value = 0;
   2706          this._flip = value;
   2707          this._m_id._key = (this._m_id._key & 0x00ffffff) | ((this._flip << 24) & 0xff000000);
   2708       }
   2709    });
   2710 })();
   2711 (function () {
   2712    var b2Color = Box2D.Common.b2Color,
   2713       b2internal = Box2D.Common.b2internal,
   2714       b2Settings = Box2D.Common.b2Settings,
   2715       b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
   2716       b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
   2717       b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
   2718       b2MassData = Box2D.Collision.Shapes.b2MassData,
   2719       b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
   2720       b2Shape = Box2D.Collision.Shapes.b2Shape,
   2721       b2Mat22 = Box2D.Common.Math.b2Mat22,
   2722       b2Mat33 = Box2D.Common.Math.b2Mat33,
   2723       b2Math = Box2D.Common.Math.b2Math,
   2724       b2Sweep = Box2D.Common.Math.b2Sweep,
   2725       b2Transform = Box2D.Common.Math.b2Transform,
   2726       b2Vec2 = Box2D.Common.Math.b2Vec2,
   2727       b2Vec3 = Box2D.Common.Math.b2Vec3,
   2728       b2Body = Box2D.Dynamics.b2Body,
   2729       b2BodyDef = Box2D.Dynamics.b2BodyDef,
   2730       b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
   2731       b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
   2732       b2ContactListener = Box2D.Dynamics.b2ContactListener,
   2733       b2ContactManager = Box2D.Dynamics.b2ContactManager,
   2734       b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
   2735       b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
   2736       b2FilterData = Box2D.Dynamics.b2FilterData,
   2737       b2Fixture = Box2D.Dynamics.b2Fixture,
   2738       b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
   2739       b2Island = Box2D.Dynamics.b2Island,
   2740       b2TimeStep = Box2D.Dynamics.b2TimeStep,
   2741       b2World = Box2D.Dynamics.b2World,
   2742       b2AABB = Box2D.Collision.b2AABB,
   2743       b2Bound = Box2D.Collision.b2Bound,
   2744       b2BoundValues = Box2D.Collision.b2BoundValues,
   2745       b2Collision = Box2D.Collision.b2Collision,
   2746       b2ContactID = Box2D.Collision.b2ContactID,
   2747       b2ContactPoint = Box2D.Collision.b2ContactPoint,
   2748       b2Distance = Box2D.Collision.b2Distance,
   2749       b2DistanceInput = Box2D.Collision.b2DistanceInput,
   2750       b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
   2751       b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
   2752       b2DynamicTree = Box2D.Collision.b2DynamicTree,
   2753       b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
   2754       b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
   2755       b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
   2756       b2Manifold = Box2D.Collision.b2Manifold,
   2757       b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
   2758       b2Point = Box2D.Collision.b2Point,
   2759       b2RayCastInput = Box2D.Collision.b2RayCastInput,
   2760       b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
   2761       b2Segment = Box2D.Collision.b2Segment,
   2762       b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
   2763       b2Simplex = Box2D.Collision.b2Simplex,
   2764       b2SimplexCache = Box2D.Collision.b2SimplexCache,
   2765       b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
   2766       b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
   2767       b2TOIInput = Box2D.Collision.b2TOIInput,
   2768       b2WorldManifold = Box2D.Collision.b2WorldManifold,
   2769       ClipVertex = Box2D.Collision.ClipVertex,
   2770       Features = Box2D.Collision.Features,
   2771       IBroadPhase = Box2D.Collision.IBroadPhase;
   2772 
   2773    Box2D.inherit(b2CircleShape, Box2D.Collision.Shapes.b2Shape);
   2774    b2CircleShape.prototype.__super = Box2D.Collision.Shapes.b2Shape.prototype;
   2775    b2CircleShape.b2CircleShape = function () {
   2776       Box2D.Collision.Shapes.b2Shape.b2Shape.apply(this, arguments);
   2777       this.m_p = new b2Vec2();
   2778    };
   2779    b2CircleShape.prototype.Copy = function () {
   2780       var s = new b2CircleShape();
   2781       s.Set(this);
   2782       return s;
   2783    }
   2784    b2CircleShape.prototype.Set = function (other) {
   2785       this.__super.Set.call(this, other);
   2786       if (Box2D.is(other, b2CircleShape)) {
   2787          var other2 = (other instanceof b2CircleShape ? other : null);
   2788          this.m_p.SetV(other2.m_p);
   2789       }
   2790    }
   2791    b2CircleShape.prototype.TestPoint = function (transform, p) {
   2792       var tMat = transform.R;
   2793       var dX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
   2794       var dY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
   2795       dX = p.x - dX;
   2796       dY = p.y - dY;
   2797       return (dX * dX + dY * dY) <= this.m_radius * this.m_radius;
   2798    }
   2799    b2CircleShape.prototype.RayCast = function (output, input, transform) {
   2800       var tMat = transform.R;
   2801       var positionX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
   2802       var positionY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
   2803       var sX = input.p1.x - positionX;
   2804       var sY = input.p1.y - positionY;
   2805       var b = (sX * sX + sY * sY) - this.m_radius * this.m_radius;
   2806       var rX = input.p2.x - input.p1.x;
   2807       var rY = input.p2.y - input.p1.y;
   2808       var c = (sX * rX + sY * rY);
   2809       var rr = (rX * rX + rY * rY);
   2810       var sigma = c * c - rr * b;
   2811       if (sigma < 0.0 || rr < Number.MIN_VALUE) {
   2812          return false;
   2813       }
   2814       var a = (-(c + Math.sqrt(sigma)));
   2815       if (0.0 <= a && a <= input.maxFraction * rr) {
   2816          a /= rr;
   2817          output.fraction = a;
   2818          output.normal.x = sX + a * rX;
   2819          output.normal.y = sY + a * rY;
   2820          output.normal.Normalize();
   2821          return true;
   2822       }
   2823       return false;
   2824    }
   2825    b2CircleShape.prototype.ComputeAABB = function (aabb, transform) {
   2826       var tMat = transform.R;
   2827       var pX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
   2828       var pY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
   2829       aabb.lowerBound.Set(pX - this.m_radius, pY - this.m_radius);
   2830       aabb.upperBound.Set(pX + this.m_radius, pY + this.m_radius);
   2831    }
   2832    b2CircleShape.prototype.ComputeMass = function (massData, density) {
   2833       if (density === undefined) density = 0;
   2834       massData.mass = density * b2Settings.b2_pi * this.m_radius * this.m_radius;
   2835       massData.center.SetV(this.m_p);
   2836       massData.I = massData.mass * (0.5 * this.m_radius * this.m_radius + (this.m_p.x * this.m_p.x + this.m_p.y * this.m_p.y));
   2837    }
   2838    b2CircleShape.prototype.ComputeSubmergedArea = function (normal, offset, xf, c) {
   2839       if (offset === undefined) offset = 0;
   2840       var p = b2Math.MulX(xf, this.m_p);
   2841       var l = (-(b2Math.Dot(normal, p) - offset));
   2842       if (l < (-this.m_radius) + Number.MIN_VALUE) {
   2843          return 0;
   2844       }
   2845       if (l > this.m_radius) {
   2846          c.SetV(p);
   2847          return Math.PI * this.m_radius * this.m_radius;
   2848       }
   2849       var r2 = this.m_radius * this.m_radius;
   2850       var l2 = l * l;
   2851       var area = r2 * (Math.asin(l / this.m_radius) + Math.PI / 2) + l * Math.sqrt(r2 - l2);
   2852       var com = (-2 / 3 * Math.pow(r2 - l2, 1.5) / area);
   2853       c.x = p.x + normal.x * com;
   2854       c.y = p.y + normal.y * com;
   2855       return area;
   2856    }
   2857    b2CircleShape.prototype.GetLocalPosition = function () {
   2858       return this.m_p;
   2859    }
   2860    b2CircleShape.prototype.SetLocalPosition = function (position) {
   2861       this.m_p.SetV(position);
   2862    }
   2863    b2CircleShape.prototype.GetRadius = function () {
   2864       return this.m_radius;
   2865    }
   2866    b2CircleShape.prototype.SetRadius = function (radius) {
   2867       if (radius === undefined) radius = 0;
   2868       this.m_radius = radius;
   2869    }
   2870    b2CircleShape.prototype.b2CircleShape = function (radius) {
   2871       if (radius === undefined) radius = 0;
   2872       this.__super.b2Shape.call(this);
   2873       this.m_type = b2Shape.e_circleShape;
   2874       this.m_radius = radius;
   2875    }
   2876    b2EdgeChainDef.b2EdgeChainDef = function () {};
   2877    b2EdgeChainDef.prototype.b2EdgeChainDef = function () {
   2878       this.vertexCount = 0;
   2879       this.isALoop = true;
   2880       this.vertices = [];
   2881    }
   2882    Box2D.inherit(b2EdgeShape, Box2D.Collision.Shapes.b2Shape);
   2883    b2EdgeShape.prototype.__super = Box2D.Collision.Shapes.b2Shape.prototype;
   2884    b2EdgeShape.b2EdgeShape = function () {
   2885       Box2D.Collision.Shapes.b2Shape.b2Shape.apply(this, arguments);
   2886       this.s_supportVec = new b2Vec2();
   2887       this.m_v1 = new b2Vec2();
   2888       this.m_v2 = new b2Vec2();
   2889       this.m_coreV1 = new b2Vec2();
   2890       this.m_coreV2 = new b2Vec2();
   2891       this.m_normal = new b2Vec2();
   2892       this.m_direction = new b2Vec2();
   2893       this.m_cornerDir1 = new b2Vec2();
   2894       this.m_cornerDir2 = new b2Vec2();
   2895    };
   2896    b2EdgeShape.prototype.TestPoint = function (transform, p) {
   2897       return false;
   2898    }
   2899    b2EdgeShape.prototype.RayCast = function (output, input, transform) {
   2900       var tMat;
   2901       var rX = input.p2.x - input.p1.x;
   2902       var rY = input.p2.y - input.p1.y;
   2903       tMat = transform.R;
   2904       var v1X = transform.position.x + (tMat.col1.x * this.m_v1.x + tMat.col2.x * this.m_v1.y);
   2905       var v1Y = transform.position.y + (tMat.col1.y * this.m_v1.x + tMat.col2.y * this.m_v1.y);
   2906       var nX = transform.position.y + (tMat.col1.y * this.m_v2.x + tMat.col2.y * this.m_v2.y) - v1Y;
   2907       var nY = (-(transform.position.x + (tMat.col1.x * this.m_v2.x + tMat.col2.x * this.m_v2.y) - v1X));
   2908       var k_slop = 100.0 * Number.MIN_VALUE;
   2909       var denom = (-(rX * nX + rY * nY));
   2910       if (denom > k_slop) {
   2911          var bX = input.p1.x - v1X;
   2912          var bY = input.p1.y - v1Y;
   2913          var a = (bX * nX + bY * nY);
   2914          if (0.0 <= a && a <= input.maxFraction * denom) {
   2915             var mu2 = (-rX * bY) + rY * bX;
   2916             if ((-k_slop * denom) <= mu2 && mu2 <= denom * (1.0 + k_slop)) {
   2917                a /= denom;
   2918                output.fraction = a;
   2919                var nLen = Math.sqrt(nX * nX + nY * nY);
   2920                output.normal.x = nX / nLen;
   2921                output.normal.y = nY / nLen;
   2922                return true;
   2923             }
   2924          }
   2925       }
   2926       return false;
   2927    }
   2928    b2EdgeShape.prototype.ComputeAABB = function (aabb, transform) {
   2929       var tMat = transform.R;
   2930       var v1X = transform.position.x + (tMat.col1.x * this.m_v1.x + tMat.col2.x * this.m_v1.y);
   2931       var v1Y = transform.position.y + (tMat.col1.y * this.m_v1.x + tMat.col2.y * this.m_v1.y);
   2932       var v2X = transform.position.x + (tMat.col1.x * this.m_v2.x + tMat.col2.x * this.m_v2.y);
   2933       var v2Y = transform.position.y + (tMat.col1.y * this.m_v2.x + tMat.col2.y * this.m_v2.y);
   2934       if (v1X < v2X) {
   2935          aabb.lowerBound.x = v1X;
   2936          aabb.upperBound.x = v2X;
   2937       }
   2938       else {
   2939          aabb.lowerBound.x = v2X;
   2940          aabb.upperBound.x = v1X;
   2941       }
   2942       if (v1Y < v2Y) {
   2943          aabb.lowerBound.y = v1Y;
   2944          aabb.upperBound.y = v2Y;
   2945       }
   2946       else {
   2947          aabb.lowerBound.y = v2Y;
   2948          aabb.upperBound.y = v1Y;
   2949       }
   2950    }
   2951    b2EdgeShape.prototype.ComputeMass = function (massData, density) {
   2952       if (density === undefined) density = 0;
   2953       massData.mass = 0;
   2954       massData.center.SetV(this.m_v1);
   2955       massData.I = 0;
   2956    }
   2957    b2EdgeShape.prototype.ComputeSubmergedArea = function (normal, offset, xf, c) {
   2958       if (offset === undefined) offset = 0;
   2959       var v0 = new b2Vec2(normal.x * offset, normal.y * offset);
   2960       var v1 = b2Math.MulX(xf, this.m_v1);
   2961       var v2 = b2Math.MulX(xf, this.m_v2);
   2962       var d1 = b2Math.Dot(normal, v1) - offset;
   2963       var d2 = b2Math.Dot(normal, v2) - offset;
   2964       if (d1 > 0) {
   2965          if (d2 > 0) {
   2966             return 0;
   2967          }
   2968          else {
   2969             v1.x = (-d2 / (d1 - d2) * v1.x) + d1 / (d1 - d2) * v2.x;
   2970             v1.y = (-d2 / (d1 - d2) * v1.y) + d1 / (d1 - d2) * v2.y;
   2971          }
   2972       }
   2973       else {
   2974          if (d2 > 0) {
   2975             v2.x = (-d2 / (d1 - d2) * v1.x) + d1 / (d1 - d2) * v2.x;
   2976             v2.y = (-d2 / (d1 - d2) * v1.y) + d1 / (d1 - d2) * v2.y;
   2977          }
   2978          else {}
   2979       }
   2980       c.x = (v0.x + v1.x + v2.x) / 3;
   2981       c.y = (v0.y + v1.y + v2.y) / 3;
   2982       return 0.5 * ((v1.x - v0.x) * (v2.y - v0.y) - (v1.y - v0.y) * (v2.x - v0.x));
   2983    }
   2984    b2EdgeShape.prototype.GetLength = function () {
   2985       return this.m_length;
   2986    }
   2987    b2EdgeShape.prototype.GetVertex1 = function () {
   2988       return this.m_v1;
   2989    }
   2990    b2EdgeShape.prototype.GetVertex2 = function () {
   2991       return this.m_v2;
   2992    }
   2993    b2EdgeShape.prototype.GetCoreVertex1 = function () {
   2994       return this.m_coreV1;
   2995    }
   2996    b2EdgeShape.prototype.GetCoreVertex2 = function () {
   2997       return this.m_coreV2;
   2998    }
   2999    b2EdgeShape.prototype.GetNormalVector = function () {
   3000       return this.m_normal;
   3001    }
   3002    b2EdgeShape.prototype.GetDirectionVector = function () {
   3003       return this.m_direction;
   3004    }
   3005    b2EdgeShape.prototype.GetCorner1Vector = function () {
   3006       return this.m_cornerDir1;
   3007    }
   3008    b2EdgeShape.prototype.GetCorner2Vector = function () {
   3009       return this.m_cornerDir2;
   3010    }
   3011    b2EdgeShape.prototype.Corner1IsConvex = function () {
   3012       return this.m_cornerConvex1;
   3013    }
   3014    b2EdgeShape.prototype.Corner2IsConvex = function () {
   3015       return this.m_cornerConvex2;
   3016    }
   3017    b2EdgeShape.prototype.GetFirstVertex = function (xf) {
   3018       var tMat = xf.R;
   3019       return new b2Vec2(xf.position.x + (tMat.col1.x * this.m_coreV1.x + tMat.col2.x * this.m_coreV1.y), xf.position.y + (tMat.col1.y * this.m_coreV1.x + tMat.col2.y * this.m_coreV1.y));
   3020    }
   3021    b2EdgeShape.prototype.GetNextEdge = function () {
   3022       return this.m_nextEdge;
   3023    }
   3024    b2EdgeShape.prototype.GetPrevEdge = function () {
   3025       return this.m_prevEdge;
   3026    }
   3027    b2EdgeShape.prototype.Support = function (xf, dX, dY) {
   3028       if (dX === undefined) dX = 0;
   3029       if (dY === undefined) dY = 0;
   3030       var tMat = xf.R;
   3031       var v1X = xf.position.x + (tMat.col1.x * this.m_coreV1.x + tMat.col2.x * this.m_coreV1.y);
   3032       var v1Y = xf.position.y + (tMat.col1.y * this.m_coreV1.x + tMat.col2.y * this.m_coreV1.y);
   3033       var v2X = xf.position.x + (tMat.col1.x * this.m_coreV2.x + tMat.col2.x * this.m_coreV2.y);
   3034       var v2Y = xf.position.y + (tMat.col1.y * this.m_coreV2.x + tMat.col2.y * this.m_coreV2.y);
   3035       if ((v1X * dX + v1Y * dY) > (v2X * dX + v2Y * dY)) {
   3036          this.s_supportVec.x = v1X;
   3037          this.s_supportVec.y = v1Y;
   3038       }
   3039       else {
   3040          this.s_supportVec.x = v2X;
   3041          this.s_supportVec.y = v2Y;
   3042       }
   3043       return this.s_supportVec;
   3044    }
   3045    b2EdgeShape.prototype.b2EdgeShape = function (v1, v2) {
   3046       this.__super.b2Shape.call(this);
   3047       this.m_type = b2Shape.e_edgeShape;
   3048       this.m_prevEdge = null;
   3049       this.m_nextEdge = null;
   3050       this.m_v1 = v1;
   3051       this.m_v2 = v2;
   3052       this.m_direction.Set(this.m_v2.x - this.m_v1.x, this.m_v2.y - this.m_v1.y);
   3053       this.m_length = this.m_direction.Normalize();
   3054       this.m_normal.Set(this.m_direction.y, (-this.m_direction.x));
   3055       this.m_coreV1.Set((-b2Settings.b2_toiSlop * (this.m_normal.x - this.m_direction.x)) + this.m_v1.x, (-b2Settings.b2_toiSlop * (this.m_normal.y - this.m_direction.y)) + this.m_v1.y);
   3056       this.m_coreV2.Set((-b2Settings.b2_toiSlop * (this.m_normal.x + this.m_direction.x)) + this.m_v2.x, (-b2Settings.b2_toiSlop * (this.m_normal.y + this.m_direction.y)) + this.m_v2.y);
   3057       this.m_cornerDir1 = this.m_normal;
   3058       this.m_cornerDir2.Set((-this.m_normal.x), (-this.m_normal.y));
   3059    }
   3060    b2EdgeShape.prototype.SetPrevEdge = function (edge, core, cornerDir, convex) {
   3061       this.m_prevEdge = edge;
   3062       this.m_coreV1 = core;
   3063       this.m_cornerDir1 = cornerDir;
   3064       this.m_cornerConvex1 = convex;
   3065    }
   3066    b2EdgeShape.prototype.SetNextEdge = function (edge, core, cornerDir, convex) {
   3067       this.m_nextEdge = edge;
   3068       this.m_coreV2 = core;
   3069       this.m_cornerDir2 = cornerDir;
   3070       this.m_cornerConvex2 = convex;
   3071    }
   3072    b2MassData.b2MassData = function () {
   3073       this.mass = 0.0;
   3074       this.center = new b2Vec2(0, 0);
   3075       this.I = 0.0;
   3076    };
   3077    Box2D.inherit(b2PolygonShape, Box2D.Collision.Shapes.b2Shape);
   3078    b2PolygonShape.prototype.__super = Box2D.Collision.Shapes.b2Shape.prototype;
   3079    b2PolygonShape.b2PolygonShape = function () {
   3080       Box2D.Collision.Shapes.b2Shape.b2Shape.apply(this, arguments);
   3081    };
   3082    b2PolygonShape.prototype.Copy = function () {
   3083       var s = new b2PolygonShape();
   3084       s.Set(this);
   3085       return s;
   3086    }
   3087    b2PolygonShape.prototype.Set = function (other) {
   3088       this.__super.Set.call(this, other);
   3089       if (Box2D.is(other, b2PolygonShape)) {
   3090          var other2 = (other instanceof b2PolygonShape ? other : null);
   3091          this.m_centroid.SetV(other2.m_centroid);
   3092          this.m_vertexCount = other2.m_vertexCount;
   3093          this.Reserve(this.m_vertexCount);
   3094          for (var i = 0; i < this.m_vertexCount; i++) {
   3095             this.m_vertices[i].SetV(other2.m_vertices[i]);
   3096             this.m_normals[i].SetV(other2.m_normals[i]);
   3097          }
   3098       }
   3099    }
   3100    b2PolygonShape.prototype.SetAsArray = function (vertices, vertexCount) {
   3101       if (vertexCount === undefined) vertexCount = 0;
   3102       var v = new Vector();
   3103       var i = 0,
   3104          tVec;
   3105       for (i = 0;
   3106       i < vertices.length; ++i) {
   3107          tVec = vertices[i];
   3108          v.push(tVec);
   3109       }
   3110       this.SetAsVector(v, vertexCount);
   3111    }
   3112    b2PolygonShape.AsArray = function (vertices, vertexCount) {
   3113       if (vertexCount === undefined) vertexCount = 0;
   3114       var polygonShape = new b2PolygonShape();
   3115       polygonShape.SetAsArray(vertices, vertexCount);
   3116       return polygonShape;
   3117    }
   3118    b2PolygonShape.prototype.SetAsVector = function (vertices, vertexCount) {
   3119       if (vertexCount === undefined) vertexCount = 0;
   3120       if (vertexCount == 0) vertexCount = vertices.length;
   3121       b2Settings.b2Assert(2 <= vertexCount);
   3122       this.m_vertexCount = vertexCount;
   3123       this.Reserve(vertexCount);
   3124       var i = 0;
   3125       for (i = 0;
   3126       i < this.m_vertexCount; i++) {
   3127          this.m_vertices[i].SetV(vertices[i]);
   3128       }
   3129       for (i = 0;
   3130       i < this.m_vertexCount; ++i) {
   3131          var i1 = parseInt(i);
   3132          var i2 = parseInt(i + 1 < this.m_vertexCount ? i + 1 : 0);
   3133          var edge = b2Math.SubtractVV(this.m_vertices[i2], this.m_vertices[i1]);
   3134          b2Settings.b2Assert(edge.LengthSquared() > Number.MIN_VALUE);
   3135          this.m_normals[i].SetV(b2Math.CrossVF(edge, 1.0));
   3136          this.m_normals[i].Normalize();
   3137       }
   3138       this.m_centroid = b2PolygonShape.ComputeCentroid(this.m_vertices, this.m_vertexCount);
   3139    }
   3140    b2PolygonShape.AsVector = function (vertices, vertexCount) {
   3141       if (vertexCount === undefined) vertexCount = 0;
   3142       var polygonShape = new b2PolygonShape();
   3143       polygonShape.SetAsVector(vertices, vertexCount);
   3144       return polygonShape;
   3145    }
   3146    b2PolygonShape.prototype.SetAsBox = function (hx, hy) {
   3147       if (hx === undefined) hx = 0;
   3148       if (hy === undefined) hy = 0;
   3149       this.m_vertexCount = 4;
   3150       this.Reserve(4);
   3151       this.m_vertices[0].Set((-hx), (-hy));
   3152       this.m_vertices[1].Set(hx, (-hy));
   3153       this.m_vertices[2].Set(hx, hy);
   3154       this.m_vertices[3].Set((-hx), hy);
   3155       this.m_normals[0].Set(0.0, (-1.0));
   3156       this.m_normals[1].Set(1.0, 0.0);
   3157       this.m_normals[2].Set(0.0, 1.0);
   3158       this.m_normals[3].Set((-1.0), 0.0);
   3159       this.m_centroid.SetZero();
   3160    }
   3161    b2PolygonShape.AsBox = function (hx, hy) {
   3162       if (hx === undefined) hx = 0;
   3163       if (hy === undefined) hy = 0;
   3164       var polygonShape = new b2PolygonShape();
   3165       polygonShape.SetAsBox(hx, hy);
   3166       return polygonShape;
   3167    }
   3168    b2PolygonShape.prototype.SetAsOrientedBox = function (hx, hy, center, angle) {
   3169       if (hx === undefined) hx = 0;
   3170       if (hy === undefined) hy = 0;
   3171       if (center === undefined) center = null;
   3172       if (angle === undefined) angle = 0.0;
   3173       this.m_vertexCount = 4;
   3174       this.Reserve(4);
   3175       this.m_vertices[0].Set((-hx), (-hy));
   3176       this.m_vertices[1].Set(hx, (-hy));
   3177       this.m_vertices[2].Set(hx, hy);
   3178       this.m_vertices[3].Set((-hx), hy);
   3179       this.m_normals[0].Set(0.0, (-1.0));
   3180       this.m_normals[1].Set(1.0, 0.0);
   3181       this.m_normals[2].Set(0.0, 1.0);
   3182       this.m_normals[3].Set((-1.0), 0.0);
   3183       this.m_centroid = center;
   3184       var xf = new b2Transform();
   3185       xf.position = center;
   3186       xf.R.Set(angle);
   3187       for (var i = 0; i < this.m_vertexCount; ++i) {
   3188          this.m_vertices[i] = b2Math.MulX(xf, this.m_vertices[i]);
   3189          this.m_normals[i] = b2Math.MulMV(xf.R, this.m_normals[i]);
   3190       }
   3191    }
   3192    b2PolygonShape.AsOrientedBox = function (hx, hy, center, angle) {
   3193       if (hx === undefined) hx = 0;
   3194       if (hy === undefined) hy = 0;
   3195       if (center === undefined) center = null;
   3196       if (angle === undefined) angle = 0.0;
   3197       var polygonShape = new b2PolygonShape();
   3198       polygonShape.SetAsOrientedBox(hx, hy, center, angle);
   3199       return polygonShape;
   3200    }
   3201    b2PolygonShape.prototype.SetAsEdge = function (v1, v2) {
   3202       this.m_vertexCount = 2;
   3203       this.Reserve(2);
   3204       this.m_vertices[0].SetV(v1);
   3205       this.m_vertices[1].SetV(v2);
   3206       this.m_centroid.x = 0.5 * (v1.x + v2.x);
   3207       this.m_centroid.y = 0.5 * (v1.y + v2.y);
   3208       this.m_normals[0] = b2Math.CrossVF(b2Math.SubtractVV(v2, v1), 1.0);
   3209       this.m_normals[0].Normalize();
   3210       this.m_normals[1].x = (-this.m_normals[0].x);
   3211       this.m_normals[1].y = (-this.m_normals[0].y);
   3212    }
   3213    b2PolygonShape.AsEdge = function (v1, v2) {
   3214       var polygonShape = new b2PolygonShape();
   3215       polygonShape.SetAsEdge(v1, v2);
   3216       return polygonShape;
   3217    }
   3218    b2PolygonShape.prototype.TestPoint = function (xf, p) {
   3219       var tVec;
   3220       var tMat = xf.R;
   3221       var tX = p.x - xf.position.x;
   3222       var tY = p.y - xf.position.y;
   3223       var pLocalX = (tX * tMat.col1.x + tY * tMat.col1.y);
   3224       var pLocalY = (tX * tMat.col2.x + tY * tMat.col2.y);
   3225       for (var i = 0; i < this.m_vertexCount; ++i) {
   3226          tVec = this.m_vertices[i];
   3227          tX = pLocalX - tVec.x;
   3228          tY = pLocalY - tVec.y;
   3229          tVec = this.m_normals[i];
   3230          var dot = (tVec.x * tX + tVec.y * tY);
   3231          if (dot > 0.0) {
   3232             return false;
   3233          }
   3234       }
   3235       return true;
   3236    }
   3237    b2PolygonShape.prototype.RayCast = function (output, input, transform) {
   3238       var lower = 0.0;
   3239       var upper = input.maxFraction;
   3240       var tX = 0;
   3241       var tY = 0;
   3242       var tMat;
   3243       var tVec;
   3244       tX = input.p1.x - transform.position.x;
   3245       tY = input.p1.y - transform.position.y;
   3246       tMat = transform.R;
   3247       var p1X = (tX * tMat.col1.x + tY * tMat.col1.y);
   3248       var p1Y = (tX * tMat.col2.x + tY * tMat.col2.y);
   3249       tX = input.p2.x - transform.position.x;
   3250       tY = input.p2.y - transform.position.y;
   3251       tMat = transform.R;
   3252       var p2X = (tX * tMat.col1.x + tY * tMat.col1.y);
   3253       var p2Y = (tX * tMat.col2.x + tY * tMat.col2.y);
   3254       var dX = p2X - p1X;
   3255       var dY = p2Y - p1Y;
   3256       var index = parseInt((-1));
   3257       for (var i = 0; i < this.m_vertexCount; ++i) {
   3258          tVec = this.m_vertices[i];
   3259          tX = tVec.x - p1X;
   3260          tY = tVec.y - p1Y;
   3261          tVec = this.m_normals[i];
   3262          var numerator = (tVec.x * tX + tVec.y * tY);
   3263          var denominator = (tVec.x * dX + tVec.y * dY);
   3264          if (denominator == 0.0) {
   3265             if (numerator < 0.0) {
   3266                return false;
   3267             }
   3268          }
   3269          else {
   3270             if (denominator < 0.0 && numerator < lower * denominator) {
   3271                lower = numerator / denominator;
   3272                index = i;
   3273             }
   3274             else if (denominator > 0.0 && numerator < upper * denominator) {
   3275                upper = numerator / denominator;
   3276             }
   3277          }
   3278          if (upper < lower - Number.MIN_VALUE) {
   3279             return false;
   3280          }
   3281       }
   3282       if (index >= 0) {
   3283          output.fraction = lower;
   3284          tMat = transform.R;
   3285          tVec = this.m_normals[index];
   3286          output.normal.x = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   3287          output.normal.y = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   3288          return true;
   3289       }
   3290       return false;
   3291    }
   3292    b2PolygonShape.prototype.ComputeAABB = function (aabb, xf) {
   3293       var tMat = xf.R;
   3294       var tVec = this.m_vertices[0];
   3295       var lowerX = xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   3296       var lowerY = xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   3297       var upperX = lowerX;
   3298       var upperY = lowerY;
   3299       for (var i = 1; i < this.m_vertexCount; ++i) {
   3300          tVec = this.m_vertices[i];
   3301          var vX = xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   3302          var vY = xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   3303          lowerX = lowerX < vX ? lowerX : vX;
   3304          lowerY = lowerY < vY ? lowerY : vY;
   3305          upperX = upperX > vX ? upperX : vX;
   3306          upperY = upperY > vY ? upperY : vY;
   3307       }
   3308       aabb.lowerBound.x = lowerX - this.m_radius;
   3309       aabb.lowerBound.y = lowerY - this.m_radius;
   3310       aabb.upperBound.x = upperX + this.m_radius;
   3311       aabb.upperBound.y = upperY + this.m_radius;
   3312    }
   3313    b2PolygonShape.prototype.ComputeMass = function (massData, density) {
   3314       if (density === undefined) density = 0;
   3315       if (this.m_vertexCount == 2) {
   3316          massData.center.x = 0.5 * (this.m_vertices[0].x + this.m_vertices[1].x);
   3317          massData.center.y = 0.5 * (this.m_vertices[0].y + this.m_vertices[1].y);
   3318          massData.mass = 0.0;
   3319          massData.I = 0.0;
   3320          return;
   3321       }
   3322       var centerX = 0.0;
   3323       var centerY = 0.0;
   3324       var area = 0.0;
   3325       var I = 0.0;
   3326       var p1X = 0.0;
   3327       var p1Y = 0.0;
   3328       var k_inv3 = 1.0 / 3.0;
   3329       for (var i = 0; i < this.m_vertexCount; ++i) {
   3330          var p2 = this.m_vertices[i];
   3331          var p3 = i + 1 < this.m_vertexCount ? this.m_vertices[parseInt(i + 1)] : this.m_vertices[0];
   3332          var e1X = p2.x - p1X;
   3333          var e1Y = p2.y - p1Y;
   3334          var e2X = p3.x - p1X;
   3335          var e2Y = p3.y - p1Y;
   3336          var D = e1X * e2Y - e1Y * e2X;
   3337          var triangleArea = 0.5 * D;area += triangleArea;
   3338          centerX += triangleArea * k_inv3 * (p1X + p2.x + p3.x);
   3339          centerY += triangleArea * k_inv3 * (p1Y + p2.y + p3.y);
   3340          var px = p1X;
   3341          var py = p1Y;
   3342          var ex1 = e1X;
   3343          var ey1 = e1Y;
   3344          var ex2 = e2X;
   3345          var ey2 = e2Y;
   3346          var intx2 = k_inv3 * (0.25 * (ex1 * ex1 + ex2 * ex1 + ex2 * ex2) + (px * ex1 + px * ex2)) + 0.5 * px * px;
   3347          var inty2 = k_inv3 * (0.25 * (ey1 * ey1 + ey2 * ey1 + ey2 * ey2) + (py * ey1 + py * ey2)) + 0.5 * py * py;I += D * (intx2 + inty2);
   3348       }
   3349       massData.mass = density * area;
   3350       centerX *= 1.0 / area;
   3351       centerY *= 1.0 / area;
   3352       massData.center.Set(centerX, centerY);
   3353       massData.I = density * I;
   3354    }
   3355    b2PolygonShape.prototype.ComputeSubmergedArea = function (normal, offset, xf, c) {
   3356       if (offset === undefined) offset = 0;
   3357       var normalL = b2Math.MulTMV(xf.R, normal);
   3358       var offsetL = offset - b2Math.Dot(normal, xf.position);
   3359       var depths = new Vector_a2j_Number();
   3360       var diveCount = 0;
   3361       var intoIndex = parseInt((-1));
   3362       var outoIndex = parseInt((-1));
   3363       var lastSubmerged = false;
   3364       var i = 0;
   3365       for (i = 0;
   3366       i < this.m_vertexCount; ++i) {
   3367          depths[i] = b2Math.Dot(normalL, this.m_vertices[i]) - offsetL;
   3368          var isSubmerged = depths[i] < (-Number.MIN_VALUE);
   3369          if (i > 0) {
   3370             if (isSubmerged) {
   3371                if (!lastSubmerged) {
   3372                   intoIndex = i - 1;
   3373                   diveCount++;
   3374                }
   3375             }
   3376             else {
   3377                if (lastSubmerged) {
   3378                   outoIndex = i - 1;
   3379                   diveCount++;
   3380                }
   3381             }
   3382          }
   3383          lastSubmerged = isSubmerged;
   3384       }
   3385       switch (diveCount) {
   3386       case 0:
   3387          if (lastSubmerged) {
   3388             var md = new b2MassData();
   3389             this.ComputeMass(md, 1);
   3390             c.SetV(b2Math.MulX(xf, md.center));
   3391             return md.mass;
   3392          }
   3393          else {
   3394             return 0;
   3395          }
   3396          break;
   3397       case 1:
   3398          if (intoIndex == (-1)) {
   3399             intoIndex = this.m_vertexCount - 1;
   3400          }
   3401          else {
   3402             outoIndex = this.m_vertexCount - 1;
   3403          }
   3404          break;
   3405       }
   3406       var intoIndex2 = parseInt((intoIndex + 1) % this.m_vertexCount);
   3407       var outoIndex2 = parseInt((outoIndex + 1) % this.m_vertexCount);
   3408       var intoLamdda = (0 - depths[intoIndex]) / (depths[intoIndex2] - depths[intoIndex]);
   3409       var outoLamdda = (0 - depths[outoIndex]) / (depths[outoIndex2] - depths[outoIndex]);
   3410       var intoVec = new b2Vec2(this.m_vertices[intoIndex].x * (1 - intoLamdda) + this.m_vertices[intoIndex2].x * intoLamdda, this.m_vertices[intoIndex].y * (1 - intoLamdda) + this.m_vertices[intoIndex2].y * intoLamdda);
   3411       var outoVec = new b2Vec2(this.m_vertices[outoIndex].x * (1 - outoLamdda) + this.m_vertices[outoIndex2].x * outoLamdda, this.m_vertices[outoIndex].y * (1 - outoLamdda) + this.m_vertices[outoIndex2].y * outoLamdda);
   3412       var area = 0;
   3413       var center = new b2Vec2();
   3414       var p2 = this.m_vertices[intoIndex2];
   3415       var p3;
   3416       i = intoIndex2;
   3417       while (i != outoIndex2) {
   3418          i = (i + 1) % this.m_vertexCount;
   3419          if (i == outoIndex2) p3 = outoVec;
   3420          else p3 = this.m_vertices[i];
   3421          var triangleArea = 0.5 * ((p2.x - intoVec.x) * (p3.y - intoVec.y) - (p2.y - intoVec.y) * (p3.x - intoVec.x));
   3422          area += triangleArea;
   3423          center.x += triangleArea * (intoVec.x + p2.x + p3.x) / 3;
   3424          center.y += triangleArea * (intoVec.y + p2.y + p3.y) / 3;
   3425          p2 = p3;
   3426       }
   3427       center.Multiply(1 / area);
   3428       c.SetV(b2Math.MulX(xf, center));
   3429       return area;
   3430    }
   3431    b2PolygonShape.prototype.GetVertexCount = function () {
   3432       return this.m_vertexCount;
   3433    }
   3434    b2PolygonShape.prototype.GetVertices = function () {
   3435       return this.m_vertices;
   3436    }
   3437    b2PolygonShape.prototype.GetNormals = function () {
   3438       return this.m_normals;
   3439    }
   3440    b2PolygonShape.prototype.GetSupport = function (d) {
   3441       var bestIndex = 0;
   3442       var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
   3443       for (var i = 1; i < this.m_vertexCount; ++i) {
   3444          var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
   3445          if (value > bestValue) {
   3446             bestIndex = i;
   3447             bestValue = value;
   3448          }
   3449       }
   3450       return bestIndex;
   3451    }
   3452    b2PolygonShape.prototype.GetSupportVertex = function (d) {
   3453       var bestIndex = 0;
   3454       var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
   3455       for (var i = 1; i < this.m_vertexCount; ++i) {
   3456          var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
   3457          if (value > bestValue) {
   3458             bestIndex = i;
   3459             bestValue = value;
   3460          }
   3461       }
   3462       return this.m_vertices[bestIndex];
   3463    }
   3464    b2PolygonShape.prototype.Validate = function () {
   3465       return false;
   3466    }
   3467    b2PolygonShape.prototype.b2PolygonShape = function () {
   3468       this.__super.b2Shape.call(this);
   3469       this.m_type = b2Shape.e_polygonShape;
   3470       this.m_centroid = new b2Vec2();
   3471       this.m_vertices = new Vector();
   3472       this.m_normals = new Vector();
   3473    }
   3474    b2PolygonShape.prototype.Reserve = function (count) {
   3475       if (count === undefined) count = 0;
   3476       for (var i = parseInt(this.m_vertices.length); i < count; i++) {
   3477          this.m_vertices[i] = new b2Vec2();
   3478          this.m_normals[i] = new b2Vec2();
   3479       }
   3480    }
   3481    b2PolygonShape.ComputeCentroid = function (vs, count) {
   3482       if (count === undefined) count = 0;
   3483       var c = new b2Vec2();
   3484       var area = 0.0;
   3485       var p1X = 0.0;
   3486       var p1Y = 0.0;
   3487       var inv3 = 1.0 / 3.0;
   3488       for (var i = 0; i < count; ++i) {
   3489          var p2 = vs[i];
   3490          var p3 = i + 1 < count ? vs[parseInt(i + 1)] : vs[0];
   3491          var e1X = p2.x - p1X;
   3492          var e1Y = p2.y - p1Y;
   3493          var e2X = p3.x - p1X;
   3494          var e2Y = p3.y - p1Y;
   3495          var D = (e1X * e2Y - e1Y * e2X);
   3496          var triangleArea = 0.5 * D;area += triangleArea;
   3497          c.x += triangleArea * inv3 * (p1X + p2.x + p3.x);
   3498          c.y += triangleArea * inv3 * (p1Y + p2.y + p3.y);
   3499       }
   3500       c.x *= 1.0 / area;
   3501       c.y *= 1.0 / area;
   3502       return c;
   3503    }
   3504    b2PolygonShape.ComputeOBB = function (obb, vs, count) {
   3505       if (count === undefined) count = 0;
   3506       var i = 0;
   3507       var p = new Vector(count + 1);
   3508       for (i = 0;
   3509       i < count; ++i) {
   3510          p[i] = vs[i];
   3511       }
   3512       p[count] = p[0];
   3513       var minArea = Number.MAX_VALUE;
   3514       for (i = 1;
   3515       i <= count; ++i) {
   3516          var root = p[parseInt(i - 1)];
   3517          var uxX = p[i].x - root.x;
   3518          var uxY = p[i].y - root.y;
   3519          var length = Math.sqrt(uxX * uxX + uxY * uxY);
   3520          uxX /= length;
   3521          uxY /= length;
   3522          var uyX = (-uxY);
   3523          var uyY = uxX;
   3524          var lowerX = Number.MAX_VALUE;
   3525          var lowerY = Number.MAX_VALUE;
   3526          var upperX = (-Number.MAX_VALUE);
   3527          var upperY = (-Number.MAX_VALUE);
   3528          for (var j = 0; j < count; ++j) {
   3529             var dX = p[j].x - root.x;
   3530             var dY = p[j].y - root.y;
   3531             var rX = (uxX * dX + uxY * dY);
   3532             var rY = (uyX * dX + uyY * dY);
   3533             if (rX < lowerX) lowerX = rX;
   3534             if (rY < lowerY) lowerY = rY;
   3535             if (rX > upperX) upperX = rX;
   3536             if (rY > upperY) upperY = rY;
   3537          }
   3538          var area = (upperX - lowerX) * (upperY - lowerY);
   3539          if (area < 0.95 * minArea) {
   3540             minArea = area;
   3541             obb.R.col1.x = uxX;
   3542             obb.R.col1.y = uxY;
   3543             obb.R.col2.x = uyX;
   3544             obb.R.col2.y = uyY;
   3545             var centerX = 0.5 * (lowerX + upperX);
   3546             var centerY = 0.5 * (lowerY + upperY);
   3547             var tMat = obb.R;
   3548             obb.center.x = root.x + (tMat.col1.x * centerX + tMat.col2.x * centerY);
   3549             obb.center.y = root.y + (tMat.col1.y * centerX + tMat.col2.y * centerY);
   3550             obb.extents.x = 0.5 * (upperX - lowerX);
   3551             obb.extents.y = 0.5 * (upperY - lowerY);
   3552          }
   3553       }
   3554    }
   3555    Box2D.postDefs.push(function () {
   3556       Box2D.Collision.Shapes.b2PolygonShape.s_mat = new b2Mat22();
   3557    });
   3558    b2Shape.b2Shape = function () {};
   3559    b2Shape.prototype.Copy = function () {
   3560       return null;
   3561    }
   3562    b2Shape.prototype.Set = function (other) {
   3563       this.m_radius = other.m_radius;
   3564    }
   3565    b2Shape.prototype.GetType = function () {
   3566       return this.m_type;
   3567    }
   3568    b2Shape.prototype.TestPoint = function (xf, p) {
   3569       return false;
   3570    }
   3571    b2Shape.prototype.RayCast = function (output, input, transform) {
   3572       return false;
   3573    }
   3574    b2Shape.prototype.ComputeAABB = function (aabb, xf) {}
   3575    b2Shape.prototype.ComputeMass = function (massData, density) {
   3576       if (density === undefined) density = 0;
   3577    }
   3578    b2Shape.prototype.ComputeSubmergedArea = function (normal, offset, xf, c) {
   3579       if (offset === undefined) offset = 0;
   3580       return 0;
   3581    }
   3582    b2Shape.TestOverlap = function (shape1, transform1, shape2, transform2) {
   3583       var input = new b2DistanceInput();
   3584       input.proxyA = new b2DistanceProxy();
   3585       input.proxyA.Set(shape1);
   3586       input.proxyB = new b2DistanceProxy();
   3587       input.proxyB.Set(shape2);
   3588       input.transformA = transform1;
   3589       input.transformB = transform2;
   3590       input.useRadii = true;
   3591       var simplexCache = new b2SimplexCache();
   3592       simplexCache.count = 0;
   3593       var output = new b2DistanceOutput();
   3594       b2Distance.Distance(output, simplexCache, input);
   3595       return output.distance < 10.0 * Number.MIN_VALUE;
   3596    }
   3597    b2Shape.prototype.b2Shape = function () {
   3598       this.m_type = b2Shape.e_unknownShape;
   3599       this.m_radius = b2Settings.b2_linearSlop;
   3600    }
   3601    Box2D.postDefs.push(function () {
   3602       Box2D.Collision.Shapes.b2Shape.e_unknownShape = parseInt((-1));
   3603       Box2D.Collision.Shapes.b2Shape.e_circleShape = 0;
   3604       Box2D.Collision.Shapes.b2Shape.e_polygonShape = 1;
   3605       Box2D.Collision.Shapes.b2Shape.e_edgeShape = 2;
   3606       Box2D.Collision.Shapes.b2Shape.e_shapeTypeCount = 3;
   3607       Box2D.Collision.Shapes.b2Shape.e_hitCollide = 1;
   3608       Box2D.Collision.Shapes.b2Shape.e_missCollide = 0;
   3609       Box2D.Collision.Shapes.b2Shape.e_startsInsideCollide = parseInt((-1));
   3610    });
   3611 })();
   3612 (function () {
   3613    var b2Color = Box2D.Common.b2Color,
   3614       b2internal = Box2D.Common.b2internal,
   3615       b2Settings = Box2D.Common.b2Settings,
   3616       b2Mat22 = Box2D.Common.Math.b2Mat22,
   3617       b2Mat33 = Box2D.Common.Math.b2Mat33,
   3618       b2Math = Box2D.Common.Math.b2Math,
   3619       b2Sweep = Box2D.Common.Math.b2Sweep,
   3620       b2Transform = Box2D.Common.Math.b2Transform,
   3621       b2Vec2 = Box2D.Common.Math.b2Vec2,
   3622       b2Vec3 = Box2D.Common.Math.b2Vec3;
   3623 
   3624    b2Color.b2Color = function () {
   3625       this._r = 0;
   3626       this._g = 0;
   3627       this._b = 0;
   3628    };
   3629    b2Color.prototype.b2Color = function (rr, gg, bb) {
   3630       if (rr === undefined) rr = 0;
   3631       if (gg === undefined) gg = 0;
   3632       if (bb === undefined) bb = 0;
   3633       this._r = Box2D.parseUInt(255 * b2Math.Clamp(rr, 0.0, 1.0));
   3634       this._g = Box2D.parseUInt(255 * b2Math.Clamp(gg, 0.0, 1.0));
   3635       this._b = Box2D.parseUInt(255 * b2Math.Clamp(bb, 0.0, 1.0));
   3636    }
   3637    b2Color.prototype.Set = function (rr, gg, bb) {
   3638       if (rr === undefined) rr = 0;
   3639       if (gg === undefined) gg = 0;
   3640       if (bb === undefined) bb = 0;
   3641       this._r = Box2D.parseUInt(255 * b2Math.Clamp(rr, 0.0, 1.0));
   3642       this._g = Box2D.parseUInt(255 * b2Math.Clamp(gg, 0.0, 1.0));
   3643       this._b = Box2D.parseUInt(255 * b2Math.Clamp(bb, 0.0, 1.0));
   3644    }
   3645    Object.defineProperty(b2Color.prototype, 'r', {
   3646       enumerable: false,
   3647       configurable: true,
   3648       set: function (rr) {
   3649          if (rr === undefined) rr = 0;
   3650          this._r = Box2D.parseUInt(255 * b2Math.Clamp(rr, 0.0, 1.0));
   3651       }
   3652    });
   3653    Object.defineProperty(b2Color.prototype, 'g', {
   3654       enumerable: false,
   3655       configurable: true,
   3656       set: function (gg) {
   3657          if (gg === undefined) gg = 0;
   3658          this._g = Box2D.parseUInt(255 * b2Math.Clamp(gg, 0.0, 1.0));
   3659       }
   3660    });
   3661    Object.defineProperty(b2Color.prototype, 'b', {
   3662       enumerable: false,
   3663       configurable: true,
   3664       set: function (bb) {
   3665          if (bb === undefined) bb = 0;
   3666          this._b = Box2D.parseUInt(255 * b2Math.Clamp(bb, 0.0, 1.0));
   3667       }
   3668    });
   3669    Object.defineProperty(b2Color.prototype, 'color', {
   3670       enumerable: false,
   3671       configurable: true,
   3672       get: function () {
   3673          return (this._r << 16) | (this._g << 8) | (this._b);
   3674       }
   3675    });
   3676    b2Settings.b2Settings = function () {};
   3677    b2Settings.b2MixFriction = function (friction1, friction2) {
   3678       if (friction1 === undefined) friction1 = 0;
   3679       if (friction2 === undefined) friction2 = 0;
   3680       return Math.sqrt(friction1 * friction2);
   3681    }
   3682    b2Settings.b2MixRestitution = function (restitution1, restitution2) {
   3683       if (restitution1 === undefined) restitution1 = 0;
   3684       if (restitution2 === undefined) restitution2 = 0;
   3685       return restitution1 > restitution2 ? restitution1 : restitution2;
   3686    }
   3687    b2Settings.b2Assert = function (a) {
   3688       if (!a) {
   3689          throw "Assertion Failed";
   3690       }
   3691    }
   3692    Box2D.postDefs.push(function () {
   3693       Box2D.Common.b2Settings.VERSION = "2.1alpha";
   3694       Box2D.Common.b2Settings.USHRT_MAX = 0x0000ffff;
   3695       Box2D.Common.b2Settings.b2_pi = Math.PI;
   3696       Box2D.Common.b2Settings.b2_maxManifoldPoints = 2;
   3697       Box2D.Common.b2Settings.b2_aabbExtension = 0.1;
   3698       Box2D.Common.b2Settings.b2_aabbMultiplier = 2.0;
   3699       Box2D.Common.b2Settings.b2_polygonRadius = 2.0 * b2Settings.b2_linearSlop;
   3700       Box2D.Common.b2Settings.b2_linearSlop = 0.005;
   3701       Box2D.Common.b2Settings.b2_angularSlop = 2.0 / 180.0 * b2Settings.b2_pi;
   3702       Box2D.Common.b2Settings.b2_toiSlop = 8.0 * b2Settings.b2_linearSlop;
   3703       Box2D.Common.b2Settings.b2_maxTOIContactsPerIsland = 32;
   3704       Box2D.Common.b2Settings.b2_maxTOIJointsPerIsland = 32;
   3705       Box2D.Common.b2Settings.b2_velocityThreshold = 1.0;
   3706       Box2D.Common.b2Settings.b2_maxLinearCorrection = 0.2;
   3707       Box2D.Common.b2Settings.b2_maxAngularCorrection = 8.0 / 180.0 * b2Settings.b2_pi;
   3708       Box2D.Common.b2Settings.b2_maxTranslation = 2.0;
   3709       Box2D.Common.b2Settings.b2_maxTranslationSquared = b2Settings.b2_maxTranslation * b2Settings.b2_maxTranslation;
   3710       Box2D.Common.b2Settings.b2_maxRotation = 0.5 * b2Settings.b2_pi;
   3711       Box2D.Common.b2Settings.b2_maxRotationSquared = b2Settings.b2_maxRotation * b2Settings.b2_maxRotation;
   3712       Box2D.Common.b2Settings.b2_contactBaumgarte = 0.2;
   3713       Box2D.Common.b2Settings.b2_timeToSleep = 0.5;
   3714       Box2D.Common.b2Settings.b2_linearSleepTolerance = 0.01;
   3715       Box2D.Common.b2Settings.b2_angularSleepTolerance = 2.0 / 180.0 * b2Settings.b2_pi;
   3716    });
   3717 })();
   3718 (function () {
   3719    var b2AABB = Box2D.Collision.b2AABB,
   3720       b2Color = Box2D.Common.b2Color,
   3721       b2internal = Box2D.Common.b2internal,
   3722       b2Settings = Box2D.Common.b2Settings,
   3723       b2Mat22 = Box2D.Common.Math.b2Mat22,
   3724       b2Mat33 = Box2D.Common.Math.b2Mat33,
   3725       b2Math = Box2D.Common.Math.b2Math,
   3726       b2Sweep = Box2D.Common.Math.b2Sweep,
   3727       b2Transform = Box2D.Common.Math.b2Transform,
   3728       b2Vec2 = Box2D.Common.Math.b2Vec2,
   3729       b2Vec3 = Box2D.Common.Math.b2Vec3;
   3730 
   3731    b2Mat22.b2Mat22 = function () {
   3732       this.col1 = new b2Vec2();
   3733       this.col2 = new b2Vec2();
   3734    };
   3735    b2Mat22.prototype.b2Mat22 = function () {
   3736       this.SetIdentity();
   3737    }
   3738    b2Mat22.FromAngle = function (angle) {
   3739       if (angle === undefined) angle = 0;
   3740       var mat = new b2Mat22();
   3741       mat.Set(angle);
   3742       return mat;
   3743    }
   3744    b2Mat22.FromVV = function (c1, c2) {
   3745       var mat = new b2Mat22();
   3746       mat.SetVV(c1, c2);
   3747       return mat;
   3748    }
   3749    b2Mat22.prototype.Set = function (angle) {
   3750       if (angle === undefined) angle = 0;
   3751       var c = Math.cos(angle);
   3752       var s = Math.sin(angle);
   3753       this.col1.x = c;
   3754       this.col2.x = (-s);
   3755       this.col1.y = s;
   3756       this.col2.y = c;
   3757    }
   3758    b2Mat22.prototype.SetVV = function (c1, c2) {
   3759       this.col1.SetV(c1);
   3760       this.col2.SetV(c2);
   3761    }
   3762    b2Mat22.prototype.Copy = function () {
   3763       var mat = new b2Mat22();
   3764       mat.SetM(this);
   3765       return mat;
   3766    }
   3767    b2Mat22.prototype.SetM = function (m) {
   3768       this.col1.SetV(m.col1);
   3769       this.col2.SetV(m.col2);
   3770    }
   3771    b2Mat22.prototype.AddM = function (m) {
   3772       this.col1.x += m.col1.x;
   3773       this.col1.y += m.col1.y;
   3774       this.col2.x += m.col2.x;
   3775       this.col2.y += m.col2.y;
   3776    }
   3777    b2Mat22.prototype.SetIdentity = function () {
   3778       this.col1.x = 1.0;
   3779       this.col2.x = 0.0;
   3780       this.col1.y = 0.0;
   3781       this.col2.y = 1.0;
   3782    }
   3783    b2Mat22.prototype.SetZero = function () {
   3784       this.col1.x = 0.0;
   3785       this.col2.x = 0.0;
   3786       this.col1.y = 0.0;
   3787       this.col2.y = 0.0;
   3788    }
   3789    b2Mat22.prototype.GetAngle = function () {
   3790       return Math.atan2(this.col1.y, this.col1.x);
   3791    }
   3792    b2Mat22.prototype.GetInverse = function (out) {
   3793       var a = this.col1.x;
   3794       var b = this.col2.x;
   3795       var c = this.col1.y;
   3796       var d = this.col2.y;
   3797       var det = a * d - b * c;
   3798       if (det != 0.0) {
   3799          det = 1.0 / det;
   3800       }
   3801       out.col1.x = det * d;
   3802       out.col2.x = (-det * b);
   3803       out.col1.y = (-det * c);
   3804       out.col2.y = det * a;
   3805       return out;
   3806    }
   3807    b2Mat22.prototype.Solve = function (out, bX, bY) {
   3808       if (bX === undefined) bX = 0;
   3809       if (bY === undefined) bY = 0;
   3810       var a11 = this.col1.x;
   3811       var a12 = this.col2.x;
   3812       var a21 = this.col1.y;
   3813       var a22 = this.col2.y;
   3814       var det = a11 * a22 - a12 * a21;
   3815       if (det != 0.0) {
   3816          det = 1.0 / det;
   3817       }
   3818       out.x = det * (a22 * bX - a12 * bY);
   3819       out.y = det * (a11 * bY - a21 * bX);
   3820       return out;
   3821    }
   3822    b2Mat22.prototype.Abs = function () {
   3823       this.col1.Abs();
   3824       this.col2.Abs();
   3825    }
   3826    b2Mat33.b2Mat33 = function () {
   3827       this.col1 = new b2Vec3();
   3828       this.col2 = new b2Vec3();
   3829       this.col3 = new b2Vec3();
   3830    };
   3831    b2Mat33.prototype.b2Mat33 = function (c1, c2, c3) {
   3832       if (c1 === undefined) c1 = null;
   3833       if (c2 === undefined) c2 = null;
   3834       if (c3 === undefined) c3 = null;
   3835       if (!c1 && !c2 && !c3) {
   3836          this.col1.SetZero();
   3837          this.col2.SetZero();
   3838          this.col3.SetZero();
   3839       }
   3840       else {
   3841          this.col1.SetV(c1);
   3842          this.col2.SetV(c2);
   3843          this.col3.SetV(c3);
   3844       }
   3845    }
   3846    b2Mat33.prototype.SetVVV = function (c1, c2, c3) {
   3847       this.col1.SetV(c1);
   3848       this.col2.SetV(c2);
   3849       this.col3.SetV(c3);
   3850    }
   3851    b2Mat33.prototype.Copy = function () {
   3852       return new b2Mat33(this.col1, this.col2, this.col3);
   3853    }
   3854    b2Mat33.prototype.SetM = function (m) {
   3855       this.col1.SetV(m.col1);
   3856       this.col2.SetV(m.col2);
   3857       this.col3.SetV(m.col3);
   3858    }
   3859    b2Mat33.prototype.AddM = function (m) {
   3860       this.col1.x += m.col1.x;
   3861       this.col1.y += m.col1.y;
   3862       this.col1.z += m.col1.z;
   3863       this.col2.x += m.col2.x;
   3864       this.col2.y += m.col2.y;
   3865       this.col2.z += m.col2.z;
   3866       this.col3.x += m.col3.x;
   3867       this.col3.y += m.col3.y;
   3868       this.col3.z += m.col3.z;
   3869    }
   3870    b2Mat33.prototype.SetIdentity = function () {
   3871       this.col1.x = 1.0;
   3872       this.col2.x = 0.0;
   3873       this.col3.x = 0.0;
   3874       this.col1.y = 0.0;
   3875       this.col2.y = 1.0;
   3876       this.col3.y = 0.0;
   3877       this.col1.z = 0.0;
   3878       this.col2.z = 0.0;
   3879       this.col3.z = 1.0;
   3880    }
   3881    b2Mat33.prototype.SetZero = function () {
   3882       this.col1.x = 0.0;
   3883       this.col2.x = 0.0;
   3884       this.col3.x = 0.0;
   3885       this.col1.y = 0.0;
   3886       this.col2.y = 0.0;
   3887       this.col3.y = 0.0;
   3888       this.col1.z = 0.0;
   3889       this.col2.z = 0.0;
   3890       this.col3.z = 0.0;
   3891    }
   3892    b2Mat33.prototype.Solve22 = function (out, bX, bY) {
   3893       if (bX === undefined) bX = 0;
   3894       if (bY === undefined) bY = 0;
   3895       var a11 = this.col1.x;
   3896       var a12 = this.col2.x;
   3897       var a21 = this.col1.y;
   3898       var a22 = this.col2.y;
   3899       var det = a11 * a22 - a12 * a21;
   3900       if (det != 0.0) {
   3901          det = 1.0 / det;
   3902       }
   3903       out.x = det * (a22 * bX - a12 * bY);
   3904       out.y = det * (a11 * bY - a21 * bX);
   3905       return out;
   3906    }
   3907    b2Mat33.prototype.Solve33 = function (out, bX, bY, bZ) {
   3908       if (bX === undefined) bX = 0;
   3909       if (bY === undefined) bY = 0;
   3910       if (bZ === undefined) bZ = 0;
   3911       var a11 = this.col1.x;
   3912       var a21 = this.col1.y;
   3913       var a31 = this.col1.z;
   3914       var a12 = this.col2.x;
   3915       var a22 = this.col2.y;
   3916       var a32 = this.col2.z;
   3917       var a13 = this.col3.x;
   3918       var a23 = this.col3.y;
   3919       var a33 = this.col3.z;
   3920       var det = a11 * (a22 * a33 - a32 * a23) + a21 * (a32 * a13 - a12 * a33) + a31 * (a12 * a23 - a22 * a13);
   3921       if (det != 0.0) {
   3922          det = 1.0 / det;
   3923       }
   3924       out.x = det * (bX * (a22 * a33 - a32 * a23) + bY * (a32 * a13 - a12 * a33) + bZ * (a12 * a23 - a22 * a13));
   3925       out.y = det * (a11 * (bY * a33 - bZ * a23) + a21 * (bZ * a13 - bX * a33) + a31 * (bX * a23 - bY * a13));
   3926       out.z = det * (a11 * (a22 * bZ - a32 * bY) + a21 * (a32 * bX - a12 * bZ) + a31 * (a12 * bY - a22 * bX));
   3927       return out;
   3928    }
   3929    b2Math.b2Math = function () {};
   3930    b2Math.IsValid = function (x) {
   3931       if (x === undefined) x = 0;
   3932       return isFinite(x);
   3933    }
   3934    b2Math.Dot = function (a, b) {
   3935       return a.x * b.x + a.y * b.y;
   3936    }
   3937    b2Math.CrossVV = function (a, b) {
   3938       return a.x * b.y - a.y * b.x;
   3939    }
   3940    b2Math.CrossVF = function (a, s) {
   3941       if (s === undefined) s = 0;
   3942       var v = new b2Vec2(s * a.y, (-s * a.x));
   3943       return v;
   3944    }
   3945    b2Math.CrossFV = function (s, a) {
   3946       if (s === undefined) s = 0;
   3947       var v = new b2Vec2((-s * a.y), s * a.x);
   3948       return v;
   3949    }
   3950    b2Math.MulMV = function (A, v) {
   3951       var u = new b2Vec2(A.col1.x * v.x + A.col2.x * v.y, A.col1.y * v.x + A.col2.y * v.y);
   3952       return u;
   3953    }
   3954    b2Math.MulTMV = function (A, v) {
   3955       var u = new b2Vec2(b2Math.Dot(v, A.col1), b2Math.Dot(v, A.col2));
   3956       return u;
   3957    }
   3958    b2Math.MulX = function (T, v) {
   3959       var a = b2Math.MulMV(T.R, v);
   3960       a.x += T.position.x;
   3961       a.y += T.position.y;
   3962       return a;
   3963    }
   3964    b2Math.MulXT = function (T, v) {
   3965       var a = b2Math.SubtractVV(v, T.position);
   3966       var tX = (a.x * T.R.col1.x + a.y * T.R.col1.y);
   3967       a.y = (a.x * T.R.col2.x + a.y * T.R.col2.y);
   3968       a.x = tX;
   3969       return a;
   3970    }
   3971    b2Math.AddVV = function (a, b) {
   3972       var v = new b2Vec2(a.x + b.x, a.y + b.y);
   3973       return v;
   3974    }
   3975    b2Math.SubtractVV = function (a, b) {
   3976       var v = new b2Vec2(a.x - b.x, a.y - b.y);
   3977       return v;
   3978    }
   3979    b2Math.Distance = function (a, b) {
   3980       var cX = a.x - b.x;
   3981       var cY = a.y - b.y;
   3982       return Math.sqrt(cX * cX + cY * cY);
   3983    }
   3984    b2Math.DistanceSquared = function (a, b) {
   3985       var cX = a.x - b.x;
   3986       var cY = a.y - b.y;
   3987       return (cX * cX + cY * cY);
   3988    }
   3989    b2Math.MulFV = function (s, a) {
   3990       if (s === undefined) s = 0;
   3991       var v = new b2Vec2(s * a.x, s * a.y);
   3992       return v;
   3993    }
   3994    b2Math.AddMM = function (A, B) {
   3995       var C = b2Mat22.FromVV(b2Math.AddVV(A.col1, B.col1), b2Math.AddVV(A.col2, B.col2));
   3996       return C;
   3997    }
   3998    b2Math.MulMM = function (A, B) {
   3999       var C = b2Mat22.FromVV(b2Math.MulMV(A, B.col1), b2Math.MulMV(A, B.col2));
   4000       return C;
   4001    }
   4002    b2Math.MulTMM = function (A, B) {
   4003       var c1 = new b2Vec2(b2Math.Dot(A.col1, B.col1), b2Math.Dot(A.col2, B.col1));
   4004       var c2 = new b2Vec2(b2Math.Dot(A.col1, B.col2), b2Math.Dot(A.col2, B.col2));
   4005       var C = b2Mat22.FromVV(c1, c2);
   4006       return C;
   4007    }
   4008    b2Math.Abs = function (a) {
   4009       if (a === undefined) a = 0;
   4010       return a > 0.0 ? a : (-a);
   4011    }
   4012    b2Math.AbsV = function (a) {
   4013       var b = new b2Vec2(b2Math.Abs(a.x), b2Math.Abs(a.y));
   4014       return b;
   4015    }
   4016    b2Math.AbsM = function (A) {
   4017       var B = b2Mat22.FromVV(b2Math.AbsV(A.col1), b2Math.AbsV(A.col2));
   4018       return B;
   4019    }
   4020    b2Math.Min = function (a, b) {
   4021       if (a === undefined) a = 0;
   4022       if (b === undefined) b = 0;
   4023       return a < b ? a : b;
   4024    }
   4025    b2Math.MinV = function (a, b) {
   4026       var c = new b2Vec2(b2Math.Min(a.x, b.x), b2Math.Min(a.y, b.y));
   4027       return c;
   4028    }
   4029    b2Math.Max = function (a, b) {
   4030       if (a === undefined) a = 0;
   4031       if (b === undefined) b = 0;
   4032       return a > b ? a : b;
   4033    }
   4034    b2Math.MaxV = function (a, b) {
   4035       var c = new b2Vec2(b2Math.Max(a.x, b.x), b2Math.Max(a.y, b.y));
   4036       return c;
   4037    }
   4038    b2Math.Clamp = function (a, low, high) {
   4039       if (a === undefined) a = 0;
   4040       if (low === undefined) low = 0;
   4041       if (high === undefined) high = 0;
   4042       return a < low ? low : a > high ? high : a;
   4043    }
   4044    b2Math.ClampV = function (a, low, high) {
   4045       return b2Math.MaxV(low, b2Math.MinV(a, high));
   4046    }
   4047    b2Math.Swap = function (a, b) {
   4048       var tmp = a[0];
   4049       a[0] = b[0];
   4050       b[0] = tmp;
   4051    }
   4052    b2Math.Random = function () {
   4053       return Math.random() * 2 - 1;
   4054    }
   4055    b2Math.RandomRange = function (lo, hi) {
   4056       if (lo === undefined) lo = 0;
   4057       if (hi === undefined) hi = 0;
   4058       var r = Math.random();
   4059       r = (hi - lo) * r + lo;
   4060       return r;
   4061    }
   4062    b2Math.NextPowerOfTwo = function (x) {
   4063       if (x === undefined) x = 0;
   4064       x |= (x >> 1) & 0x7FFFFFFF;
   4065       x |= (x >> 2) & 0x3FFFFFFF;
   4066       x |= (x >> 4) & 0x0FFFFFFF;
   4067       x |= (x >> 8) & 0x00FFFFFF;
   4068       x |= (x >> 16) & 0x0000FFFF;
   4069       return x + 1;
   4070    }
   4071    b2Math.IsPowerOfTwo = function (x) {
   4072       if (x === undefined) x = 0;
   4073       var result = x > 0 && (x & (x - 1)) == 0;
   4074       return result;
   4075    }
   4076    Box2D.postDefs.push(function () {
   4077       Box2D.Common.Math.b2Math.b2Vec2_zero = new b2Vec2(0.0, 0.0);
   4078       Box2D.Common.Math.b2Math.b2Mat22_identity = b2Mat22.FromVV(new b2Vec2(1.0, 0.0), new b2Vec2(0.0, 1.0));
   4079       Box2D.Common.Math.b2Math.b2Transform_identity = new b2Transform(b2Math.b2Vec2_zero, b2Math.b2Mat22_identity);
   4080    });
   4081    b2Sweep.b2Sweep = function () {
   4082       this.localCenter = new b2Vec2();
   4083       this.c0 = new b2Vec2;
   4084       this.c = new b2Vec2();
   4085    };
   4086    b2Sweep.prototype.Set = function (other) {
   4087       this.localCenter.SetV(other.localCenter);
   4088       this.c0.SetV(other.c0);
   4089       this.c.SetV(other.c);
   4090       this.a0 = other.a0;
   4091       this.a = other.a;
   4092       this.t0 = other.t0;
   4093    }
   4094    b2Sweep.prototype.Copy = function () {
   4095       var copy = new b2Sweep();
   4096       copy.localCenter.SetV(this.localCenter);
   4097       copy.c0.SetV(this.c0);
   4098       copy.c.SetV(this.c);
   4099       copy.a0 = this.a0;
   4100       copy.a = this.a;
   4101       copy.t0 = this.t0;
   4102       return copy;
   4103    }
   4104    b2Sweep.prototype.GetTransform = function (xf, alpha) {
   4105       if (alpha === undefined) alpha = 0;
   4106       xf.position.x = (1.0 - alpha) * this.c0.x + alpha * this.c.x;
   4107       xf.position.y = (1.0 - alpha) * this.c0.y + alpha * this.c.y;
   4108       var angle = (1.0 - alpha) * this.a0 + alpha * this.a;
   4109       xf.R.Set(angle);
   4110       var tMat = xf.R;
   4111       xf.position.x -= (tMat.col1.x * this.localCenter.x + tMat.col2.x * this.localCenter.y);
   4112       xf.position.y -= (tMat.col1.y * this.localCenter.x + tMat.col2.y * this.localCenter.y);
   4113    }
   4114    b2Sweep.prototype.Advance = function (t) {
   4115       if (t === undefined) t = 0;
   4116       if (this.t0 < t && 1.0 - this.t0 > Number.MIN_VALUE) {
   4117          var alpha = (t - this.t0) / (1.0 - this.t0);
   4118          this.c0.x = (1.0 - alpha) * this.c0.x + alpha * this.c.x;
   4119          this.c0.y = (1.0 - alpha) * this.c0.y + alpha * this.c.y;
   4120          this.a0 = (1.0 - alpha) * this.a0 + alpha * this.a;
   4121          this.t0 = t;
   4122       }
   4123    }
   4124    b2Transform.b2Transform = function () {
   4125       this.position = new b2Vec2;
   4126       this.R = new b2Mat22();
   4127    };
   4128    b2Transform.prototype.b2Transform = function (pos, r) {
   4129       if (pos === undefined) pos = null;
   4130       if (r === undefined) r = null;
   4131       if (pos) {
   4132          this.position.SetV(pos);
   4133          this.R.SetM(r);
   4134       }
   4135    }
   4136    b2Transform.prototype.Initialize = function (pos, r) {
   4137       this.position.SetV(pos);
   4138       this.R.SetM(r);
   4139    }
   4140    b2Transform.prototype.SetIdentity = function () {
   4141       this.position.SetZero();
   4142       this.R.SetIdentity();
   4143    }
   4144    b2Transform.prototype.Set = function (x) {
   4145       this.position.SetV(x.position);
   4146       this.R.SetM(x.R);
   4147    }
   4148    b2Transform.prototype.GetAngle = function () {
   4149       return Math.atan2(this.R.col1.y, this.R.col1.x);
   4150    }
   4151    b2Vec2.b2Vec2 = function () {};
   4152    b2Vec2.prototype.b2Vec2 = function (x_, y_) {
   4153       if (x_ === undefined) x_ = 0;
   4154       if (y_ === undefined) y_ = 0;
   4155       this.x = x_;
   4156       this.y = y_;
   4157    }
   4158    b2Vec2.prototype.SetZero = function () {
   4159       this.x = 0.0;
   4160       this.y = 0.0;
   4161    }
   4162    b2Vec2.prototype.Set = function (x_, y_) {
   4163       if (x_ === undefined) x_ = 0;
   4164       if (y_ === undefined) y_ = 0;
   4165       this.x = x_;
   4166       this.y = y_;
   4167    }
   4168    b2Vec2.prototype.SetV = function (v) {
   4169       this.x = v.x;
   4170       this.y = v.y;
   4171    }
   4172    b2Vec2.prototype.GetNegative = function () {
   4173       return new b2Vec2((-this.x), (-this.y));
   4174    }
   4175    b2Vec2.prototype.NegativeSelf = function () {
   4176       this.x = (-this.x);
   4177       this.y = (-this.y);
   4178    }
   4179    b2Vec2.Make = function (x_, y_) {
   4180       if (x_ === undefined) x_ = 0;
   4181       if (y_ === undefined) y_ = 0;
   4182       return new b2Vec2(x_, y_);
   4183    }
   4184    b2Vec2.prototype.Copy = function () {
   4185       return new b2Vec2(this.x, this.y);
   4186    }
   4187    b2Vec2.prototype.Add = function (v) {
   4188       this.x += v.x;
   4189       this.y += v.y;
   4190    }
   4191    b2Vec2.prototype.Subtract = function (v) {
   4192       this.x -= v.x;
   4193       this.y -= v.y;
   4194    }
   4195    b2Vec2.prototype.Multiply = function (a) {
   4196       if (a === undefined) a = 0;
   4197       this.x *= a;
   4198       this.y *= a;
   4199    }
   4200    b2Vec2.prototype.MulM = function (A) {
   4201       var tX = this.x;
   4202       this.x = A.col1.x * tX + A.col2.x * this.y;
   4203       this.y = A.col1.y * tX + A.col2.y * this.y;
   4204    }
   4205    b2Vec2.prototype.MulTM = function (A) {
   4206       var tX = b2Math.Dot(this, A.col1);
   4207       this.y = b2Math.Dot(this, A.col2);
   4208       this.x = tX;
   4209    }
   4210    b2Vec2.prototype.CrossVF = function (s) {
   4211       if (s === undefined) s = 0;
   4212       var tX = this.x;
   4213       this.x = s * this.y;
   4214       this.y = (-s * tX);
   4215    }
   4216    b2Vec2.prototype.CrossFV = function (s) {
   4217       if (s === undefined) s = 0;
   4218       var tX = this.x;
   4219       this.x = (-s * this.y);
   4220       this.y = s * tX;
   4221    }
   4222    b2Vec2.prototype.MinV = function (b) {
   4223       this.x = this.x < b.x ? this.x : b.x;
   4224       this.y = this.y < b.y ? this.y : b.y;
   4225    }
   4226    b2Vec2.prototype.MaxV = function (b) {
   4227       this.x = this.x > b.x ? this.x : b.x;
   4228       this.y = this.y > b.y ? this.y : b.y;
   4229    }
   4230    b2Vec2.prototype.Abs = function () {
   4231       if (this.x < 0) this.x = (-this.x);
   4232       if (this.y < 0) this.y = (-this.y);
   4233    }
   4234    b2Vec2.prototype.Length = function () {
   4235       return Math.sqrt(this.x * this.x + this.y * this.y);
   4236    }
   4237    b2Vec2.prototype.LengthSquared = function () {
   4238       return (this.x * this.x + this.y * this.y);
   4239    }
   4240    b2Vec2.prototype.Normalize = function () {
   4241       var length = Math.sqrt(this.x * this.x + this.y * this.y);
   4242       if (length < Number.MIN_VALUE) {
   4243          return 0.0;
   4244       }
   4245       var invLength = 1.0 / length;
   4246       this.x *= invLength;
   4247       this.y *= invLength;
   4248       return length;
   4249    }
   4250    b2Vec2.prototype.IsValid = function () {
   4251       return b2Math.IsValid(this.x) && b2Math.IsValid(this.y);
   4252    }
   4253    b2Vec3.b2Vec3 = function () {};
   4254    b2Vec3.prototype.b2Vec3 = function (x, y, z) {
   4255       if (x === undefined) x = 0;
   4256       if (y === undefined) y = 0;
   4257       if (z === undefined) z = 0;
   4258       this.x = x;
   4259       this.y = y;
   4260       this.z = z;
   4261    }
   4262    b2Vec3.prototype.SetZero = function () {
   4263       this.x = this.y = this.z = 0.0;
   4264    }
   4265    b2Vec3.prototype.Set = function (x, y, z) {
   4266       if (x === undefined) x = 0;
   4267       if (y === undefined) y = 0;
   4268       if (z === undefined) z = 0;
   4269       this.x = x;
   4270       this.y = y;
   4271       this.z = z;
   4272    }
   4273    b2Vec3.prototype.SetV = function (v) {
   4274       this.x = v.x;
   4275       this.y = v.y;
   4276       this.z = v.z;
   4277    }
   4278    b2Vec3.prototype.GetNegative = function () {
   4279       return new b2Vec3((-this.x), (-this.y), (-this.z));
   4280    }
   4281    b2Vec3.prototype.NegativeSelf = function () {
   4282       this.x = (-this.x);
   4283       this.y = (-this.y);
   4284       this.z = (-this.z);
   4285    }
   4286    b2Vec3.prototype.Copy = function () {
   4287       return new b2Vec3(this.x, this.y, this.z);
   4288    }
   4289    b2Vec3.prototype.Add = function (v) {
   4290       this.x += v.x;
   4291       this.y += v.y;
   4292       this.z += v.z;
   4293    }
   4294    b2Vec3.prototype.Subtract = function (v) {
   4295       this.x -= v.x;
   4296       this.y -= v.y;
   4297       this.z -= v.z;
   4298    }
   4299    b2Vec3.prototype.Multiply = function (a) {
   4300       if (a === undefined) a = 0;
   4301       this.x *= a;
   4302       this.y *= a;
   4303       this.z *= a;
   4304    }
   4305 })();
   4306 (function () {
   4307    var b2ControllerEdge = Box2D.Dynamics.Controllers.b2ControllerEdge,
   4308       b2Mat22 = Box2D.Common.Math.b2Mat22,
   4309       b2Mat33 = Box2D.Common.Math.b2Mat33,
   4310       b2Math = Box2D.Common.Math.b2Math,
   4311       b2Sweep = Box2D.Common.Math.b2Sweep,
   4312       b2Transform = Box2D.Common.Math.b2Transform,
   4313       b2Vec2 = Box2D.Common.Math.b2Vec2,
   4314       b2Vec3 = Box2D.Common.Math.b2Vec3,
   4315       b2Color = Box2D.Common.b2Color,
   4316       b2internal = Box2D.Common.b2internal,
   4317       b2Settings = Box2D.Common.b2Settings,
   4318       b2AABB = Box2D.Collision.b2AABB,
   4319       b2Bound = Box2D.Collision.b2Bound,
   4320       b2BoundValues = Box2D.Collision.b2BoundValues,
   4321       b2Collision = Box2D.Collision.b2Collision,
   4322       b2ContactID = Box2D.Collision.b2ContactID,
   4323       b2ContactPoint = Box2D.Collision.b2ContactPoint,
   4324       b2Distance = Box2D.Collision.b2Distance,
   4325       b2DistanceInput = Box2D.Collision.b2DistanceInput,
   4326       b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
   4327       b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
   4328       b2DynamicTree = Box2D.Collision.b2DynamicTree,
   4329       b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
   4330       b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
   4331       b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
   4332       b2Manifold = Box2D.Collision.b2Manifold,
   4333       b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
   4334       b2Point = Box2D.Collision.b2Point,
   4335       b2RayCastInput = Box2D.Collision.b2RayCastInput,
   4336       b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
   4337       b2Segment = Box2D.Collision.b2Segment,
   4338       b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
   4339       b2Simplex = Box2D.Collision.b2Simplex,
   4340       b2SimplexCache = Box2D.Collision.b2SimplexCache,
   4341       b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
   4342       b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
   4343       b2TOIInput = Box2D.Collision.b2TOIInput,
   4344       b2WorldManifold = Box2D.Collision.b2WorldManifold,
   4345       ClipVertex = Box2D.Collision.ClipVertex,
   4346       Features = Box2D.Collision.Features,
   4347       IBroadPhase = Box2D.Collision.IBroadPhase,
   4348       b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
   4349       b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
   4350       b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
   4351       b2MassData = Box2D.Collision.Shapes.b2MassData,
   4352       b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
   4353       b2Shape = Box2D.Collision.Shapes.b2Shape,
   4354       b2Body = Box2D.Dynamics.b2Body,
   4355       b2BodyDef = Box2D.Dynamics.b2BodyDef,
   4356       b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
   4357       b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
   4358       b2ContactListener = Box2D.Dynamics.b2ContactListener,
   4359       b2ContactManager = Box2D.Dynamics.b2ContactManager,
   4360       b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
   4361       b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
   4362       b2FilterData = Box2D.Dynamics.b2FilterData,
   4363       b2Fixture = Box2D.Dynamics.b2Fixture,
   4364       b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
   4365       b2Island = Box2D.Dynamics.b2Island,
   4366       b2TimeStep = Box2D.Dynamics.b2TimeStep,
   4367       b2World = Box2D.Dynamics.b2World,
   4368       b2CircleContact = Box2D.Dynamics.Contacts.b2CircleContact,
   4369       b2Contact = Box2D.Dynamics.Contacts.b2Contact,
   4370       b2ContactConstraint = Box2D.Dynamics.Contacts.b2ContactConstraint,
   4371       b2ContactConstraintPoint = Box2D.Dynamics.Contacts.b2ContactConstraintPoint,
   4372       b2ContactEdge = Box2D.Dynamics.Contacts.b2ContactEdge,
   4373       b2ContactFactory = Box2D.Dynamics.Contacts.b2ContactFactory,
   4374       b2ContactRegister = Box2D.Dynamics.Contacts.b2ContactRegister,
   4375       b2ContactResult = Box2D.Dynamics.Contacts.b2ContactResult,
   4376       b2ContactSolver = Box2D.Dynamics.Contacts.b2ContactSolver,
   4377       b2EdgeAndCircleContact = Box2D.Dynamics.Contacts.b2EdgeAndCircleContact,
   4378       b2NullContact = Box2D.Dynamics.Contacts.b2NullContact,
   4379       b2PolyAndCircleContact = Box2D.Dynamics.Contacts.b2PolyAndCircleContact,
   4380       b2PolyAndEdgeContact = Box2D.Dynamics.Contacts.b2PolyAndEdgeContact,
   4381       b2PolygonContact = Box2D.Dynamics.Contacts.b2PolygonContact,
   4382       b2PositionSolverManifold = Box2D.Dynamics.Contacts.b2PositionSolverManifold,
   4383       b2Controller = Box2D.Dynamics.Controllers.b2Controller,
   4384       b2DistanceJoint = Box2D.Dynamics.Joints.b2DistanceJoint,
   4385       b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef,
   4386       b2FrictionJoint = Box2D.Dynamics.Joints.b2FrictionJoint,
   4387       b2FrictionJointDef = Box2D.Dynamics.Joints.b2FrictionJointDef,
   4388       b2GearJoint = Box2D.Dynamics.Joints.b2GearJoint,
   4389       b2GearJointDef = Box2D.Dynamics.Joints.b2GearJointDef,
   4390       b2Jacobian = Box2D.Dynamics.Joints.b2Jacobian,
   4391       b2Joint = Box2D.Dynamics.Joints.b2Joint,
   4392       b2JointDef = Box2D.Dynamics.Joints.b2JointDef,
   4393       b2JointEdge = Box2D.Dynamics.Joints.b2JointEdge,
   4394       b2LineJoint = Box2D.Dynamics.Joints.b2LineJoint,
   4395       b2LineJointDef = Box2D.Dynamics.Joints.b2LineJointDef,
   4396       b2MouseJoint = Box2D.Dynamics.Joints.b2MouseJoint,
   4397       b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef,
   4398       b2PrismaticJoint = Box2D.Dynamics.Joints.b2PrismaticJoint,
   4399       b2PrismaticJointDef = Box2D.Dynamics.Joints.b2PrismaticJointDef,
   4400       b2PulleyJoint = Box2D.Dynamics.Joints.b2PulleyJoint,
   4401       b2PulleyJointDef = Box2D.Dynamics.Joints.b2PulleyJointDef,
   4402       b2RevoluteJoint = Box2D.Dynamics.Joints.b2RevoluteJoint,
   4403       b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef,
   4404       b2WeldJoint = Box2D.Dynamics.Joints.b2WeldJoint,
   4405       b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDef;
   4406 
   4407    b2Body.b2Body = function () {
   4408       this.m_xf = new b2Transform();
   4409       this.m_sweep = new b2Sweep();
   4410       this.m_linearVelocity = new b2Vec2();
   4411       this.m_force = new b2Vec2();
   4412    };
   4413    b2Body.prototype.connectEdges = function (s1, s2, angle1) {
   4414       if (angle1 === undefined) angle1 = 0;
   4415       var angle2 = Math.atan2(s2.GetDirectionVector().y, s2.GetDirectionVector().x);
   4416       var coreOffset = Math.tan((angle2 - angle1) * 0.5);
   4417       var core = b2Math.MulFV(coreOffset, s2.GetDirectionVector());
   4418       core = b2Math.SubtractVV(core, s2.GetNormalVector());
   4419       core = b2Math.MulFV(b2Settings.b2_toiSlop, core);
   4420       core = b2Math.AddVV(core, s2.GetVertex1());
   4421       var cornerDir = b2Math.AddVV(s1.GetDirectionVector(), s2.GetDirectionVector());
   4422       cornerDir.Normalize();
   4423       var convex = b2Math.Dot(s1.GetDirectionVector(), s2.GetNormalVector()) > 0.0;
   4424       s1.SetNextEdge(s2, core, cornerDir, convex);
   4425       s2.SetPrevEdge(s1, core, cornerDir, convex);
   4426       return angle2;
   4427    }
   4428    b2Body.prototype.CreateFixture = function (def) {
   4429       if (this.m_world.IsLocked() == true) {
   4430          return null;
   4431       }
   4432       var fixture = new b2Fixture();
   4433       fixture.Create(this, this.m_xf, def);
   4434       if (this.m_flags & b2Body.e_activeFlag) {
   4435          var broadPhase = this.m_world.m_contactManager.m_broadPhase;
   4436          fixture.CreateProxy(broadPhase, this.m_xf);
   4437       }
   4438       fixture.m_next = this.m_fixtureList;
   4439       this.m_fixtureList = fixture;
   4440       ++this.m_fixtureCount;
   4441       fixture.m_body = this;
   4442       if (fixture.m_density > 0.0) {
   4443          this.ResetMassData();
   4444       }
   4445       this.m_world.m_flags |= b2World.e_newFixture;
   4446       return fixture;
   4447    }
   4448    b2Body.prototype.CreateFixture2 = function (shape, density) {
   4449       if (density === undefined) density = 0.0;
   4450       var def = new b2FixtureDef();
   4451       def.shape = shape;
   4452       def.density = density;
   4453       return this.CreateFixture(def);
   4454    }
   4455    b2Body.prototype.DestroyFixture = function (fixture) {
   4456       if (this.m_world.IsLocked() == true) {
   4457          return;
   4458       }
   4459       var node = this.m_fixtureList;
   4460       var ppF = null;
   4461       var found = false;
   4462       while (node != null) {
   4463          if (node == fixture) {
   4464             if (ppF) ppF.m_next = fixture.m_next;
   4465             else this.m_fixtureList = fixture.m_next;
   4466             found = true;
   4467             break;
   4468          }
   4469          ppF = node;
   4470          node = node.m_next;
   4471       }
   4472       var edge = this.m_contactList;
   4473       while (edge) {
   4474          var c = edge.contact;
   4475          edge = edge.next;
   4476          var fixtureA = c.GetFixtureA();
   4477          var fixtureB = c.GetFixtureB();
   4478          if (fixture == fixtureA || fixture == fixtureB) {
   4479             this.m_world.m_contactManager.Destroy(c);
   4480          }
   4481       }
   4482       if (this.m_flags & b2Body.e_activeFlag) {
   4483          var broadPhase = this.m_world.m_contactManager.m_broadPhase;
   4484          fixture.DestroyProxy(broadPhase);
   4485       }
   4486       else {}
   4487       fixture.Destroy();
   4488       fixture.m_body = null;
   4489       fixture.m_next = null;
   4490       --this.m_fixtureCount;
   4491       this.ResetMassData();
   4492    }
   4493    b2Body.prototype.SetPositionAndAngle = function (position, angle) {
   4494       if (angle === undefined) angle = 0;
   4495       var f;
   4496       if (this.m_world.IsLocked() == true) {
   4497          return;
   4498       }
   4499       this.m_xf.R.Set(angle);
   4500       this.m_xf.position.SetV(position);
   4501       var tMat = this.m_xf.R;
   4502       var tVec = this.m_sweep.localCenter;
   4503       this.m_sweep.c.x = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   4504       this.m_sweep.c.y = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   4505       this.m_sweep.c.x += this.m_xf.position.x;
   4506       this.m_sweep.c.y += this.m_xf.position.y;
   4507       this.m_sweep.c0.SetV(this.m_sweep.c);
   4508       this.m_sweep.a0 = this.m_sweep.a = angle;
   4509       var broadPhase = this.m_world.m_contactManager.m_broadPhase;
   4510       for (f = this.m_fixtureList;
   4511       f; f = f.m_next) {
   4512          f.Synchronize(broadPhase, this.m_xf, this.m_xf);
   4513       }
   4514       this.m_world.m_contactManager.FindNewContacts();
   4515    }
   4516    b2Body.prototype.SetTransform = function (xf) {
   4517       this.SetPositionAndAngle(xf.position, xf.GetAngle());
   4518    }
   4519    b2Body.prototype.GetTransform = function () {
   4520       return this.m_xf;
   4521    }
   4522    b2Body.prototype.GetPosition = function () {
   4523       return this.m_xf.position;
   4524    }
   4525    b2Body.prototype.SetPosition = function (position) {
   4526       this.SetPositionAndAngle(position, this.GetAngle());
   4527    }
   4528    b2Body.prototype.GetAngle = function () {
   4529       return this.m_sweep.a;
   4530    }
   4531    b2Body.prototype.SetAngle = function (angle) {
   4532       if (angle === undefined) angle = 0;
   4533       this.SetPositionAndAngle(this.GetPosition(), angle);
   4534    }
   4535    b2Body.prototype.GetWorldCenter = function () {
   4536       return this.m_sweep.c;
   4537    }
   4538    b2Body.prototype.GetLocalCenter = function () {
   4539       return this.m_sweep.localCenter;
   4540    }
   4541    b2Body.prototype.SetLinearVelocity = function (v) {
   4542       if (this.m_type == b2Body.b2_staticBody) {
   4543          return;
   4544       }
   4545       this.m_linearVelocity.SetV(v);
   4546    }
   4547    b2Body.prototype.GetLinearVelocity = function () {
   4548       return this.m_linearVelocity;
   4549    }
   4550    b2Body.prototype.SetAngularVelocity = function (omega) {
   4551       if (omega === undefined) omega = 0;
   4552       if (this.m_type == b2Body.b2_staticBody) {
   4553          return;
   4554       }
   4555       this.m_angularVelocity = omega;
   4556    }
   4557    b2Body.prototype.GetAngularVelocity = function () {
   4558       return this.m_angularVelocity;
   4559    }
   4560    b2Body.prototype.GetDefinition = function () {
   4561       var bd = new b2BodyDef();
   4562       bd.type = this.GetType();
   4563       bd.allowSleep = (this.m_flags & b2Body.e_allowSleepFlag) == b2Body.e_allowSleepFlag;
   4564       bd.angle = this.GetAngle();
   4565       bd.angularDamping = this.m_angularDamping;
   4566       bd.angularVelocity = this.m_angularVelocity;
   4567       bd.fixedRotation = (this.m_flags & b2Body.e_fixedRotationFlag) == b2Body.e_fixedRotationFlag;
   4568       bd.bullet = (this.m_flags & b2Body.e_bulletFlag) == b2Body.e_bulletFlag;
   4569       bd.awake = (this.m_flags & b2Body.e_awakeFlag) == b2Body.e_awakeFlag;
   4570       bd.linearDamping = this.m_linearDamping;
   4571       bd.linearVelocity.SetV(this.GetLinearVelocity());
   4572       bd.position = this.GetPosition();
   4573       bd.userData = this.GetUserData();
   4574       return bd;
   4575    }
   4576    b2Body.prototype.ApplyForce = function (force, point) {
   4577       if (this.m_type != b2Body.b2_dynamicBody) {
   4578          return;
   4579       }
   4580       if (this.IsAwake() == false) {
   4581          this.SetAwake(true);
   4582       }
   4583       this.m_force.x += force.x;
   4584       this.m_force.y += force.y;
   4585       this.m_torque += ((point.x - this.m_sweep.c.x) * force.y - (point.y - this.m_sweep.c.y) * force.x);
   4586    }
   4587    b2Body.prototype.ApplyTorque = function (torque) {
   4588       if (torque === undefined) torque = 0;
   4589       if (this.m_type != b2Body.b2_dynamicBody) {
   4590          return;
   4591       }
   4592       if (this.IsAwake() == false) {
   4593          this.SetAwake(true);
   4594       }
   4595       this.m_torque += torque;
   4596    }
   4597    b2Body.prototype.ApplyImpulse = function (impulse, point) {
   4598       if (this.m_type != b2Body.b2_dynamicBody) {
   4599          return;
   4600       }
   4601       if (this.IsAwake() == false) {
   4602          this.SetAwake(true);
   4603       }
   4604       this.m_linearVelocity.x += this.m_invMass * impulse.x;
   4605       this.m_linearVelocity.y += this.m_invMass * impulse.y;
   4606       this.m_angularVelocity += this.m_invI * ((point.x - this.m_sweep.c.x) * impulse.y - (point.y - this.m_sweep.c.y) * impulse.x);
   4607    }
   4608    b2Body.prototype.Split = function (callback) {
   4609       var linearVelocity = this.GetLinearVelocity().Copy();
   4610       var angularVelocity = this.GetAngularVelocity();
   4611       var center = this.GetWorldCenter();
   4612       var body1 = this;
   4613       var body2 = this.m_world.CreateBody(this.GetDefinition());
   4614       var prev;
   4615       for (var f = body1.m_fixtureList; f;) {
   4616          if (callback(f)) {
   4617             var next = f.m_next;
   4618             if (prev) {
   4619                prev.m_next = next;
   4620             }
   4621             else {
   4622                body1.m_fixtureList = next;
   4623             }
   4624             body1.m_fixtureCount--;
   4625             f.m_next = body2.m_fixtureList;
   4626             body2.m_fixtureList = f;
   4627             body2.m_fixtureCount++;
   4628             f.m_body = body2;
   4629             f = next;
   4630          }
   4631          else {
   4632             prev = f;
   4633             f = f.m_next;
   4634          }
   4635       }
   4636       body1.ResetMassData();
   4637       body2.ResetMassData();
   4638       var center1 = body1.GetWorldCenter();
   4639       var center2 = body2.GetWorldCenter();
   4640       var velocity1 = b2Math.AddVV(linearVelocity, b2Math.CrossFV(angularVelocity, b2Math.SubtractVV(center1, center)));
   4641       var velocity2 = b2Math.AddVV(linearVelocity, b2Math.CrossFV(angularVelocity, b2Math.SubtractVV(center2, center)));
   4642       body1.SetLinearVelocity(velocity1);
   4643       body2.SetLinearVelocity(velocity2);
   4644       body1.SetAngularVelocity(angularVelocity);
   4645       body2.SetAngularVelocity(angularVelocity);
   4646       body1.SynchronizeFixtures();
   4647       body2.SynchronizeFixtures();
   4648       return body2;
   4649    }
   4650    b2Body.prototype.Merge = function (other) {
   4651       var f;
   4652       for (f = other.m_fixtureList;
   4653       f;) {
   4654          var next = f.m_next;
   4655          other.m_fixtureCount--;
   4656          f.m_next = this.m_fixtureList;
   4657          this.m_fixtureList = f;
   4658          this.m_fixtureCount++;
   4659          f.m_body = body2;
   4660          f = next;
   4661       }
   4662       body1.m_fixtureCount = 0;
   4663       var body1 = this;
   4664       var body2 = other;
   4665       var center1 = body1.GetWorldCenter();
   4666       var center2 = body2.GetWorldCenter();
   4667       var velocity1 = body1.GetLinearVelocity().Copy();
   4668       var velocity2 = body2.GetLinearVelocity().Copy();
   4669       var angular1 = body1.GetAngularVelocity();
   4670       var angular = body2.GetAngularVelocity();
   4671       body1.ResetMassData();
   4672       this.SynchronizeFixtures();
   4673    }
   4674    b2Body.prototype.GetMass = function () {
   4675       return this.m_mass;
   4676    }
   4677    b2Body.prototype.GetInertia = function () {
   4678       return this.m_I;
   4679    }
   4680    b2Body.prototype.GetMassData = function (data) {
   4681       data.mass = this.m_mass;
   4682       data.I = this.m_I;
   4683       data.center.SetV(this.m_sweep.localCenter);
   4684    }
   4685    b2Body.prototype.SetMassData = function (massData) {
   4686       b2Settings.b2Assert(this.m_world.IsLocked() == false);
   4687       if (this.m_world.IsLocked() == true) {
   4688          return;
   4689       }
   4690       if (this.m_type != b2Body.b2_dynamicBody) {
   4691          return;
   4692       }
   4693       this.m_invMass = 0.0;
   4694       this.m_I = 0.0;
   4695       this.m_invI = 0.0;
   4696       this.m_mass = massData.mass;
   4697       if (this.m_mass <= 0.0) {
   4698          this.m_mass = 1.0;
   4699       }
   4700       this.m_invMass = 1.0 / this.m_mass;
   4701       if (massData.I > 0.0 && (this.m_flags & b2Body.e_fixedRotationFlag) == 0) {
   4702          this.m_I = massData.I - this.m_mass * (massData.center.x * massData.center.x + massData.center.y * massData.center.y);
   4703          this.m_invI = 1.0 / this.m_I;
   4704       }
   4705       var oldCenter = this.m_sweep.c.Copy();
   4706       this.m_sweep.localCenter.SetV(massData.center);
   4707       this.m_sweep.c0.SetV(b2Math.MulX(this.m_xf, this.m_sweep.localCenter));
   4708       this.m_sweep.c.SetV(this.m_sweep.c0);
   4709       this.m_linearVelocity.x += this.m_angularVelocity * (-(this.m_sweep.c.y - oldCenter.y));
   4710       this.m_linearVelocity.y += this.m_angularVelocity * (+(this.m_sweep.c.x - oldCenter.x));
   4711    }
   4712    b2Body.prototype.ResetMassData = function () {
   4713       this.m_mass = 0.0;
   4714       this.m_invMass = 0.0;
   4715       this.m_I = 0.0;
   4716       this.m_invI = 0.0;
   4717       this.m_sweep.localCenter.SetZero();
   4718       if (this.m_type == b2Body.b2_staticBody || this.m_type == b2Body.b2_kinematicBody) {
   4719          return;
   4720       }
   4721       var center = b2Vec2.Make(0, 0);
   4722       for (var f = this.m_fixtureList; f; f = f.m_next) {
   4723          if (f.m_density == 0.0) {
   4724             continue;
   4725          }
   4726          var massData = f.GetMassData();
   4727          this.m_mass += massData.mass;
   4728          center.x += massData.center.x * massData.mass;
   4729          center.y += massData.center.y * massData.mass;
   4730          this.m_I += massData.I;
   4731       }
   4732       if (this.m_mass > 0.0) {
   4733          this.m_invMass = 1.0 / this.m_mass;
   4734          center.x *= this.m_invMass;
   4735          center.y *= this.m_invMass;
   4736       }
   4737       else {
   4738          this.m_mass = 1.0;
   4739          this.m_invMass = 1.0;
   4740       }
   4741       if (this.m_I > 0.0 && (this.m_flags & b2Body.e_fixedRotationFlag) == 0) {
   4742          this.m_I -= this.m_mass * (center.x * center.x + center.y * center.y);
   4743          this.m_I *= this.m_inertiaScale;
   4744          b2Settings.b2Assert(this.m_I > 0);
   4745          this.m_invI = 1.0 / this.m_I;
   4746       }
   4747       else {
   4748          this.m_I = 0.0;
   4749          this.m_invI = 0.0;
   4750       }
   4751       var oldCenter = this.m_sweep.c.Copy();
   4752       this.m_sweep.localCenter.SetV(center);
   4753       this.m_sweep.c0.SetV(b2Math.MulX(this.m_xf, this.m_sweep.localCenter));
   4754       this.m_sweep.c.SetV(this.m_sweep.c0);
   4755       this.m_linearVelocity.x += this.m_angularVelocity * (-(this.m_sweep.c.y - oldCenter.y));
   4756       this.m_linearVelocity.y += this.m_angularVelocity * (+(this.m_sweep.c.x - oldCenter.x));
   4757    }
   4758    b2Body.prototype.GetWorldPoint = function (localPoint) {
   4759       var A = this.m_xf.R;
   4760       var u = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y, A.col1.y * localPoint.x + A.col2.y * localPoint.y);
   4761       u.x += this.m_xf.position.x;
   4762       u.y += this.m_xf.position.y;
   4763       return u;
   4764    }
   4765    b2Body.prototype.GetWorldVector = function (localVector) {
   4766       return b2Math.MulMV(this.m_xf.R, localVector);
   4767    }
   4768    b2Body.prototype.GetLocalPoint = function (worldPoint) {
   4769       return b2Math.MulXT(this.m_xf, worldPoint);
   4770    }
   4771    b2Body.prototype.GetLocalVector = function (worldVector) {
   4772       return b2Math.MulTMV(this.m_xf.R, worldVector);
   4773    }
   4774    b2Body.prototype.GetLinearVelocityFromWorldPoint = function (worldPoint) {
   4775       return new b2Vec2(this.m_linearVelocity.x - this.m_angularVelocity * (worldPoint.y - this.m_sweep.c.y), this.m_linearVelocity.y + this.m_angularVelocity * (worldPoint.x - this.m_sweep.c.x));
   4776    }
   4777    b2Body.prototype.GetLinearVelocityFromLocalPoint = function (localPoint) {
   4778       var A = this.m_xf.R;
   4779       var worldPoint = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y, A.col1.y * localPoint.x + A.col2.y * localPoint.y);
   4780       worldPoint.x += this.m_xf.position.x;
   4781       worldPoint.y += this.m_xf.position.y;
   4782       return new b2Vec2(this.m_linearVelocity.x - this.m_angularVelocity * (worldPoint.y - this.m_sweep.c.y), this.m_linearVelocity.y + this.m_angularVelocity * (worldPoint.x - this.m_sweep.c.x));
   4783    }
   4784    b2Body.prototype.GetLinearDamping = function () {
   4785       return this.m_linearDamping;
   4786    }
   4787    b2Body.prototype.SetLinearDamping = function (linearDamping) {
   4788       if (linearDamping === undefined) linearDamping = 0;
   4789       this.m_linearDamping = linearDamping;
   4790    }
   4791    b2Body.prototype.GetAngularDamping = function () {
   4792       return this.m_angularDamping;
   4793    }
   4794    b2Body.prototype.SetAngularDamping = function (angularDamping) {
   4795       if (angularDamping === undefined) angularDamping = 0;
   4796       this.m_angularDamping = angularDamping;
   4797    }
   4798    b2Body.prototype.SetType = function (type) {
   4799       if (type === undefined) type = 0;
   4800       if (this.m_type == type) {
   4801          return;
   4802       }
   4803       this.m_type = type;
   4804       this.ResetMassData();
   4805       if (this.m_type == b2Body.b2_staticBody) {
   4806          this.m_linearVelocity.SetZero();
   4807          this.m_angularVelocity = 0.0;
   4808       }
   4809       this.SetAwake(true);
   4810       this.m_force.SetZero();
   4811       this.m_torque = 0.0;
   4812       for (var ce = this.m_contactList; ce; ce = ce.next) {
   4813          ce.contact.FlagForFiltering();
   4814       }
   4815    }
   4816    b2Body.prototype.GetType = function () {
   4817       return this.m_type;
   4818    }
   4819    b2Body.prototype.SetBullet = function (flag) {
   4820       if (flag) {
   4821          this.m_flags |= b2Body.e_bulletFlag;
   4822       }
   4823       else {
   4824          this.m_flags &= ~b2Body.e_bulletFlag;
   4825       }
   4826    }
   4827    b2Body.prototype.IsBullet = function () {
   4828       return (this.m_flags & b2Body.e_bulletFlag) == b2Body.e_bulletFlag;
   4829    }
   4830    b2Body.prototype.SetSleepingAllowed = function (flag) {
   4831       if (flag) {
   4832          this.m_flags |= b2Body.e_allowSleepFlag;
   4833       }
   4834       else {
   4835          this.m_flags &= ~b2Body.e_allowSleepFlag;
   4836          this.SetAwake(true);
   4837       }
   4838    }
   4839    b2Body.prototype.SetAwake = function (flag) {
   4840       if (flag) {
   4841          this.m_flags |= b2Body.e_awakeFlag;
   4842          this.m_sleepTime = 0.0;
   4843       }
   4844       else {
   4845          this.m_flags &= ~b2Body.e_awakeFlag;
   4846          this.m_sleepTime = 0.0;
   4847          this.m_linearVelocity.SetZero();
   4848          this.m_angularVelocity = 0.0;
   4849          this.m_force.SetZero();
   4850          this.m_torque = 0.0;
   4851       }
   4852    }
   4853    b2Body.prototype.IsAwake = function () {
   4854       return (this.m_flags & b2Body.e_awakeFlag) == b2Body.e_awakeFlag;
   4855    }
   4856    b2Body.prototype.SetFixedRotation = function (fixed) {
   4857       if (fixed) {
   4858          this.m_flags |= b2Body.e_fixedRotationFlag;
   4859       }
   4860       else {
   4861          this.m_flags &= ~b2Body.e_fixedRotationFlag;
   4862       }
   4863       this.ResetMassData();
   4864    }
   4865    b2Body.prototype.IsFixedRotation = function () {
   4866       return (this.m_flags & b2Body.e_fixedRotationFlag) == b2Body.e_fixedRotationFlag;
   4867    }
   4868    b2Body.prototype.SetActive = function (flag) {
   4869       if (flag == this.IsActive()) {
   4870          return;
   4871       }
   4872       var broadPhase;
   4873       var f;
   4874       if (flag) {
   4875          this.m_flags |= b2Body.e_activeFlag;
   4876          broadPhase = this.m_world.m_contactManager.m_broadPhase;
   4877          for (f = this.m_fixtureList;
   4878          f; f = f.m_next) {
   4879             f.CreateProxy(broadPhase, this.m_xf);
   4880          }
   4881       }
   4882       else {
   4883          this.m_flags &= ~b2Body.e_activeFlag;
   4884          broadPhase = this.m_world.m_contactManager.m_broadPhase;
   4885          for (f = this.m_fixtureList;
   4886          f; f = f.m_next) {
   4887             f.DestroyProxy(broadPhase);
   4888          }
   4889          var ce = this.m_contactList;
   4890          while (ce) {
   4891             var ce0 = ce;
   4892             ce = ce.next;
   4893             this.m_world.m_contactManager.Destroy(ce0.contact);
   4894          }
   4895          this.m_contactList = null;
   4896       }
   4897    }
   4898    b2Body.prototype.IsActive = function () {
   4899       return (this.m_flags & b2Body.e_activeFlag) == b2Body.e_activeFlag;
   4900    }
   4901    b2Body.prototype.IsSleepingAllowed = function () {
   4902       return (this.m_flags & b2Body.e_allowSleepFlag) == b2Body.e_allowSleepFlag;
   4903    }
   4904    b2Body.prototype.GetFixtureList = function () {
   4905       return this.m_fixtureList;
   4906    }
   4907    b2Body.prototype.GetJointList = function () {
   4908       return this.m_jointList;
   4909    }
   4910    b2Body.prototype.GetControllerList = function () {
   4911       return this.m_controllerList;
   4912    }
   4913    b2Body.prototype.GetContactList = function () {
   4914       return this.m_contactList;
   4915    }
   4916    b2Body.prototype.GetNext = function () {
   4917       return this.m_next;
   4918    }
   4919    b2Body.prototype.GetUserData = function () {
   4920       return this.m_userData;
   4921    }
   4922    b2Body.prototype.SetUserData = function (data) {
   4923       this.m_userData = data;
   4924    }
   4925    b2Body.prototype.GetWorld = function () {
   4926       return this.m_world;
   4927    }
   4928    b2Body.prototype.b2Body = function (bd, world) {
   4929       this.m_flags = 0;
   4930       if (bd.bullet) {
   4931          this.m_flags |= b2Body.e_bulletFlag;
   4932       }
   4933       if (bd.fixedRotation) {
   4934          this.m_flags |= b2Body.e_fixedRotationFlag;
   4935       }
   4936       if (bd.allowSleep) {
   4937          this.m_flags |= b2Body.e_allowSleepFlag;
   4938       }
   4939       if (bd.awake) {
   4940          this.m_flags |= b2Body.e_awakeFlag;
   4941       }
   4942       if (bd.active) {
   4943          this.m_flags |= b2Body.e_activeFlag;
   4944       }
   4945       this.m_world = world;
   4946       this.m_xf.position.SetV(bd.position);
   4947       this.m_xf.R.Set(bd.angle);
   4948       this.m_sweep.localCenter.SetZero();
   4949       this.m_sweep.t0 = 1.0;
   4950       this.m_sweep.a0 = this.m_sweep.a = bd.angle;
   4951       var tMat = this.m_xf.R;
   4952       var tVec = this.m_sweep.localCenter;
   4953       this.m_sweep.c.x = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   4954       this.m_sweep.c.y = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   4955       this.m_sweep.c.x += this.m_xf.position.x;
   4956       this.m_sweep.c.y += this.m_xf.position.y;
   4957       this.m_sweep.c0.SetV(this.m_sweep.c);
   4958       this.m_jointList = null;
   4959       this.m_controllerList = null;
   4960       this.m_contactList = null;
   4961       this.m_controllerCount = 0;
   4962       this.m_prev = null;
   4963       this.m_next = null;
   4964       this.m_linearVelocity.SetV(bd.linearVelocity);
   4965       this.m_angularVelocity = bd.angularVelocity;
   4966       this.m_linearDamping = bd.linearDamping;
   4967       this.m_angularDamping = bd.angularDamping;
   4968       this.m_force.Set(0.0, 0.0);
   4969       this.m_torque = 0.0;
   4970       this.m_sleepTime = 0.0;
   4971       this.m_type = bd.type;
   4972       if (this.m_type == b2Body.b2_dynamicBody) {
   4973          this.m_mass = 1.0;
   4974          this.m_invMass = 1.0;
   4975       }
   4976       else {
   4977          this.m_mass = 0.0;
   4978          this.m_invMass = 0.0;
   4979       }
   4980       this.m_I = 0.0;
   4981       this.m_invI = 0.0;
   4982       this.m_inertiaScale = bd.inertiaScale;
   4983       this.m_userData = bd.userData;
   4984       this.m_fixtureList = null;
   4985       this.m_fixtureCount = 0;
   4986    }
   4987    b2Body.prototype.SynchronizeFixtures = function () {
   4988       var xf1 = b2Body.s_xf1;
   4989       xf1.R.Set(this.m_sweep.a0);
   4990       var tMat = xf1.R;
   4991       var tVec = this.m_sweep.localCenter;
   4992       xf1.position.x = this.m_sweep.c0.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   4993       xf1.position.y = this.m_sweep.c0.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   4994       var f;
   4995       var broadPhase = this.m_world.m_contactManager.m_broadPhase;
   4996       for (f = this.m_fixtureList;
   4997       f; f = f.m_next) {
   4998          f.Synchronize(broadPhase, xf1, this.m_xf);
   4999       }
   5000    }
   5001    b2Body.prototype.SynchronizeTransform = function () {
   5002       this.m_xf.R.Set(this.m_sweep.a);
   5003       var tMat = this.m_xf.R;
   5004       var tVec = this.m_sweep.localCenter;
   5005       this.m_xf.position.x = this.m_sweep.c.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   5006       this.m_xf.position.y = this.m_sweep.c.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   5007    }
   5008    b2Body.prototype.ShouldCollide = function (other) {
   5009       if (this.m_type != b2Body.b2_dynamicBody && other.m_type != b2Body.b2_dynamicBody) {
   5010          return false;
   5011       }
   5012       for (var jn = this.m_jointList; jn; jn = jn.next) {
   5013          if (jn.other == other) if (jn.joint.m_collideConnected == false) {
   5014             return false;
   5015          }
   5016       }
   5017       return true;
   5018    }
   5019    b2Body.prototype.Advance = function (t) {
   5020       if (t === undefined) t = 0;
   5021       this.m_sweep.Advance(t);
   5022       this.m_sweep.c.SetV(this.m_sweep.c0);
   5023       this.m_sweep.a = this.m_sweep.a0;
   5024       this.SynchronizeTransform();
   5025    }
   5026    Box2D.postDefs.push(function () {
   5027       Box2D.Dynamics.b2Body.s_xf1 = new b2Transform();
   5028       Box2D.Dynamics.b2Body.e_islandFlag = 0x0001;
   5029       Box2D.Dynamics.b2Body.e_awakeFlag = 0x0002;
   5030       Box2D.Dynamics.b2Body.e_allowSleepFlag = 0x0004;
   5031       Box2D.Dynamics.b2Body.e_bulletFlag = 0x0008;
   5032       Box2D.Dynamics.b2Body.e_fixedRotationFlag = 0x0010;
   5033       Box2D.Dynamics.b2Body.e_activeFlag = 0x0020;
   5034       Box2D.Dynamics.b2Body.b2_staticBody = 0;
   5035       Box2D.Dynamics.b2Body.b2_kinematicBody = 1;
   5036       Box2D.Dynamics.b2Body.b2_dynamicBody = 2;
   5037    });
   5038    b2BodyDef.b2BodyDef = function () {
   5039       this.position = new b2Vec2();
   5040       this.linearVelocity = new b2Vec2();
   5041    };
   5042    b2BodyDef.prototype.b2BodyDef = function () {
   5043       this.userData = null;
   5044       this.position.Set(0.0, 0.0);
   5045       this.angle = 0.0;
   5046       this.linearVelocity.Set(0, 0);
   5047       this.angularVelocity = 0.0;
   5048       this.linearDamping = 0.0;
   5049       this.angularDamping = 0.0;
   5050       this.allowSleep = true;
   5051       this.awake = true;
   5052       this.fixedRotation = false;
   5053       this.bullet = false;
   5054       this.type = b2Body.b2_staticBody;
   5055       this.active = true;
   5056       this.inertiaScale = 1.0;
   5057    }
   5058    b2ContactFilter.b2ContactFilter = function () {};
   5059    b2ContactFilter.prototype.ShouldCollide = function (fixtureA, fixtureB) {
   5060       var filter1 = fixtureA.GetFilterData();
   5061       var filter2 = fixtureB.GetFilterData();
   5062       if (filter1.groupIndex == filter2.groupIndex && filter1.groupIndex != 0) {
   5063          return filter1.groupIndex > 0;
   5064       }
   5065       var collide = (filter1.maskBits & filter2.categoryBits) != 0 && (filter1.categoryBits & filter2.maskBits) != 0;
   5066       return collide;
   5067    }
   5068    b2ContactFilter.prototype.RayCollide = function (userData, fixture) {
   5069       if (!userData) return true;
   5070       return this.ShouldCollide((userData instanceof b2Fixture ? userData : null), fixture);
   5071    }
   5072    Box2D.postDefs.push(function () {
   5073       Box2D.Dynamics.b2ContactFilter.b2_defaultFilter = new b2ContactFilter();
   5074    });
   5075    b2ContactImpulse.b2ContactImpulse = function () {
   5076       this.normalImpulses = new Vector_a2j_Number(b2Settings.b2_maxManifoldPoints);
   5077       this.tangentImpulses = new Vector_a2j_Number(b2Settings.b2_maxManifoldPoints);
   5078    };
   5079    b2ContactListener.b2ContactListener = function () {};
   5080    b2ContactListener.prototype.BeginContact = function (contact) {}
   5081    b2ContactListener.prototype.EndContact = function (contact) {}
   5082    b2ContactListener.prototype.PreSolve = function (contact, oldManifold) {}
   5083    b2ContactListener.prototype.PostSolve = function (contact, impulse) {}
   5084    Box2D.postDefs.push(function () {
   5085       Box2D.Dynamics.b2ContactListener.b2_defaultListener = new b2ContactListener();
   5086    });
   5087    b2ContactManager.b2ContactManager = function () {};
   5088    b2ContactManager.prototype.b2ContactManager = function () {
   5089       this.m_world = null;
   5090       this.m_contactCount = 0;
   5091       this.m_contactFilter = b2ContactFilter.b2_defaultFilter;
   5092       this.m_contactListener = b2ContactListener.b2_defaultListener;
   5093       this.m_contactFactory = new b2ContactFactory(this.m_allocator);
   5094       this.m_broadPhase = new b2DynamicTreeBroadPhase();
   5095    }
   5096    b2ContactManager.prototype.AddPair = function (proxyUserDataA, proxyUserDataB) {
   5097       var fixtureA = (proxyUserDataA instanceof b2Fixture ? proxyUserDataA : null);
   5098       var fixtureB = (proxyUserDataB instanceof b2Fixture ? proxyUserDataB : null);
   5099       var bodyA = fixtureA.GetBody();
   5100       var bodyB = fixtureB.GetBody();
   5101       if (bodyA == bodyB) return;
   5102       var edge = bodyB.GetContactList();
   5103       while (edge) {
   5104          if (edge.other == bodyA) {
   5105             var fA = edge.contact.GetFixtureA();
   5106             var fB = edge.contact.GetFixtureB();
   5107             if (fA == fixtureA && fB == fixtureB) return;
   5108             if (fA == fixtureB && fB == fixtureA) return;
   5109          }
   5110          edge = edge.next;
   5111       }
   5112       if (bodyB.ShouldCollide(bodyA) == false) {
   5113          return;
   5114       }
   5115       if (this.m_contactFilter.ShouldCollide(fixtureA, fixtureB) == false) {
   5116          return;
   5117       }
   5118       var c = this.m_contactFactory.Create(fixtureA, fixtureB);
   5119       fixtureA = c.GetFixtureA();
   5120       fixtureB = c.GetFixtureB();
   5121       bodyA = fixtureA.m_body;
   5122       bodyB = fixtureB.m_body;
   5123       c.m_prev = null;
   5124       c.m_next = this.m_world.m_contactList;
   5125       if (this.m_world.m_contactList != null) {
   5126          this.m_world.m_contactList.m_prev = c;
   5127       }
   5128       this.m_world.m_contactList = c;
   5129       c.m_nodeA.contact = c;
   5130       c.m_nodeA.other = bodyB;
   5131       c.m_nodeA.prev = null;
   5132       c.m_nodeA.next = bodyA.m_contactList;
   5133       if (bodyA.m_contactList != null) {
   5134          bodyA.m_contactList.prev = c.m_nodeA;
   5135       }
   5136       bodyA.m_contactList = c.m_nodeA;
   5137       c.m_nodeB.contact = c;
   5138       c.m_nodeB.other = bodyA;
   5139       c.m_nodeB.prev = null;
   5140       c.m_nodeB.next = bodyB.m_contactList;
   5141       if (bodyB.m_contactList != null) {
   5142          bodyB.m_contactList.prev = c.m_nodeB;
   5143       }
   5144       bodyB.m_contactList = c.m_nodeB;
   5145       ++this.m_world.m_contactCount;
   5146       return;
   5147    }
   5148    b2ContactManager.prototype.FindNewContacts = function () {
   5149       this.m_broadPhase.UpdatePairs(Box2D.generateCallback(this, this.AddPair));
   5150    }
   5151    b2ContactManager.prototype.Destroy = function (c) {
   5152       var fixtureA = c.GetFixtureA();
   5153       var fixtureB = c.GetFixtureB();
   5154       var bodyA = fixtureA.GetBody();
   5155       var bodyB = fixtureB.GetBody();
   5156       if (c.IsTouching()) {
   5157          this.m_contactListener.EndContact(c);
   5158       }
   5159       if (c.m_prev) {
   5160          c.m_prev.m_next = c.m_next;
   5161       }
   5162       if (c.m_next) {
   5163          c.m_next.m_prev = c.m_prev;
   5164       }
   5165       if (c == this.m_world.m_contactList) {
   5166          this.m_world.m_contactList = c.m_next;
   5167       }
   5168       if (c.m_nodeA.prev) {
   5169          c.m_nodeA.prev.next = c.m_nodeA.next;
   5170       }
   5171       if (c.m_nodeA.next) {
   5172          c.m_nodeA.next.prev = c.m_nodeA.prev;
   5173       }
   5174       if (c.m_nodeA == bodyA.m_contactList) {
   5175          bodyA.m_contactList = c.m_nodeA.next;
   5176       }
   5177       if (c.m_nodeB.prev) {
   5178          c.m_nodeB.prev.next = c.m_nodeB.next;
   5179       }
   5180       if (c.m_nodeB.next) {
   5181          c.m_nodeB.next.prev = c.m_nodeB.prev;
   5182       }
   5183       if (c.m_nodeB == bodyB.m_contactList) {
   5184          bodyB.m_contactList = c.m_nodeB.next;
   5185       }
   5186       this.m_contactFactory.Destroy(c);
   5187       --this.m_contactCount;
   5188    }
   5189    b2ContactManager.prototype.Collide = function () {
   5190       var c = this.m_world.m_contactList;
   5191       while (c) {
   5192          var fixtureA = c.GetFixtureA();
   5193          var fixtureB = c.GetFixtureB();
   5194          var bodyA = fixtureA.GetBody();
   5195          var bodyB = fixtureB.GetBody();
   5196          if (bodyA.IsAwake() == false && bodyB.IsAwake() == false) {
   5197             c = c.GetNext();
   5198             continue;
   5199          }
   5200          if (c.m_flags & b2Contact.e_filterFlag) {
   5201             if (bodyB.ShouldCollide(bodyA) == false) {
   5202                var cNuke = c;
   5203                c = cNuke.GetNext();
   5204                this.Destroy(cNuke);
   5205                continue;
   5206             }
   5207             if (this.m_contactFilter.ShouldCollide(fixtureA, fixtureB) == false) {
   5208                cNuke = c;
   5209                c = cNuke.GetNext();
   5210                this.Destroy(cNuke);
   5211                continue;
   5212             }
   5213             c.m_flags &= ~b2Contact.e_filterFlag;
   5214          }
   5215          var proxyA = fixtureA.m_proxy;
   5216          var proxyB = fixtureB.m_proxy;
   5217          var overlap = this.m_broadPhase.TestOverlap(proxyA, proxyB);
   5218          if (overlap == false) {
   5219             cNuke = c;
   5220             c = cNuke.GetNext();
   5221             this.Destroy(cNuke);
   5222             continue;
   5223          }
   5224          c.Update(this.m_contactListener);
   5225          c = c.GetNext();
   5226       }
   5227    }
   5228    Box2D.postDefs.push(function () {
   5229       Box2D.Dynamics.b2ContactManager.s_evalCP = new b2ContactPoint();
   5230    });
   5231    b2DebugDraw.b2DebugDraw = function () {};
   5232    b2DebugDraw.prototype.b2DebugDraw = function () {}
   5233    b2DebugDraw.prototype.SetFlags = function (flags) {
   5234       if (flags === undefined) flags = 0;
   5235    }
   5236    b2DebugDraw.prototype.GetFlags = function () {}
   5237    b2DebugDraw.prototype.AppendFlags = function (flags) {
   5238       if (flags === undefined) flags = 0;
   5239    }
   5240    b2DebugDraw.prototype.ClearFlags = function (flags) {
   5241       if (flags === undefined) flags = 0;
   5242    }
   5243    b2DebugDraw.prototype.SetSprite = function (sprite) {}
   5244    b2DebugDraw.prototype.GetSprite = function () {}
   5245    b2DebugDraw.prototype.SetDrawScale = function (drawScale) {
   5246       if (drawScale === undefined) drawScale = 0;
   5247    }
   5248    b2DebugDraw.prototype.GetDrawScale = function () {}
   5249    b2DebugDraw.prototype.SetLineThickness = function (lineThickness) {
   5250       if (lineThickness === undefined) lineThickness = 0;
   5251    }
   5252    b2DebugDraw.prototype.GetLineThickness = function () {}
   5253    b2DebugDraw.prototype.SetAlpha = function (alpha) {
   5254       if (alpha === undefined) alpha = 0;
   5255    }
   5256    b2DebugDraw.prototype.GetAlpha = function () {}
   5257    b2DebugDraw.prototype.SetFillAlpha = function (alpha) {
   5258       if (alpha === undefined) alpha = 0;
   5259    }
   5260    b2DebugDraw.prototype.GetFillAlpha = function () {}
   5261    b2DebugDraw.prototype.SetXFormScale = function (xformScale) {
   5262       if (xformScale === undefined) xformScale = 0;
   5263    }
   5264    b2DebugDraw.prototype.GetXFormScale = function () {}
   5265    b2DebugDraw.prototype.DrawPolygon = function (vertices, vertexCount, color) {
   5266       if (vertexCount === undefined) vertexCount = 0;
   5267    }
   5268    b2DebugDraw.prototype.DrawSolidPolygon = function (vertices, vertexCount, color) {
   5269       if (vertexCount === undefined) vertexCount = 0;
   5270    }
   5271    b2DebugDraw.prototype.DrawCircle = function (center, radius, color) {
   5272       if (radius === undefined) radius = 0;
   5273    }
   5274    b2DebugDraw.prototype.DrawSolidCircle = function (center, radius, axis, color) {
   5275       if (radius === undefined) radius = 0;
   5276    }
   5277    b2DebugDraw.prototype.DrawSegment = function (p1, p2, color) {}
   5278    b2DebugDraw.prototype.DrawTransform = function (xf) {}
   5279    Box2D.postDefs.push(function () {
   5280       Box2D.Dynamics.b2DebugDraw.e_shapeBit = 0x0001;
   5281       Box2D.Dynamics.b2DebugDraw.e_jointBit = 0x0002;
   5282       Box2D.Dynamics.b2DebugDraw.e_aabbBit = 0x0004;
   5283       Box2D.Dynamics.b2DebugDraw.e_pairBit = 0x0008;
   5284       Box2D.Dynamics.b2DebugDraw.e_centerOfMassBit = 0x0010;
   5285       Box2D.Dynamics.b2DebugDraw.e_controllerBit = 0x0020;
   5286    });
   5287    b2DestructionListener.b2DestructionListener = function () {};
   5288    b2DestructionListener.prototype.SayGoodbyeJoint = function (joint) {}
   5289    b2DestructionListener.prototype.SayGoodbyeFixture = function (fixture) {}
   5290    b2FilterData.b2FilterData = function () {
   5291       this.categoryBits = 0x0001;
   5292       this.maskBits = 0xFFFF;
   5293       this.groupIndex = 0;
   5294    };
   5295    b2FilterData.prototype.Copy = function () {
   5296       var copy = new b2FilterData();
   5297       copy.categoryBits = this.categoryBits;
   5298       copy.maskBits = this.maskBits;
   5299       copy.groupIndex = this.groupIndex;
   5300       return copy;
   5301    }
   5302    b2Fixture.b2Fixture = function () {
   5303       this.m_filter = new b2FilterData();
   5304    };
   5305    b2Fixture.prototype.GetType = function () {
   5306       return this.m_shape.GetType();
   5307    }
   5308    b2Fixture.prototype.GetShape = function () {
   5309       return this.m_shape;
   5310    }
   5311    b2Fixture.prototype.SetSensor = function (sensor) {
   5312       if (this.m_isSensor == sensor) return;
   5313       this.m_isSensor = sensor;
   5314       if (this.m_body == null) return;
   5315       var edge = this.m_body.GetContactList();
   5316       while (edge) {
   5317          var contact = edge.contact;
   5318          var fixtureA = contact.GetFixtureA();
   5319          var fixtureB = contact.GetFixtureB();
   5320          if (fixtureA == this || fixtureB == this) contact.SetSensor(fixtureA.IsSensor() || fixtureB.IsSensor());
   5321          edge = edge.next;
   5322       }
   5323    }
   5324    b2Fixture.prototype.IsSensor = function () {
   5325       return this.m_isSensor;
   5326    }
   5327    b2Fixture.prototype.SetFilterData = function (filter) {
   5328       this.m_filter = filter.Copy();
   5329       if (this.m_body) return;
   5330       var edge = this.m_body.GetContactList();
   5331       while (edge) {
   5332          var contact = edge.contact;
   5333          var fixtureA = contact.GetFixtureA();
   5334          var fixtureB = contact.GetFixtureB();
   5335          if (fixtureA == this || fixtureB == this) contact.FlagForFiltering();
   5336          edge = edge.next;
   5337       }
   5338    }
   5339    b2Fixture.prototype.GetFilterData = function () {
   5340       return this.m_filter.Copy();
   5341    }
   5342    b2Fixture.prototype.GetBody = function () {
   5343       return this.m_body;
   5344    }
   5345    b2Fixture.prototype.GetNext = function () {
   5346       return this.m_next;
   5347    }
   5348    b2Fixture.prototype.GetUserData = function () {
   5349       return this.m_userData;
   5350    }
   5351    b2Fixture.prototype.SetUserData = function (data) {
   5352       this.m_userData = data;
   5353    }
   5354    b2Fixture.prototype.TestPoint = function (p) {
   5355       return this.m_shape.TestPoint(this.m_body.GetTransform(), p);
   5356    }
   5357    b2Fixture.prototype.RayCast = function (output, input) {
   5358       return this.m_shape.RayCast(output, input, this.m_body.GetTransform());
   5359    }
   5360    b2Fixture.prototype.GetMassData = function (massData) {
   5361       if (massData === undefined) massData = null;
   5362       if (massData == null) {
   5363          massData = new b2MassData();
   5364       }
   5365       this.m_shape.ComputeMass(massData, this.m_density);
   5366       return massData;
   5367    }
   5368    b2Fixture.prototype.SetDensity = function (density) {
   5369       if (density === undefined) density = 0;
   5370       this.m_density = density;
   5371    }
   5372    b2Fixture.prototype.GetDensity = function () {
   5373       return this.m_density;
   5374    }
   5375    b2Fixture.prototype.GetFriction = function () {
   5376       return this.m_friction;
   5377    }
   5378    b2Fixture.prototype.SetFriction = function (friction) {
   5379       if (friction === undefined) friction = 0;
   5380       this.m_friction = friction;
   5381    }
   5382    b2Fixture.prototype.GetRestitution = function () {
   5383       return this.m_restitution;
   5384    }
   5385    b2Fixture.prototype.SetRestitution = function (restitution) {
   5386       if (restitution === undefined) restitution = 0;
   5387       this.m_restitution = restitution;
   5388    }
   5389    b2Fixture.prototype.GetAABB = function () {
   5390       return this.m_aabb;
   5391    }
   5392    b2Fixture.prototype.b2Fixture = function () {
   5393       this.m_aabb = new b2AABB();
   5394       this.m_userData = null;
   5395       this.m_body = null;
   5396       this.m_next = null;
   5397       this.m_shape = null;
   5398       this.m_density = 0.0;
   5399       this.m_friction = 0.0;
   5400       this.m_restitution = 0.0;
   5401    }
   5402    b2Fixture.prototype.Create = function (body, xf, def) {
   5403       this.m_userData = def.userData;
   5404       this.m_friction = def.friction;
   5405       this.m_restitution = def.restitution;
   5406       this.m_body = body;
   5407       this.m_next = null;
   5408       this.m_filter = def.filter.Copy();
   5409       this.m_isSensor = def.isSensor;
   5410       this.m_shape = def.shape.Copy();
   5411       this.m_density = def.density;
   5412    }
   5413    b2Fixture.prototype.Destroy = function () {
   5414       this.m_shape = null;
   5415    }
   5416    b2Fixture.prototype.CreateProxy = function (broadPhase, xf) {
   5417       this.m_shape.ComputeAABB(this.m_aabb, xf);
   5418       this.m_proxy = broadPhase.CreateProxy(this.m_aabb, this);
   5419    }
   5420    b2Fixture.prototype.DestroyProxy = function (broadPhase) {
   5421       if (this.m_proxy == null) {
   5422          return;
   5423       }
   5424       broadPhase.DestroyProxy(this.m_proxy);
   5425       this.m_proxy = null;
   5426    }
   5427    b2Fixture.prototype.Synchronize = function (broadPhase, transform1, transform2) {
   5428       if (!this.m_proxy) return;
   5429       var aabb1 = new b2AABB();
   5430       var aabb2 = new b2AABB();
   5431       this.m_shape.ComputeAABB(aabb1, transform1);
   5432       this.m_shape.ComputeAABB(aabb2, transform2);
   5433       this.m_aabb.Combine(aabb1, aabb2);
   5434       var displacement = b2Math.SubtractVV(transform2.position, transform1.position);
   5435       broadPhase.MoveProxy(this.m_proxy, this.m_aabb, displacement);
   5436    }
   5437    b2FixtureDef.b2FixtureDef = function () {
   5438       this.filter = new b2FilterData();
   5439    };
   5440    b2FixtureDef.prototype.b2FixtureDef = function () {
   5441       this.shape = null;
   5442       this.userData = null;
   5443       this.friction = 0.2;
   5444       this.restitution = 0.0;
   5445       this.density = 0.0;
   5446       this.filter.categoryBits = 0x0001;
   5447       this.filter.maskBits = 0xFFFF;
   5448       this.filter.groupIndex = 0;
   5449       this.isSensor = false;
   5450    }
   5451    b2Island.b2Island = function () {};
   5452    b2Island.prototype.b2Island = function () {
   5453       this.m_bodies = new Vector();
   5454       this.m_contacts = new Vector();
   5455       this.m_joints = new Vector();
   5456    }
   5457    b2Island.prototype.Initialize = function (bodyCapacity, contactCapacity, jointCapacity, allocator, listener, contactSolver) {
   5458       if (bodyCapacity === undefined) bodyCapacity = 0;
   5459       if (contactCapacity === undefined) contactCapacity = 0;
   5460       if (jointCapacity === undefined) jointCapacity = 0;
   5461       var i = 0;
   5462       this.m_bodyCapacity = bodyCapacity;
   5463       this.m_contactCapacity = contactCapacity;
   5464       this.m_jointCapacity = jointCapacity;
   5465       this.m_bodyCount = 0;
   5466       this.m_contactCount = 0;
   5467       this.m_jointCount = 0;
   5468       this.m_allocator = allocator;
   5469       this.m_listener = listener;
   5470       this.m_contactSolver = contactSolver;
   5471       for (i = this.m_bodies.length;
   5472       i < bodyCapacity; i++)
   5473       this.m_bodies[i] = null;
   5474       for (i = this.m_contacts.length;
   5475       i < contactCapacity; i++)
   5476       this.m_contacts[i] = null;
   5477       for (i = this.m_joints.length;
   5478       i < jointCapacity; i++)
   5479       this.m_joints[i] = null;
   5480    }
   5481    b2Island.prototype.Clear = function () {
   5482       this.m_bodyCount = 0;
   5483       this.m_contactCount = 0;
   5484       this.m_jointCount = 0;
   5485    }
   5486    b2Island.prototype.Solve = function (step, gravity, allowSleep) {
   5487       var i = 0;
   5488       var j = 0;
   5489       var b;
   5490       var joint;
   5491       for (i = 0;
   5492       i < this.m_bodyCount; ++i) {
   5493          b = this.m_bodies[i];
   5494          if (b.GetType() != b2Body.b2_dynamicBody) continue;
   5495          b.m_linearVelocity.x += step.dt * (gravity.x + b.m_invMass * b.m_force.x);
   5496          b.m_linearVelocity.y += step.dt * (gravity.y + b.m_invMass * b.m_force.y);
   5497          b.m_angularVelocity += step.dt * b.m_invI * b.m_torque;
   5498          b.m_linearVelocity.Multiply(b2Math.Clamp(1.0 - step.dt * b.m_linearDamping, 0.0, 1.0));
   5499          b.m_angularVelocity *= b2Math.Clamp(1.0 - step.dt * b.m_angularDamping, 0.0, 1.0);
   5500       }
   5501       this.m_contactSolver.Initialize(step, this.m_contacts, this.m_contactCount, this.m_allocator);
   5502       var contactSolver = this.m_contactSolver;
   5503       contactSolver.InitVelocityConstraints(step);
   5504       for (i = 0;
   5505       i < this.m_jointCount; ++i) {
   5506          joint = this.m_joints[i];
   5507          joint.InitVelocityConstraints(step);
   5508       }
   5509       for (i = 0;
   5510       i < step.velocityIterations; ++i) {
   5511          for (j = 0;
   5512          j < this.m_jointCount; ++j) {
   5513             joint = this.m_joints[j];
   5514             joint.SolveVelocityConstraints(step);
   5515          }
   5516          contactSolver.SolveVelocityConstraints();
   5517       }
   5518       for (i = 0;
   5519       i < this.m_jointCount; ++i) {
   5520          joint = this.m_joints[i];
   5521          joint.FinalizeVelocityConstraints();
   5522       }
   5523       contactSolver.FinalizeVelocityConstraints();
   5524       for (i = 0;
   5525       i < this.m_bodyCount; ++i) {
   5526          b = this.m_bodies[i];
   5527          if (b.GetType() == b2Body.b2_staticBody) continue;
   5528          var translationX = step.dt * b.m_linearVelocity.x;
   5529          var translationY = step.dt * b.m_linearVelocity.y;
   5530          if ((translationX * translationX + translationY * translationY) > b2Settings.b2_maxTranslationSquared) {
   5531             b.m_linearVelocity.Normalize();
   5532             b.m_linearVelocity.x *= b2Settings.b2_maxTranslation * step.inv_dt;
   5533             b.m_linearVelocity.y *= b2Settings.b2_maxTranslation * step.inv_dt;
   5534          }
   5535          var rotation = step.dt * b.m_angularVelocity;
   5536          if (rotation * rotation > b2Settings.b2_maxRotationSquared) {
   5537             if (b.m_angularVelocity < 0.0) {
   5538                b.m_angularVelocity = (-b2Settings.b2_maxRotation * step.inv_dt);
   5539             }
   5540             else {
   5541                b.m_angularVelocity = b2Settings.b2_maxRotation * step.inv_dt;
   5542             }
   5543          }
   5544          b.m_sweep.c0.SetV(b.m_sweep.c);
   5545          b.m_sweep.a0 = b.m_sweep.a;
   5546          b.m_sweep.c.x += step.dt * b.m_linearVelocity.x;
   5547          b.m_sweep.c.y += step.dt * b.m_linearVelocity.y;
   5548          b.m_sweep.a += step.dt * b.m_angularVelocity;
   5549          b.SynchronizeTransform();
   5550       }
   5551       for (i = 0;
   5552       i < step.positionIterations; ++i) {
   5553          var contactsOkay = contactSolver.SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
   5554          var jointsOkay = true;
   5555          for (j = 0;
   5556          j < this.m_jointCount; ++j) {
   5557             joint = this.m_joints[j];
   5558             var jointOkay = joint.SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
   5559             jointsOkay = jointsOkay && jointOkay;
   5560          }
   5561          if (contactsOkay && jointsOkay) {
   5562             break;
   5563          }
   5564       }
   5565       this.Report(contactSolver.m_constraints);
   5566       if (allowSleep) {
   5567          var minSleepTime = Number.MAX_VALUE;
   5568          var linTolSqr = b2Settings.b2_linearSleepTolerance * b2Settings.b2_linearSleepTolerance;
   5569          var angTolSqr = b2Settings.b2_angularSleepTolerance * b2Settings.b2_angularSleepTolerance;
   5570          for (i = 0;
   5571          i < this.m_bodyCount; ++i) {
   5572             b = this.m_bodies[i];
   5573             if (b.GetType() == b2Body.b2_staticBody) {
   5574                continue;
   5575             }
   5576             if ((b.m_flags & b2Body.e_allowSleepFlag) == 0) {
   5577                b.m_sleepTime = 0.0;
   5578                minSleepTime = 0.0;
   5579             }
   5580             if ((b.m_flags & b2Body.e_allowSleepFlag) == 0 || b.m_angularVelocity * b.m_angularVelocity > angTolSqr || b2Math.Dot(b.m_linearVelocity, b.m_linearVelocity) > linTolSqr) {
   5581                b.m_sleepTime = 0.0;
   5582                minSleepTime = 0.0;
   5583             }
   5584             else {
   5585                b.m_sleepTime += step.dt;
   5586                minSleepTime = b2Math.Min(minSleepTime, b.m_sleepTime);
   5587             }
   5588          }
   5589          if (minSleepTime >= b2Settings.b2_timeToSleep) {
   5590             for (i = 0;
   5591             i < this.m_bodyCount; ++i) {
   5592                b = this.m_bodies[i];
   5593                b.SetAwake(false);
   5594             }
   5595          }
   5596       }
   5597    }
   5598    b2Island.prototype.SolveTOI = function (subStep) {
   5599       var i = 0;
   5600       var j = 0;
   5601       this.m_contactSolver.Initialize(subStep, this.m_contacts, this.m_contactCount, this.m_allocator);
   5602       var contactSolver = this.m_contactSolver;
   5603       for (i = 0;
   5604       i < this.m_jointCount; ++i) {
   5605          this.m_joints[i].InitVelocityConstraints(subStep);
   5606       }
   5607       for (i = 0;
   5608       i < subStep.velocityIterations; ++i) {
   5609          contactSolver.SolveVelocityConstraints();
   5610          for (j = 0;
   5611          j < this.m_jointCount; ++j) {
   5612             this.m_joints[j].SolveVelocityConstraints(subStep);
   5613          }
   5614       }
   5615       for (i = 0;
   5616       i < this.m_bodyCount; ++i) {
   5617          var b = this.m_bodies[i];
   5618          if (b.GetType() == b2Body.b2_staticBody) continue;
   5619          var translationX = subStep.dt * b.m_linearVelocity.x;
   5620          var translationY = subStep.dt * b.m_linearVelocity.y;
   5621          if ((translationX * translationX + translationY * translationY) > b2Settings.b2_maxTranslationSquared) {
   5622             b.m_linearVelocity.Normalize();
   5623             b.m_linearVelocity.x *= b2Settings.b2_maxTranslation * subStep.inv_dt;
   5624             b.m_linearVelocity.y *= b2Settings.b2_maxTranslation * subStep.inv_dt;
   5625          }
   5626          var rotation = subStep.dt * b.m_angularVelocity;
   5627          if (rotation * rotation > b2Settings.b2_maxRotationSquared) {
   5628             if (b.m_angularVelocity < 0.0) {
   5629                b.m_angularVelocity = (-b2Settings.b2_maxRotation * subStep.inv_dt);
   5630             }
   5631             else {
   5632                b.m_angularVelocity = b2Settings.b2_maxRotation * subStep.inv_dt;
   5633             }
   5634          }
   5635          b.m_sweep.c0.SetV(b.m_sweep.c);
   5636          b.m_sweep.a0 = b.m_sweep.a;
   5637          b.m_sweep.c.x += subStep.dt * b.m_linearVelocity.x;
   5638          b.m_sweep.c.y += subStep.dt * b.m_linearVelocity.y;
   5639          b.m_sweep.a += subStep.dt * b.m_angularVelocity;
   5640          b.SynchronizeTransform();
   5641       }
   5642       var k_toiBaumgarte = 0.75;
   5643       for (i = 0;
   5644       i < subStep.positionIterations; ++i) {
   5645          var contactsOkay = contactSolver.SolvePositionConstraints(k_toiBaumgarte);
   5646          var jointsOkay = true;
   5647          for (j = 0;
   5648          j < this.m_jointCount; ++j) {
   5649             var jointOkay = this.m_joints[j].SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
   5650             jointsOkay = jointsOkay && jointOkay;
   5651          }
   5652          if (contactsOkay && jointsOkay) {
   5653             break;
   5654          }
   5655       }
   5656       this.Report(contactSolver.m_constraints);
   5657    }
   5658    b2Island.prototype.Report = function (constraints) {
   5659       if (this.m_listener == null) {
   5660          return;
   5661       }
   5662       for (var i = 0; i < this.m_contactCount; ++i) {
   5663          var c = this.m_contacts[i];
   5664          var cc = constraints[i];
   5665          for (var j = 0; j < cc.pointCount; ++j) {
   5666             b2Island.s_impulse.normalImpulses[j] = cc.points[j].normalImpulse;
   5667             b2Island.s_impulse.tangentImpulses[j] = cc.points[j].tangentImpulse;
   5668          }
   5669          this.m_listener.PostSolve(c, b2Island.s_impulse);
   5670       }
   5671    }
   5672    b2Island.prototype.AddBody = function (body) {
   5673       body.m_islandIndex = this.m_bodyCount;
   5674       this.m_bodies[this.m_bodyCount++] = body;
   5675    }
   5676    b2Island.prototype.AddContact = function (contact) {
   5677       this.m_contacts[this.m_contactCount++] = contact;
   5678    }
   5679    b2Island.prototype.AddJoint = function (joint) {
   5680       this.m_joints[this.m_jointCount++] = joint;
   5681    }
   5682    Box2D.postDefs.push(function () {
   5683       Box2D.Dynamics.b2Island.s_impulse = new b2ContactImpulse();
   5684    });
   5685    b2TimeStep.b2TimeStep = function () {};
   5686    b2TimeStep.prototype.Set = function (step) {
   5687       this.dt = step.dt;
   5688       this.inv_dt = step.inv_dt;
   5689       this.positionIterations = step.positionIterations;
   5690       this.velocityIterations = step.velocityIterations;
   5691       this.warmStarting = step.warmStarting;
   5692    }
   5693    b2World.b2World = function () {
   5694       this.s_stack = new Vector();
   5695       this.m_contactManager = new b2ContactManager();
   5696       this.m_contactSolver = new b2ContactSolver();
   5697       this.m_island = new b2Island();
   5698    };
   5699    b2World.prototype.b2World = function (gravity, doSleep) {
   5700       this.m_destructionListener = null;
   5701       this.m_debugDraw = null;
   5702       this.m_bodyList = null;
   5703       this.m_contactList = null;
   5704       this.m_jointList = null;
   5705       this.m_controllerList = null;
   5706       this.m_bodyCount = 0;
   5707       this.m_contactCount = 0;
   5708       this.m_jointCount = 0;
   5709       this.m_controllerCount = 0;
   5710       b2World.m_warmStarting = true;
   5711       b2World.m_continuousPhysics = true;
   5712       this.m_allowSleep = doSleep;
   5713       this.m_gravity = gravity;
   5714       this.m_inv_dt0 = 0.0;
   5715       this.m_contactManager.m_world = this;
   5716       var bd = new b2BodyDef();
   5717       this.m_groundBody = this.CreateBody(bd);
   5718    }
   5719    b2World.prototype.SetDestructionListener = function (listener) {
   5720       this.m_destructionListener = listener;
   5721    }
   5722    b2World.prototype.SetContactFilter = function (filter) {
   5723       this.m_contactManager.m_contactFilter = filter;
   5724    }
   5725    b2World.prototype.SetContactListener = function (listener) {
   5726       this.m_contactManager.m_contactListener = listener;
   5727    }
   5728    b2World.prototype.SetDebugDraw = function (debugDraw) {
   5729       this.m_debugDraw = debugDraw;
   5730    }
   5731    b2World.prototype.SetBroadPhase = function (broadPhase) {
   5732       var oldBroadPhase = this.m_contactManager.m_broadPhase;
   5733       this.m_contactManager.m_broadPhase = broadPhase;
   5734       for (var b = this.m_bodyList; b; b = b.m_next) {
   5735          for (var f = b.m_fixtureList; f; f = f.m_next) {
   5736             f.m_proxy = broadPhase.CreateProxy(oldBroadPhase.GetFatAABB(f.m_proxy), f);
   5737          }
   5738       }
   5739    }
   5740    b2World.prototype.Validate = function () {
   5741       this.m_contactManager.m_broadPhase.Validate();
   5742    }
   5743    b2World.prototype.GetProxyCount = function () {
   5744       return this.m_contactManager.m_broadPhase.GetProxyCount();
   5745    }
   5746    b2World.prototype.CreateBody = function (def) {
   5747       if (this.IsLocked() == true) {
   5748          return null;
   5749       }
   5750       var b = new b2Body(def, this);
   5751       b.m_prev = null;
   5752       b.m_next = this.m_bodyList;
   5753       if (this.m_bodyList) {
   5754          this.m_bodyList.m_prev = b;
   5755       }
   5756       this.m_bodyList = b;
   5757       ++this.m_bodyCount;
   5758       return b;
   5759    }
   5760    b2World.prototype.DestroyBody = function (b) {
   5761       if (this.IsLocked() == true) {
   5762          return;
   5763       }
   5764       var jn = b.m_jointList;
   5765       while (jn) {
   5766          var jn0 = jn;
   5767          jn = jn.next;
   5768          if (this.m_destructionListener) {
   5769             this.m_destructionListener.SayGoodbyeJoint(jn0.joint);
   5770          }
   5771          this.DestroyJoint(jn0.joint);
   5772       }
   5773       var coe = b.m_controllerList;
   5774       while (coe) {
   5775          var coe0 = coe;
   5776          coe = coe.nextController;
   5777          coe0.controller.RemoveBody(b);
   5778       }
   5779       var ce = b.m_contactList;
   5780       while (ce) {
   5781          var ce0 = ce;
   5782          ce = ce.next;
   5783          this.m_contactManager.Destroy(ce0.contact);
   5784       }
   5785       b.m_contactList = null;
   5786       var f = b.m_fixtureList;
   5787       while (f) {
   5788          var f0 = f;
   5789          f = f.m_next;
   5790          if (this.m_destructionListener) {
   5791             this.m_destructionListener.SayGoodbyeFixture(f0);
   5792          }
   5793          f0.DestroyProxy(this.m_contactManager.m_broadPhase);
   5794          f0.Destroy();
   5795       }
   5796       b.m_fixtureList = null;
   5797       b.m_fixtureCount = 0;
   5798       if (b.m_prev) {
   5799          b.m_prev.m_next = b.m_next;
   5800       }
   5801       if (b.m_next) {
   5802          b.m_next.m_prev = b.m_prev;
   5803       }
   5804       if (b == this.m_bodyList) {
   5805          this.m_bodyList = b.m_next;
   5806       }--this.m_bodyCount;
   5807    }
   5808    b2World.prototype.CreateJoint = function (def) {
   5809       var j = b2Joint.Create(def, null);
   5810       j.m_prev = null;
   5811       j.m_next = this.m_jointList;
   5812       if (this.m_jointList) {
   5813          this.m_jointList.m_prev = j;
   5814       }
   5815       this.m_jointList = j;
   5816       ++this.m_jointCount;
   5817       j.m_edgeA.joint = j;
   5818       j.m_edgeA.other = j.m_bodyB;
   5819       j.m_edgeA.prev = null;
   5820       j.m_edgeA.next = j.m_bodyA.m_jointList;
   5821       if (j.m_bodyA.m_jointList) j.m_bodyA.m_jointList.prev = j.m_edgeA;
   5822       j.m_bodyA.m_jointList = j.m_edgeA;
   5823       j.m_edgeB.joint = j;
   5824       j.m_edgeB.other = j.m_bodyA;
   5825       j.m_edgeB.prev = null;
   5826       j.m_edgeB.next = j.m_bodyB.m_jointList;
   5827       if (j.m_bodyB.m_jointList) j.m_bodyB.m_jointList.prev = j.m_edgeB;
   5828       j.m_bodyB.m_jointList = j.m_edgeB;
   5829       var bodyA = def.bodyA;
   5830       var bodyB = def.bodyB;
   5831       if (def.collideConnected == false) {
   5832          var edge = bodyB.GetContactList();
   5833          while (edge) {
   5834             if (edge.other == bodyA) {
   5835                edge.contact.FlagForFiltering();
   5836             }
   5837             edge = edge.next;
   5838          }
   5839       }
   5840       return j;
   5841    }
   5842    b2World.prototype.DestroyJoint = function (j) {
   5843       var collideConnected = j.m_collideConnected;
   5844       if (j.m_prev) {
   5845          j.m_prev.m_next = j.m_next;
   5846       }
   5847       if (j.m_next) {
   5848          j.m_next.m_prev = j.m_prev;
   5849       }
   5850       if (j == this.m_jointList) {
   5851          this.m_jointList = j.m_next;
   5852       }
   5853       var bodyA = j.m_bodyA;
   5854       var bodyB = j.m_bodyB;
   5855       bodyA.SetAwake(true);
   5856       bodyB.SetAwake(true);
   5857       if (j.m_edgeA.prev) {
   5858          j.m_edgeA.prev.next = j.m_edgeA.next;
   5859       }
   5860       if (j.m_edgeA.next) {
   5861          j.m_edgeA.next.prev = j.m_edgeA.prev;
   5862       }
   5863       if (j.m_edgeA == bodyA.m_jointList) {
   5864          bodyA.m_jointList = j.m_edgeA.next;
   5865       }
   5866       j.m_edgeA.prev = null;
   5867       j.m_edgeA.next = null;
   5868       if (j.m_edgeB.prev) {
   5869          j.m_edgeB.prev.next = j.m_edgeB.next;
   5870       }
   5871       if (j.m_edgeB.next) {
   5872          j.m_edgeB.next.prev = j.m_edgeB.prev;
   5873       }
   5874       if (j.m_edgeB == bodyB.m_jointList) {
   5875          bodyB.m_jointList = j.m_edgeB.next;
   5876       }
   5877       j.m_edgeB.prev = null;
   5878       j.m_edgeB.next = null;
   5879       b2Joint.Destroy(j, null);
   5880       --this.m_jointCount;
   5881       if (collideConnected == false) {
   5882          var edge = bodyB.GetContactList();
   5883          while (edge) {
   5884             if (edge.other == bodyA) {
   5885                edge.contact.FlagForFiltering();
   5886             }
   5887             edge = edge.next;
   5888          }
   5889       }
   5890    }
   5891    b2World.prototype.AddController = function (c) {
   5892       c.m_next = this.m_controllerList;
   5893       c.m_prev = null;
   5894       this.m_controllerList = c;
   5895       c.m_world = this;
   5896       this.m_controllerCount++;
   5897       return c;
   5898    }
   5899    b2World.prototype.RemoveController = function (c) {
   5900       if (c.m_prev) c.m_prev.m_next = c.m_next;
   5901       if (c.m_next) c.m_next.m_prev = c.m_prev;
   5902       if (this.m_controllerList == c) this.m_controllerList = c.m_next;
   5903       this.m_controllerCount--;
   5904    }
   5905    b2World.prototype.CreateController = function (controller) {
   5906       if (controller.m_world != this) throw new Error("Controller can only be a member of one world");
   5907       controller.m_next = this.m_controllerList;
   5908       controller.m_prev = null;
   5909       if (this.m_controllerList) this.m_controllerList.m_prev = controller;
   5910       this.m_controllerList = controller;
   5911       ++this.m_controllerCount;
   5912       controller.m_world = this;
   5913       return controller;
   5914    }
   5915    b2World.prototype.DestroyController = function (controller) {
   5916       controller.Clear();
   5917       if (controller.m_next) controller.m_next.m_prev = controller.m_prev;
   5918       if (controller.m_prev) controller.m_prev.m_next = controller.m_next;
   5919       if (controller == this.m_controllerList) this.m_controllerList = controller.m_next;
   5920       --this.m_controllerCount;
   5921    }
   5922    b2World.prototype.SetWarmStarting = function (flag) {
   5923       b2World.m_warmStarting = flag;
   5924    }
   5925    b2World.prototype.SetContinuousPhysics = function (flag) {
   5926       b2World.m_continuousPhysics = flag;
   5927    }
   5928    b2World.prototype.GetBodyCount = function () {
   5929       return this.m_bodyCount;
   5930    }
   5931    b2World.prototype.GetJointCount = function () {
   5932       return this.m_jointCount;
   5933    }
   5934    b2World.prototype.GetContactCount = function () {
   5935       return this.m_contactCount;
   5936    }
   5937    b2World.prototype.SetGravity = function (gravity) {
   5938       this.m_gravity = gravity;
   5939    }
   5940    b2World.prototype.GetGravity = function () {
   5941       return this.m_gravity;
   5942    }
   5943    b2World.prototype.GetGroundBody = function () {
   5944       return this.m_groundBody;
   5945    }
   5946    b2World.prototype.Step = function (dt, velocityIterations, positionIterations) {
   5947       if (dt === undefined) dt = 0;
   5948       if (velocityIterations === undefined) velocityIterations = 0;
   5949       if (positionIterations === undefined) positionIterations = 0;
   5950       if (this.m_flags & b2World.e_newFixture) {
   5951          this.m_contactManager.FindNewContacts();
   5952          this.m_flags &= ~b2World.e_newFixture;
   5953       }
   5954       this.m_flags |= b2World.e_locked;
   5955       var step = b2World.s_timestep2;
   5956       step.dt = dt;
   5957       step.velocityIterations = velocityIterations;
   5958       step.positionIterations = positionIterations;
   5959       if (dt > 0.0) {
   5960          step.inv_dt = 1.0 / dt;
   5961       }
   5962       else {
   5963          step.inv_dt = 0.0;
   5964       }
   5965       step.dtRatio = this.m_inv_dt0 * dt;
   5966       step.warmStarting = b2World.m_warmStarting;
   5967       this.m_contactManager.Collide();
   5968       if (step.dt > 0.0) {
   5969          this.Solve(step);
   5970       }
   5971       if (b2World.m_continuousPhysics && step.dt > 0.0) {
   5972          this.SolveTOI(step);
   5973       }
   5974       if (step.dt > 0.0) {
   5975          this.m_inv_dt0 = step.inv_dt;
   5976       }
   5977       this.m_flags &= ~b2World.e_locked;
   5978    }
   5979    b2World.prototype.ClearForces = function () {
   5980       for (var body = this.m_bodyList; body; body = body.m_next) {
   5981          body.m_force.SetZero();
   5982          body.m_torque = 0.0;
   5983       }
   5984    }
   5985    b2World.prototype.DrawDebugData = function () {
   5986       if (this.m_debugDraw == null) {
   5987          return;
   5988       }
   5989       this.m_debugDraw.m_sprite.graphics.clear();
   5990       var flags = this.m_debugDraw.GetFlags();
   5991       var i = 0;
   5992       var b;
   5993       var f;
   5994       var s;
   5995       var j;
   5996       var bp;
   5997       var invQ = new b2Vec2;
   5998       var x1 = new b2Vec2;
   5999       var x2 = new b2Vec2;
   6000       var xf;
   6001       var b1 = new b2AABB();
   6002       var b2 = new b2AABB();
   6003       var vs = [new b2Vec2(), new b2Vec2(), new b2Vec2(), new b2Vec2()];
   6004       var color = new b2Color(0, 0, 0);
   6005       if (flags & b2DebugDraw.e_shapeBit) {
   6006          for (b = this.m_bodyList;
   6007          b; b = b.m_next) {
   6008             xf = b.m_xf;
   6009             for (f = b.GetFixtureList();
   6010             f; f = f.m_next) {
   6011                s = f.GetShape();
   6012                if (b.IsActive() == false) {
   6013                   color.Set(0.5, 0.5, 0.3);
   6014                   this.DrawShape(s, xf, color);
   6015                }
   6016                else if (b.GetType() == b2Body.b2_staticBody) {
   6017                   color.Set(0.5, 0.9, 0.5);
   6018                   this.DrawShape(s, xf, color);
   6019                }
   6020                else if (b.GetType() == b2Body.b2_kinematicBody) {
   6021                   color.Set(0.5, 0.5, 0.9);
   6022                   this.DrawShape(s, xf, color);
   6023                }
   6024                else if (b.IsAwake() == false) {
   6025                   color.Set(0.6, 0.6, 0.6);
   6026                   this.DrawShape(s, xf, color);
   6027                }
   6028                else {
   6029                   color.Set(0.9, 0.7, 0.7);
   6030                   this.DrawShape(s, xf, color);
   6031                }
   6032             }
   6033          }
   6034       }
   6035       if (flags & b2DebugDraw.e_jointBit) {
   6036          for (j = this.m_jointList;
   6037          j; j = j.m_next) {
   6038             this.DrawJoint(j);
   6039          }
   6040       }
   6041       if (flags & b2DebugDraw.e_controllerBit) {
   6042          for (var c = this.m_controllerList; c; c = c.m_next) {
   6043             c.Draw(this.m_debugDraw);
   6044          }
   6045       }
   6046       if (flags & b2DebugDraw.e_pairBit) {
   6047          color.Set(0.3, 0.9, 0.9);
   6048          for (var contact = this.m_contactManager.m_contactList; contact; contact = contact.GetNext()) {
   6049             var fixtureA = contact.GetFixtureA();
   6050             var fixtureB = contact.GetFixtureB();
   6051             var cA = fixtureA.GetAABB().GetCenter();
   6052             var cB = fixtureB.GetAABB().GetCenter();
   6053             this.m_debugDraw.DrawSegment(cA, cB, color);
   6054          }
   6055       }
   6056       if (flags & b2DebugDraw.e_aabbBit) {
   6057          bp = this.m_contactManager.m_broadPhase;
   6058          vs = [new b2Vec2(), new b2Vec2(), new b2Vec2(), new b2Vec2()];
   6059          for (b = this.m_bodyList;
   6060          b; b = b.GetNext()) {
   6061             if (b.IsActive() == false) {
   6062                continue;
   6063             }
   6064             for (f = b.GetFixtureList();
   6065             f; f = f.GetNext()) {
   6066                var aabb = bp.GetFatAABB(f.m_proxy);
   6067                vs[0].Set(aabb.lowerBound.x, aabb.lowerBound.y);
   6068                vs[1].Set(aabb.upperBound.x, aabb.lowerBound.y);
   6069                vs[2].Set(aabb.upperBound.x, aabb.upperBound.y);
   6070                vs[3].Set(aabb.lowerBound.x, aabb.upperBound.y);
   6071                this.m_debugDraw.DrawPolygon(vs, 4, color);
   6072             }
   6073          }
   6074       }
   6075       if (flags & b2DebugDraw.e_centerOfMassBit) {
   6076          for (b = this.m_bodyList;
   6077          b; b = b.m_next) {
   6078             xf = b2World.s_xf;
   6079             xf.R = b.m_xf.R;
   6080             xf.position = b.GetWorldCenter();
   6081             this.m_debugDraw.DrawTransform(xf);
   6082          }
   6083       }
   6084    }
   6085    b2World.prototype.QueryAABB = function (callback, aabb) {
   6086       var __this = this;
   6087       var broadPhase = __this.m_contactManager.m_broadPhase;
   6088 
   6089       function WorldQueryWrapper(proxy) {
   6090          return callback(broadPhase.GetUserData(proxy));
   6091       };
   6092       broadPhase.Query(WorldQueryWrapper, aabb);
   6093    }
   6094    b2World.prototype.QueryShape = function (callback, shape, transform) {
   6095       var __this = this;
   6096       if (transform === undefined) transform = null;
   6097       if (transform == null) {
   6098          transform = new b2Transform();
   6099          transform.SetIdentity();
   6100       }
   6101       var broadPhase = __this.m_contactManager.m_broadPhase;
   6102 
   6103       function WorldQueryWrapper(proxy) {
   6104          var fixture = (broadPhase.GetUserData(proxy) instanceof b2Fixture ? broadPhase.GetUserData(proxy) : null);
   6105          if (b2Shape.TestOverlap(shape, transform, fixture.GetShape(), fixture.GetBody().GetTransform())) return callback(fixture);
   6106          return true;
   6107       };
   6108       var aabb = new b2AABB();
   6109       shape.ComputeAABB(aabb, transform);
   6110       broadPhase.Query(WorldQueryWrapper, aabb);
   6111    }
   6112    b2World.prototype.QueryPoint = function (callback, p) {
   6113       var __this = this;
   6114       var broadPhase = __this.m_contactManager.m_broadPhase;
   6115 
   6116       function WorldQueryWrapper(proxy) {
   6117          var fixture = (broadPhase.GetUserData(proxy) instanceof b2Fixture ? broadPhase.GetUserData(proxy) : null);
   6118          if (fixture.TestPoint(p)) return callback(fixture);
   6119          return true;
   6120       };
   6121       var aabb = new b2AABB();
   6122       aabb.lowerBound.Set(p.x - b2Settings.b2_linearSlop, p.y - b2Settings.b2_linearSlop);
   6123       aabb.upperBound.Set(p.x + b2Settings.b2_linearSlop, p.y + b2Settings.b2_linearSlop);
   6124       broadPhase.Query(WorldQueryWrapper, aabb);
   6125    }
   6126    b2World.prototype.RayCast = function (callback, point1, point2) {
   6127       var __this = this;
   6128       var broadPhase = __this.m_contactManager.m_broadPhase;
   6129       var output = new b2RayCastOutput;
   6130 
   6131       function RayCastWrapper(input, proxy) {
   6132          var userData = broadPhase.GetUserData(proxy);
   6133          var fixture = (userData instanceof b2Fixture ? userData : null);
   6134          var hit = fixture.RayCast(output, input);
   6135          if (hit) {
   6136             var fraction = output.fraction;
   6137             var point = new b2Vec2((1.0 - fraction) * point1.x + fraction * point2.x, (1.0 - fraction) * point1.y + fraction * point2.y);
   6138             return callback(fixture, point, output.normal, fraction);
   6139          }
   6140          return input.maxFraction;
   6141       };
   6142       var input = new b2RayCastInput(point1, point2);
   6143       broadPhase.RayCast(RayCastWrapper, input);
   6144    }
   6145    b2World.prototype.RayCastOne = function (point1, point2) {
   6146       var __this = this;
   6147       var result;
   6148 
   6149       function RayCastOneWrapper(fixture, point, normal, fraction) {
   6150          if (fraction === undefined) fraction = 0;
   6151          result = fixture;
   6152          return fraction;
   6153       };
   6154       __this.RayCast(RayCastOneWrapper, point1, point2);
   6155       return result;
   6156    }
   6157    b2World.prototype.RayCastAll = function (point1, point2) {
   6158       var __this = this;
   6159       var result = new Vector();
   6160 
   6161       function RayCastAllWrapper(fixture, point, normal, fraction) {
   6162          if (fraction === undefined) fraction = 0;
   6163          result[result.length] = fixture;
   6164          return 1;
   6165       };
   6166       __this.RayCast(RayCastAllWrapper, point1, point2);
   6167       return result;
   6168    }
   6169    b2World.prototype.GetBodyList = function () {
   6170       return this.m_bodyList;
   6171    }
   6172    b2World.prototype.GetJointList = function () {
   6173       return this.m_jointList;
   6174    }
   6175    b2World.prototype.GetContactList = function () {
   6176       return this.m_contactList;
   6177    }
   6178    b2World.prototype.IsLocked = function () {
   6179       return (this.m_flags & b2World.e_locked) > 0;
   6180    }
   6181    b2World.prototype.Solve = function (step) {
   6182       var b;
   6183       for (var controller = this.m_controllerList; controller; controller = controller.m_next) {
   6184          controller.Step(step);
   6185       }
   6186       var island = this.m_island;
   6187       island.Initialize(this.m_bodyCount, this.m_contactCount, this.m_jointCount, null, this.m_contactManager.m_contactListener, this.m_contactSolver);
   6188       for (b = this.m_bodyList;
   6189       b; b = b.m_next) {
   6190          b.m_flags &= ~b2Body.e_islandFlag;
   6191       }
   6192       for (var c = this.m_contactList; c; c = c.m_next) {
   6193          c.m_flags &= ~b2Contact.e_islandFlag;
   6194       }
   6195       for (var j = this.m_jointList; j; j = j.m_next) {
   6196          j.m_islandFlag = false;
   6197       }
   6198       var stackSize = parseInt(this.m_bodyCount);
   6199       var stack = this.s_stack;
   6200       for (var seed = this.m_bodyList; seed; seed = seed.m_next) {
   6201          if (seed.m_flags & b2Body.e_islandFlag) {
   6202             continue;
   6203          }
   6204          if (seed.IsAwake() == false || seed.IsActive() == false) {
   6205             continue;
   6206          }
   6207          if (seed.GetType() == b2Body.b2_staticBody) {
   6208             continue;
   6209          }
   6210          island.Clear();
   6211          var stackCount = 0;
   6212          stack[stackCount++] = seed;
   6213          seed.m_flags |= b2Body.e_islandFlag;
   6214          while (stackCount > 0) {
   6215             b = stack[--stackCount];
   6216             island.AddBody(b);
   6217             if (b.IsAwake() == false) {
   6218                b.SetAwake(true);
   6219             }
   6220             if (b.GetType() == b2Body.b2_staticBody) {
   6221                continue;
   6222             }
   6223             var other;
   6224             for (var ce = b.m_contactList; ce; ce = ce.next) {
   6225                if (ce.contact.m_flags & b2Contact.e_islandFlag) {
   6226                   continue;
   6227                }
   6228                if (ce.contact.IsSensor() == true || ce.contact.IsEnabled() == false || ce.contact.IsTouching() == false) {
   6229                   continue;
   6230                }
   6231                island.AddContact(ce.contact);
   6232                ce.contact.m_flags |= b2Contact.e_islandFlag;
   6233                other = ce.other;
   6234                if (other.m_flags & b2Body.e_islandFlag) {
   6235                   continue;
   6236                }
   6237                stack[stackCount++] = other;
   6238                other.m_flags |= b2Body.e_islandFlag;
   6239             }
   6240             for (var jn = b.m_jointList; jn; jn = jn.next) {
   6241                if (jn.joint.m_islandFlag == true) {
   6242                   continue;
   6243                }
   6244                other = jn.other;
   6245                if (other.IsActive() == false) {
   6246                   continue;
   6247                }
   6248                island.AddJoint(jn.joint);
   6249                jn.joint.m_islandFlag = true;
   6250                if (other.m_flags & b2Body.e_islandFlag) {
   6251                   continue;
   6252                }
   6253                stack[stackCount++] = other;
   6254                other.m_flags |= b2Body.e_islandFlag;
   6255             }
   6256          }
   6257          island.Solve(step, this.m_gravity, this.m_allowSleep);
   6258          for (var i = 0; i < island.m_bodyCount; ++i) {
   6259             b = island.m_bodies[i];
   6260             if (b.GetType() == b2Body.b2_staticBody) {
   6261                b.m_flags &= ~b2Body.e_islandFlag;
   6262             }
   6263          }
   6264       }
   6265       for (i = 0;
   6266       i < stack.length; ++i) {
   6267          if (!stack[i]) break;
   6268          stack[i] = null;
   6269       }
   6270       for (b = this.m_bodyList;
   6271       b; b = b.m_next) {
   6272          if (b.IsAwake() == false || b.IsActive() == false) {
   6273             continue;
   6274          }
   6275          if (b.GetType() == b2Body.b2_staticBody) {
   6276             continue;
   6277          }
   6278          b.SynchronizeFixtures();
   6279       }
   6280       this.m_contactManager.FindNewContacts();
   6281    }
   6282    b2World.prototype.SolveTOI = function (step) {
   6283       var b;
   6284       var fA;
   6285       var fB;
   6286       var bA;
   6287       var bB;
   6288       var cEdge;
   6289       var j;
   6290       var island = this.m_island;
   6291       island.Initialize(this.m_bodyCount, b2Settings.b2_maxTOIContactsPerIsland, b2Settings.b2_maxTOIJointsPerIsland, null, this.m_contactManager.m_contactListener, this.m_contactSolver);
   6292       var queue = b2World.s_queue;
   6293       for (b = this.m_bodyList;
   6294       b; b = b.m_next) {
   6295          b.m_flags &= ~b2Body.e_islandFlag;
   6296          b.m_sweep.t0 = 0.0;
   6297       }
   6298       var c;
   6299       for (c = this.m_contactList;
   6300       c; c = c.m_next) {
   6301          c.m_flags &= ~ (b2Contact.e_toiFlag | b2Contact.e_islandFlag);
   6302       }
   6303       for (j = this.m_jointList;
   6304       j; j = j.m_next) {
   6305          j.m_islandFlag = false;
   6306       }
   6307       for (;;) {
   6308          var minContact = null;
   6309          var minTOI = 1.0;
   6310          for (c = this.m_contactList;
   6311          c; c = c.m_next) {
   6312             if (c.IsSensor() == true || c.IsEnabled() == false || c.IsContinuous() == false) {
   6313                continue;
   6314             }
   6315             var toi = 1.0;
   6316             if (c.m_flags & b2Contact.e_toiFlag) {
   6317                toi = c.m_toi;
   6318             }
   6319             else {
   6320                fA = c.m_fixtureA;
   6321                fB = c.m_fixtureB;
   6322                bA = fA.m_body;
   6323                bB = fB.m_body;
   6324                if ((bA.GetType() != b2Body.b2_dynamicBody || bA.IsAwake() == false) && (bB.GetType() != b2Body.b2_dynamicBody || bB.IsAwake() == false)) {
   6325                   continue;
   6326                }
   6327                var t0 = bA.m_sweep.t0;
   6328                if (bA.m_sweep.t0 < bB.m_sweep.t0) {
   6329                   t0 = bB.m_sweep.t0;
   6330                   bA.m_sweep.Advance(t0);
   6331                }
   6332                else if (bB.m_sweep.t0 < bA.m_sweep.t0) {
   6333                   t0 = bA.m_sweep.t0;
   6334                   bB.m_sweep.Advance(t0);
   6335                }
   6336                toi = c.ComputeTOI(bA.m_sweep, bB.m_sweep);
   6337                b2Settings.b2Assert(0.0 <= toi && toi <= 1.0);
   6338                if (toi > 0.0 && toi < 1.0) {
   6339                   toi = (1.0 - toi) * t0 + toi;
   6340                   if (toi > 1) toi = 1;
   6341                }
   6342                c.m_toi = toi;
   6343                c.m_flags |= b2Contact.e_toiFlag;
   6344             }
   6345             if (Number.MIN_VALUE < toi && toi < minTOI) {
   6346                minContact = c;
   6347                minTOI = toi;
   6348             }
   6349          }
   6350          if (minContact == null || 1.0 - 100.0 * Number.MIN_VALUE < minTOI) {
   6351             break;
   6352          }
   6353          fA = minContact.m_fixtureA;
   6354          fB = minContact.m_fixtureB;
   6355          bA = fA.m_body;
   6356          bB = fB.m_body;
   6357          b2World.s_backupA.Set(bA.m_sweep);
   6358          b2World.s_backupB.Set(bB.m_sweep);
   6359          bA.Advance(minTOI);
   6360          bB.Advance(minTOI);
   6361          minContact.Update(this.m_contactManager.m_contactListener);
   6362          minContact.m_flags &= ~b2Contact.e_toiFlag;
   6363          if (minContact.IsSensor() == true || minContact.IsEnabled() == false) {
   6364             bA.m_sweep.Set(b2World.s_backupA);
   6365             bB.m_sweep.Set(b2World.s_backupB);
   6366             bA.SynchronizeTransform();
   6367             bB.SynchronizeTransform();
   6368             continue;
   6369          }
   6370          if (minContact.IsTouching() == false) {
   6371             continue;
   6372          }
   6373          var seed = bA;
   6374          if (seed.GetType() != b2Body.b2_dynamicBody) {
   6375             seed = bB;
   6376          }
   6377          island.Clear();
   6378          var queueStart = 0;
   6379          var queueSize = 0;
   6380          queue[queueStart + queueSize++] = seed;
   6381          seed.m_flags |= b2Body.e_islandFlag;
   6382          while (queueSize > 0) {
   6383             b = queue[queueStart++];
   6384             --queueSize;
   6385             island.AddBody(b);
   6386             if (b.IsAwake() == false) {
   6387                b.SetAwake(true);
   6388             }
   6389             if (b.GetType() != b2Body.b2_dynamicBody) {
   6390                continue;
   6391             }
   6392             for (cEdge = b.m_contactList;
   6393             cEdge; cEdge = cEdge.next) {
   6394                if (island.m_contactCount == island.m_contactCapacity) {
   6395                   break;
   6396                }
   6397                if (cEdge.contact.m_flags & b2Contact.e_islandFlag) {
   6398                   continue;
   6399                }
   6400                if (cEdge.contact.IsSensor() == true || cEdge.contact.IsEnabled() == false || cEdge.contact.IsTouching() == false) {
   6401                   continue;
   6402                }
   6403                island.AddContact(cEdge.contact);
   6404                cEdge.contact.m_flags |= b2Contact.e_islandFlag;
   6405                var other = cEdge.other;
   6406                if (other.m_flags & b2Body.e_islandFlag) {
   6407                   continue;
   6408                }
   6409                if (other.GetType() != b2Body.b2_staticBody) {
   6410                   other.Advance(minTOI);
   6411                   other.SetAwake(true);
   6412                }
   6413                queue[queueStart + queueSize] = other;
   6414                ++queueSize;
   6415                other.m_flags |= b2Body.e_islandFlag;
   6416             }
   6417             for (var jEdge = b.m_jointList; jEdge; jEdge = jEdge.next) {
   6418                if (island.m_jointCount == island.m_jointCapacity) continue;
   6419                if (jEdge.joint.m_islandFlag == true) continue;
   6420                other = jEdge.other;
   6421                if (other.IsActive() == false) {
   6422                   continue;
   6423                }
   6424                island.AddJoint(jEdge.joint);
   6425                jEdge.joint.m_islandFlag = true;
   6426                if (other.m_flags & b2Body.e_islandFlag) continue;
   6427                if (other.GetType() != b2Body.b2_staticBody) {
   6428                   other.Advance(minTOI);
   6429                   other.SetAwake(true);
   6430                }
   6431                queue[queueStart + queueSize] = other;
   6432                ++queueSize;
   6433                other.m_flags |= b2Body.e_islandFlag;
   6434             }
   6435          }
   6436          var subStep = b2World.s_timestep;
   6437          subStep.warmStarting = false;
   6438          subStep.dt = (1.0 - minTOI) * step.dt;
   6439          subStep.inv_dt = 1.0 / subStep.dt;
   6440          subStep.dtRatio = 0.0;
   6441          subStep.velocityIterations = step.velocityIterations;
   6442          subStep.positionIterations = step.positionIterations;
   6443          island.SolveTOI(subStep);
   6444          var i = 0;
   6445          for (i = 0;
   6446          i < island.m_bodyCount; ++i) {
   6447             b = island.m_bodies[i];
   6448             b.m_flags &= ~b2Body.e_islandFlag;
   6449             if (b.IsAwake() == false) {
   6450                continue;
   6451             }
   6452             if (b.GetType() != b2Body.b2_dynamicBody) {
   6453                continue;
   6454             }
   6455             b.SynchronizeFixtures();
   6456             for (cEdge = b.m_contactList;
   6457             cEdge; cEdge = cEdge.next) {
   6458                cEdge.contact.m_flags &= ~b2Contact.e_toiFlag;
   6459             }
   6460          }
   6461          for (i = 0;
   6462          i < island.m_contactCount; ++i) {
   6463             c = island.m_contacts[i];
   6464             c.m_flags &= ~ (b2Contact.e_toiFlag | b2Contact.e_islandFlag);
   6465          }
   6466          for (i = 0;
   6467          i < island.m_jointCount; ++i) {
   6468             j = island.m_joints[i];
   6469             j.m_islandFlag = false;
   6470          }
   6471          this.m_contactManager.FindNewContacts();
   6472       }
   6473    }
   6474    b2World.prototype.DrawJoint = function (joint) {
   6475       var b1 = joint.GetBodyA();
   6476       var b2 = joint.GetBodyB();
   6477       var xf1 = b1.m_xf;
   6478       var xf2 = b2.m_xf;
   6479       var x1 = xf1.position;
   6480       var x2 = xf2.position;
   6481       var p1 = joint.GetAnchorA();
   6482       var p2 = joint.GetAnchorB();
   6483       var color = b2World.s_jointColor;
   6484       switch (joint.m_type) {
   6485       case b2Joint.e_distanceJoint:
   6486          this.m_debugDraw.DrawSegment(p1, p2, color);
   6487          break;
   6488       case b2Joint.e_pulleyJoint:
   6489          {
   6490             var pulley = ((joint instanceof b2PulleyJoint ? joint : null));
   6491             var s1 = pulley.GetGroundAnchorA();
   6492             var s2 = pulley.GetGroundAnchorB();
   6493             this.m_debugDraw.DrawSegment(s1, p1, color);
   6494             this.m_debugDraw.DrawSegment(s2, p2, color);
   6495             this.m_debugDraw.DrawSegment(s1, s2, color);
   6496          }
   6497          break;
   6498       case b2Joint.e_mouseJoint:
   6499          this.m_debugDraw.DrawSegment(p1, p2, color);
   6500          break;
   6501       default:
   6502          if (b1 != this.m_groundBody) this.m_debugDraw.DrawSegment(x1, p1, color);
   6503          this.m_debugDraw.DrawSegment(p1, p2, color);
   6504          if (b2 != this.m_groundBody) this.m_debugDraw.DrawSegment(x2, p2, color);
   6505       }
   6506    }
   6507    b2World.prototype.DrawShape = function (shape, xf, color) {
   6508       switch (shape.m_type) {
   6509       case b2Shape.e_circleShape:
   6510          {
   6511             var circle = ((shape instanceof b2CircleShape ? shape : null));
   6512             var center = b2Math.MulX(xf, circle.m_p);
   6513             var radius = circle.m_radius;
   6514             var axis = xf.R.col1;
   6515             this.m_debugDraw.DrawSolidCircle(center, radius, axis, color);
   6516          }
   6517          break;
   6518       case b2Shape.e_polygonShape:
   6519          {
   6520             var i = 0;
   6521             var poly = ((shape instanceof b2PolygonShape ? shape : null));
   6522             var vertexCount = parseInt(poly.GetVertexCount());
   6523             var localVertices = poly.GetVertices();
   6524             var vertices = new Vector(vertexCount);
   6525             for (i = 0;
   6526             i < vertexCount; ++i) {
   6527                vertices[i] = b2Math.MulX(xf, localVertices[i]);
   6528             }
   6529             this.m_debugDraw.DrawSolidPolygon(vertices, vertexCount, color);
   6530          }
   6531          break;
   6532       case b2Shape.e_edgeShape:
   6533          {
   6534             var edge = (shape instanceof b2EdgeShape ? shape : null);
   6535             this.m_debugDraw.DrawSegment(b2Math.MulX(xf, edge.GetVertex1()), b2Math.MulX(xf, edge.GetVertex2()), color);
   6536          }
   6537          break;
   6538       }
   6539    }
   6540    Box2D.postDefs.push(function () {
   6541       Box2D.Dynamics.b2World.s_timestep2 = new b2TimeStep();
   6542       Box2D.Dynamics.b2World.s_xf = new b2Transform();
   6543       Box2D.Dynamics.b2World.s_backupA = new b2Sweep();
   6544       Box2D.Dynamics.b2World.s_backupB = new b2Sweep();
   6545       Box2D.Dynamics.b2World.s_timestep = new b2TimeStep();
   6546       Box2D.Dynamics.b2World.s_queue = new Vector();
   6547       Box2D.Dynamics.b2World.s_jointColor = new b2Color(0.5, 0.8, 0.8);
   6548       Box2D.Dynamics.b2World.e_newFixture = 0x0001;
   6549       Box2D.Dynamics.b2World.e_locked = 0x0002;
   6550    });
   6551 })();
   6552 (function () {
   6553    var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
   6554       b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
   6555       b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
   6556       b2MassData = Box2D.Collision.Shapes.b2MassData,
   6557       b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
   6558       b2Shape = Box2D.Collision.Shapes.b2Shape,
   6559       b2CircleContact = Box2D.Dynamics.Contacts.b2CircleContact,
   6560       b2Contact = Box2D.Dynamics.Contacts.b2Contact,
   6561       b2ContactConstraint = Box2D.Dynamics.Contacts.b2ContactConstraint,
   6562       b2ContactConstraintPoint = Box2D.Dynamics.Contacts.b2ContactConstraintPoint,
   6563       b2ContactEdge = Box2D.Dynamics.Contacts.b2ContactEdge,
   6564       b2ContactFactory = Box2D.Dynamics.Contacts.b2ContactFactory,
   6565       b2ContactRegister = Box2D.Dynamics.Contacts.b2ContactRegister,
   6566       b2ContactResult = Box2D.Dynamics.Contacts.b2ContactResult,
   6567       b2ContactSolver = Box2D.Dynamics.Contacts.b2ContactSolver,
   6568       b2EdgeAndCircleContact = Box2D.Dynamics.Contacts.b2EdgeAndCircleContact,
   6569       b2NullContact = Box2D.Dynamics.Contacts.b2NullContact,
   6570       b2PolyAndCircleContact = Box2D.Dynamics.Contacts.b2PolyAndCircleContact,
   6571       b2PolyAndEdgeContact = Box2D.Dynamics.Contacts.b2PolyAndEdgeContact,
   6572       b2PolygonContact = Box2D.Dynamics.Contacts.b2PolygonContact,
   6573       b2PositionSolverManifold = Box2D.Dynamics.Contacts.b2PositionSolverManifold,
   6574       b2Body = Box2D.Dynamics.b2Body,
   6575       b2BodyDef = Box2D.Dynamics.b2BodyDef,
   6576       b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
   6577       b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
   6578       b2ContactListener = Box2D.Dynamics.b2ContactListener,
   6579       b2ContactManager = Box2D.Dynamics.b2ContactManager,
   6580       b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
   6581       b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
   6582       b2FilterData = Box2D.Dynamics.b2FilterData,
   6583       b2Fixture = Box2D.Dynamics.b2Fixture,
   6584       b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
   6585       b2Island = Box2D.Dynamics.b2Island,
   6586       b2TimeStep = Box2D.Dynamics.b2TimeStep,
   6587       b2World = Box2D.Dynamics.b2World,
   6588       b2Color = Box2D.Common.b2Color,
   6589       b2internal = Box2D.Common.b2internal,
   6590       b2Settings = Box2D.Common.b2Settings,
   6591       b2Mat22 = Box2D.Common.Math.b2Mat22,
   6592       b2Mat33 = Box2D.Common.Math.b2Mat33,
   6593       b2Math = Box2D.Common.Math.b2Math,
   6594       b2Sweep = Box2D.Common.Math.b2Sweep,
   6595       b2Transform = Box2D.Common.Math.b2Transform,
   6596       b2Vec2 = Box2D.Common.Math.b2Vec2,
   6597       b2Vec3 = Box2D.Common.Math.b2Vec3,
   6598       b2AABB = Box2D.Collision.b2AABB,
   6599       b2Bound = Box2D.Collision.b2Bound,
   6600       b2BoundValues = Box2D.Collision.b2BoundValues,
   6601       b2Collision = Box2D.Collision.b2Collision,
   6602       b2ContactID = Box2D.Collision.b2ContactID,
   6603       b2ContactPoint = Box2D.Collision.b2ContactPoint,
   6604       b2Distance = Box2D.Collision.b2Distance,
   6605       b2DistanceInput = Box2D.Collision.b2DistanceInput,
   6606       b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
   6607       b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
   6608       b2DynamicTree = Box2D.Collision.b2DynamicTree,
   6609       b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
   6610       b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
   6611       b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
   6612       b2Manifold = Box2D.Collision.b2Manifold,
   6613       b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
   6614       b2Point = Box2D.Collision.b2Point,
   6615       b2RayCastInput = Box2D.Collision.b2RayCastInput,
   6616       b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
   6617       b2Segment = Box2D.Collision.b2Segment,
   6618       b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
   6619       b2Simplex = Box2D.Collision.b2Simplex,
   6620       b2SimplexCache = Box2D.Collision.b2SimplexCache,
   6621       b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
   6622       b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
   6623       b2TOIInput = Box2D.Collision.b2TOIInput,
   6624       b2WorldManifold = Box2D.Collision.b2WorldManifold,
   6625       ClipVertex = Box2D.Collision.ClipVertex,
   6626       Features = Box2D.Collision.Features,
   6627       IBroadPhase = Box2D.Collision.IBroadPhase;
   6628 
   6629    Box2D.inherit(b2CircleContact, Box2D.Dynamics.Contacts.b2Contact);
   6630    b2CircleContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
   6631    b2CircleContact.b2CircleContact = function () {
   6632       Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
   6633    };
   6634    b2CircleContact.Create = function (allocator) {
   6635       return new b2CircleContact();
   6636    }
   6637    b2CircleContact.Destroy = function (contact, allocator) {}
   6638    b2CircleContact.prototype.Reset = function (fixtureA, fixtureB) {
   6639       this.__super.Reset.call(this, fixtureA, fixtureB);
   6640    }
   6641    b2CircleContact.prototype.Evaluate = function () {
   6642       var bA = this.m_fixtureA.GetBody();
   6643       var bB = this.m_fixtureB.GetBody();
   6644       b2Collision.CollideCircles(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2CircleShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2CircleShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
   6645    }
   6646    b2Contact.b2Contact = function () {
   6647       this.m_nodeA = new b2ContactEdge();
   6648       this.m_nodeB = new b2ContactEdge();
   6649       this.m_manifold = new b2Manifold();
   6650       this.m_oldManifold = new b2Manifold();
   6651    };
   6652    b2Contact.prototype.GetManifold = function () {
   6653       return this.m_manifold;
   6654    }
   6655    b2Contact.prototype.GetWorldManifold = function (worldManifold) {
   6656       var bodyA = this.m_fixtureA.GetBody();
   6657       var bodyB = this.m_fixtureB.GetBody();
   6658       var shapeA = this.m_fixtureA.GetShape();
   6659       var shapeB = this.m_fixtureB.GetShape();
   6660       worldManifold.Initialize(this.m_manifold, bodyA.GetTransform(), shapeA.m_radius, bodyB.GetTransform(), shapeB.m_radius);
   6661    }
   6662    b2Contact.prototype.IsTouching = function () {
   6663       return (this.m_flags & b2Contact.e_touchingFlag) == b2Contact.e_touchingFlag;
   6664    }
   6665    b2Contact.prototype.IsContinuous = function () {
   6666       return (this.m_flags & b2Contact.e_continuousFlag) == b2Contact.e_continuousFlag;
   6667    }
   6668    b2Contact.prototype.SetSensor = function (sensor) {
   6669       if (sensor) {
   6670          this.m_flags |= b2Contact.e_sensorFlag;
   6671       }
   6672       else {
   6673          this.m_flags &= ~b2Contact.e_sensorFlag;
   6674       }
   6675    }
   6676    b2Contact.prototype.IsSensor = function () {
   6677       return (this.m_flags & b2Contact.e_sensorFlag) == b2Contact.e_sensorFlag;
   6678    }
   6679    b2Contact.prototype.SetEnabled = function (flag) {
   6680       if (flag) {
   6681          this.m_flags |= b2Contact.e_enabledFlag;
   6682       }
   6683       else {
   6684          this.m_flags &= ~b2Contact.e_enabledFlag;
   6685       }
   6686    }
   6687    b2Contact.prototype.IsEnabled = function () {
   6688       return (this.m_flags & b2Contact.e_enabledFlag) == b2Contact.e_enabledFlag;
   6689    }
   6690    b2Contact.prototype.GetNext = function () {
   6691       return this.m_next;
   6692    }
   6693    b2Contact.prototype.GetFixtureA = function () {
   6694       return this.m_fixtureA;
   6695    }
   6696    b2Contact.prototype.GetFixtureB = function () {
   6697       return this.m_fixtureB;
   6698    }
   6699    b2Contact.prototype.FlagForFiltering = function () {
   6700       this.m_flags |= b2Contact.e_filterFlag;
   6701    }
   6702    b2Contact.prototype.b2Contact = function () {}
   6703    b2Contact.prototype.Reset = function (fixtureA, fixtureB) {
   6704       if (fixtureA === undefined) fixtureA = null;
   6705       if (fixtureB === undefined) fixtureB = null;
   6706       this.m_flags = b2Contact.e_enabledFlag;
   6707       if (!fixtureA || !fixtureB) {
   6708          this.m_fixtureA = null;
   6709          this.m_fixtureB = null;
   6710          return;
   6711       }
   6712       if (fixtureA.IsSensor() || fixtureB.IsSensor()) {
   6713          this.m_flags |= b2Contact.e_sensorFlag;
   6714       }
   6715       var bodyA = fixtureA.GetBody();
   6716       var bodyB = fixtureB.GetBody();
   6717       if (bodyA.GetType() != b2Body.b2_dynamicBody || bodyA.IsBullet() || bodyB.GetType() != b2Body.b2_dynamicBody || bodyB.IsBullet()) {
   6718          this.m_flags |= b2Contact.e_continuousFlag;
   6719       }
   6720       this.m_fixtureA = fixtureA;
   6721       this.m_fixtureB = fixtureB;
   6722       this.m_manifold.m_pointCount = 0;
   6723       this.m_prev = null;
   6724       this.m_next = null;
   6725       this.m_nodeA.contact = null;
   6726       this.m_nodeA.prev = null;
   6727       this.m_nodeA.next = null;
   6728       this.m_nodeA.other = null;
   6729       this.m_nodeB.contact = null;
   6730       this.m_nodeB.prev = null;
   6731       this.m_nodeB.next = null;
   6732       this.m_nodeB.other = null;
   6733    }
   6734    b2Contact.prototype.Update = function (listener) {
   6735       var tManifold = this.m_oldManifold;
   6736       this.m_oldManifold = this.m_manifold;
   6737       this.m_manifold = tManifold;
   6738       this.m_flags |= b2Contact.e_enabledFlag;
   6739       var touching = false;
   6740       var wasTouching = (this.m_flags & b2Contact.e_touchingFlag) == b2Contact.e_touchingFlag;
   6741       var bodyA = this.m_fixtureA.m_body;
   6742       var bodyB = this.m_fixtureB.m_body;
   6743       var aabbOverlap = this.m_fixtureA.m_aabb.TestOverlap(this.m_fixtureB.m_aabb);
   6744       if (this.m_flags & b2Contact.e_sensorFlag) {
   6745          if (aabbOverlap) {
   6746             var shapeA = this.m_fixtureA.GetShape();
   6747             var shapeB = this.m_fixtureB.GetShape();
   6748             var xfA = bodyA.GetTransform();
   6749             var xfB = bodyB.GetTransform();
   6750             touching = b2Shape.TestOverlap(shapeA, xfA, shapeB, xfB);
   6751          }
   6752          this.m_manifold.m_pointCount = 0;
   6753       }
   6754       else {
   6755          if (bodyA.GetType() != b2Body.b2_dynamicBody || bodyA.IsBullet() || bodyB.GetType() != b2Body.b2_dynamicBody || bodyB.IsBullet()) {
   6756             this.m_flags |= b2Contact.e_continuousFlag;
   6757          }
   6758          else {
   6759             this.m_flags &= ~b2Contact.e_continuousFlag;
   6760          }
   6761          if (aabbOverlap) {
   6762             this.Evaluate();
   6763             touching = this.m_manifold.m_pointCount > 0;
   6764             for (var i = 0; i < this.m_manifold.m_pointCount; ++i) {
   6765                var mp2 = this.m_manifold.m_points[i];
   6766                mp2.m_normalImpulse = 0.0;
   6767                mp2.m_tangentImpulse = 0.0;
   6768                var id2 = mp2.m_id;
   6769                for (var j = 0; j < this.m_oldManifold.m_pointCount; ++j) {
   6770                   var mp1 = this.m_oldManifold.m_points[j];
   6771                   if (mp1.m_id.key == id2.key) {
   6772                      mp2.m_normalImpulse = mp1.m_normalImpulse;
   6773                      mp2.m_tangentImpulse = mp1.m_tangentImpulse;
   6774                      break;
   6775                   }
   6776                }
   6777             }
   6778          }
   6779          else {
   6780             this.m_manifold.m_pointCount = 0;
   6781          }
   6782          if (touching != wasTouching) {
   6783             bodyA.SetAwake(true);
   6784             bodyB.SetAwake(true);
   6785          }
   6786       }
   6787       if (touching) {
   6788          this.m_flags |= b2Contact.e_touchingFlag;
   6789       }
   6790       else {
   6791          this.m_flags &= ~b2Contact.e_touchingFlag;
   6792       }
   6793       if (wasTouching == false && touching == true) {
   6794          listener.BeginContact(this);
   6795       }
   6796       if (wasTouching == true && touching == false) {
   6797          listener.EndContact(this);
   6798       }
   6799       if ((this.m_flags & b2Contact.e_sensorFlag) == 0) {
   6800          listener.PreSolve(this, this.m_oldManifold);
   6801       }
   6802    }
   6803    b2Contact.prototype.Evaluate = function () {}
   6804    b2Contact.prototype.ComputeTOI = function (sweepA, sweepB) {
   6805       b2Contact.s_input.proxyA.Set(this.m_fixtureA.GetShape());
   6806       b2Contact.s_input.proxyB.Set(this.m_fixtureB.GetShape());
   6807       b2Contact.s_input.sweepA = sweepA;
   6808       b2Contact.s_input.sweepB = sweepB;
   6809       b2Contact.s_input.tolerance = b2Settings.b2_linearSlop;
   6810       return b2TimeOfImpact.TimeOfImpact(b2Contact.s_input);
   6811    }
   6812    Box2D.postDefs.push(function () {
   6813       Box2D.Dynamics.Contacts.b2Contact.e_sensorFlag = 0x0001;
   6814       Box2D.Dynamics.Contacts.b2Contact.e_continuousFlag = 0x0002;
   6815       Box2D.Dynamics.Contacts.b2Contact.e_islandFlag = 0x0004;
   6816       Box2D.Dynamics.Contacts.b2Contact.e_toiFlag = 0x0008;
   6817       Box2D.Dynamics.Contacts.b2Contact.e_touchingFlag = 0x0010;
   6818       Box2D.Dynamics.Contacts.b2Contact.e_enabledFlag = 0x0020;
   6819       Box2D.Dynamics.Contacts.b2Contact.e_filterFlag = 0x0040;
   6820       Box2D.Dynamics.Contacts.b2Contact.s_input = new b2TOIInput();
   6821    });
   6822    b2ContactConstraint.b2ContactConstraint = function () {
   6823       this.localPlaneNormal = new b2Vec2();
   6824       this.localPoint = new b2Vec2();
   6825       this.normal = new b2Vec2();
   6826       this.normalMass = new b2Mat22();
   6827       this.K = new b2Mat22();
   6828    };
   6829    b2ContactConstraint.prototype.b2ContactConstraint = function () {
   6830       this.points = new Vector(b2Settings.b2_maxManifoldPoints);
   6831       for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
   6832          this.points[i] = new b2ContactConstraintPoint();
   6833       }
   6834    }
   6835    b2ContactConstraintPoint.b2ContactConstraintPoint = function () {
   6836       this.localPoint = new b2Vec2();
   6837       this.rA = new b2Vec2();
   6838       this.rB = new b2Vec2();
   6839    };
   6840    b2ContactEdge.b2ContactEdge = function () {};
   6841    b2ContactFactory.b2ContactFactory = function () {};
   6842    b2ContactFactory.prototype.b2ContactFactory = function (allocator) {
   6843       this.m_allocator = allocator;
   6844       this.InitializeRegisters();
   6845    }
   6846    b2ContactFactory.prototype.AddType = function (createFcn, destroyFcn, type1, type2) {
   6847       if (type1 === undefined) type1 = 0;
   6848       if (type2 === undefined) type2 = 0;
   6849       this.m_registers[type1][type2].createFcn = createFcn;
   6850       this.m_registers[type1][type2].destroyFcn = destroyFcn;
   6851       this.m_registers[type1][type2].primary = true;
   6852       if (type1 != type2) {
   6853          this.m_registers[type2][type1].createFcn = createFcn;
   6854          this.m_registers[type2][type1].destroyFcn = destroyFcn;
   6855          this.m_registers[type2][type1].primary = false;
   6856       }
   6857    }
   6858    b2ContactFactory.prototype.InitializeRegisters = function () {
   6859       this.m_registers = new Vector(b2Shape.e_shapeTypeCount);
   6860       for (var i = 0; i < b2Shape.e_shapeTypeCount; i++) {
   6861          this.m_registers[i] = new Vector(b2Shape.e_shapeTypeCount);
   6862          for (var j = 0; j < b2Shape.e_shapeTypeCount; j++) {
   6863             this.m_registers[i][j] = new b2ContactRegister();
   6864          }
   6865       }
   6866       this.AddType(b2CircleContact.Create, b2CircleContact.Destroy, b2Shape.e_circleShape, b2Shape.e_circleShape);
   6867       this.AddType(b2PolyAndCircleContact.Create, b2PolyAndCircleContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_circleShape);
   6868       this.AddType(b2PolygonContact.Create, b2PolygonContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_polygonShape);
   6869       this.AddType(b2EdgeAndCircleContact.Create, b2EdgeAndCircleContact.Destroy, b2Shape.e_edgeShape, b2Shape.e_circleShape);
   6870       this.AddType(b2PolyAndEdgeContact.Create, b2PolyAndEdgeContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_edgeShape);
   6871    }
   6872    b2ContactFactory.prototype.Create = function (fixtureA, fixtureB) {
   6873       var type1 = parseInt(fixtureA.GetType());
   6874       var type2 = parseInt(fixtureB.GetType());
   6875       var reg = this.m_registers[type1][type2];
   6876       var c;
   6877       if (reg.pool) {
   6878          c = reg.pool;
   6879          reg.pool = c.m_next;
   6880          reg.poolCount--;
   6881          c.Reset(fixtureA, fixtureB);
   6882          return c;
   6883       }
   6884       var createFcn = reg.createFcn;
   6885       if (createFcn != null) {
   6886          if (reg.primary) {
   6887             c = createFcn(this.m_allocator);
   6888             c.Reset(fixtureA, fixtureB);
   6889             return c;
   6890          }
   6891          else {
   6892             c = createFcn(this.m_allocator);
   6893             c.Reset(fixtureB, fixtureA);
   6894             return c;
   6895          }
   6896       }
   6897       else {
   6898          return null;
   6899       }
   6900    }
   6901    b2ContactFactory.prototype.Destroy = function (contact) {
   6902       if (contact.m_manifold.m_pointCount > 0) {
   6903          contact.m_fixtureA.m_body.SetAwake(true);
   6904          contact.m_fixtureB.m_body.SetAwake(true);
   6905       }
   6906       var type1 = parseInt(contact.m_fixtureA.GetType());
   6907       var type2 = parseInt(contact.m_fixtureB.GetType());
   6908       var reg = this.m_registers[type1][type2];
   6909       if (true) {
   6910          reg.poolCount++;
   6911          contact.m_next = reg.pool;
   6912          reg.pool = contact;
   6913       }
   6914       var destroyFcn = reg.destroyFcn;
   6915       destroyFcn(contact, this.m_allocator);
   6916    }
   6917    b2ContactRegister.b2ContactRegister = function () {};
   6918    b2ContactResult.b2ContactResult = function () {
   6919       this.position = new b2Vec2();
   6920       this.normal = new b2Vec2();
   6921       this.id = new b2ContactID();
   6922    };
   6923    b2ContactSolver.b2ContactSolver = function () {
   6924       this.m_step = new b2TimeStep();
   6925       this.m_constraints = new Vector();
   6926    };
   6927    b2ContactSolver.prototype.b2ContactSolver = function () {}
   6928    b2ContactSolver.prototype.Initialize = function (step, contacts, contactCount, allocator) {
   6929       if (contactCount === undefined) contactCount = 0;
   6930       var contact;
   6931       this.m_step.Set(step);
   6932       this.m_allocator = allocator;
   6933       var i = 0;
   6934       var tVec;
   6935       var tMat;
   6936       this.m_constraintCount = contactCount;
   6937       while (this.m_constraints.length < this.m_constraintCount) {
   6938          this.m_constraints[this.m_constraints.length] = new b2ContactConstraint();
   6939       }
   6940       for (i = 0;
   6941       i < contactCount; ++i) {
   6942          contact = contacts[i];
   6943          var fixtureA = contact.m_fixtureA;
   6944          var fixtureB = contact.m_fixtureB;
   6945          var shapeA = fixtureA.m_shape;
   6946          var shapeB = fixtureB.m_shape;
   6947          var radiusA = shapeA.m_radius;
   6948          var radiusB = shapeB.m_radius;
   6949          var bodyA = fixtureA.m_body;
   6950          var bodyB = fixtureB.m_body;
   6951          var manifold = contact.GetManifold();
   6952          var friction = b2Settings.b2MixFriction(fixtureA.GetFriction(), fixtureB.GetFriction());
   6953          var restitution = b2Settings.b2MixRestitution(fixtureA.GetRestitution(), fixtureB.GetRestitution());
   6954          var vAX = bodyA.m_linearVelocity.x;
   6955          var vAY = bodyA.m_linearVelocity.y;
   6956          var vBX = bodyB.m_linearVelocity.x;
   6957          var vBY = bodyB.m_linearVelocity.y;
   6958          var wA = bodyA.m_angularVelocity;
   6959          var wB = bodyB.m_angularVelocity;
   6960          b2Settings.b2Assert(manifold.m_pointCount > 0);
   6961          b2ContactSolver.s_worldManifold.Initialize(manifold, bodyA.m_xf, radiusA, bodyB.m_xf, radiusB);
   6962          var normalX = b2ContactSolver.s_worldManifold.m_normal.x;
   6963          var normalY = b2ContactSolver.s_worldManifold.m_normal.y;
   6964          var cc = this.m_constraints[i];
   6965          cc.bodyA = bodyA;
   6966          cc.bodyB = bodyB;
   6967          cc.manifold = manifold;
   6968          cc.normal.x = normalX;
   6969          cc.normal.y = normalY;
   6970          cc.pointCount = manifold.m_pointCount;
   6971          cc.friction = friction;
   6972          cc.restitution = restitution;
   6973          cc.localPlaneNormal.x = manifold.m_localPlaneNormal.x;
   6974          cc.localPlaneNormal.y = manifold.m_localPlaneNormal.y;
   6975          cc.localPoint.x = manifold.m_localPoint.x;
   6976          cc.localPoint.y = manifold.m_localPoint.y;
   6977          cc.radius = radiusA + radiusB;
   6978          cc.type = manifold.m_type;
   6979          for (var k = 0; k < cc.pointCount; ++k) {
   6980             var cp = manifold.m_points[k];
   6981             var ccp = cc.points[k];
   6982             ccp.normalImpulse = cp.m_normalImpulse;
   6983             ccp.tangentImpulse = cp.m_tangentImpulse;
   6984             ccp.localPoint.SetV(cp.m_localPoint);
   6985             var rAX = ccp.rA.x = b2ContactSolver.s_worldManifold.m_points[k].x - bodyA.m_sweep.c.x;
   6986             var rAY = ccp.rA.y = b2ContactSolver.s_worldManifold.m_points[k].y - bodyA.m_sweep.c.y;
   6987             var rBX = ccp.rB.x = b2ContactSolver.s_worldManifold.m_points[k].x - bodyB.m_sweep.c.x;
   6988             var rBY = ccp.rB.y = b2ContactSolver.s_worldManifold.m_points[k].y - bodyB.m_sweep.c.y;
   6989             var rnA = rAX * normalY - rAY * normalX;
   6990             var rnB = rBX * normalY - rBY * normalX;
   6991             rnA *= rnA;
   6992             rnB *= rnB;
   6993             var kNormal = bodyA.m_invMass + bodyB.m_invMass + bodyA.m_invI * rnA + bodyB.m_invI * rnB;
   6994             ccp.normalMass = 1.0 / kNormal;
   6995             var kEqualized = bodyA.m_mass * bodyA.m_invMass + bodyB.m_mass * bodyB.m_invMass;
   6996             kEqualized += bodyA.m_mass * bodyA.m_invI * rnA + bodyB.m_mass * bodyB.m_invI * rnB;
   6997             ccp.equalizedMass = 1.0 / kEqualized;
   6998             var tangentX = normalY;
   6999             var tangentY = (-normalX);
   7000             var rtA = rAX * tangentY - rAY * tangentX;
   7001             var rtB = rBX * tangentY - rBY * tangentX;
   7002             rtA *= rtA;
   7003             rtB *= rtB;
   7004             var kTangent = bodyA.m_invMass + bodyB.m_invMass + bodyA.m_invI * rtA + bodyB.m_invI * rtB;
   7005             ccp.tangentMass = 1.0 / kTangent;
   7006             ccp.velocityBias = 0.0;
   7007             var tX = vBX + ((-wB * rBY)) - vAX - ((-wA * rAY));
   7008             var tY = vBY + (wB * rBX) - vAY - (wA * rAX);
   7009             var vRel = cc.normal.x * tX + cc.normal.y * tY;
   7010             if (vRel < (-b2Settings.b2_velocityThreshold)) {
   7011                ccp.velocityBias += (-cc.restitution * vRel);
   7012             }
   7013          }
   7014          if (cc.pointCount == 2) {
   7015             var ccp1 = cc.points[0];
   7016             var ccp2 = cc.points[1];
   7017             var invMassA = bodyA.m_invMass;
   7018             var invIA = bodyA.m_invI;
   7019             var invMassB = bodyB.m_invMass;
   7020             var invIB = bodyB.m_invI;
   7021             var rn1A = ccp1.rA.x * normalY - ccp1.rA.y * normalX;
   7022             var rn1B = ccp1.rB.x * normalY - ccp1.rB.y * normalX;
   7023             var rn2A = ccp2.rA.x * normalY - ccp2.rA.y * normalX;
   7024             var rn2B = ccp2.rB.x * normalY - ccp2.rB.y * normalX;
   7025             var k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
   7026             var k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
   7027             var k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;
   7028             var k_maxConditionNumber = 100.0;
   7029             if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12)) {
   7030                cc.K.col1.Set(k11, k12);
   7031                cc.K.col2.Set(k12, k22);
   7032                cc.K.GetInverse(cc.normalMass);
   7033             }
   7034             else {
   7035                cc.pointCount = 1;
   7036             }
   7037          }
   7038       }
   7039    }
   7040    b2ContactSolver.prototype.InitVelocityConstraints = function (step) {
   7041       var tVec;
   7042       var tVec2;
   7043       var tMat;
   7044       for (var i = 0; i < this.m_constraintCount; ++i) {
   7045          var c = this.m_constraints[i];
   7046          var bodyA = c.bodyA;
   7047          var bodyB = c.bodyB;
   7048          var invMassA = bodyA.m_invMass;
   7049          var invIA = bodyA.m_invI;
   7050          var invMassB = bodyB.m_invMass;
   7051          var invIB = bodyB.m_invI;
   7052          var normalX = c.normal.x;
   7053          var normalY = c.normal.y;
   7054          var tangentX = normalY;
   7055          var tangentY = (-normalX);
   7056          var tX = 0;
   7057          var j = 0;
   7058          var tCount = 0;
   7059          if (step.warmStarting) {
   7060             tCount = c.pointCount;
   7061             for (j = 0;
   7062             j < tCount; ++j) {
   7063                var ccp = c.points[j];
   7064                ccp.normalImpulse *= step.dtRatio;
   7065                ccp.tangentImpulse *= step.dtRatio;
   7066                var PX = ccp.normalImpulse * normalX + ccp.tangentImpulse * tangentX;
   7067                var PY = ccp.normalImpulse * normalY + ccp.tangentImpulse * tangentY;
   7068                bodyA.m_angularVelocity -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
   7069                bodyA.m_linearVelocity.x -= invMassA * PX;
   7070                bodyA.m_linearVelocity.y -= invMassA * PY;
   7071                bodyB.m_angularVelocity += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
   7072                bodyB.m_linearVelocity.x += invMassB * PX;
   7073                bodyB.m_linearVelocity.y += invMassB * PY;
   7074             }
   7075          }
   7076          else {
   7077             tCount = c.pointCount;
   7078             for (j = 0;
   7079             j < tCount; ++j) {
   7080                var ccp2 = c.points[j];
   7081                ccp2.normalImpulse = 0.0;
   7082                ccp2.tangentImpulse = 0.0;
   7083             }
   7084          }
   7085       }
   7086    }
   7087    b2ContactSolver.prototype.SolveVelocityConstraints = function () {
   7088       var j = 0;
   7089       var ccp;
   7090       var rAX = 0;
   7091       var rAY = 0;
   7092       var rBX = 0;
   7093       var rBY = 0;
   7094       var dvX = 0;
   7095       var dvY = 0;
   7096       var vn = 0;
   7097       var vt = 0;
   7098       var lambda = 0;
   7099       var maxFriction = 0;
   7100       var newImpulse = 0;
   7101       var PX = 0;
   7102       var PY = 0;
   7103       var dX = 0;
   7104       var dY = 0;
   7105       var P1X = 0;
   7106       var P1Y = 0;
   7107       var P2X = 0;
   7108       var P2Y = 0;
   7109       var tMat;
   7110       var tVec;
   7111       for (var i = 0; i < this.m_constraintCount; ++i) {
   7112          var c = this.m_constraints[i];
   7113          var bodyA = c.bodyA;
   7114          var bodyB = c.bodyB;
   7115          var wA = bodyA.m_angularVelocity;
   7116          var wB = bodyB.m_angularVelocity;
   7117          var vA = bodyA.m_linearVelocity;
   7118          var vB = bodyB.m_linearVelocity;
   7119          var invMassA = bodyA.m_invMass;
   7120          var invIA = bodyA.m_invI;
   7121          var invMassB = bodyB.m_invMass;
   7122          var invIB = bodyB.m_invI;
   7123          var normalX = c.normal.x;
   7124          var normalY = c.normal.y;
   7125          var tangentX = normalY;
   7126          var tangentY = (-normalX);
   7127          var friction = c.friction;
   7128          var tX = 0;
   7129          for (j = 0;
   7130          j < c.pointCount; j++) {
   7131             ccp = c.points[j];
   7132             dvX = vB.x - wB * ccp.rB.y - vA.x + wA * ccp.rA.y;
   7133             dvY = vB.y + wB * ccp.rB.x - vA.y - wA * ccp.rA.x;
   7134             vt = dvX * tangentX + dvY * tangentY;
   7135             lambda = ccp.tangentMass * (-vt);
   7136             maxFriction = friction * ccp.normalImpulse;
   7137             newImpulse = b2Math.Clamp(ccp.tangentImpulse + lambda, (-maxFriction), maxFriction);
   7138             lambda = newImpulse - ccp.tangentImpulse;
   7139             PX = lambda * tangentX;
   7140             PY = lambda * tangentY;
   7141             vA.x -= invMassA * PX;
   7142             vA.y -= invMassA * PY;
   7143             wA -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
   7144             vB.x += invMassB * PX;
   7145             vB.y += invMassB * PY;
   7146             wB += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
   7147             ccp.tangentImpulse = newImpulse;
   7148          }
   7149          var tCount = parseInt(c.pointCount);
   7150          if (c.pointCount == 1) {
   7151             ccp = c.points[0];
   7152             dvX = vB.x + ((-wB * ccp.rB.y)) - vA.x - ((-wA * ccp.rA.y));
   7153             dvY = vB.y + (wB * ccp.rB.x) - vA.y - (wA * ccp.rA.x);
   7154             vn = dvX * normalX + dvY * normalY;
   7155             lambda = (-ccp.normalMass * (vn - ccp.velocityBias));
   7156             newImpulse = ccp.normalImpulse + lambda;
   7157             newImpulse = newImpulse > 0 ? newImpulse : 0.0;
   7158             lambda = newImpulse - ccp.normalImpulse;
   7159             PX = lambda * normalX;
   7160             PY = lambda * normalY;
   7161             vA.x -= invMassA * PX;
   7162             vA.y -= invMassA * PY;
   7163             wA -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
   7164             vB.x += invMassB * PX;
   7165             vB.y += invMassB * PY;
   7166             wB += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
   7167             ccp.normalImpulse = newImpulse;
   7168          }
   7169          else {
   7170             var cp1 = c.points[0];
   7171             var cp2 = c.points[1];
   7172             var aX = cp1.normalImpulse;
   7173             var aY = cp2.normalImpulse;
   7174             var dv1X = vB.x - wB * cp1.rB.y - vA.x + wA * cp1.rA.y;
   7175             var dv1Y = vB.y + wB * cp1.rB.x - vA.y - wA * cp1.rA.x;
   7176             var dv2X = vB.x - wB * cp2.rB.y - vA.x + wA * cp2.rA.y;
   7177             var dv2Y = vB.y + wB * cp2.rB.x - vA.y - wA * cp2.rA.x;
   7178             var vn1 = dv1X * normalX + dv1Y * normalY;
   7179             var vn2 = dv2X * normalX + dv2Y * normalY;
   7180             var bX = vn1 - cp1.velocityBias;
   7181             var bY = vn2 - cp2.velocityBias;
   7182             tMat = c.K;
   7183             bX -= tMat.col1.x * aX + tMat.col2.x * aY;
   7184             bY -= tMat.col1.y * aX + tMat.col2.y * aY;
   7185             var k_errorTol = 0.001;
   7186             for (;;) {
   7187                tMat = c.normalMass;
   7188                var xX = (-(tMat.col1.x * bX + tMat.col2.x * bY));
   7189                var xY = (-(tMat.col1.y * bX + tMat.col2.y * bY));
   7190                if (xX >= 0.0 && xY >= 0.0) {
   7191                   dX = xX - aX;
   7192                   dY = xY - aY;
   7193                   P1X = dX * normalX;
   7194                   P1Y = dX * normalY;
   7195                   P2X = dY * normalX;
   7196                   P2Y = dY * normalY;
   7197                   vA.x -= invMassA * (P1X + P2X);
   7198                   vA.y -= invMassA * (P1Y + P2Y);
   7199                   wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
   7200                   vB.x += invMassB * (P1X + P2X);
   7201                   vB.y += invMassB * (P1Y + P2Y);
   7202                   wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
   7203                   cp1.normalImpulse = xX;
   7204                   cp2.normalImpulse = xY;
   7205                   break;
   7206                }
   7207                xX = (-cp1.normalMass * bX);
   7208                xY = 0.0;
   7209                vn1 = 0.0;
   7210                vn2 = c.K.col1.y * xX + bY;
   7211                if (xX >= 0.0 && vn2 >= 0.0) {
   7212                   dX = xX - aX;
   7213                   dY = xY - aY;
   7214                   P1X = dX * normalX;
   7215                   P1Y = dX * normalY;
   7216                   P2X = dY * normalX;
   7217                   P2Y = dY * normalY;
   7218                   vA.x -= invMassA * (P1X + P2X);
   7219                   vA.y -= invMassA * (P1Y + P2Y);
   7220                   wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
   7221                   vB.x += invMassB * (P1X + P2X);
   7222                   vB.y += invMassB * (P1Y + P2Y);
   7223                   wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
   7224                   cp1.normalImpulse = xX;
   7225                   cp2.normalImpulse = xY;
   7226                   break;
   7227                }
   7228                xX = 0.0;
   7229                xY = (-cp2.normalMass * bY);
   7230                vn1 = c.K.col2.x * xY + bX;
   7231                vn2 = 0.0;
   7232                if (xY >= 0.0 && vn1 >= 0.0) {
   7233                   dX = xX - aX;
   7234                   dY = xY - aY;
   7235                   P1X = dX * normalX;
   7236                   P1Y = dX * normalY;
   7237                   P2X = dY * normalX;
   7238                   P2Y = dY * normalY;
   7239                   vA.x -= invMassA * (P1X + P2X);
   7240                   vA.y -= invMassA * (P1Y + P2Y);
   7241                   wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
   7242                   vB.x += invMassB * (P1X + P2X);
   7243                   vB.y += invMassB * (P1Y + P2Y);
   7244                   wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
   7245                   cp1.normalImpulse = xX;
   7246                   cp2.normalImpulse = xY;
   7247                   break;
   7248                }
   7249                xX = 0.0;
   7250                xY = 0.0;
   7251                vn1 = bX;
   7252                vn2 = bY;
   7253                if (vn1 >= 0.0 && vn2 >= 0.0) {
   7254                   dX = xX - aX;
   7255                   dY = xY - aY;
   7256                   P1X = dX * normalX;
   7257                   P1Y = dX * normalY;
   7258                   P2X = dY * normalX;
   7259                   P2Y = dY * normalY;
   7260                   vA.x -= invMassA * (P1X + P2X);
   7261                   vA.y -= invMassA * (P1Y + P2Y);
   7262                   wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
   7263                   vB.x += invMassB * (P1X + P2X);
   7264                   vB.y += invMassB * (P1Y + P2Y);
   7265                   wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
   7266                   cp1.normalImpulse = xX;
   7267                   cp2.normalImpulse = xY;
   7268                   break;
   7269                }
   7270                break;
   7271             }
   7272          }
   7273          bodyA.m_angularVelocity = wA;
   7274          bodyB.m_angularVelocity = wB;
   7275       }
   7276    }
   7277    b2ContactSolver.prototype.FinalizeVelocityConstraints = function () {
   7278       for (var i = 0; i < this.m_constraintCount; ++i) {
   7279          var c = this.m_constraints[i];
   7280          var m = c.manifold;
   7281          for (var j = 0; j < c.pointCount; ++j) {
   7282             var point1 = m.m_points[j];
   7283             var point2 = c.points[j];
   7284             point1.m_normalImpulse = point2.normalImpulse;
   7285             point1.m_tangentImpulse = point2.tangentImpulse;
   7286          }
   7287       }
   7288    }
   7289    b2ContactSolver.prototype.SolvePositionConstraints = function (baumgarte) {
   7290       if (baumgarte === undefined) baumgarte = 0;
   7291       var minSeparation = 0.0;
   7292       for (var i = 0; i < this.m_constraintCount; i++) {
   7293          var c = this.m_constraints[i];
   7294          var bodyA = c.bodyA;
   7295          var bodyB = c.bodyB;
   7296          var invMassA = bodyA.m_mass * bodyA.m_invMass;
   7297          var invIA = bodyA.m_mass * bodyA.m_invI;
   7298          var invMassB = bodyB.m_mass * bodyB.m_invMass;
   7299          var invIB = bodyB.m_mass * bodyB.m_invI;
   7300          b2ContactSolver.s_psm.Initialize(c);
   7301          var normal = b2ContactSolver.s_psm.m_normal;
   7302          for (var j = 0; j < c.pointCount; j++) {
   7303             var ccp = c.points[j];
   7304             var point = b2ContactSolver.s_psm.m_points[j];
   7305             var separation = b2ContactSolver.s_psm.m_separations[j];
   7306             var rAX = point.x - bodyA.m_sweep.c.x;
   7307             var rAY = point.y - bodyA.m_sweep.c.y;
   7308             var rBX = point.x - bodyB.m_sweep.c.x;
   7309             var rBY = point.y - bodyB.m_sweep.c.y;
   7310             minSeparation = minSeparation < separation ? minSeparation : separation;
   7311             var C = b2Math.Clamp(baumgarte * (separation + b2Settings.b2_linearSlop), (-b2Settings.b2_maxLinearCorrection), 0.0);
   7312             var impulse = (-ccp.equalizedMass * C);
   7313             var PX = impulse * normal.x;
   7314             var PY = impulse * normal.y;bodyA.m_sweep.c.x -= invMassA * PX;
   7315             bodyA.m_sweep.c.y -= invMassA * PY;
   7316             bodyA.m_sweep.a -= invIA * (rAX * PY - rAY * PX);
   7317             bodyA.SynchronizeTransform();
   7318             bodyB.m_sweep.c.x += invMassB * PX;
   7319             bodyB.m_sweep.c.y += invMassB * PY;
   7320             bodyB.m_sweep.a += invIB * (rBX * PY - rBY * PX);
   7321             bodyB.SynchronizeTransform();
   7322          }
   7323       }
   7324       return minSeparation > (-1.5 * b2Settings.b2_linearSlop);
   7325    }
   7326    Box2D.postDefs.push(function () {
   7327       Box2D.Dynamics.Contacts.b2ContactSolver.s_worldManifold = new b2WorldManifold();
   7328       Box2D.Dynamics.Contacts.b2ContactSolver.s_psm = new b2PositionSolverManifold();
   7329    });
   7330    Box2D.inherit(b2EdgeAndCircleContact, Box2D.Dynamics.Contacts.b2Contact);
   7331    b2EdgeAndCircleContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
   7332    b2EdgeAndCircleContact.b2EdgeAndCircleContact = function () {
   7333       Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
   7334    };
   7335    b2EdgeAndCircleContact.Create = function (allocator) {
   7336       return new b2EdgeAndCircleContact();
   7337    }
   7338    b2EdgeAndCircleContact.Destroy = function (contact, allocator) {}
   7339    b2EdgeAndCircleContact.prototype.Reset = function (fixtureA, fixtureB) {
   7340       this.__super.Reset.call(this, fixtureA, fixtureB);
   7341    }
   7342    b2EdgeAndCircleContact.prototype.Evaluate = function () {
   7343       var bA = this.m_fixtureA.GetBody();
   7344       var bB = this.m_fixtureB.GetBody();
   7345       this.b2CollideEdgeAndCircle(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2EdgeShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2CircleShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
   7346    }
   7347    b2EdgeAndCircleContact.prototype.b2CollideEdgeAndCircle = function (manifold, edge, xf1, circle, xf2) {}
   7348    Box2D.inherit(b2NullContact, Box2D.Dynamics.Contacts.b2Contact);
   7349    b2NullContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
   7350    b2NullContact.b2NullContact = function () {
   7351       Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
   7352    };
   7353    b2NullContact.prototype.b2NullContact = function () {
   7354       this.__super.b2Contact.call(this);
   7355    }
   7356    b2NullContact.prototype.Evaluate = function () {}
   7357    Box2D.inherit(b2PolyAndCircleContact, Box2D.Dynamics.Contacts.b2Contact);
   7358    b2PolyAndCircleContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
   7359    b2PolyAndCircleContact.b2PolyAndCircleContact = function () {
   7360       Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
   7361    };
   7362    b2PolyAndCircleContact.Create = function (allocator) {
   7363       return new b2PolyAndCircleContact();
   7364    }
   7365    b2PolyAndCircleContact.Destroy = function (contact, allocator) {}
   7366    b2PolyAndCircleContact.prototype.Reset = function (fixtureA, fixtureB) {
   7367       this.__super.Reset.call(this, fixtureA, fixtureB);
   7368       b2Settings.b2Assert(fixtureA.GetType() == b2Shape.e_polygonShape);
   7369       b2Settings.b2Assert(fixtureB.GetType() == b2Shape.e_circleShape);
   7370    }
   7371    b2PolyAndCircleContact.prototype.Evaluate = function () {
   7372       var bA = this.m_fixtureA.m_body;
   7373       var bB = this.m_fixtureB.m_body;
   7374       b2Collision.CollidePolygonAndCircle(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2PolygonShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2CircleShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
   7375    }
   7376    Box2D.inherit(b2PolyAndEdgeContact, Box2D.Dynamics.Contacts.b2Contact);
   7377    b2PolyAndEdgeContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
   7378    b2PolyAndEdgeContact.b2PolyAndEdgeContact = function () {
   7379       Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
   7380    };
   7381    b2PolyAndEdgeContact.Create = function (allocator) {
   7382       return new b2PolyAndEdgeContact();
   7383    }
   7384    b2PolyAndEdgeContact.Destroy = function (contact, allocator) {}
   7385    b2PolyAndEdgeContact.prototype.Reset = function (fixtureA, fixtureB) {
   7386       this.__super.Reset.call(this, fixtureA, fixtureB);
   7387       b2Settings.b2Assert(fixtureA.GetType() == b2Shape.e_polygonShape);
   7388       b2Settings.b2Assert(fixtureB.GetType() == b2Shape.e_edgeShape);
   7389    }
   7390    b2PolyAndEdgeContact.prototype.Evaluate = function () {
   7391       var bA = this.m_fixtureA.GetBody();
   7392       var bB = this.m_fixtureB.GetBody();
   7393       this.b2CollidePolyAndEdge(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2PolygonShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2EdgeShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
   7394    }
   7395    b2PolyAndEdgeContact.prototype.b2CollidePolyAndEdge = function (manifold, polygon, xf1, edge, xf2) {}
   7396    Box2D.inherit(b2PolygonContact, Box2D.Dynamics.Contacts.b2Contact);
   7397    b2PolygonContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
   7398    b2PolygonContact.b2PolygonContact = function () {
   7399       Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
   7400    };
   7401    b2PolygonContact.Create = function (allocator) {
   7402       return new b2PolygonContact();
   7403    }
   7404    b2PolygonContact.Destroy = function (contact, allocator) {}
   7405    b2PolygonContact.prototype.Reset = function (fixtureA, fixtureB) {
   7406       this.__super.Reset.call(this, fixtureA, fixtureB);
   7407    }
   7408    b2PolygonContact.prototype.Evaluate = function () {
   7409       var bA = this.m_fixtureA.GetBody();
   7410       var bB = this.m_fixtureB.GetBody();
   7411       b2Collision.CollidePolygons(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2PolygonShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2PolygonShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
   7412    }
   7413    b2PositionSolverManifold.b2PositionSolverManifold = function () {};
   7414    b2PositionSolverManifold.prototype.b2PositionSolverManifold = function () {
   7415       this.m_normal = new b2Vec2();
   7416       this.m_separations = new Vector_a2j_Number(b2Settings.b2_maxManifoldPoints);
   7417       this.m_points = new Vector(b2Settings.b2_maxManifoldPoints);
   7418       for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
   7419          this.m_points[i] = new b2Vec2();
   7420       }
   7421    }
   7422    b2PositionSolverManifold.prototype.Initialize = function (cc) {
   7423       b2Settings.b2Assert(cc.pointCount > 0);
   7424       var i = 0;
   7425       var clipPointX = 0;
   7426       var clipPointY = 0;
   7427       var tMat;
   7428       var tVec;
   7429       var planePointX = 0;
   7430       var planePointY = 0;
   7431       switch (cc.type) {
   7432       case b2Manifold.e_circles:
   7433          {
   7434             tMat = cc.bodyA.m_xf.R;
   7435             tVec = cc.localPoint;
   7436             var pointAX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   7437             var pointAY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   7438             tMat = cc.bodyB.m_xf.R;
   7439             tVec = cc.points[0].localPoint;
   7440             var pointBX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   7441             var pointBY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   7442             var dX = pointBX - pointAX;
   7443             var dY = pointBY - pointAY;
   7444             var d2 = dX * dX + dY * dY;
   7445             if (d2 > Number.MIN_VALUE * Number.MIN_VALUE) {
   7446                var d = Math.sqrt(d2);
   7447                this.m_normal.x = dX / d;
   7448                this.m_normal.y = dY / d;
   7449             }
   7450             else {
   7451                this.m_normal.x = 1.0;
   7452                this.m_normal.y = 0.0;
   7453             }
   7454             this.m_points[0].x = 0.5 * (pointAX + pointBX);
   7455             this.m_points[0].y = 0.5 * (pointAY + pointBY);
   7456             this.m_separations[0] = dX * this.m_normal.x + dY * this.m_normal.y - cc.radius;
   7457          }
   7458          break;
   7459       case b2Manifold.e_faceA:
   7460          {
   7461             tMat = cc.bodyA.m_xf.R;
   7462             tVec = cc.localPlaneNormal;
   7463             this.m_normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   7464             this.m_normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   7465             tMat = cc.bodyA.m_xf.R;
   7466             tVec = cc.localPoint;
   7467             planePointX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   7468             planePointY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   7469             tMat = cc.bodyB.m_xf.R;
   7470             for (i = 0;
   7471             i < cc.pointCount; ++i) {
   7472                tVec = cc.points[i].localPoint;
   7473                clipPointX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   7474                clipPointY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   7475                this.m_separations[i] = (clipPointX - planePointX) * this.m_normal.x + (clipPointY - planePointY) * this.m_normal.y - cc.radius;
   7476                this.m_points[i].x = clipPointX;
   7477                this.m_points[i].y = clipPointY;
   7478             }
   7479          }
   7480          break;
   7481       case b2Manifold.e_faceB:
   7482          {
   7483             tMat = cc.bodyB.m_xf.R;
   7484             tVec = cc.localPlaneNormal;
   7485             this.m_normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   7486             this.m_normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   7487             tMat = cc.bodyB.m_xf.R;
   7488             tVec = cc.localPoint;
   7489             planePointX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   7490             planePointY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   7491             tMat = cc.bodyA.m_xf.R;
   7492             for (i = 0;
   7493             i < cc.pointCount; ++i) {
   7494                tVec = cc.points[i].localPoint;
   7495                clipPointX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
   7496                clipPointY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
   7497                this.m_separations[i] = (clipPointX - planePointX) * this.m_normal.x + (clipPointY - planePointY) * this.m_normal.y - cc.radius;
   7498                this.m_points[i].Set(clipPointX, clipPointY);
   7499             }
   7500             this.m_normal.x *= (-1);
   7501             this.m_normal.y *= (-1);
   7502          }
   7503          break;
   7504       }
   7505    }
   7506    Box2D.postDefs.push(function () {
   7507       Box2D.Dynamics.Contacts.b2PositionSolverManifold.circlePointA = new b2Vec2();
   7508       Box2D.Dynamics.Contacts.b2PositionSolverManifold.circlePointB = new b2Vec2();
   7509    });
   7510 })();
   7511 (function () {
   7512    var b2Body = Box2D.Dynamics.b2Body,
   7513       b2BodyDef = Box2D.Dynamics.b2BodyDef,
   7514       b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
   7515       b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
   7516       b2ContactListener = Box2D.Dynamics.b2ContactListener,
   7517       b2ContactManager = Box2D.Dynamics.b2ContactManager,
   7518       b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
   7519       b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
   7520       b2FilterData = Box2D.Dynamics.b2FilterData,
   7521       b2Fixture = Box2D.Dynamics.b2Fixture,
   7522       b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
   7523       b2Island = Box2D.Dynamics.b2Island,
   7524       b2TimeStep = Box2D.Dynamics.b2TimeStep,
   7525       b2World = Box2D.Dynamics.b2World,
   7526       b2Mat22 = Box2D.Common.Math.b2Mat22,
   7527       b2Mat33 = Box2D.Common.Math.b2Mat33,
   7528       b2Math = Box2D.Common.Math.b2Math,
   7529       b2Sweep = Box2D.Common.Math.b2Sweep,
   7530       b2Transform = Box2D.Common.Math.b2Transform,
   7531       b2Vec2 = Box2D.Common.Math.b2Vec2,
   7532       b2Vec3 = Box2D.Common.Math.b2Vec3,
   7533       b2Color = Box2D.Common.b2Color,
   7534       b2internal = Box2D.Common.b2internal,
   7535       b2Settings = Box2D.Common.b2Settings,
   7536       b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
   7537       b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
   7538       b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
   7539       b2MassData = Box2D.Collision.Shapes.b2MassData,
   7540       b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
   7541       b2Shape = Box2D.Collision.Shapes.b2Shape,
   7542       b2BuoyancyController = Box2D.Dynamics.Controllers.b2BuoyancyController,
   7543       b2ConstantAccelController = Box2D.Dynamics.Controllers.b2ConstantAccelController,
   7544       b2ConstantForceController = Box2D.Dynamics.Controllers.b2ConstantForceController,
   7545       b2Controller = Box2D.Dynamics.Controllers.b2Controller,
   7546       b2ControllerEdge = Box2D.Dynamics.Controllers.b2ControllerEdge,
   7547       b2GravityController = Box2D.Dynamics.Controllers.b2GravityController,
   7548       b2TensorDampingController = Box2D.Dynamics.Controllers.b2TensorDampingController;
   7549 
   7550    Box2D.inherit(b2BuoyancyController, Box2D.Dynamics.Controllers.b2Controller);
   7551    b2BuoyancyController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
   7552    b2BuoyancyController.b2BuoyancyController = function () {
   7553       Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
   7554       this.normal = new b2Vec2(0, (-1));
   7555       this.offset = 0;
   7556       this.density = 0;
   7557       this.velocity = new b2Vec2(0, 0);
   7558       this.linearDrag = 2;
   7559       this.angularDrag = 1;
   7560       this.useDensity = false;
   7561       this.useWorldGravity = true;
   7562       this.gravity = null;
   7563    };
   7564    b2BuoyancyController.prototype.Step = function (step) {
   7565       if (!this.m_bodyList) return;
   7566       if (this.useWorldGravity) {
   7567          this.gravity = this.GetWorld().GetGravity().Copy();
   7568       }
   7569       for (var i = this.m_bodyList; i; i = i.nextBody) {
   7570          var body = i.body;
   7571          if (body.IsAwake() == false) {
   7572             continue;
   7573          }
   7574          var areac = new b2Vec2();
   7575          var massc = new b2Vec2();
   7576          var area = 0.0;
   7577          var mass = 0.0;
   7578          for (var fixture = body.GetFixtureList(); fixture; fixture = fixture.GetNext()) {
   7579             var sc = new b2Vec2();
   7580             var sarea = fixture.GetShape().ComputeSubmergedArea(this.normal, this.offset, body.GetTransform(), sc);
   7581             area += sarea;
   7582             areac.x += sarea * sc.x;
   7583             areac.y += sarea * sc.y;
   7584             var shapeDensity = 0;
   7585             if (this.useDensity) {
   7586                shapeDensity = 1;
   7587             }
   7588             else {
   7589                shapeDensity = 1;
   7590             }
   7591             mass += sarea * shapeDensity;
   7592             massc.x += sarea * sc.x * shapeDensity;
   7593             massc.y += sarea * sc.y * shapeDensity;
   7594          }
   7595          areac.x /= area;
   7596          areac.y /= area;
   7597          massc.x /= mass;
   7598          massc.y /= mass;
   7599          if (area < Number.MIN_VALUE) continue;
   7600          var buoyancyForce = this.gravity.GetNegative();
   7601          buoyancyForce.Multiply(this.density * area);
   7602          body.ApplyForce(buoyancyForce, massc);
   7603          var dragForce = body.GetLinearVelocityFromWorldPoint(areac);
   7604          dragForce.Subtract(this.velocity);
   7605          dragForce.Multiply((-this.linearDrag * area));
   7606          body.ApplyForce(dragForce, areac);
   7607          body.ApplyTorque((-body.GetInertia() / body.GetMass() * area * body.GetAngularVelocity() * this.angularDrag));
   7608       }
   7609    }
   7610    b2BuoyancyController.prototype.Draw = function (debugDraw) {
   7611       var r = 1000;
   7612       var p1 = new b2Vec2();
   7613       var p2 = new b2Vec2();
   7614       p1.x = this.normal.x * this.offset + this.normal.y * r;
   7615       p1.y = this.normal.y * this.offset - this.normal.x * r;
   7616       p2.x = this.normal.x * this.offset - this.normal.y * r;
   7617       p2.y = this.normal.y * this.offset + this.normal.x * r;
   7618       var color = new b2Color(0, 0, 1);
   7619       debugDraw.DrawSegment(p1, p2, color);
   7620    }
   7621    Box2D.inherit(b2ConstantAccelController, Box2D.Dynamics.Controllers.b2Controller);
   7622    b2ConstantAccelController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
   7623    b2ConstantAccelController.b2ConstantAccelController = function () {
   7624       Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
   7625       this.A = new b2Vec2(0, 0);
   7626    };
   7627    b2ConstantAccelController.prototype.Step = function (step) {
   7628       var smallA = new b2Vec2(this.A.x * step.dt, this.A.y * step.dt);
   7629       for (var i = this.m_bodyList; i; i = i.nextBody) {
   7630          var body = i.body;
   7631          if (!body.IsAwake()) continue;
   7632          body.SetLinearVelocity(new b2Vec2(body.GetLinearVelocity().x + smallA.x, body.GetLinearVelocity().y + smallA.y));
   7633       }
   7634    }
   7635    Box2D.inherit(b2ConstantForceController, Box2D.Dynamics.Controllers.b2Controller);
   7636    b2ConstantForceController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
   7637    b2ConstantForceController.b2ConstantForceController = function () {
   7638       Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
   7639       this.F = new b2Vec2(0, 0);
   7640    };
   7641    b2ConstantForceController.prototype.Step = function (step) {
   7642       for (var i = this.m_bodyList; i; i = i.nextBody) {
   7643          var body = i.body;
   7644          if (!body.IsAwake()) continue;
   7645          body.ApplyForce(this.F, body.GetWorldCenter());
   7646       }
   7647    }
   7648    b2Controller.b2Controller = function () {};
   7649    b2Controller.prototype.Step = function (step) {}
   7650    b2Controller.prototype.Draw = function (debugDraw) {}
   7651    b2Controller.prototype.AddBody = function (body) {
   7652       var edge = new b2ControllerEdge();
   7653       edge.controller = this;
   7654       edge.body = body;
   7655       edge.nextBody = this.m_bodyList;
   7656       edge.prevBody = null;
   7657       this.m_bodyList = edge;
   7658       if (edge.nextBody) edge.nextBody.prevBody = edge;
   7659       this.m_bodyCount++;
   7660       edge.nextController = body.m_controllerList;
   7661       edge.prevController = null;
   7662       body.m_controllerList = edge;
   7663       if (edge.nextController) edge.nextController.prevController = edge;
   7664       body.m_controllerCount++;
   7665    }
   7666    b2Controller.prototype.RemoveBody = function (body) {
   7667       var edge = body.m_controllerList;
   7668       while (edge && edge.controller != this)
   7669       edge = edge.nextController;
   7670       if (edge.prevBody) edge.prevBody.nextBody = edge.nextBody;
   7671       if (edge.nextBody) edge.nextBody.prevBody = edge.prevBody;
   7672       if (edge.nextController) edge.nextController.prevController = edge.prevController;
   7673       if (edge.prevController) edge.prevController.nextController = edge.nextController;
   7674       if (this.m_bodyList == edge) this.m_bodyList = edge.nextBody;
   7675       if (body.m_controllerList == edge) body.m_controllerList = edge.nextController;
   7676       body.m_controllerCount--;
   7677       this.m_bodyCount--;
   7678    }
   7679    b2Controller.prototype.Clear = function () {
   7680       while (this.m_bodyList)
   7681       this.RemoveBody(this.m_bodyList.body);
   7682    }
   7683    b2Controller.prototype.GetNext = function () {
   7684       return this.m_next;
   7685    }
   7686    b2Controller.prototype.GetWorld = function () {
   7687       return this.m_world;
   7688    }
   7689    b2Controller.prototype.GetBodyList = function () {
   7690       return this.m_bodyList;
   7691    }
   7692    b2ControllerEdge.b2ControllerEdge = function () {};
   7693    Box2D.inherit(b2GravityController, Box2D.Dynamics.Controllers.b2Controller);
   7694    b2GravityController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
   7695    b2GravityController.b2GravityController = function () {
   7696       Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
   7697       this.G = 1;
   7698       this.invSqr = true;
   7699    };
   7700    b2GravityController.prototype.Step = function (step) {
   7701       var i = null;
   7702       var body1 = null;
   7703       var p1 = null;
   7704       var mass1 = 0;
   7705       var j = null;
   7706       var body2 = null;
   7707       var p2 = null;
   7708       var dx = 0;
   7709       var dy = 0;
   7710       var r2 = 0;
   7711       var f = null;
   7712       if (this.invSqr) {
   7713          for (i = this.m_bodyList;
   7714          i; i = i.nextBody) {
   7715             body1 = i.body;
   7716             p1 = body1.GetWorldCenter();
   7717             mass1 = body1.GetMass();
   7718             for (j = this.m_bodyList;
   7719             j != i; j = j.nextBody) {
   7720                body2 = j.body;
   7721                p2 = body2.GetWorldCenter();
   7722                dx = p2.x - p1.x;
   7723                dy = p2.y - p1.y;
   7724                r2 = dx * dx + dy * dy;
   7725                if (r2 < Number.MIN_VALUE) continue;
   7726                f = new b2Vec2(dx, dy);
   7727                f.Multiply(this.G / r2 / Math.sqrt(r2) * mass1 * body2.GetMass());
   7728                if (body1.IsAwake()) body1.ApplyForce(f, p1);
   7729                f.Multiply((-1));
   7730                if (body2.IsAwake()) body2.ApplyForce(f, p2);
   7731             }
   7732          }
   7733       }
   7734       else {
   7735          for (i = this.m_bodyList;
   7736          i; i = i.nextBody) {
   7737             body1 = i.body;
   7738             p1 = body1.GetWorldCenter();
   7739             mass1 = body1.GetMass();
   7740             for (j = this.m_bodyList;
   7741             j != i; j = j.nextBody) {
   7742                body2 = j.body;
   7743                p2 = body2.GetWorldCenter();
   7744                dx = p2.x - p1.x;
   7745                dy = p2.y - p1.y;
   7746                r2 = dx * dx + dy * dy;
   7747                if (r2 < Number.MIN_VALUE) continue;
   7748                f = new b2Vec2(dx, dy);
   7749                f.Multiply(this.G / r2 * mass1 * body2.GetMass());
   7750                if (body1.IsAwake()) body1.ApplyForce(f, p1);
   7751                f.Multiply((-1));
   7752                if (body2.IsAwake()) body2.ApplyForce(f, p2);
   7753             }
   7754          }
   7755       }
   7756    }
   7757    Box2D.inherit(b2TensorDampingController, Box2D.Dynamics.Controllers.b2Controller);
   7758    b2TensorDampingController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
   7759    b2TensorDampingController.b2TensorDampingController = function () {
   7760       Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
   7761       this.T = new b2Mat22();
   7762       this.maxTimestep = 0;
   7763    };
   7764    b2TensorDampingController.prototype.SetAxisAligned = function (xDamping, yDamping) {
   7765       if (xDamping === undefined) xDamping = 0;
   7766       if (yDamping === undefined) yDamping = 0;
   7767       this.T.col1.x = (-xDamping);
   7768       this.T.col1.y = 0;
   7769       this.T.col2.x = 0;
   7770       this.T.col2.y = (-yDamping);
   7771       if (xDamping > 0 || yDamping > 0) {
   7772          this.maxTimestep = 1 / Math.max(xDamping, yDamping);
   7773       }
   7774       else {
   7775          this.maxTimestep = 0;
   7776       }
   7777    }
   7778    b2TensorDampingController.prototype.Step = function (step) {
   7779       var timestep = step.dt;
   7780       if (timestep <= Number.MIN_VALUE) return;
   7781       if (timestep > this.maxTimestep && this.maxTimestep > 0) timestep = this.maxTimestep;
   7782       for (var i = this.m_bodyList; i; i = i.nextBody) {
   7783          var body = i.body;
   7784          if (!body.IsAwake()) {
   7785             continue;
   7786          }
   7787          var damping = body.GetWorldVector(b2Math.MulMV(this.T, body.GetLocalVector(body.GetLinearVelocity())));
   7788          body.SetLinearVelocity(new b2Vec2(body.GetLinearVelocity().x + damping.x * timestep, body.GetLinearVelocity().y + damping.y * timestep));
   7789       }
   7790    }
   7791 })();
   7792 (function () {
   7793    var b2Color = Box2D.Common.b2Color,
   7794       b2internal = Box2D.Common.b2internal,
   7795       b2Settings = Box2D.Common.b2Settings,
   7796       b2Mat22 = Box2D.Common.Math.b2Mat22,
   7797       b2Mat33 = Box2D.Common.Math.b2Mat33,
   7798       b2Math = Box2D.Common.Math.b2Math,
   7799       b2Sweep = Box2D.Common.Math.b2Sweep,
   7800       b2Transform = Box2D.Common.Math.b2Transform,
   7801       b2Vec2 = Box2D.Common.Math.b2Vec2,
   7802       b2Vec3 = Box2D.Common.Math.b2Vec3,
   7803       b2DistanceJoint = Box2D.Dynamics.Joints.b2DistanceJoint,
   7804       b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef,
   7805       b2FrictionJoint = Box2D.Dynamics.Joints.b2FrictionJoint,
   7806       b2FrictionJointDef = Box2D.Dynamics.Joints.b2FrictionJointDef,
   7807       b2GearJoint = Box2D.Dynamics.Joints.b2GearJoint,
   7808       b2GearJointDef = Box2D.Dynamics.Joints.b2GearJointDef,
   7809       b2Jacobian = Box2D.Dynamics.Joints.b2Jacobian,
   7810       b2Joint = Box2D.Dynamics.Joints.b2Joint,
   7811       b2JointDef = Box2D.Dynamics.Joints.b2JointDef,
   7812       b2JointEdge = Box2D.Dynamics.Joints.b2JointEdge,
   7813       b2LineJoint = Box2D.Dynamics.Joints.b2LineJoint,
   7814       b2LineJointDef = Box2D.Dynamics.Joints.b2LineJointDef,
   7815       b2MouseJoint = Box2D.Dynamics.Joints.b2MouseJoint,
   7816       b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef,
   7817       b2PrismaticJoint = Box2D.Dynamics.Joints.b2PrismaticJoint,
   7818       b2PrismaticJointDef = Box2D.Dynamics.Joints.b2PrismaticJointDef,
   7819       b2PulleyJoint = Box2D.Dynamics.Joints.b2PulleyJoint,
   7820       b2PulleyJointDef = Box2D.Dynamics.Joints.b2PulleyJointDef,
   7821       b2RevoluteJoint = Box2D.Dynamics.Joints.b2RevoluteJoint,
   7822       b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef,
   7823       b2WeldJoint = Box2D.Dynamics.Joints.b2WeldJoint,
   7824       b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDef,
   7825       b2Body = Box2D.Dynamics.b2Body,
   7826       b2BodyDef = Box2D.Dynamics.b2BodyDef,
   7827       b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
   7828       b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
   7829       b2ContactListener = Box2D.Dynamics.b2ContactListener,
   7830       b2ContactManager = Box2D.Dynamics.b2ContactManager,
   7831       b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
   7832       b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
   7833       b2FilterData = Box2D.Dynamics.b2FilterData,
   7834       b2Fixture = Box2D.Dynamics.b2Fixture,
   7835       b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
   7836       b2Island = Box2D.Dynamics.b2Island,
   7837       b2TimeStep = Box2D.Dynamics.b2TimeStep,
   7838       b2World = Box2D.Dynamics.b2World;
   7839 
   7840    Box2D.inherit(b2DistanceJoint, Box2D.Dynamics.Joints.b2Joint);
   7841    b2DistanceJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
   7842    b2DistanceJoint.b2DistanceJoint = function () {
   7843       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
   7844       this.m_localAnchor1 = new b2Vec2();
   7845       this.m_localAnchor2 = new b2Vec2();
   7846       this.m_u = new b2Vec2();
   7847    };
   7848    b2DistanceJoint.prototype.GetAnchorA = function () {
   7849       return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
   7850    }
   7851    b2DistanceJoint.prototype.GetAnchorB = function () {
   7852       return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
   7853    }
   7854    b2DistanceJoint.prototype.GetReactionForce = function (inv_dt) {
   7855       if (inv_dt === undefined) inv_dt = 0;
   7856       return new b2Vec2(inv_dt * this.m_impulse * this.m_u.x, inv_dt * this.m_impulse * this.m_u.y);
   7857    }
   7858    b2DistanceJoint.prototype.GetReactionTorque = function (inv_dt) {
   7859       if (inv_dt === undefined) inv_dt = 0;
   7860       return 0.0;
   7861    }
   7862    b2DistanceJoint.prototype.GetLength = function () {
   7863       return this.m_length;
   7864    }
   7865    b2DistanceJoint.prototype.SetLength = function (length) {
   7866       if (length === undefined) length = 0;
   7867       this.m_length = length;
   7868    }
   7869    b2DistanceJoint.prototype.GetFrequency = function () {
   7870       return this.m_frequencyHz;
   7871    }
   7872    b2DistanceJoint.prototype.SetFrequency = function (hz) {
   7873       if (hz === undefined) hz = 0;
   7874       this.m_frequencyHz = hz;
   7875    }
   7876    b2DistanceJoint.prototype.GetDampingRatio = function () {
   7877       return this.m_dampingRatio;
   7878    }
   7879    b2DistanceJoint.prototype.SetDampingRatio = function (ratio) {
   7880       if (ratio === undefined) ratio = 0;
   7881       this.m_dampingRatio = ratio;
   7882    }
   7883    b2DistanceJoint.prototype.b2DistanceJoint = function (def) {
   7884       this.__super.b2Joint.call(this, def);
   7885       var tMat;
   7886       var tX = 0;
   7887       var tY = 0;
   7888       this.m_localAnchor1.SetV(def.localAnchorA);
   7889       this.m_localAnchor2.SetV(def.localAnchorB);
   7890       this.m_length = def.length;
   7891       this.m_frequencyHz = def.frequencyHz;
   7892       this.m_dampingRatio = def.dampingRatio;
   7893       this.m_impulse = 0.0;
   7894       this.m_gamma = 0.0;
   7895       this.m_bias = 0.0;
   7896    }
   7897    b2DistanceJoint.prototype.InitVelocityConstraints = function (step) {
   7898       var tMat;
   7899       var tX = 0;
   7900       var bA = this.m_bodyA;
   7901       var bB = this.m_bodyB;
   7902       tMat = bA.m_xf.R;
   7903       var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   7904       var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   7905       tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   7906       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   7907       r1X = tX;
   7908       tMat = bB.m_xf.R;
   7909       var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   7910       var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   7911       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   7912       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   7913       r2X = tX;
   7914       this.m_u.x = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
   7915       this.m_u.y = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
   7916       var length = Math.sqrt(this.m_u.x * this.m_u.x + this.m_u.y * this.m_u.y);
   7917       if (length > b2Settings.b2_linearSlop) {
   7918          this.m_u.Multiply(1.0 / length);
   7919       }
   7920       else {
   7921          this.m_u.SetZero();
   7922       }
   7923       var cr1u = (r1X * this.m_u.y - r1Y * this.m_u.x);
   7924       var cr2u = (r2X * this.m_u.y - r2Y * this.m_u.x);
   7925       var invMass = bA.m_invMass + bA.m_invI * cr1u * cr1u + bB.m_invMass + bB.m_invI * cr2u * cr2u;
   7926       this.m_mass = invMass != 0.0 ? 1.0 / invMass : 0.0;
   7927       if (this.m_frequencyHz > 0.0) {
   7928          var C = length - this.m_length;
   7929          var omega = 2.0 * Math.PI * this.m_frequencyHz;
   7930          var d = 2.0 * this.m_mass * this.m_dampingRatio * omega;
   7931          var k = this.m_mass * omega * omega;
   7932          this.m_gamma = step.dt * (d + step.dt * k);
   7933          this.m_gamma = this.m_gamma != 0.0 ? 1 / this.m_gamma : 0.0;
   7934          this.m_bias = C * step.dt * k * this.m_gamma;
   7935          this.m_mass = invMass + this.m_gamma;
   7936          this.m_mass = this.m_mass != 0.0 ? 1.0 / this.m_mass : 0.0;
   7937       }
   7938       if (step.warmStarting) {
   7939          this.m_impulse *= step.dtRatio;
   7940          var PX = this.m_impulse * this.m_u.x;
   7941          var PY = this.m_impulse * this.m_u.y;
   7942          bA.m_linearVelocity.x -= bA.m_invMass * PX;
   7943          bA.m_linearVelocity.y -= bA.m_invMass * PY;
   7944          bA.m_angularVelocity -= bA.m_invI * (r1X * PY - r1Y * PX);
   7945          bB.m_linearVelocity.x += bB.m_invMass * PX;
   7946          bB.m_linearVelocity.y += bB.m_invMass * PY;
   7947          bB.m_angularVelocity += bB.m_invI * (r2X * PY - r2Y * PX);
   7948       }
   7949       else {
   7950          this.m_impulse = 0.0;
   7951       }
   7952    }
   7953    b2DistanceJoint.prototype.SolveVelocityConstraints = function (step) {
   7954       var tMat;
   7955       var bA = this.m_bodyA;
   7956       var bB = this.m_bodyB;
   7957       tMat = bA.m_xf.R;
   7958       var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   7959       var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   7960       var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   7961       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   7962       r1X = tX;
   7963       tMat = bB.m_xf.R;
   7964       var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   7965       var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   7966       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   7967       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   7968       r2X = tX;
   7969       var v1X = bA.m_linearVelocity.x + ((-bA.m_angularVelocity * r1Y));
   7970       var v1Y = bA.m_linearVelocity.y + (bA.m_angularVelocity * r1X);
   7971       var v2X = bB.m_linearVelocity.x + ((-bB.m_angularVelocity * r2Y));
   7972       var v2Y = bB.m_linearVelocity.y + (bB.m_angularVelocity * r2X);
   7973       var Cdot = (this.m_u.x * (v2X - v1X) + this.m_u.y * (v2Y - v1Y));
   7974       var impulse = (-this.m_mass * (Cdot + this.m_bias + this.m_gamma * this.m_impulse));
   7975       this.m_impulse += impulse;
   7976       var PX = impulse * this.m_u.x;
   7977       var PY = impulse * this.m_u.y;
   7978       bA.m_linearVelocity.x -= bA.m_invMass * PX;
   7979       bA.m_linearVelocity.y -= bA.m_invMass * PY;
   7980       bA.m_angularVelocity -= bA.m_invI * (r1X * PY - r1Y * PX);
   7981       bB.m_linearVelocity.x += bB.m_invMass * PX;
   7982       bB.m_linearVelocity.y += bB.m_invMass * PY;
   7983       bB.m_angularVelocity += bB.m_invI * (r2X * PY - r2Y * PX);
   7984    }
   7985    b2DistanceJoint.prototype.SolvePositionConstraints = function (baumgarte) {
   7986       if (baumgarte === undefined) baumgarte = 0;
   7987       var tMat;
   7988       if (this.m_frequencyHz > 0.0) {
   7989          return true;
   7990       }
   7991       var bA = this.m_bodyA;
   7992       var bB = this.m_bodyB;
   7993       tMat = bA.m_xf.R;
   7994       var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   7995       var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   7996       var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   7997       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   7998       r1X = tX;
   7999       tMat = bB.m_xf.R;
   8000       var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   8001       var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   8002       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   8003       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   8004       r2X = tX;
   8005       var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
   8006       var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
   8007       var length = Math.sqrt(dX * dX + dY * dY);
   8008       dX /= length;
   8009       dY /= length;
   8010       var C = length - this.m_length;
   8011       C = b2Math.Clamp(C, (-b2Settings.b2_maxLinearCorrection), b2Settings.b2_maxLinearCorrection);
   8012       var impulse = (-this.m_mass * C);
   8013       this.m_u.Set(dX, dY);
   8014       var PX = impulse * this.m_u.x;
   8015       var PY = impulse * this.m_u.y;
   8016       bA.m_sweep.c.x -= bA.m_invMass * PX;
   8017       bA.m_sweep.c.y -= bA.m_invMass * PY;
   8018       bA.m_sweep.a -= bA.m_invI * (r1X * PY - r1Y * PX);
   8019       bB.m_sweep.c.x += bB.m_invMass * PX;
   8020       bB.m_sweep.c.y += bB.m_invMass * PY;
   8021       bB.m_sweep.a += bB.m_invI * (r2X * PY - r2Y * PX);
   8022       bA.SynchronizeTransform();
   8023       bB.SynchronizeTransform();
   8024       return b2Math.Abs(C) < b2Settings.b2_linearSlop;
   8025    }
   8026    Box2D.inherit(b2DistanceJointDef, Box2D.Dynamics.Joints.b2JointDef);
   8027    b2DistanceJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
   8028    b2DistanceJointDef.b2DistanceJointDef = function () {
   8029       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
   8030       this.localAnchorA = new b2Vec2();
   8031       this.localAnchorB = new b2Vec2();
   8032    };
   8033    b2DistanceJointDef.prototype.b2DistanceJointDef = function () {
   8034       this.__super.b2JointDef.call(this);
   8035       this.type = b2Joint.e_distanceJoint;
   8036       this.length = 1.0;
   8037       this.frequencyHz = 0.0;
   8038       this.dampingRatio = 0.0;
   8039    }
   8040    b2DistanceJointDef.prototype.Initialize = function (bA, bB, anchorA, anchorB) {
   8041       this.bodyA = bA;
   8042       this.bodyB = bB;
   8043       this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchorA));
   8044       this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchorB));
   8045       var dX = anchorB.x - anchorA.x;
   8046       var dY = anchorB.y - anchorA.y;
   8047       this.length = Math.sqrt(dX * dX + dY * dY);
   8048       this.frequencyHz = 0.0;
   8049       this.dampingRatio = 0.0;
   8050    }
   8051    Box2D.inherit(b2FrictionJoint, Box2D.Dynamics.Joints.b2Joint);
   8052    b2FrictionJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
   8053    b2FrictionJoint.b2FrictionJoint = function () {
   8054       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
   8055       this.m_localAnchorA = new b2Vec2();
   8056       this.m_localAnchorB = new b2Vec2();
   8057       this.m_linearMass = new b2Mat22();
   8058       this.m_linearImpulse = new b2Vec2();
   8059    };
   8060    b2FrictionJoint.prototype.GetAnchorA = function () {
   8061       return this.m_bodyA.GetWorldPoint(this.m_localAnchorA);
   8062    }
   8063    b2FrictionJoint.prototype.GetAnchorB = function () {
   8064       return this.m_bodyB.GetWorldPoint(this.m_localAnchorB);
   8065    }
   8066    b2FrictionJoint.prototype.GetReactionForce = function (inv_dt) {
   8067       if (inv_dt === undefined) inv_dt = 0;
   8068       return new b2Vec2(inv_dt * this.m_linearImpulse.x, inv_dt * this.m_linearImpulse.y);
   8069    }
   8070    b2FrictionJoint.prototype.GetReactionTorque = function (inv_dt) {
   8071       if (inv_dt === undefined) inv_dt = 0;
   8072       return inv_dt * this.m_angularImpulse;
   8073    }
   8074    b2FrictionJoint.prototype.SetMaxForce = function (force) {
   8075       if (force === undefined) force = 0;
   8076       this.m_maxForce = force;
   8077    }
   8078    b2FrictionJoint.prototype.GetMaxForce = function () {
   8079       return this.m_maxForce;
   8080    }
   8081    b2FrictionJoint.prototype.SetMaxTorque = function (torque) {
   8082       if (torque === undefined) torque = 0;
   8083       this.m_maxTorque = torque;
   8084    }
   8085    b2FrictionJoint.prototype.GetMaxTorque = function () {
   8086       return this.m_maxTorque;
   8087    }
   8088    b2FrictionJoint.prototype.b2FrictionJoint = function (def) {
   8089       this.__super.b2Joint.call(this, def);
   8090       this.m_localAnchorA.SetV(def.localAnchorA);
   8091       this.m_localAnchorB.SetV(def.localAnchorB);
   8092       this.m_linearMass.SetZero();
   8093       this.m_angularMass = 0.0;
   8094       this.m_linearImpulse.SetZero();
   8095       this.m_angularImpulse = 0.0;
   8096       this.m_maxForce = def.maxForce;
   8097       this.m_maxTorque = def.maxTorque;
   8098    }
   8099    b2FrictionJoint.prototype.InitVelocityConstraints = function (step) {
   8100       var tMat;
   8101       var tX = 0;
   8102       var bA = this.m_bodyA;
   8103       var bB = this.m_bodyB;
   8104       tMat = bA.m_xf.R;
   8105       var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
   8106       var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
   8107       tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
   8108       rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
   8109       rAX = tX;
   8110       tMat = bB.m_xf.R;
   8111       var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
   8112       var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
   8113       tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
   8114       rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
   8115       rBX = tX;
   8116       var mA = bA.m_invMass;
   8117       var mB = bB.m_invMass;
   8118       var iA = bA.m_invI;
   8119       var iB = bB.m_invI;
   8120       var K = new b2Mat22();
   8121       K.col1.x = mA + mB;
   8122       K.col2.x = 0.0;
   8123       K.col1.y = 0.0;
   8124       K.col2.y = mA + mB;
   8125       K.col1.x += iA * rAY * rAY;
   8126       K.col2.x += (-iA * rAX * rAY);
   8127       K.col1.y += (-iA * rAX * rAY);
   8128       K.col2.y += iA * rAX * rAX;
   8129       K.col1.x += iB * rBY * rBY;
   8130       K.col2.x += (-iB * rBX * rBY);
   8131       K.col1.y += (-iB * rBX * rBY);
   8132       K.col2.y += iB * rBX * rBX;
   8133       K.GetInverse(this.m_linearMass);
   8134       this.m_angularMass = iA + iB;
   8135       if (this.m_angularMass > 0.0) {
   8136          this.m_angularMass = 1.0 / this.m_angularMass;
   8137       }
   8138       if (step.warmStarting) {
   8139          this.m_linearImpulse.x *= step.dtRatio;
   8140          this.m_linearImpulse.y *= step.dtRatio;
   8141          this.m_angularImpulse *= step.dtRatio;
   8142          var P = this.m_linearImpulse;
   8143          bA.m_linearVelocity.x -= mA * P.x;
   8144          bA.m_linearVelocity.y -= mA * P.y;
   8145          bA.m_angularVelocity -= iA * (rAX * P.y - rAY * P.x + this.m_angularImpulse);
   8146          bB.m_linearVelocity.x += mB * P.x;
   8147          bB.m_linearVelocity.y += mB * P.y;
   8148          bB.m_angularVelocity += iB * (rBX * P.y - rBY * P.x + this.m_angularImpulse);
   8149       }
   8150       else {
   8151          this.m_linearImpulse.SetZero();
   8152          this.m_angularImpulse = 0.0;
   8153       }
   8154    }
   8155    b2FrictionJoint.prototype.SolveVelocityConstraints = function (step) {
   8156       var tMat;
   8157       var tX = 0;
   8158       var bA = this.m_bodyA;
   8159       var bB = this.m_bodyB;
   8160       var vA = bA.m_linearVelocity;
   8161       var wA = bA.m_angularVelocity;
   8162       var vB = bB.m_linearVelocity;
   8163       var wB = bB.m_angularVelocity;
   8164       var mA = bA.m_invMass;
   8165       var mB = bB.m_invMass;
   8166       var iA = bA.m_invI;
   8167       var iB = bB.m_invI;
   8168       tMat = bA.m_xf.R;
   8169       var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
   8170       var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
   8171       tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
   8172       rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
   8173       rAX = tX;
   8174       tMat = bB.m_xf.R;
   8175       var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
   8176       var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
   8177       tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
   8178       rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
   8179       rBX = tX;
   8180       var maxImpulse = 0; {
   8181          var Cdot = wB - wA;
   8182          var impulse = (-this.m_angularMass * Cdot);
   8183          var oldImpulse = this.m_angularImpulse;
   8184          maxImpulse = step.dt * this.m_maxTorque;
   8185          this.m_angularImpulse = b2Math.Clamp(this.m_angularImpulse + impulse, (-maxImpulse), maxImpulse);
   8186          impulse = this.m_angularImpulse - oldImpulse;
   8187          wA -= iA * impulse;
   8188          wB += iB * impulse;
   8189       } {
   8190          var CdotX = vB.x - wB * rBY - vA.x + wA * rAY;
   8191          var CdotY = vB.y + wB * rBX - vA.y - wA * rAX;
   8192          var impulseV = b2Math.MulMV(this.m_linearMass, new b2Vec2((-CdotX), (-CdotY)));
   8193          var oldImpulseV = this.m_linearImpulse.Copy();
   8194          this.m_linearImpulse.Add(impulseV);
   8195          maxImpulse = step.dt * this.m_maxForce;
   8196          if (this.m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse) {
   8197             this.m_linearImpulse.Normalize();
   8198             this.m_linearImpulse.Multiply(maxImpulse);
   8199          }
   8200          impulseV = b2Math.SubtractVV(this.m_linearImpulse, oldImpulseV);
   8201          vA.x -= mA * impulseV.x;
   8202          vA.y -= mA * impulseV.y;
   8203          wA -= iA * (rAX * impulseV.y - rAY * impulseV.x);
   8204          vB.x += mB * impulseV.x;
   8205          vB.y += mB * impulseV.y;
   8206          wB += iB * (rBX * impulseV.y - rBY * impulseV.x);
   8207       }
   8208       bA.m_angularVelocity = wA;
   8209       bB.m_angularVelocity = wB;
   8210    }
   8211    b2FrictionJoint.prototype.SolvePositionConstraints = function (baumgarte) {
   8212       if (baumgarte === undefined) baumgarte = 0;
   8213       return true;
   8214    }
   8215    Box2D.inherit(b2FrictionJointDef, Box2D.Dynamics.Joints.b2JointDef);
   8216    b2FrictionJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
   8217    b2FrictionJointDef.b2FrictionJointDef = function () {
   8218       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
   8219       this.localAnchorA = new b2Vec2();
   8220       this.localAnchorB = new b2Vec2();
   8221    };
   8222    b2FrictionJointDef.prototype.b2FrictionJointDef = function () {
   8223       this.__super.b2JointDef.call(this);
   8224       this.type = b2Joint.e_frictionJoint;
   8225       this.maxForce = 0.0;
   8226       this.maxTorque = 0.0;
   8227    }
   8228    b2FrictionJointDef.prototype.Initialize = function (bA, bB, anchor) {
   8229       this.bodyA = bA;
   8230       this.bodyB = bB;
   8231       this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchor));
   8232       this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchor));
   8233    }
   8234    Box2D.inherit(b2GearJoint, Box2D.Dynamics.Joints.b2Joint);
   8235    b2GearJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
   8236    b2GearJoint.b2GearJoint = function () {
   8237       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
   8238       this.m_groundAnchor1 = new b2Vec2();
   8239       this.m_groundAnchor2 = new b2Vec2();
   8240       this.m_localAnchor1 = new b2Vec2();
   8241       this.m_localAnchor2 = new b2Vec2();
   8242       this.m_J = new b2Jacobian();
   8243    };
   8244    b2GearJoint.prototype.GetAnchorA = function () {
   8245       return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
   8246    }
   8247    b2GearJoint.prototype.GetAnchorB = function () {
   8248       return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
   8249    }
   8250    b2GearJoint.prototype.GetReactionForce = function (inv_dt) {
   8251       if (inv_dt === undefined) inv_dt = 0;
   8252       return new b2Vec2(inv_dt * this.m_impulse * this.m_J.linearB.x, inv_dt * this.m_impulse * this.m_J.linearB.y);
   8253    }
   8254    b2GearJoint.prototype.GetReactionTorque = function (inv_dt) {
   8255       if (inv_dt === undefined) inv_dt = 0;
   8256       var tMat = this.m_bodyB.m_xf.R;
   8257       var rX = this.m_localAnchor1.x - this.m_bodyB.m_sweep.localCenter.x;
   8258       var rY = this.m_localAnchor1.y - this.m_bodyB.m_sweep.localCenter.y;
   8259       var tX = tMat.col1.x * rX + tMat.col2.x * rY;
   8260       rY = tMat.col1.y * rX + tMat.col2.y * rY;
   8261       rX = tX;
   8262       var PX = this.m_impulse * this.m_J.linearB.x;
   8263       var PY = this.m_impulse * this.m_J.linearB.y;
   8264       return inv_dt * (this.m_impulse * this.m_J.angularB - rX * PY + rY * PX);
   8265    }
   8266    b2GearJoint.prototype.GetRatio = function () {
   8267       return this.m_ratio;
   8268    }
   8269    b2GearJoint.prototype.SetRatio = function (ratio) {
   8270       if (ratio === undefined) ratio = 0;
   8271       this.m_ratio = ratio;
   8272    }
   8273    b2GearJoint.prototype.b2GearJoint = function (def) {
   8274       this.__super.b2Joint.call(this, def);
   8275       var type1 = parseInt(def.joint1.m_type);
   8276       var type2 = parseInt(def.joint2.m_type);
   8277       this.m_revolute1 = null;
   8278       this.m_prismatic1 = null;
   8279       this.m_revolute2 = null;
   8280       this.m_prismatic2 = null;
   8281       var coordinate1 = 0;
   8282       var coordinate2 = 0;
   8283       this.m_ground1 = def.joint1.GetBodyA();
   8284       this.m_bodyA = def.joint1.GetBodyB();
   8285       if (type1 == b2Joint.e_revoluteJoint) {
   8286          this.m_revolute1 = (def.joint1 instanceof b2RevoluteJoint ? def.joint1 : null);
   8287          this.m_groundAnchor1.SetV(this.m_revolute1.m_localAnchor1);
   8288          this.m_localAnchor1.SetV(this.m_revolute1.m_localAnchor2);
   8289          coordinate1 = this.m_revolute1.GetJointAngle();
   8290       }
   8291       else {
   8292          this.m_prismatic1 = (def.joint1 instanceof b2PrismaticJoint ? def.joint1 : null);
   8293          this.m_groundAnchor1.SetV(this.m_prismatic1.m_localAnchor1);
   8294          this.m_localAnchor1.SetV(this.m_prismatic1.m_localAnchor2);
   8295          coordinate1 = this.m_prismatic1.GetJointTranslation();
   8296       }
   8297       this.m_ground2 = def.joint2.GetBodyA();
   8298       this.m_bodyB = def.joint2.GetBodyB();
   8299       if (type2 == b2Joint.e_revoluteJoint) {
   8300          this.m_revolute2 = (def.joint2 instanceof b2RevoluteJoint ? def.joint2 : null);
   8301          this.m_groundAnchor2.SetV(this.m_revolute2.m_localAnchor1);
   8302          this.m_localAnchor2.SetV(this.m_revolute2.m_localAnchor2);
   8303          coordinate2 = this.m_revolute2.GetJointAngle();
   8304       }
   8305       else {
   8306          this.m_prismatic2 = (def.joint2 instanceof b2PrismaticJoint ? def.joint2 : null);
   8307          this.m_groundAnchor2.SetV(this.m_prismatic2.m_localAnchor1);
   8308          this.m_localAnchor2.SetV(this.m_prismatic2.m_localAnchor2);
   8309          coordinate2 = this.m_prismatic2.GetJointTranslation();
   8310       }
   8311       this.m_ratio = def.ratio;
   8312       this.m_constant = coordinate1 + this.m_ratio * coordinate2;
   8313       this.m_impulse = 0.0;
   8314    }
   8315    b2GearJoint.prototype.InitVelocityConstraints = function (step) {
   8316       var g1 = this.m_ground1;
   8317       var g2 = this.m_ground2;
   8318       var bA = this.m_bodyA;
   8319       var bB = this.m_bodyB;
   8320       var ugX = 0;
   8321       var ugY = 0;
   8322       var rX = 0;
   8323       var rY = 0;
   8324       var tMat;
   8325       var tVec;
   8326       var crug = 0;
   8327       var tX = 0;
   8328       var K = 0.0;
   8329       this.m_J.SetZero();
   8330       if (this.m_revolute1) {
   8331          this.m_J.angularA = (-1.0);
   8332          K += bA.m_invI;
   8333       }
   8334       else {
   8335          tMat = g1.m_xf.R;
   8336          tVec = this.m_prismatic1.m_localXAxis1;
   8337          ugX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   8338          ugY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   8339          tMat = bA.m_xf.R;
   8340          rX = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   8341          rY = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   8342          tX = tMat.col1.x * rX + tMat.col2.x * rY;
   8343          rY = tMat.col1.y * rX + tMat.col2.y * rY;
   8344          rX = tX;
   8345          crug = rX * ugY - rY * ugX;
   8346          this.m_J.linearA.Set((-ugX), (-ugY));
   8347          this.m_J.angularA = (-crug);
   8348          K += bA.m_invMass + bA.m_invI * crug * crug;
   8349       }
   8350       if (this.m_revolute2) {
   8351          this.m_J.angularB = (-this.m_ratio);
   8352          K += this.m_ratio * this.m_ratio * bB.m_invI;
   8353       }
   8354       else {
   8355          tMat = g2.m_xf.R;
   8356          tVec = this.m_prismatic2.m_localXAxis1;
   8357          ugX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
   8358          ugY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
   8359          tMat = bB.m_xf.R;
   8360          rX = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   8361          rY = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   8362          tX = tMat.col1.x * rX + tMat.col2.x * rY;
   8363          rY = tMat.col1.y * rX + tMat.col2.y * rY;
   8364          rX = tX;
   8365          crug = rX * ugY - rY * ugX;
   8366          this.m_J.linearB.Set((-this.m_ratio * ugX), (-this.m_ratio * ugY));
   8367          this.m_J.angularB = (-this.m_ratio * crug);
   8368          K += this.m_ratio * this.m_ratio * (bB.m_invMass + bB.m_invI * crug * crug);
   8369       }
   8370       this.m_mass = K > 0.0 ? 1.0 / K : 0.0;
   8371       if (step.warmStarting) {
   8372          bA.m_linearVelocity.x += bA.m_invMass * this.m_impulse * this.m_J.linearA.x;
   8373          bA.m_linearVelocity.y += bA.m_invMass * this.m_impulse * this.m_J.linearA.y;
   8374          bA.m_angularVelocity += bA.m_invI * this.m_impulse * this.m_J.angularA;
   8375          bB.m_linearVelocity.x += bB.m_invMass * this.m_impulse * this.m_J.linearB.x;
   8376          bB.m_linearVelocity.y += bB.m_invMass * this.m_impulse * this.m_J.linearB.y;
   8377          bB.m_angularVelocity += bB.m_invI * this.m_impulse * this.m_J.angularB;
   8378       }
   8379       else {
   8380          this.m_impulse = 0.0;
   8381       }
   8382    }
   8383    b2GearJoint.prototype.SolveVelocityConstraints = function (step) {
   8384       var bA = this.m_bodyA;
   8385       var bB = this.m_bodyB;
   8386       var Cdot = this.m_J.Compute(bA.m_linearVelocity, bA.m_angularVelocity, bB.m_linearVelocity, bB.m_angularVelocity);
   8387       var impulse = (-this.m_mass * Cdot);
   8388       this.m_impulse += impulse;
   8389       bA.m_linearVelocity.x += bA.m_invMass * impulse * this.m_J.linearA.x;
   8390       bA.m_linearVelocity.y += bA.m_invMass * impulse * this.m_J.linearA.y;
   8391       bA.m_angularVelocity += bA.m_invI * impulse * this.m_J.angularA;
   8392       bB.m_linearVelocity.x += bB.m_invMass * impulse * this.m_J.linearB.x;
   8393       bB.m_linearVelocity.y += bB.m_invMass * impulse * this.m_J.linearB.y;
   8394       bB.m_angularVelocity += bB.m_invI * impulse * this.m_J.angularB;
   8395    }
   8396    b2GearJoint.prototype.SolvePositionConstraints = function (baumgarte) {
   8397       if (baumgarte === undefined) baumgarte = 0;
   8398       var linearError = 0.0;
   8399       var bA = this.m_bodyA;
   8400       var bB = this.m_bodyB;
   8401       var coordinate1 = 0;
   8402       var coordinate2 = 0;
   8403       if (this.m_revolute1) {
   8404          coordinate1 = this.m_revolute1.GetJointAngle();
   8405       }
   8406       else {
   8407          coordinate1 = this.m_prismatic1.GetJointTranslation();
   8408       }
   8409       if (this.m_revolute2) {
   8410          coordinate2 = this.m_revolute2.GetJointAngle();
   8411       }
   8412       else {
   8413          coordinate2 = this.m_prismatic2.GetJointTranslation();
   8414       }
   8415       var C = this.m_constant - (coordinate1 + this.m_ratio * coordinate2);
   8416       var impulse = (-this.m_mass * C);
   8417       bA.m_sweep.c.x += bA.m_invMass * impulse * this.m_J.linearA.x;
   8418       bA.m_sweep.c.y += bA.m_invMass * impulse * this.m_J.linearA.y;
   8419       bA.m_sweep.a += bA.m_invI * impulse * this.m_J.angularA;
   8420       bB.m_sweep.c.x += bB.m_invMass * impulse * this.m_J.linearB.x;
   8421       bB.m_sweep.c.y += bB.m_invMass * impulse * this.m_J.linearB.y;
   8422       bB.m_sweep.a += bB.m_invI * impulse * this.m_J.angularB;
   8423       bA.SynchronizeTransform();
   8424       bB.SynchronizeTransform();
   8425       return linearError < b2Settings.b2_linearSlop;
   8426    }
   8427    Box2D.inherit(b2GearJointDef, Box2D.Dynamics.Joints.b2JointDef);
   8428    b2GearJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
   8429    b2GearJointDef.b2GearJointDef = function () {
   8430       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
   8431    };
   8432    b2GearJointDef.prototype.b2GearJointDef = function () {
   8433       this.__super.b2JointDef.call(this);
   8434       this.type = b2Joint.e_gearJoint;
   8435       this.joint1 = null;
   8436       this.joint2 = null;
   8437       this.ratio = 1.0;
   8438    }
   8439    b2Jacobian.b2Jacobian = function () {
   8440       this.linearA = new b2Vec2();
   8441       this.linearB = new b2Vec2();
   8442    };
   8443    b2Jacobian.prototype.SetZero = function () {
   8444       this.linearA.SetZero();
   8445       this.angularA = 0.0;
   8446       this.linearB.SetZero();
   8447       this.angularB = 0.0;
   8448    }
   8449    b2Jacobian.prototype.Set = function (x1, a1, x2, a2) {
   8450       if (a1 === undefined) a1 = 0;
   8451       if (a2 === undefined) a2 = 0;
   8452       this.linearA.SetV(x1);
   8453       this.angularA = a1;
   8454       this.linearB.SetV(x2);
   8455       this.angularB = a2;
   8456    }
   8457    b2Jacobian.prototype.Compute = function (x1, a1, x2, a2) {
   8458       if (a1 === undefined) a1 = 0;
   8459       if (a2 === undefined) a2 = 0;
   8460       return (this.linearA.x * x1.x + this.linearA.y * x1.y) + this.angularA * a1 + (this.linearB.x * x2.x + this.linearB.y * x2.y) + this.angularB * a2;
   8461    }
   8462    b2Joint.b2Joint = function () {
   8463       this.m_edgeA = new b2JointEdge();
   8464       this.m_edgeB = new b2JointEdge();
   8465       this.m_localCenterA = new b2Vec2();
   8466       this.m_localCenterB = new b2Vec2();
   8467    };
   8468    b2Joint.prototype.GetType = function () {
   8469       return this.m_type;
   8470    }
   8471    b2Joint.prototype.GetAnchorA = function () {
   8472       return null;
   8473    }
   8474    b2Joint.prototype.GetAnchorB = function () {
   8475       return null;
   8476    }
   8477    b2Joint.prototype.GetReactionForce = function (inv_dt) {
   8478       if (inv_dt === undefined) inv_dt = 0;
   8479       return null;
   8480    }
   8481    b2Joint.prototype.GetReactionTorque = function (inv_dt) {
   8482       if (inv_dt === undefined) inv_dt = 0;
   8483       return 0.0;
   8484    }
   8485    b2Joint.prototype.GetBodyA = function () {
   8486       return this.m_bodyA;
   8487    }
   8488    b2Joint.prototype.GetBodyB = function () {
   8489       return this.m_bodyB;
   8490    }
   8491    b2Joint.prototype.GetNext = function () {
   8492       return this.m_next;
   8493    }
   8494    b2Joint.prototype.GetUserData = function () {
   8495       return this.m_userData;
   8496    }
   8497    b2Joint.prototype.SetUserData = function (data) {
   8498       this.m_userData = data;
   8499    }
   8500    b2Joint.prototype.IsActive = function () {
   8501       return this.m_bodyA.IsActive() && this.m_bodyB.IsActive();
   8502    }
   8503    b2Joint.Create = function (def, allocator) {
   8504       var joint = null;
   8505       switch (def.type) {
   8506       case b2Joint.e_distanceJoint:
   8507          {
   8508             joint = new b2DistanceJoint((def instanceof b2DistanceJointDef ? def : null));
   8509          }
   8510          break;
   8511       case b2Joint.e_mouseJoint:
   8512          {
   8513             joint = new b2MouseJoint((def instanceof b2MouseJointDef ? def : null));
   8514          }
   8515          break;
   8516       case b2Joint.e_prismaticJoint:
   8517          {
   8518             joint = new b2PrismaticJoint((def instanceof b2PrismaticJointDef ? def : null));
   8519          }
   8520          break;
   8521       case b2Joint.e_revoluteJoint:
   8522          {
   8523             joint = new b2RevoluteJoint((def instanceof b2RevoluteJointDef ? def : null));
   8524          }
   8525          break;
   8526       case b2Joint.e_pulleyJoint:
   8527          {
   8528             joint = new b2PulleyJoint((def instanceof b2PulleyJointDef ? def : null));
   8529          }
   8530          break;
   8531       case b2Joint.e_gearJoint:
   8532          {
   8533             joint = new b2GearJoint((def instanceof b2GearJointDef ? def : null));
   8534          }
   8535          break;
   8536       case b2Joint.e_lineJoint:
   8537          {
   8538             joint = new b2LineJoint((def instanceof b2LineJointDef ? def : null));
   8539          }
   8540          break;
   8541       case b2Joint.e_weldJoint:
   8542          {
   8543             joint = new b2WeldJoint((def instanceof b2WeldJointDef ? def : null));
   8544          }
   8545          break;
   8546       case b2Joint.e_frictionJoint:
   8547          {
   8548             joint = new b2FrictionJoint((def instanceof b2FrictionJointDef ? def : null));
   8549          }
   8550          break;
   8551       default:
   8552          break;
   8553       }
   8554       return joint;
   8555    }
   8556    b2Joint.Destroy = function (joint, allocator) {}
   8557    b2Joint.prototype.b2Joint = function (def) {
   8558       b2Settings.b2Assert(def.bodyA != def.bodyB);
   8559       this.m_type = def.type;
   8560       this.m_prev = null;
   8561       this.m_next = null;
   8562       this.m_bodyA = def.bodyA;
   8563       this.m_bodyB = def.bodyB;
   8564       this.m_collideConnected = def.collideConnected;
   8565       this.m_islandFlag = false;
   8566       this.m_userData = def.userData;
   8567    }
   8568    b2Joint.prototype.InitVelocityConstraints = function (step) {}
   8569    b2Joint.prototype.SolveVelocityConstraints = function (step) {}
   8570    b2Joint.prototype.FinalizeVelocityConstraints = function () {}
   8571    b2Joint.prototype.SolvePositionConstraints = function (baumgarte) {
   8572       if (baumgarte === undefined) baumgarte = 0;
   8573       return false;
   8574    }
   8575    Box2D.postDefs.push(function () {
   8576       Box2D.Dynamics.Joints.b2Joint.e_unknownJoint = 0;
   8577       Box2D.Dynamics.Joints.b2Joint.e_revoluteJoint = 1;
   8578       Box2D.Dynamics.Joints.b2Joint.e_prismaticJoint = 2;
   8579       Box2D.Dynamics.Joints.b2Joint.e_distanceJoint = 3;
   8580       Box2D.Dynamics.Joints.b2Joint.e_pulleyJoint = 4;
   8581       Box2D.Dynamics.Joints.b2Joint.e_mouseJoint = 5;
   8582       Box2D.Dynamics.Joints.b2Joint.e_gearJoint = 6;
   8583       Box2D.Dynamics.Joints.b2Joint.e_lineJoint = 7;
   8584       Box2D.Dynamics.Joints.b2Joint.e_weldJoint = 8;
   8585       Box2D.Dynamics.Joints.b2Joint.e_frictionJoint = 9;
   8586       Box2D.Dynamics.Joints.b2Joint.e_inactiveLimit = 0;
   8587       Box2D.Dynamics.Joints.b2Joint.e_atLowerLimit = 1;
   8588       Box2D.Dynamics.Joints.b2Joint.e_atUpperLimit = 2;
   8589       Box2D.Dynamics.Joints.b2Joint.e_equalLimits = 3;
   8590    });
   8591    b2JointDef.b2JointDef = function () {};
   8592    b2JointDef.prototype.b2JointDef = function () {
   8593       this.type = b2Joint.e_unknownJoint;
   8594       this.userData = null;
   8595       this.bodyA = null;
   8596       this.bodyB = null;
   8597       this.collideConnected = false;
   8598    }
   8599    b2JointEdge.b2JointEdge = function () {};
   8600    Box2D.inherit(b2LineJoint, Box2D.Dynamics.Joints.b2Joint);
   8601    b2LineJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
   8602    b2LineJoint.b2LineJoint = function () {
   8603       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
   8604       this.m_localAnchor1 = new b2Vec2();
   8605       this.m_localAnchor2 = new b2Vec2();
   8606       this.m_localXAxis1 = new b2Vec2();
   8607       this.m_localYAxis1 = new b2Vec2();
   8608       this.m_axis = new b2Vec2();
   8609       this.m_perp = new b2Vec2();
   8610       this.m_K = new b2Mat22();
   8611       this.m_impulse = new b2Vec2();
   8612    };
   8613    b2LineJoint.prototype.GetAnchorA = function () {
   8614       return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
   8615    }
   8616    b2LineJoint.prototype.GetAnchorB = function () {
   8617       return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
   8618    }
   8619    b2LineJoint.prototype.GetReactionForce = function (inv_dt) {
   8620       if (inv_dt === undefined) inv_dt = 0;
   8621       return new b2Vec2(inv_dt * (this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.x), inv_dt * (this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.y));
   8622    }
   8623    b2LineJoint.prototype.GetReactionTorque = function (inv_dt) {
   8624       if (inv_dt === undefined) inv_dt = 0;
   8625       return inv_dt * this.m_impulse.y;
   8626    }
   8627    b2LineJoint.prototype.GetJointTranslation = function () {
   8628       var bA = this.m_bodyA;
   8629       var bB = this.m_bodyB;
   8630       var tMat;
   8631       var p1 = bA.GetWorldPoint(this.m_localAnchor1);
   8632       var p2 = bB.GetWorldPoint(this.m_localAnchor2);
   8633       var dX = p2.x - p1.x;
   8634       var dY = p2.y - p1.y;
   8635       var axis = bA.GetWorldVector(this.m_localXAxis1);
   8636       var translation = axis.x * dX + axis.y * dY;
   8637       return translation;
   8638    }
   8639    b2LineJoint.prototype.GetJointSpeed = function () {
   8640       var bA = this.m_bodyA;
   8641       var bB = this.m_bodyB;
   8642       var tMat;
   8643       tMat = bA.m_xf.R;
   8644       var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   8645       var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   8646       var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   8647       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   8648       r1X = tX;
   8649       tMat = bB.m_xf.R;
   8650       var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   8651       var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   8652       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   8653       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   8654       r2X = tX;
   8655       var p1X = bA.m_sweep.c.x + r1X;
   8656       var p1Y = bA.m_sweep.c.y + r1Y;
   8657       var p2X = bB.m_sweep.c.x + r2X;
   8658       var p2Y = bB.m_sweep.c.y + r2Y;
   8659       var dX = p2X - p1X;
   8660       var dY = p2Y - p1Y;
   8661       var axis = bA.GetWorldVector(this.m_localXAxis1);
   8662       var v1 = bA.m_linearVelocity;
   8663       var v2 = bB.m_linearVelocity;
   8664       var w1 = bA.m_angularVelocity;
   8665       var w2 = bB.m_angularVelocity;
   8666       var speed = (dX * ((-w1 * axis.y)) + dY * (w1 * axis.x)) + (axis.x * (((v2.x + ((-w2 * r2Y))) - v1.x) - ((-w1 * r1Y))) + axis.y * (((v2.y + (w2 * r2X)) - v1.y) - (w1 * r1X)));
   8667       return speed;
   8668    }
   8669    b2LineJoint.prototype.IsLimitEnabled = function () {
   8670       return this.m_enableLimit;
   8671    }
   8672    b2LineJoint.prototype.EnableLimit = function (flag) {
   8673       this.m_bodyA.SetAwake(true);
   8674       this.m_bodyB.SetAwake(true);
   8675       this.m_enableLimit = flag;
   8676    }
   8677    b2LineJoint.prototype.GetLowerLimit = function () {
   8678       return this.m_lowerTranslation;
   8679    }
   8680    b2LineJoint.prototype.GetUpperLimit = function () {
   8681       return this.m_upperTranslation;
   8682    }
   8683    b2LineJoint.prototype.SetLimits = function (lower, upper) {
   8684       if (lower === undefined) lower = 0;
   8685       if (upper === undefined) upper = 0;
   8686       this.m_bodyA.SetAwake(true);
   8687       this.m_bodyB.SetAwake(true);
   8688       this.m_lowerTranslation = lower;
   8689       this.m_upperTranslation = upper;
   8690    }
   8691    b2LineJoint.prototype.IsMotorEnabled = function () {
   8692       return this.m_enableMotor;
   8693    }
   8694    b2LineJoint.prototype.EnableMotor = function (flag) {
   8695       this.m_bodyA.SetAwake(true);
   8696       this.m_bodyB.SetAwake(true);
   8697       this.m_enableMotor = flag;
   8698    }
   8699    b2LineJoint.prototype.SetMotorSpeed = function (speed) {
   8700       if (speed === undefined) speed = 0;
   8701       this.m_bodyA.SetAwake(true);
   8702       this.m_bodyB.SetAwake(true);
   8703       this.m_motorSpeed = speed;
   8704    }
   8705    b2LineJoint.prototype.GetMotorSpeed = function () {
   8706       return this.m_motorSpeed;
   8707    }
   8708    b2LineJoint.prototype.SetMaxMotorForce = function (force) {
   8709       if (force === undefined) force = 0;
   8710       this.m_bodyA.SetAwake(true);
   8711       this.m_bodyB.SetAwake(true);
   8712       this.m_maxMotorForce = force;
   8713    }
   8714    b2LineJoint.prototype.GetMaxMotorForce = function () {
   8715       return this.m_maxMotorForce;
   8716    }
   8717    b2LineJoint.prototype.GetMotorForce = function () {
   8718       return this.m_motorImpulse;
   8719    }
   8720    b2LineJoint.prototype.b2LineJoint = function (def) {
   8721       this.__super.b2Joint.call(this, def);
   8722       var tMat;
   8723       var tX = 0;
   8724       var tY = 0;
   8725       this.m_localAnchor1.SetV(def.localAnchorA);
   8726       this.m_localAnchor2.SetV(def.localAnchorB);
   8727       this.m_localXAxis1.SetV(def.localAxisA);
   8728       this.m_localYAxis1.x = (-this.m_localXAxis1.y);
   8729       this.m_localYAxis1.y = this.m_localXAxis1.x;
   8730       this.m_impulse.SetZero();
   8731       this.m_motorMass = 0.0;
   8732       this.m_motorImpulse = 0.0;
   8733       this.m_lowerTranslation = def.lowerTranslation;
   8734       this.m_upperTranslation = def.upperTranslation;
   8735       this.m_maxMotorForce = def.maxMotorForce;
   8736       this.m_motorSpeed = def.motorSpeed;
   8737       this.m_enableLimit = def.enableLimit;
   8738       this.m_enableMotor = def.enableMotor;
   8739       this.m_limitState = b2Joint.e_inactiveLimit;
   8740       this.m_axis.SetZero();
   8741       this.m_perp.SetZero();
   8742    }
   8743    b2LineJoint.prototype.InitVelocityConstraints = function (step) {
   8744       var bA = this.m_bodyA;
   8745       var bB = this.m_bodyB;
   8746       var tMat;
   8747       var tX = 0;
   8748       this.m_localCenterA.SetV(bA.GetLocalCenter());
   8749       this.m_localCenterB.SetV(bB.GetLocalCenter());
   8750       var xf1 = bA.GetTransform();
   8751       var xf2 = bB.GetTransform();
   8752       tMat = bA.m_xf.R;
   8753       var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
   8754       var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
   8755       tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   8756       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   8757       r1X = tX;
   8758       tMat = bB.m_xf.R;
   8759       var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
   8760       var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
   8761       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   8762       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   8763       r2X = tX;
   8764       var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
   8765       var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
   8766       this.m_invMassA = bA.m_invMass;
   8767       this.m_invMassB = bB.m_invMass;
   8768       this.m_invIA = bA.m_invI;
   8769       this.m_invIB = bB.m_invI; {
   8770          this.m_axis.SetV(b2Math.MulMV(xf1.R, this.m_localXAxis1));
   8771          this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
   8772          this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
   8773          this.m_motorMass = this.m_invMassA + this.m_invMassB + this.m_invIA * this.m_a1 * this.m_a1 + this.m_invIB * this.m_a2 * this.m_a2;
   8774          this.m_motorMass = this.m_motorMass > Number.MIN_VALUE ? 1.0 / this.m_motorMass : 0.0;
   8775       } {
   8776          this.m_perp.SetV(b2Math.MulMV(xf1.R, this.m_localYAxis1));
   8777          this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
   8778          this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
   8779          var m1 = this.m_invMassA;
   8780          var m2 = this.m_invMassB;
   8781          var i1 = this.m_invIA;
   8782          var i2 = this.m_invIB;
   8783          this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
   8784          this.m_K.col1.y = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
   8785          this.m_K.col2.x = this.m_K.col1.y;
   8786          this.m_K.col2.y = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
   8787       }
   8788       if (this.m_enableLimit) {
   8789          var jointTransition = this.m_axis.x * dX + this.m_axis.y * dY;
   8790          if (b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2.0 * b2Settings.b2_linearSlop) {
   8791             this.m_limitState = b2Joint.e_equalLimits;
   8792          }
   8793          else if (jointTransition <= this.m_lowerTranslation) {
   8794             if (this.m_limitState != b2Joint.e_atLowerLimit) {
   8795                this.m_limitState = b2Joint.e_atLowerLimit;
   8796                this.m_impulse.y = 0.0;
   8797             }
   8798          }
   8799          else if (jointTransition >= this.m_upperTranslation) {
   8800             if (this.m_limitState != b2Joint.e_atUpperLimit) {
   8801                this.m_limitState = b2Joint.e_atUpperLimit;
   8802                this.m_impulse.y = 0.0;
   8803             }
   8804          }
   8805          else {
   8806             this.m_limitState = b2Joint.e_inactiveLimit;
   8807             this.m_impulse.y = 0.0;
   8808          }
   8809       }
   8810       else {
   8811          this.m_limitState = b2Joint.e_inactiveLimit;
   8812       }
   8813       if (this.m_enableMotor == false) {
   8814          this.m_motorImpulse = 0.0;
   8815       }
   8816       if (step.warmStarting) {
   8817          this.m_impulse.x *= step.dtRatio;
   8818          this.m_impulse.y *= step.dtRatio;
   8819          this.m_motorImpulse *= step.dtRatio;
   8820          var PX = this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.x;
   8821          var PY = this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.y;
   8822          var L1 = this.m_impulse.x * this.m_s1 + (this.m_motorImpulse + this.m_impulse.y) * this.m_a1;
   8823          var L2 = this.m_impulse.x * this.m_s2 + (this.m_motorImpulse + this.m_impulse.y) * this.m_a2;
   8824          bA.m_linearVelocity.x -= this.m_invMassA * PX;
   8825          bA.m_linearVelocity.y -= this.m_invMassA * PY;
   8826          bA.m_angularVelocity -= this.m_invIA * L1;
   8827          bB.m_linearVelocity.x += this.m_invMassB * PX;
   8828          bB.m_linearVelocity.y += this.m_invMassB * PY;
   8829          bB.m_angularVelocity += this.m_invIB * L2;
   8830       }
   8831       else {
   8832          this.m_impulse.SetZero();
   8833          this.m_motorImpulse = 0.0;
   8834       }
   8835    }
   8836    b2LineJoint.prototype.SolveVelocityConstraints = function (step) {
   8837       var bA = this.m_bodyA;
   8838       var bB = this.m_bodyB;
   8839       var v1 = bA.m_linearVelocity;
   8840       var w1 = bA.m_angularVelocity;
   8841       var v2 = bB.m_linearVelocity;
   8842       var w2 = bB.m_angularVelocity;
   8843       var PX = 0;
   8844       var PY = 0;
   8845       var L1 = 0;
   8846       var L2 = 0;
   8847       if (this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
   8848          var Cdot = this.m_axis.x * (v2.x - v1.x) + this.m_axis.y * (v2.y - v1.y) + this.m_a2 * w2 - this.m_a1 * w1;
   8849          var impulse = this.m_motorMass * (this.m_motorSpeed - Cdot);
   8850          var oldImpulse = this.m_motorImpulse;
   8851          var maxImpulse = step.dt * this.m_maxMotorForce;
   8852          this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, (-maxImpulse), maxImpulse);
   8853          impulse = this.m_motorImpulse - oldImpulse;
   8854          PX = impulse * this.m_axis.x;
   8855          PY = impulse * this.m_axis.y;
   8856          L1 = impulse * this.m_a1;
   8857          L2 = impulse * this.m_a2;
   8858          v1.x -= this.m_invMassA * PX;
   8859          v1.y -= this.m_invMassA * PY;
   8860          w1 -= this.m_invIA * L1;
   8861          v2.x += this.m_invMassB * PX;
   8862          v2.y += this.m_invMassB * PY;
   8863          w2 += this.m_invIB * L2;
   8864       }
   8865       var Cdot1 = this.m_perp.x * (v2.x - v1.x) + this.m_perp.y * (v2.y - v1.y) + this.m_s2 * w2 - this.m_s1 * w1;
   8866       if (this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
   8867          var Cdot2 = this.m_axis.x * (v2.x - v1.x) + this.m_axis.y * (v2.y - v1.y) + this.m_a2 * w2 - this.m_a1 * w1;
   8868          var f1 = this.m_impulse.Copy();
   8869          var df = this.m_K.Solve(new b2Vec2(), (-Cdot1), (-Cdot2));
   8870          this.m_impulse.Add(df);
   8871          if (this.m_limitState == b2Joint.e_atLowerLimit) {
   8872             this.m_impulse.y = b2Math.Max(this.m_impulse.y, 0.0);
   8873          }
   8874          else if (this.m_limitState == b2Joint.e_atUpperLimit) {
   8875             this.m_impulse.y = b2Math.Min(this.m_impulse.y, 0.0);
   8876          }
   8877          var b = (-Cdot1) - (this.m_impulse.y - f1.y) * this.m_K.col2.x;
   8878          var f2r = 0;
   8879          if (this.m_K.col1.x != 0.0) {
   8880             f2r = b / this.m_K.col1.x + f1.x;
   8881          }
   8882          else {
   8883             f2r = f1.x;
   8884          }
   8885          this.m_impulse.x = f2r;
   8886          df.x = this.m_impulse.x - f1.x;
   8887          df.y = this.m_impulse.y - f1.y;
   8888          PX = df.x * this.m_perp.x + df.y * this.m_axis.x;
   8889          PY = df.x * this.m_perp.y + df.y * this.m_axis.y;
   8890          L1 = df.x * this.m_s1 + df.y * this.m_a1;
   8891          L2 = df.x * this.m_s2 + df.y * this.m_a2;
   8892          v1.x -= this.m_invMassA * PX;
   8893          v1.y -= this.m_invMassA * PY;
   8894          w1 -= this.m_invIA * L1;
   8895          v2.x += this.m_invMassB * PX;
   8896          v2.y += this.m_invMassB * PY;
   8897          w2 += this.m_invIB * L2;
   8898       }
   8899       else {
   8900          var df2 = 0;
   8901          if (this.m_K.col1.x != 0.0) {
   8902             df2 = ((-Cdot1)) / this.m_K.col1.x;
   8903          }
   8904          else {
   8905             df2 = 0.0;
   8906          }
   8907          this.m_impulse.x += df2;
   8908          PX = df2 * this.m_perp.x;
   8909          PY = df2 * this.m_perp.y;
   8910          L1 = df2 * this.m_s1;
   8911          L2 = df2 * this.m_s2;
   8912          v1.x -= this.m_invMassA * PX;
   8913          v1.y -= this.m_invMassA * PY;
   8914          w1 -= this.m_invIA * L1;
   8915          v2.x += this.m_invMassB * PX;
   8916          v2.y += this.m_invMassB * PY;
   8917          w2 += this.m_invIB * L2;
   8918       }
   8919       bA.m_linearVelocity.SetV(v1);
   8920       bA.m_angularVelocity = w1;
   8921       bB.m_linearVelocity.SetV(v2);
   8922       bB.m_angularVelocity = w2;
   8923    }
   8924    b2LineJoint.prototype.SolvePositionConstraints = function (baumgarte) {
   8925       if (baumgarte === undefined) baumgarte = 0;
   8926       var limitC = 0;
   8927       var oldLimitImpulse = 0;
   8928       var bA = this.m_bodyA;
   8929       var bB = this.m_bodyB;
   8930       var c1 = bA.m_sweep.c;
   8931       var a1 = bA.m_sweep.a;
   8932       var c2 = bB.m_sweep.c;
   8933       var a2 = bB.m_sweep.a;
   8934       var tMat;
   8935       var tX = 0;
   8936       var m1 = 0;
   8937       var m2 = 0;
   8938       var i1 = 0;
   8939       var i2 = 0;
   8940       var linearError = 0.0;
   8941       var angularError = 0.0;
   8942       var active = false;
   8943       var C2 = 0.0;
   8944       var R1 = b2Mat22.FromAngle(a1);
   8945       var R2 = b2Mat22.FromAngle(a2);
   8946       tMat = R1;
   8947       var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
   8948       var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
   8949       tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   8950       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   8951       r1X = tX;
   8952       tMat = R2;
   8953       var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
   8954       var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
   8955       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   8956       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   8957       r2X = tX;
   8958       var dX = c2.x + r2X - c1.x - r1X;
   8959       var dY = c2.y + r2Y - c1.y - r1Y;
   8960       if (this.m_enableLimit) {
   8961          this.m_axis = b2Math.MulMV(R1, this.m_localXAxis1);
   8962          this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
   8963          this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
   8964          var translation = this.m_axis.x * dX + this.m_axis.y * dY;
   8965          if (b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2.0 * b2Settings.b2_linearSlop) {
   8966             C2 = b2Math.Clamp(translation, (-b2Settings.b2_maxLinearCorrection), b2Settings.b2_maxLinearCorrection);
   8967             linearError = b2Math.Abs(translation);
   8968             active = true;
   8969          }
   8970          else if (translation <= this.m_lowerTranslation) {
   8971             C2 = b2Math.Clamp(translation - this.m_lowerTranslation + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
   8972             linearError = this.m_lowerTranslation - translation;
   8973             active = true;
   8974          }
   8975          else if (translation >= this.m_upperTranslation) {
   8976             C2 = b2Math.Clamp(translation - this.m_upperTranslation + b2Settings.b2_linearSlop, 0.0, b2Settings.b2_maxLinearCorrection);
   8977             linearError = translation - this.m_upperTranslation;
   8978             active = true;
   8979          }
   8980       }
   8981       this.m_perp = b2Math.MulMV(R1, this.m_localYAxis1);
   8982       this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
   8983       this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
   8984       var impulse = new b2Vec2();
   8985       var C1 = this.m_perp.x * dX + this.m_perp.y * dY;
   8986       linearError = b2Math.Max(linearError, b2Math.Abs(C1));
   8987       angularError = 0.0;
   8988       if (active) {
   8989          m1 = this.m_invMassA;
   8990          m2 = this.m_invMassB;
   8991          i1 = this.m_invIA;
   8992          i2 = this.m_invIB;
   8993          this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
   8994          this.m_K.col1.y = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
   8995          this.m_K.col2.x = this.m_K.col1.y;
   8996          this.m_K.col2.y = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
   8997          this.m_K.Solve(impulse, (-C1), (-C2));
   8998       }
   8999       else {
   9000          m1 = this.m_invMassA;
   9001          m2 = this.m_invMassB;
   9002          i1 = this.m_invIA;
   9003          i2 = this.m_invIB;
   9004          var k11 = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
   9005          var impulse1 = 0;
   9006          if (k11 != 0.0) {
   9007             impulse1 = ((-C1)) / k11;
   9008          }
   9009          else {
   9010             impulse1 = 0.0;
   9011          }
   9012          impulse.x = impulse1;
   9013          impulse.y = 0.0;
   9014       }
   9015       var PX = impulse.x * this.m_perp.x + impulse.y * this.m_axis.x;
   9016       var PY = impulse.x * this.m_perp.y + impulse.y * this.m_axis.y;
   9017       var L1 = impulse.x * this.m_s1 + impulse.y * this.m_a1;
   9018       var L2 = impulse.x * this.m_s2 + impulse.y * this.m_a2;
   9019       c1.x -= this.m_invMassA * PX;
   9020       c1.y -= this.m_invMassA * PY;
   9021       a1 -= this.m_invIA * L1;
   9022       c2.x += this.m_invMassB * PX;
   9023       c2.y += this.m_invMassB * PY;
   9024       a2 += this.m_invIB * L2;
   9025       bA.m_sweep.a = a1;
   9026       bB.m_sweep.a = a2;
   9027       bA.SynchronizeTransform();
   9028       bB.SynchronizeTransform();
   9029       return linearError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
   9030    }
   9031    Box2D.inherit(b2LineJointDef, Box2D.Dynamics.Joints.b2JointDef);
   9032    b2LineJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
   9033    b2LineJointDef.b2LineJointDef = function () {
   9034       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
   9035       this.localAnchorA = new b2Vec2();
   9036       this.localAnchorB = new b2Vec2();
   9037       this.localAxisA = new b2Vec2();
   9038    };
   9039    b2LineJointDef.prototype.b2LineJointDef = function () {
   9040       this.__super.b2JointDef.call(this);
   9041       this.type = b2Joint.e_lineJoint;
   9042       this.localAxisA.Set(1.0, 0.0);
   9043       this.enableLimit = false;
   9044       this.lowerTranslation = 0.0;
   9045       this.upperTranslation = 0.0;
   9046       this.enableMotor = false;
   9047       this.maxMotorForce = 0.0;
   9048       this.motorSpeed = 0.0;
   9049    }
   9050    b2LineJointDef.prototype.Initialize = function (bA, bB, anchor, axis) {
   9051       this.bodyA = bA;
   9052       this.bodyB = bB;
   9053       this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
   9054       this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
   9055       this.localAxisA = this.bodyA.GetLocalVector(axis);
   9056    }
   9057    Box2D.inherit(b2MouseJoint, Box2D.Dynamics.Joints.b2Joint);
   9058    b2MouseJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
   9059    b2MouseJoint.b2MouseJoint = function () {
   9060       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
   9061       this.K = new b2Mat22();
   9062       this.K1 = new b2Mat22();
   9063       this.K2 = new b2Mat22();
   9064       this.m_localAnchor = new b2Vec2();
   9065       this.m_target = new b2Vec2();
   9066       this.m_impulse = new b2Vec2();
   9067       this.m_mass = new b2Mat22();
   9068       this.m_C = new b2Vec2();
   9069    };
   9070    b2MouseJoint.prototype.GetAnchorA = function () {
   9071       return this.m_target;
   9072    }
   9073    b2MouseJoint.prototype.GetAnchorB = function () {
   9074       return this.m_bodyB.GetWorldPoint(this.m_localAnchor);
   9075    }
   9076    b2MouseJoint.prototype.GetReactionForce = function (inv_dt) {
   9077       if (inv_dt === undefined) inv_dt = 0;
   9078       return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y);
   9079    }
   9080    b2MouseJoint.prototype.GetReactionTorque = function (inv_dt) {
   9081       if (inv_dt === undefined) inv_dt = 0;
   9082       return 0.0;
   9083    }
   9084    b2MouseJoint.prototype.GetTarget = function () {
   9085       return this.m_target;
   9086    }
   9087    b2MouseJoint.prototype.SetTarget = function (target) {
   9088       if (this.m_bodyB.IsAwake() == false) {
   9089          this.m_bodyB.SetAwake(true);
   9090       }
   9091       this.m_target = target;
   9092    }
   9093    b2MouseJoint.prototype.GetMaxForce = function () {
   9094       return this.m_maxForce;
   9095    }
   9096    b2MouseJoint.prototype.SetMaxForce = function (maxForce) {
   9097       if (maxForce === undefined) maxForce = 0;
   9098       this.m_maxForce = maxForce;
   9099    }
   9100    b2MouseJoint.prototype.GetFrequency = function () {
   9101       return this.m_frequencyHz;
   9102    }
   9103    b2MouseJoint.prototype.SetFrequency = function (hz) {
   9104       if (hz === undefined) hz = 0;
   9105       this.m_frequencyHz = hz;
   9106    }
   9107    b2MouseJoint.prototype.GetDampingRatio = function () {
   9108       return this.m_dampingRatio;
   9109    }
   9110    b2MouseJoint.prototype.SetDampingRatio = function (ratio) {
   9111       if (ratio === undefined) ratio = 0;
   9112       this.m_dampingRatio = ratio;
   9113    }
   9114    b2MouseJoint.prototype.b2MouseJoint = function (def) {
   9115       this.__super.b2Joint.call(this, def);
   9116       this.m_target.SetV(def.target);
   9117       var tX = this.m_target.x - this.m_bodyB.m_xf.position.x;
   9118       var tY = this.m_target.y - this.m_bodyB.m_xf.position.y;
   9119       var tMat = this.m_bodyB.m_xf.R;
   9120       this.m_localAnchor.x = (tX * tMat.col1.x + tY * tMat.col1.y);
   9121       this.m_localAnchor.y = (tX * tMat.col2.x + tY * tMat.col2.y);
   9122       this.m_maxForce = def.maxForce;
   9123       this.m_impulse.SetZero();
   9124       this.m_frequencyHz = def.frequencyHz;
   9125       this.m_dampingRatio = def.dampingRatio;
   9126       this.m_beta = 0.0;
   9127       this.m_gamma = 0.0;
   9128    }
   9129    b2MouseJoint.prototype.InitVelocityConstraints = function (step) {
   9130       var b = this.m_bodyB;
   9131       var mass = b.GetMass();
   9132       var omega = 2.0 * Math.PI * this.m_frequencyHz;
   9133       var d = 2.0 * mass * this.m_dampingRatio * omega;
   9134       var k = mass * omega * omega;
   9135       this.m_gamma = step.dt * (d + step.dt * k);
   9136       this.m_gamma = this.m_gamma != 0 ? 1 / this.m_gamma : 0.0;
   9137       this.m_beta = step.dt * k * this.m_gamma;
   9138       var tMat;tMat = b.m_xf.R;
   9139       var rX = this.m_localAnchor.x - b.m_sweep.localCenter.x;
   9140       var rY = this.m_localAnchor.y - b.m_sweep.localCenter.y;
   9141       var tX = (tMat.col1.x * rX + tMat.col2.x * rY);rY = (tMat.col1.y * rX + tMat.col2.y * rY);
   9142       rX = tX;
   9143       var invMass = b.m_invMass;
   9144       var invI = b.m_invI;this.K1.col1.x = invMass;
   9145       this.K1.col2.x = 0.0;
   9146       this.K1.col1.y = 0.0;
   9147       this.K1.col2.y = invMass;
   9148       this.K2.col1.x = invI * rY * rY;
   9149       this.K2.col2.x = (-invI * rX * rY);
   9150       this.K2.col1.y = (-invI * rX * rY);
   9151       this.K2.col2.y = invI * rX * rX;
   9152       this.K.SetM(this.K1);
   9153       this.K.AddM(this.K2);
   9154       this.K.col1.x += this.m_gamma;
   9155       this.K.col2.y += this.m_gamma;
   9156       this.K.GetInverse(this.m_mass);
   9157       this.m_C.x = b.m_sweep.c.x + rX - this.m_target.x;
   9158       this.m_C.y = b.m_sweep.c.y + rY - this.m_target.y;
   9159       b.m_angularVelocity *= 0.98;
   9160       this.m_impulse.x *= step.dtRatio;
   9161       this.m_impulse.y *= step.dtRatio;
   9162       b.m_linearVelocity.x += invMass * this.m_impulse.x;
   9163       b.m_linearVelocity.y += invMass * this.m_impulse.y;
   9164       b.m_angularVelocity += invI * (rX * this.m_impulse.y - rY * this.m_impulse.x);
   9165    }
   9166    b2MouseJoint.prototype.SolveVelocityConstraints = function (step) {
   9167       var b = this.m_bodyB;
   9168       var tMat;
   9169       var tX = 0;
   9170       var tY = 0;
   9171       tMat = b.m_xf.R;
   9172       var rX = this.m_localAnchor.x - b.m_sweep.localCenter.x;
   9173       var rY = this.m_localAnchor.y - b.m_sweep.localCenter.y;
   9174       tX = (tMat.col1.x * rX + tMat.col2.x * rY);
   9175       rY = (tMat.col1.y * rX + tMat.col2.y * rY);
   9176       rX = tX;
   9177       var CdotX = b.m_linearVelocity.x + ((-b.m_angularVelocity * rY));
   9178       var CdotY = b.m_linearVelocity.y + (b.m_angularVelocity * rX);
   9179       tMat = this.m_mass;
   9180       tX = CdotX + this.m_beta * this.m_C.x + this.m_gamma * this.m_impulse.x;
   9181       tY = CdotY + this.m_beta * this.m_C.y + this.m_gamma * this.m_impulse.y;
   9182       var impulseX = (-(tMat.col1.x * tX + tMat.col2.x * tY));
   9183       var impulseY = (-(tMat.col1.y * tX + tMat.col2.y * tY));
   9184       var oldImpulseX = this.m_impulse.x;
   9185       var oldImpulseY = this.m_impulse.y;
   9186       this.m_impulse.x += impulseX;
   9187       this.m_impulse.y += impulseY;
   9188       var maxImpulse = step.dt * this.m_maxForce;
   9189       if (this.m_impulse.LengthSquared() > maxImpulse * maxImpulse) {
   9190          this.m_impulse.Multiply(maxImpulse / this.m_impulse.Length());
   9191       }
   9192       impulseX = this.m_impulse.x - oldImpulseX;
   9193       impulseY = this.m_impulse.y - oldImpulseY;
   9194       b.m_linearVelocity.x += b.m_invMass * impulseX;
   9195       b.m_linearVelocity.y += b.m_invMass * impulseY;
   9196       b.m_angularVelocity += b.m_invI * (rX * impulseY - rY * impulseX);
   9197    }
   9198    b2MouseJoint.prototype.SolvePositionConstraints = function (baumgarte) {
   9199       if (baumgarte === undefined) baumgarte = 0;
   9200       return true;
   9201    }
   9202    Box2D.inherit(b2MouseJointDef, Box2D.Dynamics.Joints.b2JointDef);
   9203    b2MouseJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
   9204    b2MouseJointDef.b2MouseJointDef = function () {
   9205       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
   9206       this.target = new b2Vec2();
   9207    };
   9208    b2MouseJointDef.prototype.b2MouseJointDef = function () {
   9209       this.__super.b2JointDef.call(this);
   9210       this.type = b2Joint.e_mouseJoint;
   9211       this.maxForce = 0.0;
   9212       this.frequencyHz = 5.0;
   9213       this.dampingRatio = 0.7;
   9214    }
   9215    Box2D.inherit(b2PrismaticJoint, Box2D.Dynamics.Joints.b2Joint);
   9216    b2PrismaticJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
   9217    b2PrismaticJoint.b2PrismaticJoint = function () {
   9218       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
   9219       this.m_localAnchor1 = new b2Vec2();
   9220       this.m_localAnchor2 = new b2Vec2();
   9221       this.m_localXAxis1 = new b2Vec2();
   9222       this.m_localYAxis1 = new b2Vec2();
   9223       this.m_axis = new b2Vec2();
   9224       this.m_perp = new b2Vec2();
   9225       this.m_K = new b2Mat33();
   9226       this.m_impulse = new b2Vec3();
   9227    };
   9228    b2PrismaticJoint.prototype.GetAnchorA = function () {
   9229       return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
   9230    }
   9231    b2PrismaticJoint.prototype.GetAnchorB = function () {
   9232       return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
   9233    }
   9234    b2PrismaticJoint.prototype.GetReactionForce = function (inv_dt) {
   9235       if (inv_dt === undefined) inv_dt = 0;
   9236       return new b2Vec2(inv_dt * (this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.x), inv_dt * (this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.y));
   9237    }
   9238    b2PrismaticJoint.prototype.GetReactionTorque = function (inv_dt) {
   9239       if (inv_dt === undefined) inv_dt = 0;
   9240       return inv_dt * this.m_impulse.y;
   9241    }
   9242    b2PrismaticJoint.prototype.GetJointTranslation = function () {
   9243       var bA = this.m_bodyA;
   9244       var bB = this.m_bodyB;
   9245       var tMat;
   9246       var p1 = bA.GetWorldPoint(this.m_localAnchor1);
   9247       var p2 = bB.GetWorldPoint(this.m_localAnchor2);
   9248       var dX = p2.x - p1.x;
   9249       var dY = p2.y - p1.y;
   9250       var axis = bA.GetWorldVector(this.m_localXAxis1);
   9251       var translation = axis.x * dX + axis.y * dY;
   9252       return translation;
   9253    }
   9254    b2PrismaticJoint.prototype.GetJointSpeed = function () {
   9255       var bA = this.m_bodyA;
   9256       var bB = this.m_bodyB;
   9257       var tMat;
   9258       tMat = bA.m_xf.R;
   9259       var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   9260       var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   9261       var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   9262       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   9263       r1X = tX;
   9264       tMat = bB.m_xf.R;
   9265       var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   9266       var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   9267       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   9268       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   9269       r2X = tX;
   9270       var p1X = bA.m_sweep.c.x + r1X;
   9271       var p1Y = bA.m_sweep.c.y + r1Y;
   9272       var p2X = bB.m_sweep.c.x + r2X;
   9273       var p2Y = bB.m_sweep.c.y + r2Y;
   9274       var dX = p2X - p1X;
   9275       var dY = p2Y - p1Y;
   9276       var axis = bA.GetWorldVector(this.m_localXAxis1);
   9277       var v1 = bA.m_linearVelocity;
   9278       var v2 = bB.m_linearVelocity;
   9279       var w1 = bA.m_angularVelocity;
   9280       var w2 = bB.m_angularVelocity;
   9281       var speed = (dX * ((-w1 * axis.y)) + dY * (w1 * axis.x)) + (axis.x * (((v2.x + ((-w2 * r2Y))) - v1.x) - ((-w1 * r1Y))) + axis.y * (((v2.y + (w2 * r2X)) - v1.y) - (w1 * r1X)));
   9282       return speed;
   9283    }
   9284    b2PrismaticJoint.prototype.IsLimitEnabled = function () {
   9285       return this.m_enableLimit;
   9286    }
   9287    b2PrismaticJoint.prototype.EnableLimit = function (flag) {
   9288       this.m_bodyA.SetAwake(true);
   9289       this.m_bodyB.SetAwake(true);
   9290       this.m_enableLimit = flag;
   9291    }
   9292    b2PrismaticJoint.prototype.GetLowerLimit = function () {
   9293       return this.m_lowerTranslation;
   9294    }
   9295    b2PrismaticJoint.prototype.GetUpperLimit = function () {
   9296       return this.m_upperTranslation;
   9297    }
   9298    b2PrismaticJoint.prototype.SetLimits = function (lower, upper) {
   9299       if (lower === undefined) lower = 0;
   9300       if (upper === undefined) upper = 0;
   9301       this.m_bodyA.SetAwake(true);
   9302       this.m_bodyB.SetAwake(true);
   9303       this.m_lowerTranslation = lower;
   9304       this.m_upperTranslation = upper;
   9305    }
   9306    b2PrismaticJoint.prototype.IsMotorEnabled = function () {
   9307       return this.m_enableMotor;
   9308    }
   9309    b2PrismaticJoint.prototype.EnableMotor = function (flag) {
   9310       this.m_bodyA.SetAwake(true);
   9311       this.m_bodyB.SetAwake(true);
   9312       this.m_enableMotor = flag;
   9313    }
   9314    b2PrismaticJoint.prototype.SetMotorSpeed = function (speed) {
   9315       if (speed === undefined) speed = 0;
   9316       this.m_bodyA.SetAwake(true);
   9317       this.m_bodyB.SetAwake(true);
   9318       this.m_motorSpeed = speed;
   9319    }
   9320    b2PrismaticJoint.prototype.GetMotorSpeed = function () {
   9321       return this.m_motorSpeed;
   9322    }
   9323    b2PrismaticJoint.prototype.SetMaxMotorForce = function (force) {
   9324       if (force === undefined) force = 0;
   9325       this.m_bodyA.SetAwake(true);
   9326       this.m_bodyB.SetAwake(true);
   9327       this.m_maxMotorForce = force;
   9328    }
   9329    b2PrismaticJoint.prototype.GetMotorForce = function () {
   9330       return this.m_motorImpulse;
   9331    }
   9332    b2PrismaticJoint.prototype.b2PrismaticJoint = function (def) {
   9333       this.__super.b2Joint.call(this, def);
   9334       var tMat;
   9335       var tX = 0;
   9336       var tY = 0;
   9337       this.m_localAnchor1.SetV(def.localAnchorA);
   9338       this.m_localAnchor2.SetV(def.localAnchorB);
   9339       this.m_localXAxis1.SetV(def.localAxisA);
   9340       this.m_localYAxis1.x = (-this.m_localXAxis1.y);
   9341       this.m_localYAxis1.y = this.m_localXAxis1.x;
   9342       this.m_refAngle = def.referenceAngle;
   9343       this.m_impulse.SetZero();
   9344       this.m_motorMass = 0.0;
   9345       this.m_motorImpulse = 0.0;
   9346       this.m_lowerTranslation = def.lowerTranslation;
   9347       this.m_upperTranslation = def.upperTranslation;
   9348       this.m_maxMotorForce = def.maxMotorForce;
   9349       this.m_motorSpeed = def.motorSpeed;
   9350       this.m_enableLimit = def.enableLimit;
   9351       this.m_enableMotor = def.enableMotor;
   9352       this.m_limitState = b2Joint.e_inactiveLimit;
   9353       this.m_axis.SetZero();
   9354       this.m_perp.SetZero();
   9355    }
   9356    b2PrismaticJoint.prototype.InitVelocityConstraints = function (step) {
   9357       var bA = this.m_bodyA;
   9358       var bB = this.m_bodyB;
   9359       var tMat;
   9360       var tX = 0;
   9361       this.m_localCenterA.SetV(bA.GetLocalCenter());
   9362       this.m_localCenterB.SetV(bB.GetLocalCenter());
   9363       var xf1 = bA.GetTransform();
   9364       var xf2 = bB.GetTransform();
   9365       tMat = bA.m_xf.R;
   9366       var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
   9367       var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
   9368       tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   9369       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   9370       r1X = tX;
   9371       tMat = bB.m_xf.R;
   9372       var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
   9373       var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
   9374       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   9375       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   9376       r2X = tX;
   9377       var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
   9378       var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
   9379       this.m_invMassA = bA.m_invMass;
   9380       this.m_invMassB = bB.m_invMass;
   9381       this.m_invIA = bA.m_invI;
   9382       this.m_invIB = bB.m_invI; {
   9383          this.m_axis.SetV(b2Math.MulMV(xf1.R, this.m_localXAxis1));
   9384          this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
   9385          this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
   9386          this.m_motorMass = this.m_invMassA + this.m_invMassB + this.m_invIA * this.m_a1 * this.m_a1 + this.m_invIB * this.m_a2 * this.m_a2;
   9387          if (this.m_motorMass > Number.MIN_VALUE) this.m_motorMass = 1.0 / this.m_motorMass;
   9388       } {
   9389          this.m_perp.SetV(b2Math.MulMV(xf1.R, this.m_localYAxis1));
   9390          this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
   9391          this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
   9392          var m1 = this.m_invMassA;
   9393          var m2 = this.m_invMassB;
   9394          var i1 = this.m_invIA;
   9395          var i2 = this.m_invIB;
   9396          this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
   9397          this.m_K.col1.y = i1 * this.m_s1 + i2 * this.m_s2;
   9398          this.m_K.col1.z = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
   9399          this.m_K.col2.x = this.m_K.col1.y;
   9400          this.m_K.col2.y = i1 + i2;
   9401          this.m_K.col2.z = i1 * this.m_a1 + i2 * this.m_a2;
   9402          this.m_K.col3.x = this.m_K.col1.z;
   9403          this.m_K.col3.y = this.m_K.col2.z;
   9404          this.m_K.col3.z = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
   9405       }
   9406       if (this.m_enableLimit) {
   9407          var jointTransition = this.m_axis.x * dX + this.m_axis.y * dY;
   9408          if (b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2.0 * b2Settings.b2_linearSlop) {
   9409             this.m_limitState = b2Joint.e_equalLimits;
   9410          }
   9411          else if (jointTransition <= this.m_lowerTranslation) {
   9412             if (this.m_limitState != b2Joint.e_atLowerLimit) {
   9413                this.m_limitState = b2Joint.e_atLowerLimit;
   9414                this.m_impulse.z = 0.0;
   9415             }
   9416          }
   9417          else if (jointTransition >= this.m_upperTranslation) {
   9418             if (this.m_limitState != b2Joint.e_atUpperLimit) {
   9419                this.m_limitState = b2Joint.e_atUpperLimit;
   9420                this.m_impulse.z = 0.0;
   9421             }
   9422          }
   9423          else {
   9424             this.m_limitState = b2Joint.e_inactiveLimit;
   9425             this.m_impulse.z = 0.0;
   9426          }
   9427       }
   9428       else {
   9429          this.m_limitState = b2Joint.e_inactiveLimit;
   9430       }
   9431       if (this.m_enableMotor == false) {
   9432          this.m_motorImpulse = 0.0;
   9433       }
   9434       if (step.warmStarting) {
   9435          this.m_impulse.x *= step.dtRatio;
   9436          this.m_impulse.y *= step.dtRatio;
   9437          this.m_motorImpulse *= step.dtRatio;
   9438          var PX = this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.x;
   9439          var PY = this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.y;
   9440          var L1 = this.m_impulse.x * this.m_s1 + this.m_impulse.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_a1;
   9441          var L2 = this.m_impulse.x * this.m_s2 + this.m_impulse.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_a2;
   9442          bA.m_linearVelocity.x -= this.m_invMassA * PX;
   9443          bA.m_linearVelocity.y -= this.m_invMassA * PY;
   9444          bA.m_angularVelocity -= this.m_invIA * L1;
   9445          bB.m_linearVelocity.x += this.m_invMassB * PX;
   9446          bB.m_linearVelocity.y += this.m_invMassB * PY;
   9447          bB.m_angularVelocity += this.m_invIB * L2;
   9448       }
   9449       else {
   9450          this.m_impulse.SetZero();
   9451          this.m_motorImpulse = 0.0;
   9452       }
   9453    }
   9454    b2PrismaticJoint.prototype.SolveVelocityConstraints = function (step) {
   9455       var bA = this.m_bodyA;
   9456       var bB = this.m_bodyB;
   9457       var v1 = bA.m_linearVelocity;
   9458       var w1 = bA.m_angularVelocity;
   9459       var v2 = bB.m_linearVelocity;
   9460       var w2 = bB.m_angularVelocity;
   9461       var PX = 0;
   9462       var PY = 0;
   9463       var L1 = 0;
   9464       var L2 = 0;
   9465       if (this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
   9466          var Cdot = this.m_axis.x * (v2.x - v1.x) + this.m_axis.y * (v2.y - v1.y) + this.m_a2 * w2 - this.m_a1 * w1;
   9467          var impulse = this.m_motorMass * (this.m_motorSpeed - Cdot);
   9468          var oldImpulse = this.m_motorImpulse;
   9469          var maxImpulse = step.dt * this.m_maxMotorForce;
   9470          this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, (-maxImpulse), maxImpulse);
   9471          impulse = this.m_motorImpulse - oldImpulse;
   9472          PX = impulse * this.m_axis.x;
   9473          PY = impulse * this.m_axis.y;
   9474          L1 = impulse * this.m_a1;
   9475          L2 = impulse * this.m_a2;
   9476          v1.x -= this.m_invMassA * PX;
   9477          v1.y -= this.m_invMassA * PY;
   9478          w1 -= this.m_invIA * L1;
   9479          v2.x += this.m_invMassB * PX;
   9480          v2.y += this.m_invMassB * PY;
   9481          w2 += this.m_invIB * L2;
   9482       }
   9483       var Cdot1X = this.m_perp.x * (v2.x - v1.x) + this.m_perp.y * (v2.y - v1.y) + this.m_s2 * w2 - this.m_s1 * w1;
   9484       var Cdot1Y = w2 - w1;
   9485       if (this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
   9486          var Cdot2 = this.m_axis.x * (v2.x - v1.x) + this.m_axis.y * (v2.y - v1.y) + this.m_a2 * w2 - this.m_a1 * w1;
   9487          var f1 = this.m_impulse.Copy();
   9488          var df = this.m_K.Solve33(new b2Vec3(), (-Cdot1X), (-Cdot1Y), (-Cdot2));
   9489          this.m_impulse.Add(df);
   9490          if (this.m_limitState == b2Joint.e_atLowerLimit) {
   9491             this.m_impulse.z = b2Math.Max(this.m_impulse.z, 0.0);
   9492          }
   9493          else if (this.m_limitState == b2Joint.e_atUpperLimit) {
   9494             this.m_impulse.z = b2Math.Min(this.m_impulse.z, 0.0);
   9495          }
   9496          var bX = (-Cdot1X) - (this.m_impulse.z - f1.z) * this.m_K.col3.x;
   9497          var bY = (-Cdot1Y) - (this.m_impulse.z - f1.z) * this.m_K.col3.y;
   9498          var f2r = this.m_K.Solve22(new b2Vec2(), bX, bY);
   9499          f2r.x += f1.x;
   9500          f2r.y += f1.y;
   9501          this.m_impulse.x = f2r.x;
   9502          this.m_impulse.y = f2r.y;
   9503          df.x = this.m_impulse.x - f1.x;
   9504          df.y = this.m_impulse.y - f1.y;
   9505          df.z = this.m_impulse.z - f1.z;
   9506          PX = df.x * this.m_perp.x + df.z * this.m_axis.x;
   9507          PY = df.x * this.m_perp.y + df.z * this.m_axis.y;
   9508          L1 = df.x * this.m_s1 + df.y + df.z * this.m_a1;
   9509          L2 = df.x * this.m_s2 + df.y + df.z * this.m_a2;
   9510          v1.x -= this.m_invMassA * PX;
   9511          v1.y -= this.m_invMassA * PY;
   9512          w1 -= this.m_invIA * L1;
   9513          v2.x += this.m_invMassB * PX;
   9514          v2.y += this.m_invMassB * PY;
   9515          w2 += this.m_invIB * L2;
   9516       }
   9517       else {
   9518          var df2 = this.m_K.Solve22(new b2Vec2(), (-Cdot1X), (-Cdot1Y));
   9519          this.m_impulse.x += df2.x;
   9520          this.m_impulse.y += df2.y;
   9521          PX = df2.x * this.m_perp.x;
   9522          PY = df2.x * this.m_perp.y;
   9523          L1 = df2.x * this.m_s1 + df2.y;
   9524          L2 = df2.x * this.m_s2 + df2.y;
   9525          v1.x -= this.m_invMassA * PX;
   9526          v1.y -= this.m_invMassA * PY;
   9527          w1 -= this.m_invIA * L1;
   9528          v2.x += this.m_invMassB * PX;
   9529          v2.y += this.m_invMassB * PY;
   9530          w2 += this.m_invIB * L2;
   9531       }
   9532       bA.m_linearVelocity.SetV(v1);
   9533       bA.m_angularVelocity = w1;
   9534       bB.m_linearVelocity.SetV(v2);
   9535       bB.m_angularVelocity = w2;
   9536    }
   9537    b2PrismaticJoint.prototype.SolvePositionConstraints = function (baumgarte) {
   9538       if (baumgarte === undefined) baumgarte = 0;
   9539       var limitC = 0;
   9540       var oldLimitImpulse = 0;
   9541       var bA = this.m_bodyA;
   9542       var bB = this.m_bodyB;
   9543       var c1 = bA.m_sweep.c;
   9544       var a1 = bA.m_sweep.a;
   9545       var c2 = bB.m_sweep.c;
   9546       var a2 = bB.m_sweep.a;
   9547       var tMat;
   9548       var tX = 0;
   9549       var m1 = 0;
   9550       var m2 = 0;
   9551       var i1 = 0;
   9552       var i2 = 0;
   9553       var linearError = 0.0;
   9554       var angularError = 0.0;
   9555       var active = false;
   9556       var C2 = 0.0;
   9557       var R1 = b2Mat22.FromAngle(a1);
   9558       var R2 = b2Mat22.FromAngle(a2);
   9559       tMat = R1;
   9560       var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
   9561       var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
   9562       tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   9563       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   9564       r1X = tX;
   9565       tMat = R2;
   9566       var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
   9567       var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
   9568       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   9569       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   9570       r2X = tX;
   9571       var dX = c2.x + r2X - c1.x - r1X;
   9572       var dY = c2.y + r2Y - c1.y - r1Y;
   9573       if (this.m_enableLimit) {
   9574          this.m_axis = b2Math.MulMV(R1, this.m_localXAxis1);
   9575          this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
   9576          this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
   9577          var translation = this.m_axis.x * dX + this.m_axis.y * dY;
   9578          if (b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2.0 * b2Settings.b2_linearSlop) {
   9579             C2 = b2Math.Clamp(translation, (-b2Settings.b2_maxLinearCorrection), b2Settings.b2_maxLinearCorrection);
   9580             linearError = b2Math.Abs(translation);
   9581             active = true;
   9582          }
   9583          else if (translation <= this.m_lowerTranslation) {
   9584             C2 = b2Math.Clamp(translation - this.m_lowerTranslation + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
   9585             linearError = this.m_lowerTranslation - translation;
   9586             active = true;
   9587          }
   9588          else if (translation >= this.m_upperTranslation) {
   9589             C2 = b2Math.Clamp(translation - this.m_upperTranslation + b2Settings.b2_linearSlop, 0.0, b2Settings.b2_maxLinearCorrection);
   9590             linearError = translation - this.m_upperTranslation;
   9591             active = true;
   9592          }
   9593       }
   9594       this.m_perp = b2Math.MulMV(R1, this.m_localYAxis1);
   9595       this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
   9596       this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
   9597       var impulse = new b2Vec3();
   9598       var C1X = this.m_perp.x * dX + this.m_perp.y * dY;
   9599       var C1Y = a2 - a1 - this.m_refAngle;
   9600       linearError = b2Math.Max(linearError, b2Math.Abs(C1X));
   9601       angularError = b2Math.Abs(C1Y);
   9602       if (active) {
   9603          m1 = this.m_invMassA;
   9604          m2 = this.m_invMassB;
   9605          i1 = this.m_invIA;
   9606          i2 = this.m_invIB;
   9607          this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
   9608          this.m_K.col1.y = i1 * this.m_s1 + i2 * this.m_s2;
   9609          this.m_K.col1.z = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
   9610          this.m_K.col2.x = this.m_K.col1.y;
   9611          this.m_K.col2.y = i1 + i2;
   9612          this.m_K.col2.z = i1 * this.m_a1 + i2 * this.m_a2;
   9613          this.m_K.col3.x = this.m_K.col1.z;
   9614          this.m_K.col3.y = this.m_K.col2.z;
   9615          this.m_K.col3.z = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
   9616          this.m_K.Solve33(impulse, (-C1X), (-C1Y), (-C2));
   9617       }
   9618       else {
   9619          m1 = this.m_invMassA;
   9620          m2 = this.m_invMassB;
   9621          i1 = this.m_invIA;
   9622          i2 = this.m_invIB;
   9623          var k11 = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
   9624          var k12 = i1 * this.m_s1 + i2 * this.m_s2;
   9625          var k22 = i1 + i2;
   9626          this.m_K.col1.Set(k11, k12, 0.0);
   9627          this.m_K.col2.Set(k12, k22, 0.0);
   9628          var impulse1 = this.m_K.Solve22(new b2Vec2(), (-C1X), (-C1Y));
   9629          impulse.x = impulse1.x;
   9630          impulse.y = impulse1.y;
   9631          impulse.z = 0.0;
   9632       }
   9633       var PX = impulse.x * this.m_perp.x + impulse.z * this.m_axis.x;
   9634       var PY = impulse.x * this.m_perp.y + impulse.z * this.m_axis.y;
   9635       var L1 = impulse.x * this.m_s1 + impulse.y + impulse.z * this.m_a1;
   9636       var L2 = impulse.x * this.m_s2 + impulse.y + impulse.z * this.m_a2;
   9637       c1.x -= this.m_invMassA * PX;
   9638       c1.y -= this.m_invMassA * PY;
   9639       a1 -= this.m_invIA * L1;
   9640       c2.x += this.m_invMassB * PX;
   9641       c2.y += this.m_invMassB * PY;
   9642       a2 += this.m_invIB * L2;
   9643       bA.m_sweep.a = a1;
   9644       bB.m_sweep.a = a2;
   9645       bA.SynchronizeTransform();
   9646       bB.SynchronizeTransform();
   9647       return linearError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
   9648    }
   9649    Box2D.inherit(b2PrismaticJointDef, Box2D.Dynamics.Joints.b2JointDef);
   9650    b2PrismaticJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
   9651    b2PrismaticJointDef.b2PrismaticJointDef = function () {
   9652       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
   9653       this.localAnchorA = new b2Vec2();
   9654       this.localAnchorB = new b2Vec2();
   9655       this.localAxisA = new b2Vec2();
   9656    };
   9657    b2PrismaticJointDef.prototype.b2PrismaticJointDef = function () {
   9658       this.__super.b2JointDef.call(this);
   9659       this.type = b2Joint.e_prismaticJoint;
   9660       this.localAxisA.Set(1.0, 0.0);
   9661       this.referenceAngle = 0.0;
   9662       this.enableLimit = false;
   9663       this.lowerTranslation = 0.0;
   9664       this.upperTranslation = 0.0;
   9665       this.enableMotor = false;
   9666       this.maxMotorForce = 0.0;
   9667       this.motorSpeed = 0.0;
   9668    }
   9669    b2PrismaticJointDef.prototype.Initialize = function (bA, bB, anchor, axis) {
   9670       this.bodyA = bA;
   9671       this.bodyB = bB;
   9672       this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
   9673       this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
   9674       this.localAxisA = this.bodyA.GetLocalVector(axis);
   9675       this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle();
   9676    }
   9677    Box2D.inherit(b2PulleyJoint, Box2D.Dynamics.Joints.b2Joint);
   9678    b2PulleyJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
   9679    b2PulleyJoint.b2PulleyJoint = function () {
   9680       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
   9681       this.m_groundAnchor1 = new b2Vec2();
   9682       this.m_groundAnchor2 = new b2Vec2();
   9683       this.m_localAnchor1 = new b2Vec2();
   9684       this.m_localAnchor2 = new b2Vec2();
   9685       this.m_u1 = new b2Vec2();
   9686       this.m_u2 = new b2Vec2();
   9687    };
   9688    b2PulleyJoint.prototype.GetAnchorA = function () {
   9689       return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
   9690    }
   9691    b2PulleyJoint.prototype.GetAnchorB = function () {
   9692       return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
   9693    }
   9694    b2PulleyJoint.prototype.GetReactionForce = function (inv_dt) {
   9695       if (inv_dt === undefined) inv_dt = 0;
   9696       return new b2Vec2(inv_dt * this.m_impulse * this.m_u2.x, inv_dt * this.m_impulse * this.m_u2.y);
   9697    }
   9698    b2PulleyJoint.prototype.GetReactionTorque = function (inv_dt) {
   9699       if (inv_dt === undefined) inv_dt = 0;
   9700       return 0.0;
   9701    }
   9702    b2PulleyJoint.prototype.GetGroundAnchorA = function () {
   9703       var a = this.m_ground.m_xf.position.Copy();
   9704       a.Add(this.m_groundAnchor1);
   9705       return a;
   9706    }
   9707    b2PulleyJoint.prototype.GetGroundAnchorB = function () {
   9708       var a = this.m_ground.m_xf.position.Copy();
   9709       a.Add(this.m_groundAnchor2);
   9710       return a;
   9711    }
   9712    b2PulleyJoint.prototype.GetLength1 = function () {
   9713       var p = this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
   9714       var sX = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
   9715       var sY = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
   9716       var dX = p.x - sX;
   9717       var dY = p.y - sY;
   9718       return Math.sqrt(dX * dX + dY * dY);
   9719    }
   9720    b2PulleyJoint.prototype.GetLength2 = function () {
   9721       var p = this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
   9722       var sX = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
   9723       var sY = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
   9724       var dX = p.x - sX;
   9725       var dY = p.y - sY;
   9726       return Math.sqrt(dX * dX + dY * dY);
   9727    }
   9728    b2PulleyJoint.prototype.GetRatio = function () {
   9729       return this.m_ratio;
   9730    }
   9731    b2PulleyJoint.prototype.b2PulleyJoint = function (def) {
   9732       this.__super.b2Joint.call(this, def);
   9733       var tMat;
   9734       var tX = 0;
   9735       var tY = 0;
   9736       this.m_ground = this.m_bodyA.m_world.m_groundBody;
   9737       this.m_groundAnchor1.x = def.groundAnchorA.x - this.m_ground.m_xf.position.x;
   9738       this.m_groundAnchor1.y = def.groundAnchorA.y - this.m_ground.m_xf.position.y;
   9739       this.m_groundAnchor2.x = def.groundAnchorB.x - this.m_ground.m_xf.position.x;
   9740       this.m_groundAnchor2.y = def.groundAnchorB.y - this.m_ground.m_xf.position.y;
   9741       this.m_localAnchor1.SetV(def.localAnchorA);
   9742       this.m_localAnchor2.SetV(def.localAnchorB);
   9743       this.m_ratio = def.ratio;
   9744       this.m_constant = def.lengthA + this.m_ratio * def.lengthB;
   9745       this.m_maxLength1 = b2Math.Min(def.maxLengthA, this.m_constant - this.m_ratio * b2PulleyJoint.b2_minPulleyLength);
   9746       this.m_maxLength2 = b2Math.Min(def.maxLengthB, (this.m_constant - b2PulleyJoint.b2_minPulleyLength) / this.m_ratio);
   9747       this.m_impulse = 0.0;
   9748       this.m_limitImpulse1 = 0.0;
   9749       this.m_limitImpulse2 = 0.0;
   9750    }
   9751    b2PulleyJoint.prototype.InitVelocityConstraints = function (step) {
   9752       var bA = this.m_bodyA;
   9753       var bB = this.m_bodyB;
   9754       var tMat;
   9755       tMat = bA.m_xf.R;
   9756       var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   9757       var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   9758       var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   9759       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   9760       r1X = tX;
   9761       tMat = bB.m_xf.R;
   9762       var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   9763       var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   9764       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   9765       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   9766       r2X = tX;
   9767       var p1X = bA.m_sweep.c.x + r1X;
   9768       var p1Y = bA.m_sweep.c.y + r1Y;
   9769       var p2X = bB.m_sweep.c.x + r2X;
   9770       var p2Y = bB.m_sweep.c.y + r2Y;
   9771       var s1X = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
   9772       var s1Y = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
   9773       var s2X = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
   9774       var s2Y = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
   9775       this.m_u1.Set(p1X - s1X, p1Y - s1Y);
   9776       this.m_u2.Set(p2X - s2X, p2Y - s2Y);
   9777       var length1 = this.m_u1.Length();
   9778       var length2 = this.m_u2.Length();
   9779       if (length1 > b2Settings.b2_linearSlop) {
   9780          this.m_u1.Multiply(1.0 / length1);
   9781       }
   9782       else {
   9783          this.m_u1.SetZero();
   9784       }
   9785       if (length2 > b2Settings.b2_linearSlop) {
   9786          this.m_u2.Multiply(1.0 / length2);
   9787       }
   9788       else {
   9789          this.m_u2.SetZero();
   9790       }
   9791       var C = this.m_constant - length1 - this.m_ratio * length2;
   9792       if (C > 0.0) {
   9793          this.m_state = b2Joint.e_inactiveLimit;
   9794          this.m_impulse = 0.0;
   9795       }
   9796       else {
   9797          this.m_state = b2Joint.e_atUpperLimit;
   9798       }
   9799       if (length1 < this.m_maxLength1) {
   9800          this.m_limitState1 = b2Joint.e_inactiveLimit;
   9801          this.m_limitImpulse1 = 0.0;
   9802       }
   9803       else {
   9804          this.m_limitState1 = b2Joint.e_atUpperLimit;
   9805       }
   9806       if (length2 < this.m_maxLength2) {
   9807          this.m_limitState2 = b2Joint.e_inactiveLimit;
   9808          this.m_limitImpulse2 = 0.0;
   9809       }
   9810       else {
   9811          this.m_limitState2 = b2Joint.e_atUpperLimit;
   9812       }
   9813       var cr1u1 = r1X * this.m_u1.y - r1Y * this.m_u1.x;
   9814       var cr2u2 = r2X * this.m_u2.y - r2Y * this.m_u2.x;
   9815       this.m_limitMass1 = bA.m_invMass + bA.m_invI * cr1u1 * cr1u1;
   9816       this.m_limitMass2 = bB.m_invMass + bB.m_invI * cr2u2 * cr2u2;
   9817       this.m_pulleyMass = this.m_limitMass1 + this.m_ratio * this.m_ratio * this.m_limitMass2;
   9818       this.m_limitMass1 = 1.0 / this.m_limitMass1;
   9819       this.m_limitMass2 = 1.0 / this.m_limitMass2;
   9820       this.m_pulleyMass = 1.0 / this.m_pulleyMass;
   9821       if (step.warmStarting) {
   9822          this.m_impulse *= step.dtRatio;
   9823          this.m_limitImpulse1 *= step.dtRatio;
   9824          this.m_limitImpulse2 *= step.dtRatio;
   9825          var P1X = ((-this.m_impulse) - this.m_limitImpulse1) * this.m_u1.x;
   9826          var P1Y = ((-this.m_impulse) - this.m_limitImpulse1) * this.m_u1.y;
   9827          var P2X = ((-this.m_ratio * this.m_impulse) - this.m_limitImpulse2) * this.m_u2.x;
   9828          var P2Y = ((-this.m_ratio * this.m_impulse) - this.m_limitImpulse2) * this.m_u2.y;
   9829          bA.m_linearVelocity.x += bA.m_invMass * P1X;
   9830          bA.m_linearVelocity.y += bA.m_invMass * P1Y;
   9831          bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X);
   9832          bB.m_linearVelocity.x += bB.m_invMass * P2X;
   9833          bB.m_linearVelocity.y += bB.m_invMass * P2Y;
   9834          bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X);
   9835       }
   9836       else {
   9837          this.m_impulse = 0.0;
   9838          this.m_limitImpulse1 = 0.0;
   9839          this.m_limitImpulse2 = 0.0;
   9840       }
   9841    }
   9842    b2PulleyJoint.prototype.SolveVelocityConstraints = function (step) {
   9843       var bA = this.m_bodyA;
   9844       var bB = this.m_bodyB;
   9845       var tMat;
   9846       tMat = bA.m_xf.R;
   9847       var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   9848       var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   9849       var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   9850       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   9851       r1X = tX;
   9852       tMat = bB.m_xf.R;
   9853       var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   9854       var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   9855       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   9856       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   9857       r2X = tX;
   9858       var v1X = 0;
   9859       var v1Y = 0;
   9860       var v2X = 0;
   9861       var v2Y = 0;
   9862       var P1X = 0;
   9863       var P1Y = 0;
   9864       var P2X = 0;
   9865       var P2Y = 0;
   9866       var Cdot = 0;
   9867       var impulse = 0;
   9868       var oldImpulse = 0;
   9869       if (this.m_state == b2Joint.e_atUpperLimit) {
   9870          v1X = bA.m_linearVelocity.x + ((-bA.m_angularVelocity * r1Y));
   9871          v1Y = bA.m_linearVelocity.y + (bA.m_angularVelocity * r1X);
   9872          v2X = bB.m_linearVelocity.x + ((-bB.m_angularVelocity * r2Y));
   9873          v2Y = bB.m_linearVelocity.y + (bB.m_angularVelocity * r2X);
   9874          Cdot = (-(this.m_u1.x * v1X + this.m_u1.y * v1Y)) - this.m_ratio * (this.m_u2.x * v2X + this.m_u2.y * v2Y);
   9875          impulse = this.m_pulleyMass * ((-Cdot));
   9876          oldImpulse = this.m_impulse;
   9877          this.m_impulse = b2Math.Max(0.0, this.m_impulse + impulse);
   9878          impulse = this.m_impulse - oldImpulse;
   9879          P1X = (-impulse * this.m_u1.x);
   9880          P1Y = (-impulse * this.m_u1.y);
   9881          P2X = (-this.m_ratio * impulse * this.m_u2.x);
   9882          P2Y = (-this.m_ratio * impulse * this.m_u2.y);
   9883          bA.m_linearVelocity.x += bA.m_invMass * P1X;
   9884          bA.m_linearVelocity.y += bA.m_invMass * P1Y;
   9885          bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X);
   9886          bB.m_linearVelocity.x += bB.m_invMass * P2X;
   9887          bB.m_linearVelocity.y += bB.m_invMass * P2Y;
   9888          bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X);
   9889       }
   9890       if (this.m_limitState1 == b2Joint.e_atUpperLimit) {
   9891          v1X = bA.m_linearVelocity.x + ((-bA.m_angularVelocity * r1Y));
   9892          v1Y = bA.m_linearVelocity.y + (bA.m_angularVelocity * r1X);
   9893          Cdot = (-(this.m_u1.x * v1X + this.m_u1.y * v1Y));
   9894          impulse = (-this.m_limitMass1 * Cdot);
   9895          oldImpulse = this.m_limitImpulse1;
   9896          this.m_limitImpulse1 = b2Math.Max(0.0, this.m_limitImpulse1 + impulse);
   9897          impulse = this.m_limitImpulse1 - oldImpulse;
   9898          P1X = (-impulse * this.m_u1.x);
   9899          P1Y = (-impulse * this.m_u1.y);
   9900          bA.m_linearVelocity.x += bA.m_invMass * P1X;
   9901          bA.m_linearVelocity.y += bA.m_invMass * P1Y;
   9902          bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X);
   9903       }
   9904       if (this.m_limitState2 == b2Joint.e_atUpperLimit) {
   9905          v2X = bB.m_linearVelocity.x + ((-bB.m_angularVelocity * r2Y));
   9906          v2Y = bB.m_linearVelocity.y + (bB.m_angularVelocity * r2X);
   9907          Cdot = (-(this.m_u2.x * v2X + this.m_u2.y * v2Y));
   9908          impulse = (-this.m_limitMass2 * Cdot);
   9909          oldImpulse = this.m_limitImpulse2;
   9910          this.m_limitImpulse2 = b2Math.Max(0.0, this.m_limitImpulse2 + impulse);
   9911          impulse = this.m_limitImpulse2 - oldImpulse;
   9912          P2X = (-impulse * this.m_u2.x);
   9913          P2Y = (-impulse * this.m_u2.y);
   9914          bB.m_linearVelocity.x += bB.m_invMass * P2X;
   9915          bB.m_linearVelocity.y += bB.m_invMass * P2Y;
   9916          bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X);
   9917       }
   9918    }
   9919    b2PulleyJoint.prototype.SolvePositionConstraints = function (baumgarte) {
   9920       if (baumgarte === undefined) baumgarte = 0;
   9921       var bA = this.m_bodyA;
   9922       var bB = this.m_bodyB;
   9923       var tMat;
   9924       var s1X = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
   9925       var s1Y = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
   9926       var s2X = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
   9927       var s2Y = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
   9928       var r1X = 0;
   9929       var r1Y = 0;
   9930       var r2X = 0;
   9931       var r2Y = 0;
   9932       var p1X = 0;
   9933       var p1Y = 0;
   9934       var p2X = 0;
   9935       var p2Y = 0;
   9936       var length1 = 0;
   9937       var length2 = 0;
   9938       var C = 0;
   9939       var impulse = 0;
   9940       var oldImpulse = 0;
   9941       var oldLimitPositionImpulse = 0;
   9942       var tX = 0;
   9943       var linearError = 0.0;
   9944       if (this.m_state == b2Joint.e_atUpperLimit) {
   9945          tMat = bA.m_xf.R;
   9946          r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   9947          r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   9948          tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   9949          r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
   9950          r1X = tX;
   9951          tMat = bB.m_xf.R;
   9952          r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
   9953          r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
   9954          tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
   9955          r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
   9956          r2X = tX;
   9957          p1X = bA.m_sweep.c.x + r1X;
   9958          p1Y = bA.m_sweep.c.y + r1Y;
   9959          p2X = bB.m_sweep.c.x + r2X;
   9960          p2Y = bB.m_sweep.c.y + r2Y;
   9961          this.m_u1.Set(p1X - s1X, p1Y - s1Y);
   9962          this.m_u2.Set(p2X - s2X, p2Y - s2Y);
   9963          length1 = this.m_u1.Length();
   9964          length2 = this.m_u2.Length();
   9965          if (length1 > b2Settings.b2_linearSlop) {
   9966             this.m_u1.Multiply(1.0 / length1);
   9967          }
   9968          else {
   9969             this.m_u1.SetZero();
   9970          }
   9971          if (length2 > b2Settings.b2_linearSlop) {
   9972             this.m_u2.Multiply(1.0 / length2);
   9973          }
   9974          else {
   9975             this.m_u2.SetZero();
   9976          }
   9977          C = this.m_constant - length1 - this.m_ratio * length2;
   9978          linearError = b2Math.Max(linearError, (-C));
   9979          C = b2Math.Clamp(C + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
   9980          impulse = (-this.m_pulleyMass * C);
   9981          p1X = (-impulse * this.m_u1.x);
   9982          p1Y = (-impulse * this.m_u1.y);
   9983          p2X = (-this.m_ratio * impulse * this.m_u2.x);
   9984          p2Y = (-this.m_ratio * impulse * this.m_u2.y);
   9985          bA.m_sweep.c.x += bA.m_invMass * p1X;
   9986          bA.m_sweep.c.y += bA.m_invMass * p1Y;
   9987          bA.m_sweep.a += bA.m_invI * (r1X * p1Y - r1Y * p1X);
   9988          bB.m_sweep.c.x += bB.m_invMass * p2X;
   9989          bB.m_sweep.c.y += bB.m_invMass * p2Y;
   9990          bB.m_sweep.a += bB.m_invI * (r2X * p2Y - r2Y * p2X);
   9991          bA.SynchronizeTransform();
   9992          bB.SynchronizeTransform();
   9993       }
   9994       if (this.m_limitState1 == b2Joint.e_atUpperLimit) {
   9995          tMat = bA.m_xf.R;
   9996          r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
   9997          r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
   9998          tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
   9999          r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10000          r1X = tX;
  10001          p1X = bA.m_sweep.c.x + r1X;
  10002          p1Y = bA.m_sweep.c.y + r1Y;
  10003          this.m_u1.Set(p1X - s1X, p1Y - s1Y);
  10004          length1 = this.m_u1.Length();
  10005          if (length1 > b2Settings.b2_linearSlop) {
  10006             this.m_u1.x *= 1.0 / length1;
  10007             this.m_u1.y *= 1.0 / length1;
  10008          }
  10009          else {
  10010             this.m_u1.SetZero();
  10011          }
  10012          C = this.m_maxLength1 - length1;
  10013          linearError = b2Math.Max(linearError, (-C));
  10014          C = b2Math.Clamp(C + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
  10015          impulse = (-this.m_limitMass1 * C);
  10016          p1X = (-impulse * this.m_u1.x);
  10017          p1Y = (-impulse * this.m_u1.y);
  10018          bA.m_sweep.c.x += bA.m_invMass * p1X;
  10019          bA.m_sweep.c.y += bA.m_invMass * p1Y;
  10020          bA.m_sweep.a += bA.m_invI * (r1X * p1Y - r1Y * p1X);
  10021          bA.SynchronizeTransform();
  10022       }
  10023       if (this.m_limitState2 == b2Joint.e_atUpperLimit) {
  10024          tMat = bB.m_xf.R;
  10025          r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10026          r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10027          tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10028          r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10029          r2X = tX;
  10030          p2X = bB.m_sweep.c.x + r2X;
  10031          p2Y = bB.m_sweep.c.y + r2Y;
  10032          this.m_u2.Set(p2X - s2X, p2Y - s2Y);
  10033          length2 = this.m_u2.Length();
  10034          if (length2 > b2Settings.b2_linearSlop) {
  10035             this.m_u2.x *= 1.0 / length2;
  10036             this.m_u2.y *= 1.0 / length2;
  10037          }
  10038          else {
  10039             this.m_u2.SetZero();
  10040          }
  10041          C = this.m_maxLength2 - length2;
  10042          linearError = b2Math.Max(linearError, (-C));
  10043          C = b2Math.Clamp(C + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
  10044          impulse = (-this.m_limitMass2 * C);
  10045          p2X = (-impulse * this.m_u2.x);
  10046          p2Y = (-impulse * this.m_u2.y);
  10047          bB.m_sweep.c.x += bB.m_invMass * p2X;
  10048          bB.m_sweep.c.y += bB.m_invMass * p2Y;
  10049          bB.m_sweep.a += bB.m_invI * (r2X * p2Y - r2Y * p2X);
  10050          bB.SynchronizeTransform();
  10051       }
  10052       return linearError < b2Settings.b2_linearSlop;
  10053    }
  10054    Box2D.postDefs.push(function () {
  10055       Box2D.Dynamics.Joints.b2PulleyJoint.b2_minPulleyLength = 2.0;
  10056    });
  10057    Box2D.inherit(b2PulleyJointDef, Box2D.Dynamics.Joints.b2JointDef);
  10058    b2PulleyJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  10059    b2PulleyJointDef.b2PulleyJointDef = function () {
  10060       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  10061       this.groundAnchorA = new b2Vec2();
  10062       this.groundAnchorB = new b2Vec2();
  10063       this.localAnchorA = new b2Vec2();
  10064       this.localAnchorB = new b2Vec2();
  10065    };
  10066    b2PulleyJointDef.prototype.b2PulleyJointDef = function () {
  10067       this.__super.b2JointDef.call(this);
  10068       this.type = b2Joint.e_pulleyJoint;
  10069       this.groundAnchorA.Set((-1.0), 1.0);
  10070       this.groundAnchorB.Set(1.0, 1.0);
  10071       this.localAnchorA.Set((-1.0), 0.0);
  10072       this.localAnchorB.Set(1.0, 0.0);
  10073       this.lengthA = 0.0;
  10074       this.maxLengthA = 0.0;
  10075       this.lengthB = 0.0;
  10076       this.maxLengthB = 0.0;
  10077       this.ratio = 1.0;
  10078       this.collideConnected = true;
  10079    }
  10080    b2PulleyJointDef.prototype.Initialize = function (bA, bB, gaA, gaB, anchorA, anchorB, r) {
  10081       if (r === undefined) r = 0;
  10082       this.bodyA = bA;
  10083       this.bodyB = bB;
  10084       this.groundAnchorA.SetV(gaA);
  10085       this.groundAnchorB.SetV(gaB);
  10086       this.localAnchorA = this.bodyA.GetLocalPoint(anchorA);
  10087       this.localAnchorB = this.bodyB.GetLocalPoint(anchorB);
  10088       var d1X = anchorA.x - gaA.x;
  10089       var d1Y = anchorA.y - gaA.y;
  10090       this.lengthA = Math.sqrt(d1X * d1X + d1Y * d1Y);
  10091       var d2X = anchorB.x - gaB.x;
  10092       var d2Y = anchorB.y - gaB.y;
  10093       this.lengthB = Math.sqrt(d2X * d2X + d2Y * d2Y);
  10094       this.ratio = r;
  10095       var C = this.lengthA + this.ratio * this.lengthB;
  10096       this.maxLengthA = C - this.ratio * b2PulleyJoint.b2_minPulleyLength;
  10097       this.maxLengthB = (C - b2PulleyJoint.b2_minPulleyLength) / this.ratio;
  10098    }
  10099    Box2D.inherit(b2RevoluteJoint, Box2D.Dynamics.Joints.b2Joint);
  10100    b2RevoluteJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  10101    b2RevoluteJoint.b2RevoluteJoint = function () {
  10102       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  10103       this.K = new b2Mat22();
  10104       this.K1 = new b2Mat22();
  10105       this.K2 = new b2Mat22();
  10106       this.K3 = new b2Mat22();
  10107       this.impulse3 = new b2Vec3();
  10108       this.impulse2 = new b2Vec2();
  10109       this.reduced = new b2Vec2();
  10110       this.m_localAnchor1 = new b2Vec2();
  10111       this.m_localAnchor2 = new b2Vec2();
  10112       this.m_impulse = new b2Vec3();
  10113       this.m_mass = new b2Mat33();
  10114    };
  10115    b2RevoluteJoint.prototype.GetAnchorA = function () {
  10116       return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  10117    }
  10118    b2RevoluteJoint.prototype.GetAnchorB = function () {
  10119       return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  10120    }
  10121    b2RevoluteJoint.prototype.GetReactionForce = function (inv_dt) {
  10122       if (inv_dt === undefined) inv_dt = 0;
  10123       return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y);
  10124    }
  10125    b2RevoluteJoint.prototype.GetReactionTorque = function (inv_dt) {
  10126       if (inv_dt === undefined) inv_dt = 0;
  10127       return inv_dt * this.m_impulse.z;
  10128    }
  10129    b2RevoluteJoint.prototype.GetJointAngle = function () {
  10130       return this.m_bodyB.m_sweep.a - this.m_bodyA.m_sweep.a - this.m_referenceAngle;
  10131    }
  10132    b2RevoluteJoint.prototype.GetJointSpeed = function () {
  10133       return this.m_bodyB.m_angularVelocity - this.m_bodyA.m_angularVelocity;
  10134    }
  10135    b2RevoluteJoint.prototype.IsLimitEnabled = function () {
  10136       return this.m_enableLimit;
  10137    }
  10138    b2RevoluteJoint.prototype.EnableLimit = function (flag) {
  10139       this.m_enableLimit = flag;
  10140    }
  10141    b2RevoluteJoint.prototype.GetLowerLimit = function () {
  10142       return this.m_lowerAngle;
  10143    }
  10144    b2RevoluteJoint.prototype.GetUpperLimit = function () {
  10145       return this.m_upperAngle;
  10146    }
  10147    b2RevoluteJoint.prototype.SetLimits = function (lower, upper) {
  10148       if (lower === undefined) lower = 0;
  10149       if (upper === undefined) upper = 0;
  10150       this.m_lowerAngle = lower;
  10151       this.m_upperAngle = upper;
  10152    }
  10153    b2RevoluteJoint.prototype.IsMotorEnabled = function () {
  10154       this.m_bodyA.SetAwake(true);
  10155       this.m_bodyB.SetAwake(true);
  10156       return this.m_enableMotor;
  10157    }
  10158    b2RevoluteJoint.prototype.EnableMotor = function (flag) {
  10159       this.m_enableMotor = flag;
  10160    }
  10161    b2RevoluteJoint.prototype.SetMotorSpeed = function (speed) {
  10162       if (speed === undefined) speed = 0;
  10163       this.m_bodyA.SetAwake(true);
  10164       this.m_bodyB.SetAwake(true);
  10165       this.m_motorSpeed = speed;
  10166    }
  10167    b2RevoluteJoint.prototype.GetMotorSpeed = function () {
  10168       return this.m_motorSpeed;
  10169    }
  10170    b2RevoluteJoint.prototype.SetMaxMotorTorque = function (torque) {
  10171       if (torque === undefined) torque = 0;
  10172       this.m_maxMotorTorque = torque;
  10173    }
  10174    b2RevoluteJoint.prototype.GetMotorTorque = function () {
  10175       return this.m_maxMotorTorque;
  10176    }
  10177    b2RevoluteJoint.prototype.b2RevoluteJoint = function (def) {
  10178       this.__super.b2Joint.call(this, def);
  10179       this.m_localAnchor1.SetV(def.localAnchorA);
  10180       this.m_localAnchor2.SetV(def.localAnchorB);
  10181       this.m_referenceAngle = def.referenceAngle;
  10182       this.m_impulse.SetZero();
  10183       this.m_motorImpulse = 0.0;
  10184       this.m_lowerAngle = def.lowerAngle;
  10185       this.m_upperAngle = def.upperAngle;
  10186       this.m_maxMotorTorque = def.maxMotorTorque;
  10187       this.m_motorSpeed = def.motorSpeed;
  10188       this.m_enableLimit = def.enableLimit;
  10189       this.m_enableMotor = def.enableMotor;
  10190       this.m_limitState = b2Joint.e_inactiveLimit;
  10191    }
  10192    b2RevoluteJoint.prototype.InitVelocityConstraints = function (step) {
  10193       var bA = this.m_bodyA;
  10194       var bB = this.m_bodyB;
  10195       var tMat;
  10196       var tX = 0;
  10197       if (this.m_enableMotor || this.m_enableLimit) {}
  10198       tMat = bA.m_xf.R;
  10199       var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  10200       var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  10201       tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  10202       r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10203       r1X = tX;
  10204       tMat = bB.m_xf.R;
  10205       var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10206       var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10207       tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10208       r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10209       r2X = tX;
  10210       var m1 = bA.m_invMass;
  10211       var m2 = bB.m_invMass;
  10212       var i1 = bA.m_invI;
  10213       var i2 = bB.m_invI;
  10214       this.m_mass.col1.x = m1 + m2 + r1Y * r1Y * i1 + r2Y * r2Y * i2;
  10215       this.m_mass.col2.x = (-r1Y * r1X * i1) - r2Y * r2X * i2;
  10216       this.m_mass.col3.x = (-r1Y * i1) - r2Y * i2;
  10217       this.m_mass.col1.y = this.m_mass.col2.x;
  10218       this.m_mass.col2.y = m1 + m2 + r1X * r1X * i1 + r2X * r2X * i2;
  10219       this.m_mass.col3.y = r1X * i1 + r2X * i2;
  10220       this.m_mass.col1.z = this.m_mass.col3.x;
  10221       this.m_mass.col2.z = this.m_mass.col3.y;
  10222       this.m_mass.col3.z = i1 + i2;
  10223       this.m_motorMass = 1.0 / (i1 + i2);
  10224       if (this.m_enableMotor == false) {
  10225          this.m_motorImpulse = 0.0;
  10226       }
  10227       if (this.m_enableLimit) {
  10228          var jointAngle = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  10229          if (b2Math.Abs(this.m_upperAngle - this.m_lowerAngle) < 2.0 * b2Settings.b2_angularSlop) {
  10230             this.m_limitState = b2Joint.e_equalLimits;
  10231          }
  10232          else if (jointAngle <= this.m_lowerAngle) {
  10233             if (this.m_limitState != b2Joint.e_atLowerLimit) {
  10234                this.m_impulse.z = 0.0;
  10235             }
  10236             this.m_limitState = b2Joint.e_atLowerLimit;
  10237          }
  10238          else if (jointAngle >= this.m_upperAngle) {
  10239             if (this.m_limitState != b2Joint.e_atUpperLimit) {
  10240                this.m_impulse.z = 0.0;
  10241             }
  10242             this.m_limitState = b2Joint.e_atUpperLimit;
  10243          }
  10244          else {
  10245             this.m_limitState = b2Joint.e_inactiveLimit;
  10246             this.m_impulse.z = 0.0;
  10247          }
  10248       }
  10249       else {
  10250          this.m_limitState = b2Joint.e_inactiveLimit;
  10251       }
  10252       if (step.warmStarting) {
  10253          this.m_impulse.x *= step.dtRatio;
  10254          this.m_impulse.y *= step.dtRatio;
  10255          this.m_motorImpulse *= step.dtRatio;
  10256          var PX = this.m_impulse.x;
  10257          var PY = this.m_impulse.y;
  10258          bA.m_linearVelocity.x -= m1 * PX;
  10259          bA.m_linearVelocity.y -= m1 * PY;
  10260          bA.m_angularVelocity -= i1 * ((r1X * PY - r1Y * PX) + this.m_motorImpulse + this.m_impulse.z);
  10261          bB.m_linearVelocity.x += m2 * PX;
  10262          bB.m_linearVelocity.y += m2 * PY;
  10263          bB.m_angularVelocity += i2 * ((r2X * PY - r2Y * PX) + this.m_motorImpulse + this.m_impulse.z);
  10264       }
  10265       else {
  10266          this.m_impulse.SetZero();
  10267          this.m_motorImpulse = 0.0;
  10268       }
  10269    }
  10270    b2RevoluteJoint.prototype.SolveVelocityConstraints = function (step) {
  10271       var bA = this.m_bodyA;
  10272       var bB = this.m_bodyB;
  10273       var tMat;
  10274       var tX = 0;
  10275       var newImpulse = 0;
  10276       var r1X = 0;
  10277       var r1Y = 0;
  10278       var r2X = 0;
  10279       var r2Y = 0;
  10280       var v1 = bA.m_linearVelocity;
  10281       var w1 = bA.m_angularVelocity;
  10282       var v2 = bB.m_linearVelocity;
  10283       var w2 = bB.m_angularVelocity;
  10284       var m1 = bA.m_invMass;
  10285       var m2 = bB.m_invMass;
  10286       var i1 = bA.m_invI;
  10287       var i2 = bB.m_invI;
  10288       if (this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
  10289          var Cdot = w2 - w1 - this.m_motorSpeed;
  10290          var impulse = this.m_motorMass * ((-Cdot));
  10291          var oldImpulse = this.m_motorImpulse;
  10292          var maxImpulse = step.dt * this.m_maxMotorTorque;
  10293          this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, (-maxImpulse), maxImpulse);
  10294          impulse = this.m_motorImpulse - oldImpulse;
  10295          w1 -= i1 * impulse;
  10296          w2 += i2 * impulse;
  10297       }
  10298       if (this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  10299          tMat = bA.m_xf.R;
  10300          r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  10301          r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  10302          tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  10303          r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10304          r1X = tX;
  10305          tMat = bB.m_xf.R;
  10306          r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10307          r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10308          tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10309          r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10310          r2X = tX;
  10311          var Cdot1X = v2.x + ((-w2 * r2Y)) - v1.x - ((-w1 * r1Y));
  10312          var Cdot1Y = v2.y + (w2 * r2X) - v1.y - (w1 * r1X);
  10313          var Cdot2 = w2 - w1;
  10314          this.m_mass.Solve33(this.impulse3, (-Cdot1X), (-Cdot1Y), (-Cdot2));
  10315          if (this.m_limitState == b2Joint.e_equalLimits) {
  10316             this.m_impulse.Add(this.impulse3);
  10317          }
  10318          else if (this.m_limitState == b2Joint.e_atLowerLimit) {
  10319             newImpulse = this.m_impulse.z + this.impulse3.z;
  10320             if (newImpulse < 0.0) {
  10321                this.m_mass.Solve22(this.reduced, (-Cdot1X), (-Cdot1Y));
  10322                this.impulse3.x = this.reduced.x;
  10323                this.impulse3.y = this.reduced.y;
  10324                this.impulse3.z = (-this.m_impulse.z);
  10325                this.m_impulse.x += this.reduced.x;
  10326                this.m_impulse.y += this.reduced.y;
  10327                this.m_impulse.z = 0.0;
  10328             }
  10329          }
  10330          else if (this.m_limitState == b2Joint.e_atUpperLimit) {
  10331             newImpulse = this.m_impulse.z + this.impulse3.z;
  10332             if (newImpulse > 0.0) {
  10333                this.m_mass.Solve22(this.reduced, (-Cdot1X), (-Cdot1Y));
  10334                this.impulse3.x = this.reduced.x;
  10335                this.impulse3.y = this.reduced.y;
  10336                this.impulse3.z = (-this.m_impulse.z);
  10337                this.m_impulse.x += this.reduced.x;
  10338                this.m_impulse.y += this.reduced.y;
  10339                this.m_impulse.z = 0.0;
  10340             }
  10341          }
  10342          v1.x -= m1 * this.impulse3.x;
  10343          v1.y -= m1 * this.impulse3.y;
  10344          w1 -= i1 * (r1X * this.impulse3.y - r1Y * this.impulse3.x + this.impulse3.z);
  10345          v2.x += m2 * this.impulse3.x;
  10346          v2.y += m2 * this.impulse3.y;
  10347          w2 += i2 * (r2X * this.impulse3.y - r2Y * this.impulse3.x + this.impulse3.z);
  10348       }
  10349       else {
  10350          tMat = bA.m_xf.R;
  10351          r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  10352          r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  10353          tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  10354          r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10355          r1X = tX;
  10356          tMat = bB.m_xf.R;
  10357          r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10358          r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10359          tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10360          r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10361          r2X = tX;
  10362          var CdotX = v2.x + ((-w2 * r2Y)) - v1.x - ((-w1 * r1Y));
  10363          var CdotY = v2.y + (w2 * r2X) - v1.y - (w1 * r1X);
  10364          this.m_mass.Solve22(this.impulse2, (-CdotX), (-CdotY));
  10365          this.m_impulse.x += this.impulse2.x;
  10366          this.m_impulse.y += this.impulse2.y;
  10367          v1.x -= m1 * this.impulse2.x;
  10368          v1.y -= m1 * this.impulse2.y;
  10369          w1 -= i1 * (r1X * this.impulse2.y - r1Y * this.impulse2.x);
  10370          v2.x += m2 * this.impulse2.x;
  10371          v2.y += m2 * this.impulse2.y;
  10372          w2 += i2 * (r2X * this.impulse2.y - r2Y * this.impulse2.x);
  10373       }
  10374       bA.m_linearVelocity.SetV(v1);
  10375       bA.m_angularVelocity = w1;
  10376       bB.m_linearVelocity.SetV(v2);
  10377       bB.m_angularVelocity = w2;
  10378    }
  10379    b2RevoluteJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  10380       if (baumgarte === undefined) baumgarte = 0;
  10381       var oldLimitImpulse = 0;
  10382       var C = 0;
  10383       var tMat;
  10384       var bA = this.m_bodyA;
  10385       var bB = this.m_bodyB;
  10386       var angularError = 0.0;
  10387       var positionError = 0.0;
  10388       var tX = 0;
  10389       var impulseX = 0;
  10390       var impulseY = 0;
  10391       if (this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  10392          var angle = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  10393          var limitImpulse = 0.0;
  10394          if (this.m_limitState == b2Joint.e_equalLimits) {
  10395             C = b2Math.Clamp(angle - this.m_lowerAngle, (-b2Settings.b2_maxAngularCorrection), b2Settings.b2_maxAngularCorrection);
  10396             limitImpulse = (-this.m_motorMass * C);
  10397             angularError = b2Math.Abs(C);
  10398          }
  10399          else if (this.m_limitState == b2Joint.e_atLowerLimit) {
  10400             C = angle - this.m_lowerAngle;
  10401             angularError = (-C);
  10402             C = b2Math.Clamp(C + b2Settings.b2_angularSlop, (-b2Settings.b2_maxAngularCorrection), 0.0);
  10403             limitImpulse = (-this.m_motorMass * C);
  10404          }
  10405          else if (this.m_limitState == b2Joint.e_atUpperLimit) {
  10406             C = angle - this.m_upperAngle;
  10407             angularError = C;
  10408             C = b2Math.Clamp(C - b2Settings.b2_angularSlop, 0.0, b2Settings.b2_maxAngularCorrection);
  10409             limitImpulse = (-this.m_motorMass * C);
  10410          }
  10411          bA.m_sweep.a -= bA.m_invI * limitImpulse;
  10412          bB.m_sweep.a += bB.m_invI * limitImpulse;
  10413          bA.SynchronizeTransform();
  10414          bB.SynchronizeTransform();
  10415       } {
  10416          tMat = bA.m_xf.R;
  10417          var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  10418          var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  10419          tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  10420          r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10421          r1X = tX;
  10422          tMat = bB.m_xf.R;
  10423          var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10424          var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10425          tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10426          r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10427          r2X = tX;
  10428          var CX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  10429          var CY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  10430          var CLengthSquared = CX * CX + CY * CY;
  10431          var CLength = Math.sqrt(CLengthSquared);
  10432          positionError = CLength;
  10433          var invMass1 = bA.m_invMass;
  10434          var invMass2 = bB.m_invMass;
  10435          var invI1 = bA.m_invI;
  10436          var invI2 = bB.m_invI;
  10437          var k_allowedStretch = 10.0 * b2Settings.b2_linearSlop;
  10438          if (CLengthSquared > k_allowedStretch * k_allowedStretch) {
  10439             var uX = CX / CLength;
  10440             var uY = CY / CLength;
  10441             var k = invMass1 + invMass2;
  10442             var m = 1.0 / k;
  10443             impulseX = m * ((-CX));
  10444             impulseY = m * ((-CY));
  10445             var k_beta = 0.5;
  10446             bA.m_sweep.c.x -= k_beta * invMass1 * impulseX;
  10447             bA.m_sweep.c.y -= k_beta * invMass1 * impulseY;
  10448             bB.m_sweep.c.x += k_beta * invMass2 * impulseX;
  10449             bB.m_sweep.c.y += k_beta * invMass2 * impulseY;
  10450             CX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  10451             CY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  10452          }
  10453          this.K1.col1.x = invMass1 + invMass2;
  10454          this.K1.col2.x = 0.0;
  10455          this.K1.col1.y = 0.0;
  10456          this.K1.col2.y = invMass1 + invMass2;
  10457          this.K2.col1.x = invI1 * r1Y * r1Y;
  10458          this.K2.col2.x = (-invI1 * r1X * r1Y);
  10459          this.K2.col1.y = (-invI1 * r1X * r1Y);
  10460          this.K2.col2.y = invI1 * r1X * r1X;
  10461          this.K3.col1.x = invI2 * r2Y * r2Y;
  10462          this.K3.col2.x = (-invI2 * r2X * r2Y);
  10463          this.K3.col1.y = (-invI2 * r2X * r2Y);
  10464          this.K3.col2.y = invI2 * r2X * r2X;
  10465          this.K.SetM(this.K1);
  10466          this.K.AddM(this.K2);
  10467          this.K.AddM(this.K3);
  10468          this.K.Solve(b2RevoluteJoint.tImpulse, (-CX), (-CY));
  10469          impulseX = b2RevoluteJoint.tImpulse.x;
  10470          impulseY = b2RevoluteJoint.tImpulse.y;
  10471          bA.m_sweep.c.x -= bA.m_invMass * impulseX;
  10472          bA.m_sweep.c.y -= bA.m_invMass * impulseY;
  10473          bA.m_sweep.a -= bA.m_invI * (r1X * impulseY - r1Y * impulseX);
  10474          bB.m_sweep.c.x += bB.m_invMass * impulseX;
  10475          bB.m_sweep.c.y += bB.m_invMass * impulseY;
  10476          bB.m_sweep.a += bB.m_invI * (r2X * impulseY - r2Y * impulseX);
  10477          bA.SynchronizeTransform();
  10478          bB.SynchronizeTransform();
  10479       }
  10480       return positionError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
  10481    }
  10482    Box2D.postDefs.push(function () {
  10483       Box2D.Dynamics.Joints.b2RevoluteJoint.tImpulse = new b2Vec2();
  10484    });
  10485    Box2D.inherit(b2RevoluteJointDef, Box2D.Dynamics.Joints.b2JointDef);
  10486    b2RevoluteJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  10487    b2RevoluteJointDef.b2RevoluteJointDef = function () {
  10488       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  10489       this.localAnchorA = new b2Vec2();
  10490       this.localAnchorB = new b2Vec2();
  10491    };
  10492    b2RevoluteJointDef.prototype.b2RevoluteJointDef = function () {
  10493       this.__super.b2JointDef.call(this);
  10494       this.type = b2Joint.e_revoluteJoint;
  10495       this.localAnchorA.Set(0.0, 0.0);
  10496       this.localAnchorB.Set(0.0, 0.0);
  10497       this.referenceAngle = 0.0;
  10498       this.lowerAngle = 0.0;
  10499       this.upperAngle = 0.0;
  10500       this.maxMotorTorque = 0.0;
  10501       this.motorSpeed = 0.0;
  10502       this.enableLimit = false;
  10503       this.enableMotor = false;
  10504    }
  10505    b2RevoluteJointDef.prototype.Initialize = function (bA, bB, anchor) {
  10506       this.bodyA = bA;
  10507       this.bodyB = bB;
  10508       this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
  10509       this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
  10510       this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle();
  10511    }
  10512    Box2D.inherit(b2WeldJoint, Box2D.Dynamics.Joints.b2Joint);
  10513    b2WeldJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  10514    b2WeldJoint.b2WeldJoint = function () {
  10515       Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  10516       this.m_localAnchorA = new b2Vec2();
  10517       this.m_localAnchorB = new b2Vec2();
  10518       this.m_impulse = new b2Vec3();
  10519       this.m_mass = new b2Mat33();
  10520    };
  10521    b2WeldJoint.prototype.GetAnchorA = function () {
  10522       return this.m_bodyA.GetWorldPoint(this.m_localAnchorA);
  10523    }
  10524    b2WeldJoint.prototype.GetAnchorB = function () {
  10525       return this.m_bodyB.GetWorldPoint(this.m_localAnchorB);
  10526    }
  10527    b2WeldJoint.prototype.GetReactionForce = function (inv_dt) {
  10528       if (inv_dt === undefined) inv_dt = 0;
  10529       return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y);
  10530    }
  10531    b2WeldJoint.prototype.GetReactionTorque = function (inv_dt) {
  10532       if (inv_dt === undefined) inv_dt = 0;
  10533       return inv_dt * this.m_impulse.z;
  10534    }
  10535    b2WeldJoint.prototype.b2WeldJoint = function (def) {
  10536       this.__super.b2Joint.call(this, def);
  10537       this.m_localAnchorA.SetV(def.localAnchorA);
  10538       this.m_localAnchorB.SetV(def.localAnchorB);
  10539       this.m_referenceAngle = def.referenceAngle;
  10540       this.m_impulse.SetZero();
  10541       this.m_mass = new b2Mat33();
  10542    }
  10543    b2WeldJoint.prototype.InitVelocityConstraints = function (step) {
  10544       var tMat;
  10545       var tX = 0;
  10546       var bA = this.m_bodyA;
  10547       var bB = this.m_bodyB;
  10548       tMat = bA.m_xf.R;
  10549       var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  10550       var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  10551       tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
  10552       rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
  10553       rAX = tX;
  10554       tMat = bB.m_xf.R;
  10555       var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  10556       var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  10557       tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
  10558       rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
  10559       rBX = tX;
  10560       var mA = bA.m_invMass;
  10561       var mB = bB.m_invMass;
  10562       var iA = bA.m_invI;
  10563       var iB = bB.m_invI;
  10564       this.m_mass.col1.x = mA + mB + rAY * rAY * iA + rBY * rBY * iB;
  10565       this.m_mass.col2.x = (-rAY * rAX * iA) - rBY * rBX * iB;
  10566       this.m_mass.col3.x = (-rAY * iA) - rBY * iB;
  10567       this.m_mass.col1.y = this.m_mass.col2.x;
  10568       this.m_mass.col2.y = mA + mB + rAX * rAX * iA + rBX * rBX * iB;
  10569       this.m_mass.col3.y = rAX * iA + rBX * iB;
  10570       this.m_mass.col1.z = this.m_mass.col3.x;
  10571       this.m_mass.col2.z = this.m_mass.col3.y;
  10572       this.m_mass.col3.z = iA + iB;
  10573       if (step.warmStarting) {
  10574          this.m_impulse.x *= step.dtRatio;
  10575          this.m_impulse.y *= step.dtRatio;
  10576          this.m_impulse.z *= step.dtRatio;
  10577          bA.m_linearVelocity.x -= mA * this.m_impulse.x;
  10578          bA.m_linearVelocity.y -= mA * this.m_impulse.y;
  10579          bA.m_angularVelocity -= iA * (rAX * this.m_impulse.y - rAY * this.m_impulse.x + this.m_impulse.z);
  10580          bB.m_linearVelocity.x += mB * this.m_impulse.x;
  10581          bB.m_linearVelocity.y += mB * this.m_impulse.y;
  10582          bB.m_angularVelocity += iB * (rBX * this.m_impulse.y - rBY * this.m_impulse.x + this.m_impulse.z);
  10583       }
  10584       else {
  10585          this.m_impulse.SetZero();
  10586       }
  10587    }
  10588    b2WeldJoint.prototype.SolveVelocityConstraints = function (step) {
  10589       var tMat;
  10590       var tX = 0;
  10591       var bA = this.m_bodyA;
  10592       var bB = this.m_bodyB;
  10593       var vA = bA.m_linearVelocity;
  10594       var wA = bA.m_angularVelocity;
  10595       var vB = bB.m_linearVelocity;
  10596       var wB = bB.m_angularVelocity;
  10597       var mA = bA.m_invMass;
  10598       var mB = bB.m_invMass;
  10599       var iA = bA.m_invI;
  10600       var iB = bB.m_invI;
  10601       tMat = bA.m_xf.R;
  10602       var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  10603       var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  10604       tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
  10605       rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
  10606       rAX = tX;
  10607       tMat = bB.m_xf.R;
  10608       var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  10609       var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  10610       tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
  10611       rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
  10612       rBX = tX;
  10613       var Cdot1X = vB.x - wB * rBY - vA.x + wA * rAY;
  10614       var Cdot1Y = vB.y + wB * rBX - vA.y - wA * rAX;
  10615       var Cdot2 = wB - wA;
  10616       var impulse = new b2Vec3();
  10617       this.m_mass.Solve33(impulse, (-Cdot1X), (-Cdot1Y), (-Cdot2));
  10618       this.m_impulse.Add(impulse);
  10619       vA.x -= mA * impulse.x;
  10620       vA.y -= mA * impulse.y;
  10621       wA -= iA * (rAX * impulse.y - rAY * impulse.x + impulse.z);
  10622       vB.x += mB * impulse.x;
  10623       vB.y += mB * impulse.y;
  10624       wB += iB * (rBX * impulse.y - rBY * impulse.x + impulse.z);
  10625       bA.m_angularVelocity = wA;
  10626       bB.m_angularVelocity = wB;
  10627    }
  10628    b2WeldJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  10629       if (baumgarte === undefined) baumgarte = 0;
  10630       var tMat;
  10631       var tX = 0;
  10632       var bA = this.m_bodyA;
  10633       var bB = this.m_bodyB;
  10634       tMat = bA.m_xf.R;
  10635       var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  10636       var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  10637       tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
  10638       rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
  10639       rAX = tX;
  10640       tMat = bB.m_xf.R;
  10641       var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  10642       var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  10643       tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
  10644       rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
  10645       rBX = tX;
  10646       var mA = bA.m_invMass;
  10647       var mB = bB.m_invMass;
  10648       var iA = bA.m_invI;
  10649       var iB = bB.m_invI;
  10650       var C1X = bB.m_sweep.c.x + rBX - bA.m_sweep.c.x - rAX;
  10651       var C1Y = bB.m_sweep.c.y + rBY - bA.m_sweep.c.y - rAY;
  10652       var C2 = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  10653       var k_allowedStretch = 10.0 * b2Settings.b2_linearSlop;
  10654       var positionError = Math.sqrt(C1X * C1X + C1Y * C1Y);
  10655       var angularError = b2Math.Abs(C2);
  10656       if (positionError > k_allowedStretch) {
  10657          iA *= 1.0;
  10658          iB *= 1.0;
  10659       }
  10660       this.m_mass.col1.x = mA + mB + rAY * rAY * iA + rBY * rBY * iB;
  10661       this.m_mass.col2.x = (-rAY * rAX * iA) - rBY * rBX * iB;
  10662       this.m_mass.col3.x = (-rAY * iA) - rBY * iB;
  10663       this.m_mass.col1.y = this.m_mass.col2.x;
  10664       this.m_mass.col2.y = mA + mB + rAX * rAX * iA + rBX * rBX * iB;
  10665       this.m_mass.col3.y = rAX * iA + rBX * iB;
  10666       this.m_mass.col1.z = this.m_mass.col3.x;
  10667       this.m_mass.col2.z = this.m_mass.col3.y;
  10668       this.m_mass.col3.z = iA + iB;
  10669       var impulse = new b2Vec3();
  10670       this.m_mass.Solve33(impulse, (-C1X), (-C1Y), (-C2));
  10671       bA.m_sweep.c.x -= mA * impulse.x;
  10672       bA.m_sweep.c.y -= mA * impulse.y;
  10673       bA.m_sweep.a -= iA * (rAX * impulse.y - rAY * impulse.x + impulse.z);
  10674       bB.m_sweep.c.x += mB * impulse.x;
  10675       bB.m_sweep.c.y += mB * impulse.y;
  10676       bB.m_sweep.a += iB * (rBX * impulse.y - rBY * impulse.x + impulse.z);
  10677       bA.SynchronizeTransform();
  10678       bB.SynchronizeTransform();
  10679       return positionError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
  10680    }
  10681    Box2D.inherit(b2WeldJointDef, Box2D.Dynamics.Joints.b2JointDef);
  10682    b2WeldJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  10683    b2WeldJointDef.b2WeldJointDef = function () {
  10684       Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  10685       this.localAnchorA = new b2Vec2();
  10686       this.localAnchorB = new b2Vec2();
  10687    };
  10688    b2WeldJointDef.prototype.b2WeldJointDef = function () {
  10689       this.__super.b2JointDef.call(this);
  10690       this.type = b2Joint.e_weldJoint;
  10691       this.referenceAngle = 0.0;
  10692    }
  10693    b2WeldJointDef.prototype.Initialize = function (bA, bB, anchor) {
  10694       this.bodyA = bA;
  10695       this.bodyB = bB;
  10696       this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchor));
  10697       this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchor));
  10698       this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle();
  10699    }
  10700 })();
  10701 (function () {
  10702    var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
  10703    b2DebugDraw.b2DebugDraw = function () {
  10704       this.m_drawScale = 1.0;
  10705       this.m_lineThickness = 1.0;
  10706       this.m_alpha = 1.0;
  10707       this.m_fillAlpha = 1.0;
  10708       this.m_xformScale = 1.0;
  10709       var __this = this;
  10710       //#WORKAROUND
  10711       this.m_sprite = {
  10712          graphics: {
  10713              clear: function ()
  10714              {
  10715 
  10716                  //__this.m_ctx.clearRect(0, 0, __this.m_ctx.canvas.width, __this.m_ctx.canvas.height)
  10717                  //__this.m_ctx.translate(100, 100);
  10718             }
  10719          }
  10720       };
  10721    };
  10722    b2DebugDraw.prototype._color = function (color, alpha) {
  10723       return "rgba(" + ((color & 0xFF0000) >> 16) + "," + ((color & 0xFF00) >> 8) + "," + (color & 0xFF) + "," + alpha + ")";
  10724    };
  10725    b2DebugDraw.prototype.b2DebugDraw = function () {
  10726       this.m_drawFlags = 0;
  10727    };
  10728    b2DebugDraw.prototype.SetFlags = function (flags) {
  10729       if (flags === undefined) flags = 0;
  10730       this.m_drawFlags = flags;
  10731    };
  10732    b2DebugDraw.prototype.GetFlags = function () {
  10733       return this.m_drawFlags;
  10734    };
  10735    b2DebugDraw.prototype.AppendFlags = function (flags) {
  10736       if (flags === undefined) flags = 0;
  10737       this.m_drawFlags |= flags;
  10738    };
  10739    b2DebugDraw.prototype.ClearFlags = function (flags) {
  10740       if (flags === undefined) flags = 0;
  10741       this.m_drawFlags &= ~flags;
  10742    };
  10743    b2DebugDraw.prototype.SetSprite = function (sprite) {
  10744       this.m_ctx = sprite;
  10745    };
  10746    b2DebugDraw.prototype.GetSprite = function () {
  10747       return this.m_ctx;
  10748    };
  10749    b2DebugDraw.prototype.SetDrawScale = function (drawScale) {
  10750       if (drawScale === undefined) drawScale = 0;
  10751       this.m_drawScale = drawScale;
  10752    };
  10753    b2DebugDraw.prototype.GetDrawScale = function () {
  10754       return this.m_drawScale;
  10755    };
  10756    b2DebugDraw.prototype.SetLineThickness = function (lineThickness) {
  10757       if (lineThickness === undefined) lineThickness = 0;
  10758       this.m_lineThickness = lineThickness;
  10759       this.m_ctx.strokeWidth = lineThickness;
  10760    };
  10761    b2DebugDraw.prototype.GetLineThickness = function () {
  10762       return this.m_lineThickness;
  10763    };
  10764    b2DebugDraw.prototype.SetAlpha = function (alpha) {
  10765       if (alpha === undefined) alpha = 0;
  10766       this.m_alpha = alpha;
  10767    };
  10768    b2DebugDraw.prototype.GetAlpha = function () {
  10769       return this.m_alpha;
  10770    };
  10771    b2DebugDraw.prototype.SetFillAlpha = function (alpha) {
  10772       if (alpha === undefined) alpha = 0;
  10773       this.m_fillAlpha = alpha;
  10774    };
  10775    b2DebugDraw.prototype.GetFillAlpha = function () {
  10776       return this.m_fillAlpha;
  10777    };
  10778    b2DebugDraw.prototype.SetXFormScale = function (xformScale) {
  10779       if (xformScale === undefined) xformScale = 0;
  10780       this.m_xformScale = xformScale;
  10781    };
  10782    b2DebugDraw.prototype.GetXFormScale = function () {
  10783       return this.m_xformScale;
  10784    };
  10785    b2DebugDraw.prototype.DrawPolygon = function (vertices, vertexCount, color) {
  10786       if (!vertexCount) return;
  10787       var s = this.m_ctx;
  10788       var drawScale = this.m_drawScale;
  10789       s.beginPath();
  10790       s.strokeStyle = this._color(color.color, this.m_alpha);
  10791       s.moveTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
  10792       for (var i = 1; i < vertexCount; i++) {
  10793          s.lineTo(vertices[i].x * drawScale, vertices[i].y * drawScale);
  10794       }
  10795       s.lineTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
  10796       s.closePath();
  10797       s.stroke();
  10798    };
  10799    b2DebugDraw.prototype.DrawSolidPolygon = function (vertices, vertexCount, color) {
  10800       if (!vertexCount) return;
  10801       var s = this.m_ctx;
  10802       var drawScale = this.m_drawScale;
  10803       s.beginPath();
  10804       s.strokeStyle = this._color(color.color, this.m_alpha);
  10805       s.fillStyle = this._color(color.color, this.m_fillAlpha);
  10806       s.moveTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
  10807       for (var i = 1; i < vertexCount; i++) {
  10808          s.lineTo(vertices[i].x * drawScale, vertices[i].y * drawScale);
  10809       }
  10810       s.lineTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
  10811       s.closePath();
  10812       s.fill();
  10813       s.stroke();
  10814    };
  10815    b2DebugDraw.prototype.DrawCircle = function (center, radius, color) {
  10816       if (!radius) return;
  10817       var s = this.m_ctx;
  10818       var drawScale = this.m_drawScale;
  10819       s.beginPath();
  10820       s.strokeStyle = this._color(color.color, this.m_alpha);
  10821       s.arc(center.x * drawScale, center.y * drawScale, radius * drawScale, 0, Math.PI * 2, true);
  10822       s.closePath();
  10823       s.stroke();
  10824    };
  10825    b2DebugDraw.prototype.DrawSolidCircle = function (center, radius, axis, color) {
  10826       if (!radius) return;
  10827       var s = this.m_ctx,
  10828          drawScale = this.m_drawScale,
  10829          cx = center.x * drawScale,
  10830          cy = center.y * drawScale;
  10831       s.moveTo(0, 0);
  10832       s.beginPath();
  10833       s.strokeStyle = this._color(color.color, this.m_alpha);
  10834       s.fillStyle = this._color(color.color, this.m_fillAlpha);
  10835       s.arc(cx, cy, radius * drawScale, 0, Math.PI * 2, true);
  10836       s.moveTo(cx, cy);
  10837       s.lineTo((center.x + axis.x * radius) * drawScale, (center.y + axis.y * radius) * drawScale);
  10838       s.closePath();
  10839       s.fill();
  10840       s.stroke();
  10841    };
  10842    b2DebugDraw.prototype.DrawSegment = function (p1, p2, color) {
  10843       var s = this.m_ctx,
  10844          drawScale = this.m_drawScale;
  10845       s.strokeStyle = this._color(color.color, this.m_alpha);
  10846       s.beginPath();
  10847       s.moveTo(p1.x * drawScale, p1.y * drawScale);
  10848       s.lineTo(p2.x * drawScale, p2.y * drawScale);
  10849       s.closePath();
  10850       s.stroke();
  10851    };
  10852    b2DebugDraw.prototype.DrawTransform = function (xf) {
  10853       var s = this.m_ctx,
  10854          drawScale = this.m_drawScale;
  10855       s.beginPath();
  10856       s.strokeStyle = this._color(0xff0000, this.m_alpha);
  10857       s.moveTo(xf.position.x * drawScale, xf.position.y * drawScale);
  10858       s.lineTo((xf.position.x + this.m_xformScale * xf.R.col1.x) * drawScale, (xf.position.y + this.m_xformScale * xf.R.col1.y) * drawScale);
  10859 
  10860       s.strokeStyle = this._color(0xff00, this.m_alpha);
  10861       s.moveTo(xf.position.x * drawScale, xf.position.y * drawScale);
  10862       s.lineTo((xf.position.x + this.m_xformScale * xf.R.col2.x) * drawScale, (xf.position.y + this.m_xformScale * xf.R.col2.y) * drawScale);
  10863       s.closePath();
  10864       s.stroke();
  10865    };
  10866 })(); //post-definitions
  10867 var i;
  10868 for (i = 0; i < Box2D.postDefs.length; ++i) Box2D.postDefs[i]();
  10869 delete Box2D.postDefs;