/js/DemoBox2D/lib/box2d.js
JavaScript | 11391 lines | 11391 code | 0 blank | 0 comment | 1150 complexity | cedefc0fd03aee7b397915de02ca05e6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- function extend(a, b) {
- for(var c in b) {
- a[c] = b[c]
- }
- }
- function isInstanceOf(obj, _constructor) {
- while(typeof obj === "object") {
- if(obj.constructor === _constructor) {
- return true
- }
- obj = obj._super
- }
- return false
- }
- ;var b2BoundValues = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2BoundValues.prototype.__constructor = function() {
- this.lowerValues = new Array;
- this.lowerValues[0] = 0;
- this.lowerValues[1] = 0;
- this.upperValues = new Array;
- this.upperValues[0] = 0;
- this.upperValues[1] = 0
- };
- b2BoundValues.prototype.__varz = function() {
- };
- b2BoundValues.prototype.lowerValues = null;
- b2BoundValues.prototype.upperValues = null;var b2PairManager = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2PairManager.prototype.__constructor = function() {
- this.m_pairs = new Array;
- this.m_pairBuffer = new Array;
- this.m_pairCount = 0;
- this.m_pairBufferCount = 0;
- this.m_freePair = null
- };
- b2PairManager.prototype.__varz = function() {
- };
- b2PairManager.prototype.AddPair = function(proxy1, proxy2) {
- var pair = proxy1.pairs[proxy2];
- if(pair != null) {
- return pair
- }
- if(this.m_freePair == null) {
- this.m_freePair = new b2Pair;
- this.m_pairs.push(this.m_freePair)
- }
- pair = this.m_freePair;
- this.m_freePair = pair.next;
- pair.proxy1 = proxy1;
- pair.proxy2 = proxy2;
- pair.status = 0;
- pair.userData = null;
- pair.next = null;
- proxy1.pairs[proxy2] = pair;
- proxy2.pairs[proxy1] = pair;
- ++this.m_pairCount;
- return pair
- };
- b2PairManager.prototype.RemovePair = function(proxy1, proxy2) {
- var pair = proxy1.pairs[proxy2];
- if(pair == null) {
- return null
- }
- var userData = pair.userData;
- delete proxy1.pairs[proxy2];
- delete proxy2.pairs[proxy1];
- pair.next = this.m_freePair;
- pair.proxy1 = null;
- pair.proxy2 = null;
- pair.userData = null;
- pair.status = 0;
- this.m_freePair = pair;
- --this.m_pairCount;
- return userData
- };
- b2PairManager.prototype.Find = function(proxy1, proxy2) {
- return proxy1.pairs[proxy2]
- };
- b2PairManager.prototype.ValidateBuffer = function() {
- };
- b2PairManager.prototype.ValidateTable = function() {
- };
- b2PairManager.prototype.Initialize = function(broadPhase) {
- this.m_broadPhase = broadPhase
- };
- b2PairManager.prototype.AddBufferedPair = function(proxy1, proxy2) {
- var pair = this.AddPair(proxy1, proxy2);
- if(pair.IsBuffered() == false) {
- pair.SetBuffered();
- this.m_pairBuffer[this.m_pairBufferCount] = pair;
- ++this.m_pairBufferCount
- }
- pair.ClearRemoved();
- if(b2BroadPhase.s_validate) {
- this.ValidateBuffer()
- }
- };
- b2PairManager.prototype.RemoveBufferedPair = function(proxy1, proxy2) {
- var pair = this.Find(proxy1, proxy2);
- if(pair == null) {
- return
- }
- if(pair.IsBuffered() == false) {
- pair.SetBuffered();
- this.m_pairBuffer[this.m_pairBufferCount] = pair;
- ++this.m_pairBufferCount
- }
- pair.SetRemoved();
- if(b2BroadPhase.s_validate) {
- this.ValidateBuffer()
- }
- };
- b2PairManager.prototype.Commit = function(callback) {
- var i = 0;
- var removeCount = 0;
- for(i = 0;i < this.m_pairBufferCount;++i) {
- var pair = this.m_pairBuffer[i];
- pair.ClearBuffered();
- var proxy1 = pair.proxy1;
- var proxy2 = pair.proxy2;
- if(pair.IsRemoved()) {
- }else {
- if(pair.IsFinal() == false) {
- callback(proxy1.userData, proxy2.userData)
- }
- }
- }
- this.m_pairBufferCount = 0;
- if(b2BroadPhase.s_validate) {
- this.ValidateTable()
- }
- };
- b2PairManager.prototype.m_broadPhase = null;
- b2PairManager.prototype.m_pairs = null;
- b2PairManager.prototype.m_freePair = null;
- b2PairManager.prototype.m_pairCount = 0;
- b2PairManager.prototype.m_pairBuffer = null;
- b2PairManager.prototype.m_pairBufferCount = 0;var b2TimeStep = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2TimeStep.prototype.__constructor = function() {
- };
- b2TimeStep.prototype.__varz = function() {
- };
- b2TimeStep.prototype.Set = function(step) {
- this.dt = step.dt;
- this.inv_dt = step.inv_dt;
- this.positionIterations = step.positionIterations;
- this.velocityIterations = step.velocityIterations;
- this.warmStarting = step.warmStarting
- };
- b2TimeStep.prototype.dt = null;
- b2TimeStep.prototype.inv_dt = null;
- b2TimeStep.prototype.dtRatio = null;
- b2TimeStep.prototype.velocityIterations = 0;
- b2TimeStep.prototype.positionIterations = 0;
- b2TimeStep.prototype.warmStarting = null;var b2Controller = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2Controller.prototype.__constructor = function() {
- };
- b2Controller.prototype.__varz = function() {
- };
- b2Controller.prototype.Step = function(step) {
- };
- b2Controller.prototype.Draw = function(debugDraw) {
- };
- b2Controller.prototype.AddBody = function(body) {
- var edge = new b2ControllerEdge;
- edge.controller = this;
- edge.body = body;
- edge.nextBody = m_bodyList;
- edge.prevBody = null;
- m_bodyList = edge;
- if(edge.nextBody) {
- edge.nextBody.prevBody = edge
- }
- m_bodyCount++;
- edge.nextController = body.m_controllerList;
- edge.prevController = null;
- body.m_controllerList = edge;
- if(edge.nextController) {
- edge.nextController.prevController = edge
- }
- body.m_controllerCount++
- };
- b2Controller.prototype.RemoveBody = function(body) {
- var edge = body.m_controllerList;
- while(edge && edge.controller != this) {
- edge = edge.nextController
- }
- if(edge.prevBody) {
- edge.prevBody.nextBody = edge.nextBody
- }
- if(edge.nextBody) {
- edge.nextBody.prevBody = edge.prevBody
- }
- if(edge.nextController) {
- edge.nextController.prevController = edge.prevController
- }
- if(edge.prevController) {
- edge.prevController.nextController = edge.nextController
- }
- if(m_bodyList == edge) {
- m_bodyList = edge.nextBody
- }
- if(body.m_controllerList == edge) {
- body.m_controllerList = edge.nextController
- }
- body.m_controllerCount--;
- m_bodyCount--
- };
- b2Controller.prototype.Clear = function() {
- while(m_bodyList) {
- this.RemoveBody(m_bodyList.body)
- }
- };
- b2Controller.prototype.GetNext = function() {
- return this.m_next
- };
- b2Controller.prototype.GetWorld = function() {
- return this.m_world
- };
- b2Controller.prototype.GetBodyList = function() {
- return m_bodyList
- };
- b2Controller.prototype.m_next = null;
- b2Controller.prototype.m_prev = null;
- b2Controller.prototype.m_world = null;var b2GravityController = function() {
- b2Controller.prototype.__varz.call(this);
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- extend(b2GravityController.prototype, b2Controller.prototype);
- b2GravityController.prototype._super = b2Controller.prototype;
- b2GravityController.prototype.__constructor = function() {
- this._super.__constructor.apply(this, arguments)
- };
- b2GravityController.prototype.__varz = function() {
- };
- b2GravityController.prototype.Step = function(step) {
- var i = null;
- var body1 = null;
- var p1 = null;
- var mass1 = 0;
- var j = null;
- var body2 = null;
- var p2 = null;
- var dx = 0;
- var dy = 0;
- var r2 = 0;
- var f = null;
- if(this.invSqr) {
- for(i = m_bodyList;i;i = i.nextBody) {
- body1 = i.body;
- p1 = body1.GetWorldCenter();
- mass1 = body1.GetMass();
- for(j = m_bodyList;j != i;j = j.nextBody) {
- body2 = j.body;
- p2 = body2.GetWorldCenter();
- dx = p2.x - p1.x;
- dy = p2.y - p1.y;
- r2 = dx * dx + dy * dy;
- if(r2 < Number.MIN_VALUE) {
- continue
- }
- f = new b2Vec2(dx, dy);
- f.Multiply(this.G / r2 / Math.sqrt(r2) * mass1 * body2.GetMass());
- if(body1.IsAwake()) {
- body1.ApplyForce(f, p1)
- }
- f.Multiply(-1);
- if(body2.IsAwake()) {
- body2.ApplyForce(f, p2)
- }
- }
- }
- }else {
- for(i = m_bodyList;i;i = i.nextBody) {
- body1 = i.body;
- p1 = body1.GetWorldCenter();
- mass1 = body1.GetMass();
- for(j = m_bodyList;j != i;j = j.nextBody) {
- body2 = j.body;
- p2 = body2.GetWorldCenter();
- dx = p2.x - p1.x;
- dy = p2.y - p1.y;
- r2 = dx * dx + dy * dy;
- if(r2 < Number.MIN_VALUE) {
- continue
- }
- f = new b2Vec2(dx, dy);
- f.Multiply(this.G / r2 * mass1 * body2.GetMass());
- if(body1.IsAwake()) {
- body1.ApplyForce(f, p1)
- }
- f.Multiply(-1);
- if(body2.IsAwake()) {
- body2.ApplyForce(f, p2)
- }
- }
- }
- }
- };
- b2GravityController.prototype.G = 1;
- b2GravityController.prototype.invSqr = true;var b2DestructionListener = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2DestructionListener.prototype.__constructor = function() {
- };
- b2DestructionListener.prototype.__varz = function() {
- };
- b2DestructionListener.prototype.SayGoodbyeJoint = function(joint) {
- };
- b2DestructionListener.prototype.SayGoodbyeFixture = function(fixture) {
- };var b2ContactEdge = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2ContactEdge.prototype.__constructor = function() {
- };
- b2ContactEdge.prototype.__varz = function() {
- };
- b2ContactEdge.prototype.other = null;
- b2ContactEdge.prototype.contact = null;
- b2ContactEdge.prototype.prev = null;
- b2ContactEdge.prototype.next = null;var b2EdgeChainDef = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2EdgeChainDef.prototype.__constructor = function() {
- this.vertexCount = 0;
- this.isALoop = true;
- this.vertices = []
- };
- b2EdgeChainDef.prototype.__varz = function() {
- };
- b2EdgeChainDef.prototype.vertices = null;
- b2EdgeChainDef.prototype.vertexCount = null;
- b2EdgeChainDef.prototype.isALoop = null;var b2Vec2 = function(x_, y_) {
- if(arguments.length == 2) {
- this.x = x_;
- this.y = y_
- }
- };
- b2Vec2.Make = function(x_, y_) {
- return new b2Vec2(x_, y_)
- };
- b2Vec2.prototype.SetZero = function() {
- this.x = 0;
- this.y = 0
- };
- b2Vec2.prototype.Set = function(x_, y_) {
- this.x = x_;
- this.y = y_
- };
- b2Vec2.prototype.SetV = function(v) {
- this.x = v.x;
- this.y = v.y
- };
- b2Vec2.prototype.GetNegative = function() {
- return new b2Vec2(-this.x, -this.y)
- };
- b2Vec2.prototype.NegativeSelf = function() {
- this.x = -this.x;
- this.y = -this.y
- };
- b2Vec2.prototype.Copy = function() {
- return new b2Vec2(this.x, this.y)
- };
- b2Vec2.prototype.Add = function(v) {
- this.x += v.x;
- this.y += v.y
- };
- b2Vec2.prototype.Subtract = function(v) {
- this.x -= v.x;
- this.y -= v.y
- };
- b2Vec2.prototype.Multiply = function(a) {
- this.x *= a;
- this.y *= a
- };
- b2Vec2.prototype.MulM = function(A) {
- var tX = this.x;
- this.x = A.col1.x * tX + A.col2.x * this.y;
- this.y = A.col1.y * tX + A.col2.y * this.y
- };
- b2Vec2.prototype.MulTM = function(A) {
- var tX = b2Math.Dot(this, A.col1);
- this.y = b2Math.Dot(this, A.col2);
- this.x = tX
- };
- b2Vec2.prototype.CrossVF = function(s) {
- var tX = this.x;
- this.x = s * this.y;
- this.y = -s * tX
- };
- b2Vec2.prototype.CrossFV = function(s) {
- var tX = this.x;
- this.x = -s * this.y;
- this.y = s * tX
- };
- b2Vec2.prototype.MinV = function(b) {
- this.x = this.x < b.x ? this.x : b.x;
- this.y = this.y < b.y ? this.y : b.y
- };
- b2Vec2.prototype.MaxV = function(b) {
- this.x = this.x > b.x ? this.x : b.x;
- this.y = this.y > b.y ? this.y : b.y
- };
- b2Vec2.prototype.Abs = function() {
- if(this.x < 0) {
- this.x = -this.x
- }
- if(this.y < 0) {
- this.y = -this.y
- }
- };
- b2Vec2.prototype.Length = function() {
- return Math.sqrt(this.x * this.x + this.y * this.y)
- };
- b2Vec2.prototype.LengthSquared = function() {
- return this.x * this.x + this.y * this.y
- };
- b2Vec2.prototype.Normalize = function() {
- var length = Math.sqrt(this.x * this.x + this.y * this.y);
- if(length < Number.MIN_VALUE) {
- return 0
- }
- var invLength = 1 / length;
- this.x *= invLength;
- this.y *= invLength;
- return length
- };
- b2Vec2.prototype.IsValid = function() {
- return b2Math.IsValid(this.x) && b2Math.IsValid(this.y)
- };
- b2Vec2.prototype.x = 0;
- b2Vec2.prototype.y = 0;var b2Vec3 = function(x, y, z) {
- if(arguments.length == 3) {
- this.x = x;
- this.y = y;
- this.z = z
- }
- };
- b2Vec3.prototype.SetZero = function() {
- this.x = this.y = this.z = 0
- };
- b2Vec3.prototype.Set = function(x, y, z) {
- this.x = x;
- this.y = y;
- this.z = z
- };
- b2Vec3.prototype.SetV = function(v) {
- this.x = v.x;
- this.y = v.y;
- this.z = v.z
- };
- b2Vec3.prototype.GetNegative = function() {
- return new b2Vec3(-this.x, -this.y, -this.z)
- };
- b2Vec3.prototype.NegativeSelf = function() {
- this.x = -this.x;
- this.y = -this.y;
- this.z = -this.z
- };
- b2Vec3.prototype.Copy = function() {
- return new b2Vec3(this.x, this.y, this.z)
- };
- b2Vec3.prototype.Add = function(v) {
- this.x += v.x;
- this.y += v.y;
- this.z += v.z
- };
- b2Vec3.prototype.Subtract = function(v) {
- this.x -= v.x;
- this.y -= v.y;
- this.z -= v.z
- };
- b2Vec3.prototype.Multiply = function(a) {
- this.x *= a;
- this.y *= a;
- this.z *= a
- };
- b2Vec3.prototype.x = 0;
- b2Vec3.prototype.y = 0;
- b2Vec3.prototype.z = 0;var b2DistanceProxy = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2DistanceProxy.prototype.__constructor = function() {
- };
- b2DistanceProxy.prototype.__varz = function() {
- };
- b2DistanceProxy.prototype.Set = function(shape) {
- switch(shape.GetType()) {
- case b2Shape.e_circleShape:
- var circle = shape;
- this.m_vertices = new Array(1);
- this.m_vertices[0] = circle.m_p;
- this.m_count = 1;
- this.m_radius = circle.m_radius;
- break;
- case b2Shape.e_polygonShape:
- var polygon = shape;
- this.m_vertices = polygon.m_vertices;
- this.m_count = polygon.m_vertexCount;
- this.m_radius = polygon.m_radius;
- break;
- default:
- b2Settings.b2Assert(false)
- }
- };
- b2DistanceProxy.prototype.GetSupport = function(d) {
- var bestIndex = 0;
- var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
- for(var i = 1;i < this.m_count;++i) {
- var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
- if(value > bestValue) {
- bestIndex = i;
- bestValue = value
- }
- }
- return bestIndex
- };
- b2DistanceProxy.prototype.GetSupportVertex = function(d) {
- var bestIndex = 0;
- var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
- for(var i = 1;i < this.m_count;++i) {
- var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
- if(value > bestValue) {
- bestIndex = i;
- bestValue = value
- }
- }
- return this.m_vertices[bestIndex]
- };
- b2DistanceProxy.prototype.GetVertexCount = function() {
- return this.m_count
- };
- b2DistanceProxy.prototype.GetVertex = function(index) {
- b2Settings.b2Assert(0 <= index && index < this.m_count);
- return this.m_vertices[index]
- };
- b2DistanceProxy.prototype.m_vertices = null;
- b2DistanceProxy.prototype.m_count = 0;
- b2DistanceProxy.prototype.m_radius = null;var b2ContactFactory = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2ContactFactory.prototype.__constructor = function() {
- };
- b2ContactFactory.prototype.__varz = function() {
- this.InitializeRegisters()
- };
- b2ContactFactory.prototype.AddType = function(createFcn, destroyFcn, type1, type2) {
- this.m_registers[type1][type2].createFcn = createFcn;
- this.m_registers[type1][type2].destroyFcn = destroyFcn;
- this.m_registers[type1][type2].primary = true;
- if(type1 != type2) {
- this.m_registers[type2][type1].createFcn = createFcn;
- this.m_registers[type2][type1].destroyFcn = destroyFcn;
- this.m_registers[type2][type1].primary = false
- }
- };
- b2ContactFactory.prototype.InitializeRegisters = function() {
- this.m_registers = new Array(b2Shape.e_shapeTypeCount);
- for(var i = 0;i < b2Shape.e_shapeTypeCount;i++) {
- this.m_registers[i] = new Array(b2Shape.e_shapeTypeCount);
- for(var j = 0;j < b2Shape.e_shapeTypeCount;j++) {
- this.m_registers[i][j] = new b2ContactRegister
- }
- }
- this.AddType(b2CircleContact.Create, b2CircleContact.Destroy, b2Shape.e_circleShape, b2Shape.e_circleShape);
- this.AddType(b2PolyAndCircleContact.Create, b2PolyAndCircleContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_circleShape);
- this.AddType(b2PolygonContact.Create, b2PolygonContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_polygonShape);
- this.AddType(b2EdgeAndCircleContact.Create, b2EdgeAndCircleContact.Destroy, b2Shape.e_edgeShape, b2Shape.e_circleShape);
- this.AddType(b2PolyAndEdgeContact.Create, b2PolyAndEdgeContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_edgeShape)
- };
- b2ContactFactory.prototype.Create = function(fixtureA, fixtureB) {
- var type1 = fixtureA.GetType();
- var type2 = fixtureB.GetType();
- var reg = this.m_registers[type1][type2];
- var c;
- if(reg.pool) {
- c = reg.pool;
- reg.pool = c.m_next;
- reg.poolCount--;
- c.Reset(fixtureA, fixtureB);
- return c
- }
- var createFcn = reg.createFcn;
- if(createFcn != null) {
- if(reg.primary) {
- c = createFcn(this.m_allocator);
- c.Reset(fixtureA, fixtureB);
- return c
- }else {
- c = createFcn(this.m_allocator);
- c.Reset(fixtureB, fixtureA);
- return c
- }
- }else {
- return null
- }
- };
- b2ContactFactory.prototype.Destroy = function(contact) {
- if(contact.m_manifold.m_pointCount > 0) {
- contact.m_fixtureA.m_body.SetAwake(true);
- contact.m_fixtureB.m_body.SetAwake(true)
- }
- var type1 = contact.m_fixtureA.GetType();
- var type2 = contact.m_fixtureB.GetType();
- var reg = this.m_registers[type1][type2];
- if(true) {
- reg.poolCount++;
- contact.m_next = reg.pool;
- reg.pool = contact
- }
- var destroyFcn = reg.destroyFcn;
- destroyFcn(contact, this.m_allocator)
- };
- b2ContactFactory.prototype.m_registers = null;
- b2ContactFactory.prototype.m_allocator = null;var b2ConstantAccelController = function() {
- b2Controller.prototype.__varz.call(this);
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- extend(b2ConstantAccelController.prototype, b2Controller.prototype);
- b2ConstantAccelController.prototype._super = b2Controller.prototype;
- b2ConstantAccelController.prototype.__constructor = function() {
- this._super.__constructor.apply(this, arguments)
- };
- b2ConstantAccelController.prototype.__varz = function() {
- this.A = new b2Vec2(0, 0)
- };
- b2ConstantAccelController.prototype.Step = function(step) {
- var smallA = new b2Vec2(this.A.x * step.dt, this.A.y * step.dt);
- for(var i = m_bodyList;i;i = i.nextBody) {
- var body = i.body;
- if(!body.IsAwake()) {
- continue
- }
- body.SetLinearVelocity(new b2Vec2(body.GetLinearVelocity().x + smallA.x, body.GetLinearVelocity().y + smallA.y))
- }
- };
- b2ConstantAccelController.prototype.A = new b2Vec2(0, 0);var b2SeparationFunction = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2SeparationFunction.prototype.__constructor = function() {
- };
- b2SeparationFunction.prototype.__varz = function() {
- this.m_localPoint = new b2Vec2;
- this.m_axis = new b2Vec2
- };
- b2SeparationFunction.e_points = 1;
- b2SeparationFunction.e_faceA = 2;
- b2SeparationFunction.e_faceB = 4;
- b2SeparationFunction.prototype.Initialize = function(cache, proxyA, transformA, proxyB, transformB) {
- this.m_proxyA = proxyA;
- this.m_proxyB = proxyB;
- var count = cache.count;
- b2Settings.b2Assert(0 < count && count < 3);
- var localPointA;
- var localPointA1;
- var localPointA2;
- var localPointB;
- var localPointB1;
- var localPointB2;
- var pointAX;
- var pointAY;
- var pointBX;
- var pointBY;
- var normalX;
- var normalY;
- var tMat;
- var tVec;
- var s;
- var sgn;
- if(count == 1) {
- this.m_type = b2SeparationFunction.e_points;
- localPointA = this.m_proxyA.GetVertex(cache.indexA[0]);
- localPointB = this.m_proxyB.GetVertex(cache.indexB[0]);
- tVec = localPointA;
- tMat = transformA.R;
- pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- tVec = localPointB;
- tMat = transformB.R;
- pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- this.m_axis.x = pointBX - pointAX;
- this.m_axis.y = pointBY - pointAY;
- this.m_axis.Normalize()
- }else {
- if(cache.indexB[0] == cache.indexB[1]) {
- this.m_type = b2SeparationFunction.e_faceA;
- localPointA1 = this.m_proxyA.GetVertex(cache.indexA[0]);
- localPointA2 = this.m_proxyA.GetVertex(cache.indexA[1]);
- localPointB = this.m_proxyB.GetVertex(cache.indexB[0]);
- this.m_localPoint.x = 0.5 * (localPointA1.x + localPointA2.x);
- this.m_localPoint.y = 0.5 * (localPointA1.y + localPointA2.y);
- this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointA2, localPointA1), 1);
- this.m_axis.Normalize();
- tVec = this.m_axis;
- tMat = transformA.R;
- normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- tVec = this.m_localPoint;
- tMat = transformA.R;
- pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- tVec = localPointB;
- tMat = transformB.R;
- pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- s = (pointBX - pointAX) * normalX + (pointBY - pointAY) * normalY;
- if(s < 0) {
- this.m_axis.NegativeSelf()
- }
- }else {
- if(cache.indexA[0] == cache.indexA[0]) {
- this.m_type = b2SeparationFunction.e_faceB;
- localPointB1 = this.m_proxyB.GetVertex(cache.indexB[0]);
- localPointB2 = this.m_proxyB.GetVertex(cache.indexB[1]);
- localPointA = this.m_proxyA.GetVertex(cache.indexA[0]);
- this.m_localPoint.x = 0.5 * (localPointB1.x + localPointB2.x);
- this.m_localPoint.y = 0.5 * (localPointB1.y + localPointB2.y);
- this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointB2, localPointB1), 1);
- this.m_axis.Normalize();
- tVec = this.m_axis;
- tMat = transformB.R;
- normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- tVec = this.m_localPoint;
- tMat = transformB.R;
- pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- tVec = localPointA;
- tMat = transformA.R;
- pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- s = (pointAX - pointBX) * normalX + (pointAY - pointBY) * normalY;
- if(s < 0) {
- this.m_axis.NegativeSelf()
- }
- }else {
- localPointA1 = this.m_proxyA.GetVertex(cache.indexA[0]);
- localPointA2 = this.m_proxyA.GetVertex(cache.indexA[1]);
- localPointB1 = this.m_proxyB.GetVertex(cache.indexB[0]);
- localPointB2 = this.m_proxyB.GetVertex(cache.indexB[1]);
- var pA = b2Math.MulX(transformA, localPointA);
- var dA = b2Math.MulMV(transformA.R, b2Math.SubtractVV(localPointA2, localPointA1));
- var pB = b2Math.MulX(transformB, localPointB);
- var dB = b2Math.MulMV(transformB.R, b2Math.SubtractVV(localPointB2, localPointB1));
- var a = dA.x * dA.x + dA.y * dA.y;
- var e = dB.x * dB.x + dB.y * dB.y;
- var r = b2Math.SubtractVV(dB, dA);
- var c = dA.x * r.x + dA.y * r.y;
- var f = dB.x * r.x + dB.y * r.y;
- var b = dA.x * dB.x + dA.y * dB.y;
- var denom = a * e - b * b;
- s = 0;
- if(denom != 0) {
- s = b2Math.Clamp((b * f - c * e) / denom, 0, 1)
- }
- var t = (b * s + f) / e;
- if(t < 0) {
- t = 0;
- s = b2Math.Clamp((b - c) / a, 0, 1)
- }
- localPointA = new b2Vec2;
- localPointA.x = localPointA1.x + s * (localPointA2.x - localPointA1.x);
- localPointA.y = localPointA1.y + s * (localPointA2.y - localPointA1.y);
- localPointB = new b2Vec2;
- localPointB.x = localPointB1.x + s * (localPointB2.x - localPointB1.x);
- localPointB.y = localPointB1.y + s * (localPointB2.y - localPointB1.y);
- if(s == 0 || s == 1) {
- this.m_type = b2SeparationFunction.e_faceB;
- this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointB2, localPointB1), 1);
- this.m_axis.Normalize();
- this.m_localPoint = localPointB;
- tVec = this.m_axis;
- tMat = transformB.R;
- normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- tVec = this.m_localPoint;
- tMat = transformB.R;
- pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- tVec = localPointA;
- tMat = transformA.R;
- pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- sgn = (pointAX - pointBX) * normalX + (pointAY - pointBY) * normalY;
- if(s < 0) {
- this.m_axis.NegativeSelf()
- }
- }else {
- this.m_type = b2SeparationFunction.e_faceA;
- this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointA2, localPointA1), 1);
- this.m_localPoint = localPointA;
- tVec = this.m_axis;
- tMat = transformA.R;
- normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- tVec = this.m_localPoint;
- tMat = transformA.R;
- pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- tVec = localPointB;
- tMat = transformB.R;
- pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- sgn = (pointBX - pointAX) * normalX + (pointBY - pointAY) * normalY;
- if(s < 0) {
- this.m_axis.NegativeSelf()
- }
- }
- }
- }
- }
- };
- b2SeparationFunction.prototype.Evaluate = function(transformA, transformB) {
- var axisA;
- var axisB;
- var localPointA;
- var localPointB;
- var pointA;
- var pointB;
- var seperation;
- var normal;
- switch(this.m_type) {
- case b2SeparationFunction.e_points:
- axisA = b2Math.MulTMV(transformA.R, this.m_axis);
- axisB = b2Math.MulTMV(transformB.R, this.m_axis.GetNegative());
- localPointA = this.m_proxyA.GetSupportVertex(axisA);
- localPointB = this.m_proxyB.GetSupportVertex(axisB);
- pointA = b2Math.MulX(transformA, localPointA);
- pointB = b2Math.MulX(transformB, localPointB);
- seperation = (pointB.x - pointA.x) * this.m_axis.x + (pointB.y - pointA.y) * this.m_axis.y;
- return seperation;
- case b2SeparationFunction.e_faceA:
- normal = b2Math.MulMV(transformA.R, this.m_axis);
- pointA = b2Math.MulX(transformA, this.m_localPoint);
- axisB = b2Math.MulTMV(transformB.R, normal.GetNegative());
- localPointB = this.m_proxyB.GetSupportVertex(axisB);
- pointB = b2Math.MulX(transformB, localPointB);
- seperation = (pointB.x - pointA.x) * normal.x + (pointB.y - pointA.y) * normal.y;
- return seperation;
- case b2SeparationFunction.e_faceB:
- normal = b2Math.MulMV(transformB.R, this.m_axis);
- pointB = b2Math.MulX(transformB, this.m_localPoint);
- axisA = b2Math.MulTMV(transformA.R, normal.GetNegative());
- localPointA = this.m_proxyA.GetSupportVertex(axisA);
- pointA = b2Math.MulX(transformA, localPointA);
- seperation = (pointA.x - pointB.x) * normal.x + (pointA.y - pointB.y) * normal.y;
- return seperation;
- default:
- b2Settings.b2Assert(false);
- return 0
- }
- };
- b2SeparationFunction.prototype.m_proxyA = null;
- b2SeparationFunction.prototype.m_proxyB = null;
- b2SeparationFunction.prototype.m_type = 0;
- b2SeparationFunction.prototype.m_localPoint = new b2Vec2;
- b2SeparationFunction.prototype.m_axis = new b2Vec2;var b2DynamicTreePair = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2DynamicTreePair.prototype.__constructor = function() {
- };
- b2DynamicTreePair.prototype.__varz = function() {
- };
- b2DynamicTreePair.prototype.proxyA = null;
- b2DynamicTreePair.prototype.proxyB = null;var b2ContactConstraintPoint = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2ContactConstraintPoint.prototype.__constructor = function() {
- };
- b2ContactConstraintPoint.prototype.__varz = function() {
- this.localPoint = new b2Vec2;
- this.rA = new b2Vec2;
- this.rB = new b2Vec2
- };
- b2ContactConstraintPoint.prototype.localPoint = new b2Vec2;
- b2ContactConstraintPoint.prototype.rA = new b2Vec2;
- b2ContactConstraintPoint.prototype.rB = new b2Vec2;
- b2ContactConstraintPoint.prototype.normalImpulse = null;
- b2ContactConstraintPoint.prototype.tangentImpulse = null;
- b2ContactConstraintPoint.prototype.normalMass = null;
- b2ContactConstraintPoint.prototype.tangentMass = null;
- b2ContactConstraintPoint.prototype.equalizedMass = null;
- b2ContactConstraintPoint.prototype.velocityBias = null;var b2ControllerEdge = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2ControllerEdge.prototype.__constructor = function() {
- };
- b2ControllerEdge.prototype.__varz = function() {
- };
- b2ControllerEdge.prototype.controller = null;
- b2ControllerEdge.prototype.body = null;
- b2ControllerEdge.prototype.prevBody = null;
- b2ControllerEdge.prototype.nextBody = null;
- b2ControllerEdge.prototype.prevController = null;
- b2ControllerEdge.prototype.nextController = null;var b2DistanceInput = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2DistanceInput.prototype.__constructor = function() {
- };
- b2DistanceInput.prototype.__varz = function() {
- };
- b2DistanceInput.prototype.proxyA = null;
- b2DistanceInput.prototype.proxyB = null;
- b2DistanceInput.prototype.transformA = null;
- b2DistanceInput.prototype.transformB = null;
- b2DistanceInput.prototype.useRadii = null;var b2Settings = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2Settings.prototype.__constructor = function() {
- };
- b2Settings.prototype.__varz = function() {
- };
- b2Settings.b2MixFriction = function(friction1, friction2) {
- return Math.sqrt(friction1 * friction2)
- };
- b2Settings.b2MixRestitution = function(restitution1, restitution2) {
- return restitution1 > restitution2 ? restitution1 : restitution2
- };
- b2Settings.b2Assert = function(a) {
- if(!a) {
- throw"Assertion Failed";
- }
- };
- b2Settings.VERSION = "2.1alpha";
- b2Settings.USHRT_MAX = 65535;
- b2Settings.b2_pi = Math.PI;
- b2Settings.b2_maxManifoldPoints = 2;
- b2Settings.b2_aabbExtension = 0.1;
- b2Settings.b2_aabbMultiplier = 2;
- b2Settings.b2_polygonRadius = 2 * b2Settings.b2_linearSlop;
- b2Settings.b2_linearSlop = 0.0050;
- b2Settings.b2_angularSlop = 2 / 180 * b2Settings.b2_pi;
- b2Settings.b2_toiSlop = 8 * b2Settings.b2_linearSlop;
- b2Settings.b2_maxTOIContactsPerIsland = 32;
- b2Settings.b2_maxTOIJointsPerIsland = 32;
- b2Settings.b2_velocityThreshold = 1;
- b2Settings.b2_maxLinearCorrection = 0.2;
- b2Settings.b2_maxAngularCorrection = 8 / 180 * b2Settings.b2_pi;
- b2Settings.b2_maxTranslation = 2;
- b2Settings.b2_maxTranslationSquared = b2Settings.b2_maxTranslation * b2Settings.b2_maxTranslation;
- b2Settings.b2_maxRotation = 0.5 * b2Settings.b2_pi;
- b2Settings.b2_maxRotationSquared = b2Settings.b2_maxRotation * b2Settings.b2_maxRotation;
- b2Settings.b2_contactBaumgarte = 0.2;
- b2Settings.b2_timeToSleep = 0.5;
- b2Settings.b2_linearSleepTolerance = 0.01;
- b2Settings.b2_angularSleepTolerance = 2 / 180 * b2Settings.b2_pi;var b2Proxy = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2Proxy.prototype.__constructor = function() {
- };
- b2Proxy.prototype.__varz = function() {
- this.lowerBounds = new Array(2);
- this.upperBounds = new Array(2);
- this.pairs = new Object
- };
- b2Proxy.prototype.IsValid = function() {
- return this.overlapCount != b2BroadPhase.b2_invalid
- };
- b2Proxy.prototype.lowerBounds = new Array(2);
- b2Proxy.prototype.upperBounds = new Array(2);
- b2Proxy.prototype.overlapCount = 0;
- b2Proxy.prototype.timeStamp = 0;
- b2Proxy.prototype.pairs = new Object;
- b2Proxy.prototype.next = null;
- b2Proxy.prototype.userData = null;var b2Point = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2Point.prototype.__constructor = function() {
- };
- b2Point.prototype.__varz = function() {
- this.p = new b2Vec2
- };
- b2Point.prototype.Support = function(xf, vX, vY) {
- return this.p
- };
- b2Point.prototype.GetFirstVertex = function(xf) {
- return this.p
- };
- b2Point.prototype.p = new b2Vec2;var b2WorldManifold = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2WorldManifold.prototype.__constructor = function() {
- this.m_points = new Array(b2Settings.b2_maxManifoldPoints);
- for(var i = 0;i < b2Settings.b2_maxManifoldPoints;i++) {
- this.m_points[i] = new b2Vec2
- }
- };
- b2WorldManifold.prototype.__varz = function() {
- this.m_normal = new b2Vec2
- };
- b2WorldManifold.prototype.Initialize = function(manifold, xfA, radiusA, xfB, radiusB) {
- if(manifold.m_pointCount == 0) {
- return
- }
- var i = 0;
- var tVec;
- var tMat;
- var normalX;
- var normalY;
- var planePointX;
- var planePointY;
- var clipPointX;
- var clipPointY;
- switch(manifold.m_type) {
- case b2Manifold.e_circles:
- tMat = xfA.R;
- tVec = manifold.m_localPoint;
- var pointAX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- var pointAY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- tMat = xfB.R;
- tVec = manifold.m_points[0].m_localPoint;
- var pointBX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- var pointBY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- var dX = pointBX - pointAX;
- var dY = pointBY - pointAY;
- var d2 = dX * dX + dY * dY;
- if(d2 > Number.MIN_VALUE * Number.MIN_VALUE) {
- var d = Math.sqrt(d2);
- this.m_normal.x = dX / d;
- this.m_normal.y = dY / d
- }else {
- this.m_normal.x = 1;
- this.m_normal.y = 0
- }
- var cAX = pointAX + radiusA * this.m_normal.x;
- var cAY = pointAY + radiusA * this.m_normal.y;
- var cBX = pointBX - radiusB * this.m_normal.x;
- var cBY = pointBY - radiusB * this.m_normal.y;
- this.m_points[0].x = 0.5 * (cAX + cBX);
- this.m_points[0].y = 0.5 * (cAY + cBY);
- break;
- case b2Manifold.e_faceA:
- tMat = xfA.R;
- tVec = manifold.m_localPlaneNormal;
- normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- tMat = xfA.R;
- tVec = manifold.m_localPoint;
- planePointX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- planePointY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- this.m_normal.x = normalX;
- this.m_normal.y = normalY;
- for(i = 0;i < manifold.m_pointCount;i++) {
- tMat = xfB.R;
- tVec = manifold.m_points[i].m_localPoint;
- clipPointX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- clipPointY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- this.m_points[i].x = clipPointX + 0.5 * (radiusA - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusB) * normalX;
- this.m_points[i].y = clipPointY + 0.5 * (radiusA - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusB) * normalY
- }
- break;
- case b2Manifold.e_faceB:
- tMat = xfB.R;
- tVec = manifold.m_localPlaneNormal;
- normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- tMat = xfB.R;
- tVec = manifold.m_localPoint;
- planePointX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- planePointY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- this.m_normal.x = -normalX;
- this.m_normal.y = -normalY;
- for(i = 0;i < manifold.m_pointCount;i++) {
- tMat = xfA.R;
- tVec = manifold.m_points[i].m_localPoint;
- clipPointX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- clipPointY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- this.m_points[i].x = clipPointX + 0.5 * (radiusB - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusA) * normalX;
- this.m_points[i].y = clipPointY + 0.5 * (radiusB - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusA) * normalY
- }
- break
- }
- };
- b2WorldManifold.prototype.m_normal = new b2Vec2;
- b2WorldManifold.prototype.m_points = null;var b2RayCastOutput = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2RayCastOutput.prototype.__constructor = function() {
- };
- b2RayCastOutput.prototype.__varz = function() {
- this.normal = new b2Vec2
- };
- b2RayCastOutput.prototype.normal = new b2Vec2;
- b2RayCastOutput.prototype.fraction = null;var b2ConstantForceController = function() {
- b2Controller.prototype.__varz.call(this);
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- extend(b2ConstantForceController.prototype, b2Controller.prototype);
- b2ConstantForceController.prototype._super = b2Controller.prototype;
- b2ConstantForceController.prototype.__constructor = function() {
- this._super.__constructor.apply(this, arguments)
- };
- b2ConstantForceController.prototype.__varz = function() {
- this.F = new b2Vec2(0, 0)
- };
- b2ConstantForceController.prototype.Step = function(step) {
- for(var i = m_bodyList;i;i = i.nextBody) {
- var body = i.body;
- if(!body.IsAwake()) {
- continue
- }
- body.ApplyForce(this.F, body.GetWorldCenter())
- }
- };
- b2ConstantForceController.prototype.F = new b2Vec2(0, 0);var b2MassData = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2MassData.prototype.__constructor = function() {
- };
- b2MassData.prototype.__varz = function() {
- this.center = new b2Vec2(0, 0)
- };
- b2MassData.prototype.mass = 0;
- b2MassData.prototype.center = new b2Vec2(0, 0);
- b2MassData.prototype.I = 0;var b2DynamicTree = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2DynamicTree.prototype.__constructor = function() {
- this.m_root = null;
- this.m_freeList = null;
- this.m_path = 0;
- this.m_insertionCount = 0
- };
- b2DynamicTree.prototype.__varz = function() {
- };
- b2DynamicTree.prototype.AllocateNode = function() {
- if(this.m_freeList) {
- var node = this.m_freeList;
- this.m_freeList = node.parent;
- node.parent = null;
- node.child1 = null;
- node.child2 = null;
- return node
- }
- return new b2DynamicTreeNode
- };
- b2DynamicTree.prototype.FreeNode = function(node) {
- node.parent = this.m_freeList;
- this.m_freeList = node
- };
- b2DynamicTree.prototype.InsertLeaf = function(leaf) {
- ++this.m_insertionCount;
- if(this.m_root == null) {
- this.m_root = leaf;
- this.m_root.parent = null;
- return
- }
- var center = leaf.aabb.GetCenter();
- var sibling = this.m_root;
- if(sibling.IsLeaf() == false) {
- do {
- var child1 = sibling.child1;
- var child2 = sibling.child2;
- 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);
- 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);
- if(norm1 < norm2) {
- sibling = child1
- }else {
- sibling = child2
- }
- }while(sibling.IsLeaf() == false)
- }
- var node1 = sibling.parent;
- var node2 = this.AllocateNode();
- node2.parent = node1;
- node2.userData = null;
- node2.aabb.Combine(leaf.aabb, sibling.aabb);
- if(node1) {
- if(sibling.parent.child1 == sibling) {
- node1.child1 = node2
- }else {
- node1.child2 = node2
- }
- node2.child1 = sibling;
- node2.child2 = leaf;
- sibling.parent = node2;
- leaf.parent = node2;
- do {
- if(node1.aabb.Contains(node2.aabb)) {
- break
- }
- node1.aabb.Combine(node1.child1.aabb, node1.child2.aabb);
- node2 = node1;
- node1 = node1.parent
- }while(node1)
- }else {
- node2.child1 = sibling;
- node2.child2 = leaf;
- sibling.parent = node2;
- leaf.parent = node2;
- this.m_root = node2
- }
- };
- b2DynamicTree.prototype.RemoveLeaf = function(leaf) {
- if(leaf == this.m_root) {
- this.m_root = null;
- return
- }
- var node2 = leaf.parent;
- var node1 = node2.parent;
- var sibling;
- if(node2.child1 == leaf) {
- sibling = node2.child2
- }else {
- sibling = node2.child1
- }
- if(node1) {
- if(node1.child1 == node2) {
- node1.child1 = sibling
- }else {
- node1.child2 = sibling
- }
- sibling.parent = node1;
- this.FreeNode(node2);
- while(node1) {
- var oldAABB = node1.aabb;
- node1.aabb = b2AABB.Combine(node1.child1.aabb, node1.child2.aabb);
- if(oldAABB.Contains(node1.aabb)) {
- break
- }
- node1 = node1.parent
- }
- }else {
- this.m_root = sibling;
- sibling.parent = null;
- this.FreeNode(node2)
- }
- };
- b2DynamicTree.prototype.CreateProxy = function(aabb, userData) {
- var node = this.AllocateNode();
- var extendX = b2Settings.b2_aabbExtension;
- var extendY = b2Settings.b2_aabbExtension;
- node.aabb.lowerBound.x = aabb.lowerBound.x - extendX;
- node.aabb.lowerBound.y = aabb.lowerBound.y - extendY;
- node.aabb.upperBound.x = aabb.upperBound.x + extendX;
- node.aabb.upperBound.y = aabb.upperBound.y + extendY;
- node.userData = userData;
- this.InsertLeaf(node);
- return node
- };
- b2DynamicTree.prototype.DestroyProxy = function(proxy) {
- this.RemoveLeaf(proxy);
- this.FreeNode(proxy)
- };
- b2DynamicTree.prototype.MoveProxy = function(proxy, aabb, displacement) {
- b2Settings.b2Assert(proxy.IsLeaf());
- if(proxy.aabb.Contains(aabb)) {
- return false
- }
- this.RemoveLeaf(proxy);
- var extendX = b2Settings.b2_aabbExtension + b2Settings.b2_aabbMultiplier * (displacement.x > 0 ? displacement.x : -displacement.x);
- var extendY = b2Settings.b2_aabbExtension + b2Settings.b2_aabbMultiplier * (displacement.y > 0 ? displacement.y : -displacement.y);
- proxy.aabb.lowerBound.x = aabb.lowerBound.x - extendX;
- proxy.aabb.lowerBound.y = aabb.lowerBound.y - extendY;
- proxy.aabb.upperBound.x = aabb.upperBound.x + extendX;
- proxy.aabb.upperBound.y = aabb.upperBound.y + extendY;
- this.InsertLeaf(proxy);
- return true
- };
- b2DynamicTree.prototype.Rebalance = function(iterations) {
- if(this.m_root == null) {
- return
- }
- for(var i = 0;i < iterations;i++) {
- var node = this.m_root;
- var bit = 0;
- while(node.IsLeaf() == false) {
- node = this.m_path >> bit & 1 ? node.child2 : node.child1;
- bit = bit + 1 & 31
- }
- ++this.m_path;
- this.RemoveLeaf(node);
- this.InsertLeaf(node)
- }
- };
- b2DynamicTree.prototype.GetFatAABB = function(proxy) {
- return proxy.aabb
- };
- b2DynamicTree.prototype.GetUserData = function(proxy) {
- return proxy.userData
- };
- b2DynamicTree.prototype.Query = function(callback, aabb) {
- if(this.m_root == null) {
- return
- }
- var stack = new Array;
- var count = 0;
- stack[count++] = this.m_root;
- while(count > 0) {
- var node = stack[--count];
- if(node.aabb.TestOverlap(aabb)) {
- if(node.IsLeaf()) {
- var proceed = callback(node);
- if(!proceed) {
- return
- }
- }else {
- stack[count++] = node.child1;
- stack[count++] = node.child2
- }
- }
- }
- };
- b2DynamicTree.prototype.RayCast = function(callback, input) {
- if(this.m_root == null) {
- return
- }
- var p1 = input.p1;
- var p2 = input.p2;
- var r = b2Math.SubtractVV(p1, p2);
- r.Normalize();
- var v = b2Math.CrossFV(1, r);
- var abs_v = b2Math.AbsV(v);
- var maxFraction = input.maxFraction;
- var segmentAABB = new b2AABB;
- var tX;
- var tY;
- tX = p1.x + maxFraction * (p2.x - p1.x);
- tY = p1.y + maxFraction * (p2.y - p1.y);
- segmentAABB.lowerBound.x = Math.min(p1.x, tX);
- segmentAABB.lowerBound.y = Math.min(p1.y, tY);
- segmentAABB.upperBound.x = Math.max(p1.x, tX);
- segmentAABB.upperBound.y = Math.max(p1.y, tY);
- var stack = new Array;
- var count = 0;
- stack[count++] = this.m_root;
- while(count > 0) {
- var node = stack[--count];
- if(node.aabb.TestOverlap(segmentAABB) == false) {
- continue
- }
- var c = node.aabb.GetCenter();
- var h = node.aabb.GetExtents();
- 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;
- if(separation > 0) {
- continue
- }
- if(node.IsLeaf()) {
- var subInput = new b2RayCastInput;
- subInput.p1 = input.p1;
- subInput.p2 = input.p2;
- subInput.maxFraction = input.maxFraction;
- maxFraction = callback(subInput, node);
- if(maxFraction == 0) {
- return
- }
- tX = p1.x + maxFraction * (p2.x - p1.x);
- tY = p1.y + maxFraction * (p2.y - p1.y);
- segmentAABB.lowerBound.x = Math.min(p1.x, tX);
- segmentAABB.lowerBound.y = Math.min(p1.y, tY);
- segmentAABB.upperBound.x = Math.max(p1.x, tX);
- segmentAABB.upperBound.y = Math.max(p1.y, tY)
- }else {
- stack[count++] = node.child1;
- stack[count++] = node.child2
- }
- }
- };
- b2DynamicTree.prototype.m_root = null;
- b2DynamicTree.prototype.m_freeList = null;
- b2DynamicTree.prototype.m_path = 0;
- b2DynamicTree.prototype.m_insertionCount = 0;var b2JointEdge = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2JointEdge.prototype.__constructor = function() {
- };
- b2JointEdge.prototype.__varz = function() {
- };
- b2JointEdge.prototype.other = null;
- b2JointEdge.prototype.joint = null;
- b2JointEdge.prototype.prev = null;
- b2JointEdge.prototype.next = null;var b2RayCastInput = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2RayCastInput.prototype.__constructor = function() {
- };
- b2RayCastInput.prototype.__varz = function() {
- this.p1 = new b2Vec2;
- this.p2 = new b2Vec2
- };
- b2RayCastInput.prototype.p1 = new b2Vec2;
- b2RayCastInput.prototype.p2 = new b2Vec2;
- b2RayCastInput.prototype.maxFraction = null;var Features = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- Features.prototype.__constructor = function() {
- };
- Features.prototype.__varz = function() {
- };
- Features.prototype.__defineGetter__("referenceEdge", function() {
- return this._referenceEdge
- });
- Features.prototype.__defineSetter__("referenceEdge", function(value) {
- this._referenceEdge = value;
- this._m_id._key = this._m_id._key & 4294967040 | this._referenceEdge & 255
- });
- Features.prototype.__defineGetter__("incidentEdge", function() {
- return this._incidentEdge
- });
- Features.prototype.__defineSetter__("incidentEdge", function(value) {
- this._incidentEdge = value;
- this._m_id._key = this._m_id._key & 4294902015 | this._incidentEdge << 8 & 65280
- });
- Features.prototype.__defineGetter__("incidentVertex", function() {
- return this._incidentVertex
- });
- Features.prototype.__defineSetter__("incidentVertex", function(value) {
- this._incidentVertex = value;
- this._m_id._key = this._m_id._key & 4278255615 | this._incidentVertex << 16 & 16711680
- });
- Features.prototype.__defineGetter__("flip", function() {
- return this._flip
- });
- Features.prototype.__defineSetter__("flip", function(value) {
- this._flip = value;
- this._m_id._key = this._m_id._key & 16777215 | this._flip << 24 & 4278190080
- });
- Features.prototype._referenceEdge = 0;
- Features.prototype._incidentEdge = 0;
- Features.prototype._incidentVertex = 0;
- Features.prototype._flip = 0;
- Features.prototype._m_id = null;var b2FilterData = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2FilterData.prototype.__constructor = function() {
- };
- b2FilterData.prototype.__varz = function() {
- this.categoryBits = 1;
- this.maskBits = 65535
- };
- b2FilterData.prototype.Copy = function() {
- var copy = new b2FilterData;
- copy.categoryBits = this.categoryBits;
- copy.maskBits = this.maskBits;
- copy.groupIndex = this.groupIndex;
- return copy
- };
- b2FilterData.prototype.categoryBits = 1;
- b2FilterData.prototype.maskBits = 65535;
- b2FilterData.prototype.groupIndex = 0;var b2AABB = function() {
- this.__varz();
- this.__constructor.apply(this, arguments)
- };
- b2AABB.prototype.__constructor = function() {
- };
- b2AABB.prototype.__varz = function() {
- this.lowerBound = new b2Vec2;
- this.upperBound = new b2Vec2
- };
- b2AABB.Combine = function(aabb1, aabb2) {
- var aabb = new b2AABB;
- aabb.Combine(aabb1, aabb2);
- return aabb
- };
- b2AABB.prototype.IsValid = function() {
- var dX = this.upperBound.x - this.lowerBound.x;
- var dY = this.upperBound.y - this.lowerBound.y;
- var valid = dX >= 0 && dY >= 0;
- valid = valid && this.lowerBound.IsValid() && this.upperBound.IsValid();
- return valid
- };
- b2AABB.prototype.GetCenter = function() {
- return new b2Vec2((this.lowerBound.x + this.upperBound.x) / 2, (this.lowerBound.y + this.upperBound.y) / 2)
- };
- b2AABB.prototype.GetExtents = function() {
- return new b2Vec2((this.upperBound.x - this.lowerBound.x) / 2, (this.upperBound.y - this.lowerBound.y) / 2)
- };
- b2AABB.prototype.Contains = function(aabb) {
- var result = true && this.lowerBound.x <= aabb.lowerBound.x && this.lowerBound.y <= aabb.lowerBound.y && aabb.upperBound.x <= this.upperBound.x && aabb.upperBound.y <= this.upperBound.y;
- return result
- };
- b2A…
Large files files are truncated, but you can click here to view the full file