PageRenderTime 178ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 2ms

/js/DemoBox2D/lib/box2d.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
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
  1. function extend(a, b) {
  2. for(var c in b) {
  3. a[c] = b[c]
  4. }
  5. }
  6. function isInstanceOf(obj, _constructor) {
  7. while(typeof obj === "object") {
  8. if(obj.constructor === _constructor) {
  9. return true
  10. }
  11. obj = obj._super
  12. }
  13. return false
  14. }
  15. ;var b2BoundValues = function() {
  16. this.__varz();
  17. this.__constructor.apply(this, arguments)
  18. };
  19. b2BoundValues.prototype.__constructor = function() {
  20. this.lowerValues = new Array;
  21. this.lowerValues[0] = 0;
  22. this.lowerValues[1] = 0;
  23. this.upperValues = new Array;
  24. this.upperValues[0] = 0;
  25. this.upperValues[1] = 0
  26. };
  27. b2BoundValues.prototype.__varz = function() {
  28. };
  29. b2BoundValues.prototype.lowerValues = null;
  30. b2BoundValues.prototype.upperValues = null;var b2PairManager = function() {
  31. this.__varz();
  32. this.__constructor.apply(this, arguments)
  33. };
  34. b2PairManager.prototype.__constructor = function() {
  35. this.m_pairs = new Array;
  36. this.m_pairBuffer = new Array;
  37. this.m_pairCount = 0;
  38. this.m_pairBufferCount = 0;
  39. this.m_freePair = null
  40. };
  41. b2PairManager.prototype.__varz = function() {
  42. };
  43. b2PairManager.prototype.AddPair = function(proxy1, proxy2) {
  44. var pair = proxy1.pairs[proxy2];
  45. if(pair != null) {
  46. return pair
  47. }
  48. if(this.m_freePair == null) {
  49. this.m_freePair = new b2Pair;
  50. this.m_pairs.push(this.m_freePair)
  51. }
  52. pair = this.m_freePair;
  53. this.m_freePair = pair.next;
  54. pair.proxy1 = proxy1;
  55. pair.proxy2 = proxy2;
  56. pair.status = 0;
  57. pair.userData = null;
  58. pair.next = null;
  59. proxy1.pairs[proxy2] = pair;
  60. proxy2.pairs[proxy1] = pair;
  61. ++this.m_pairCount;
  62. return pair
  63. };
  64. b2PairManager.prototype.RemovePair = function(proxy1, proxy2) {
  65. var pair = proxy1.pairs[proxy2];
  66. if(pair == null) {
  67. return null
  68. }
  69. var userData = pair.userData;
  70. delete proxy1.pairs[proxy2];
  71. delete proxy2.pairs[proxy1];
  72. pair.next = this.m_freePair;
  73. pair.proxy1 = null;
  74. pair.proxy2 = null;
  75. pair.userData = null;
  76. pair.status = 0;
  77. this.m_freePair = pair;
  78. --this.m_pairCount;
  79. return userData
  80. };
  81. b2PairManager.prototype.Find = function(proxy1, proxy2) {
  82. return proxy1.pairs[proxy2]
  83. };
  84. b2PairManager.prototype.ValidateBuffer = function() {
  85. };
  86. b2PairManager.prototype.ValidateTable = function() {
  87. };
  88. b2PairManager.prototype.Initialize = function(broadPhase) {
  89. this.m_broadPhase = broadPhase
  90. };
  91. b2PairManager.prototype.AddBufferedPair = function(proxy1, proxy2) {
  92. var pair = this.AddPair(proxy1, proxy2);
  93. if(pair.IsBuffered() == false) {
  94. pair.SetBuffered();
  95. this.m_pairBuffer[this.m_pairBufferCount] = pair;
  96. ++this.m_pairBufferCount
  97. }
  98. pair.ClearRemoved();
  99. if(b2BroadPhase.s_validate) {
  100. this.ValidateBuffer()
  101. }
  102. };
  103. b2PairManager.prototype.RemoveBufferedPair = function(proxy1, proxy2) {
  104. var pair = this.Find(proxy1, proxy2);
  105. if(pair == null) {
  106. return
  107. }
  108. if(pair.IsBuffered() == false) {
  109. pair.SetBuffered();
  110. this.m_pairBuffer[this.m_pairBufferCount] = pair;
  111. ++this.m_pairBufferCount
  112. }
  113. pair.SetRemoved();
  114. if(b2BroadPhase.s_validate) {
  115. this.ValidateBuffer()
  116. }
  117. };
  118. b2PairManager.prototype.Commit = function(callback) {
  119. var i = 0;
  120. var removeCount = 0;
  121. for(i = 0;i < this.m_pairBufferCount;++i) {
  122. var pair = this.m_pairBuffer[i];
  123. pair.ClearBuffered();
  124. var proxy1 = pair.proxy1;
  125. var proxy2 = pair.proxy2;
  126. if(pair.IsRemoved()) {
  127. }else {
  128. if(pair.IsFinal() == false) {
  129. callback(proxy1.userData, proxy2.userData)
  130. }
  131. }
  132. }
  133. this.m_pairBufferCount = 0;
  134. if(b2BroadPhase.s_validate) {
  135. this.ValidateTable()
  136. }
  137. };
  138. b2PairManager.prototype.m_broadPhase = null;
  139. b2PairManager.prototype.m_pairs = null;
  140. b2PairManager.prototype.m_freePair = null;
  141. b2PairManager.prototype.m_pairCount = 0;
  142. b2PairManager.prototype.m_pairBuffer = null;
  143. b2PairManager.prototype.m_pairBufferCount = 0;var b2TimeStep = function() {
  144. this.__varz();
  145. this.__constructor.apply(this, arguments)
  146. };
  147. b2TimeStep.prototype.__constructor = function() {
  148. };
  149. b2TimeStep.prototype.__varz = function() {
  150. };
  151. b2TimeStep.prototype.Set = function(step) {
  152. this.dt = step.dt;
  153. this.inv_dt = step.inv_dt;
  154. this.positionIterations = step.positionIterations;
  155. this.velocityIterations = step.velocityIterations;
  156. this.warmStarting = step.warmStarting
  157. };
  158. b2TimeStep.prototype.dt = null;
  159. b2TimeStep.prototype.inv_dt = null;
  160. b2TimeStep.prototype.dtRatio = null;
  161. b2TimeStep.prototype.velocityIterations = 0;
  162. b2TimeStep.prototype.positionIterations = 0;
  163. b2TimeStep.prototype.warmStarting = null;var b2Controller = function() {
  164. this.__varz();
  165. this.__constructor.apply(this, arguments)
  166. };
  167. b2Controller.prototype.__constructor = function() {
  168. };
  169. b2Controller.prototype.__varz = function() {
  170. };
  171. b2Controller.prototype.Step = function(step) {
  172. };
  173. b2Controller.prototype.Draw = function(debugDraw) {
  174. };
  175. b2Controller.prototype.AddBody = function(body) {
  176. var edge = new b2ControllerEdge;
  177. edge.controller = this;
  178. edge.body = body;
  179. edge.nextBody = m_bodyList;
  180. edge.prevBody = null;
  181. m_bodyList = edge;
  182. if(edge.nextBody) {
  183. edge.nextBody.prevBody = edge
  184. }
  185. m_bodyCount++;
  186. edge.nextController = body.m_controllerList;
  187. edge.prevController = null;
  188. body.m_controllerList = edge;
  189. if(edge.nextController) {
  190. edge.nextController.prevController = edge
  191. }
  192. body.m_controllerCount++
  193. };
  194. b2Controller.prototype.RemoveBody = function(body) {
  195. var edge = body.m_controllerList;
  196. while(edge && edge.controller != this) {
  197. edge = edge.nextController
  198. }
  199. if(edge.prevBody) {
  200. edge.prevBody.nextBody = edge.nextBody
  201. }
  202. if(edge.nextBody) {
  203. edge.nextBody.prevBody = edge.prevBody
  204. }
  205. if(edge.nextController) {
  206. edge.nextController.prevController = edge.prevController
  207. }
  208. if(edge.prevController) {
  209. edge.prevController.nextController = edge.nextController
  210. }
  211. if(m_bodyList == edge) {
  212. m_bodyList = edge.nextBody
  213. }
  214. if(body.m_controllerList == edge) {
  215. body.m_controllerList = edge.nextController
  216. }
  217. body.m_controllerCount--;
  218. m_bodyCount--
  219. };
  220. b2Controller.prototype.Clear = function() {
  221. while(m_bodyList) {
  222. this.RemoveBody(m_bodyList.body)
  223. }
  224. };
  225. b2Controller.prototype.GetNext = function() {
  226. return this.m_next
  227. };
  228. b2Controller.prototype.GetWorld = function() {
  229. return this.m_world
  230. };
  231. b2Controller.prototype.GetBodyList = function() {
  232. return m_bodyList
  233. };
  234. b2Controller.prototype.m_next = null;
  235. b2Controller.prototype.m_prev = null;
  236. b2Controller.prototype.m_world = null;var b2GravityController = function() {
  237. b2Controller.prototype.__varz.call(this);
  238. this.__varz();
  239. this.__constructor.apply(this, arguments)
  240. };
  241. extend(b2GravityController.prototype, b2Controller.prototype);
  242. b2GravityController.prototype._super = b2Controller.prototype;
  243. b2GravityController.prototype.__constructor = function() {
  244. this._super.__constructor.apply(this, arguments)
  245. };
  246. b2GravityController.prototype.__varz = function() {
  247. };
  248. b2GravityController.prototype.Step = function(step) {
  249. var i = null;
  250. var body1 = null;
  251. var p1 = null;
  252. var mass1 = 0;
  253. var j = null;
  254. var body2 = null;
  255. var p2 = null;
  256. var dx = 0;
  257. var dy = 0;
  258. var r2 = 0;
  259. var f = null;
  260. if(this.invSqr) {
  261. for(i = m_bodyList;i;i = i.nextBody) {
  262. body1 = i.body;
  263. p1 = body1.GetWorldCenter();
  264. mass1 = body1.GetMass();
  265. for(j = m_bodyList;j != i;j = j.nextBody) {
  266. body2 = j.body;
  267. p2 = body2.GetWorldCenter();
  268. dx = p2.x - p1.x;
  269. dy = p2.y - p1.y;
  270. r2 = dx * dx + dy * dy;
  271. if(r2 < Number.MIN_VALUE) {
  272. continue
  273. }
  274. f = new b2Vec2(dx, dy);
  275. f.Multiply(this.G / r2 / Math.sqrt(r2) * mass1 * body2.GetMass());
  276. if(body1.IsAwake()) {
  277. body1.ApplyForce(f, p1)
  278. }
  279. f.Multiply(-1);
  280. if(body2.IsAwake()) {
  281. body2.ApplyForce(f, p2)
  282. }
  283. }
  284. }
  285. }else {
  286. for(i = m_bodyList;i;i = i.nextBody) {
  287. body1 = i.body;
  288. p1 = body1.GetWorldCenter();
  289. mass1 = body1.GetMass();
  290. for(j = m_bodyList;j != i;j = j.nextBody) {
  291. body2 = j.body;
  292. p2 = body2.GetWorldCenter();
  293. dx = p2.x - p1.x;
  294. dy = p2.y - p1.y;
  295. r2 = dx * dx + dy * dy;
  296. if(r2 < Number.MIN_VALUE) {
  297. continue
  298. }
  299. f = new b2Vec2(dx, dy);
  300. f.Multiply(this.G / r2 * mass1 * body2.GetMass());
  301. if(body1.IsAwake()) {
  302. body1.ApplyForce(f, p1)
  303. }
  304. f.Multiply(-1);
  305. if(body2.IsAwake()) {
  306. body2.ApplyForce(f, p2)
  307. }
  308. }
  309. }
  310. }
  311. };
  312. b2GravityController.prototype.G = 1;
  313. b2GravityController.prototype.invSqr = true;var b2DestructionListener = function() {
  314. this.__varz();
  315. this.__constructor.apply(this, arguments)
  316. };
  317. b2DestructionListener.prototype.__constructor = function() {
  318. };
  319. b2DestructionListener.prototype.__varz = function() {
  320. };
  321. b2DestructionListener.prototype.SayGoodbyeJoint = function(joint) {
  322. };
  323. b2DestructionListener.prototype.SayGoodbyeFixture = function(fixture) {
  324. };var b2ContactEdge = function() {
  325. this.__varz();
  326. this.__constructor.apply(this, arguments)
  327. };
  328. b2ContactEdge.prototype.__constructor = function() {
  329. };
  330. b2ContactEdge.prototype.__varz = function() {
  331. };
  332. b2ContactEdge.prototype.other = null;
  333. b2ContactEdge.prototype.contact = null;
  334. b2ContactEdge.prototype.prev = null;
  335. b2ContactEdge.prototype.next = null;var b2EdgeChainDef = function() {
  336. this.__varz();
  337. this.__constructor.apply(this, arguments)
  338. };
  339. b2EdgeChainDef.prototype.__constructor = function() {
  340. this.vertexCount = 0;
  341. this.isALoop = true;
  342. this.vertices = []
  343. };
  344. b2EdgeChainDef.prototype.__varz = function() {
  345. };
  346. b2EdgeChainDef.prototype.vertices = null;
  347. b2EdgeChainDef.prototype.vertexCount = null;
  348. b2EdgeChainDef.prototype.isALoop = null;var b2Vec2 = function(x_, y_) {
  349. if(arguments.length == 2) {
  350. this.x = x_;
  351. this.y = y_
  352. }
  353. };
  354. b2Vec2.Make = function(x_, y_) {
  355. return new b2Vec2(x_, y_)
  356. };
  357. b2Vec2.prototype.SetZero = function() {
  358. this.x = 0;
  359. this.y = 0
  360. };
  361. b2Vec2.prototype.Set = function(x_, y_) {
  362. this.x = x_;
  363. this.y = y_
  364. };
  365. b2Vec2.prototype.SetV = function(v) {
  366. this.x = v.x;
  367. this.y = v.y
  368. };
  369. b2Vec2.prototype.GetNegative = function() {
  370. return new b2Vec2(-this.x, -this.y)
  371. };
  372. b2Vec2.prototype.NegativeSelf = function() {
  373. this.x = -this.x;
  374. this.y = -this.y
  375. };
  376. b2Vec2.prototype.Copy = function() {
  377. return new b2Vec2(this.x, this.y)
  378. };
  379. b2Vec2.prototype.Add = function(v) {
  380. this.x += v.x;
  381. this.y += v.y
  382. };
  383. b2Vec2.prototype.Subtract = function(v) {
  384. this.x -= v.x;
  385. this.y -= v.y
  386. };
  387. b2Vec2.prototype.Multiply = function(a) {
  388. this.x *= a;
  389. this.y *= a
  390. };
  391. b2Vec2.prototype.MulM = function(A) {
  392. var tX = this.x;
  393. this.x = A.col1.x * tX + A.col2.x * this.y;
  394. this.y = A.col1.y * tX + A.col2.y * this.y
  395. };
  396. b2Vec2.prototype.MulTM = function(A) {
  397. var tX = b2Math.Dot(this, A.col1);
  398. this.y = b2Math.Dot(this, A.col2);
  399. this.x = tX
  400. };
  401. b2Vec2.prototype.CrossVF = function(s) {
  402. var tX = this.x;
  403. this.x = s * this.y;
  404. this.y = -s * tX
  405. };
  406. b2Vec2.prototype.CrossFV = function(s) {
  407. var tX = this.x;
  408. this.x = -s * this.y;
  409. this.y = s * tX
  410. };
  411. b2Vec2.prototype.MinV = function(b) {
  412. this.x = this.x < b.x ? this.x : b.x;
  413. this.y = this.y < b.y ? this.y : b.y
  414. };
  415. b2Vec2.prototype.MaxV = function(b) {
  416. this.x = this.x > b.x ? this.x : b.x;
  417. this.y = this.y > b.y ? this.y : b.y
  418. };
  419. b2Vec2.prototype.Abs = function() {
  420. if(this.x < 0) {
  421. this.x = -this.x
  422. }
  423. if(this.y < 0) {
  424. this.y = -this.y
  425. }
  426. };
  427. b2Vec2.prototype.Length = function() {
  428. return Math.sqrt(this.x * this.x + this.y * this.y)
  429. };
  430. b2Vec2.prototype.LengthSquared = function() {
  431. return this.x * this.x + this.y * this.y
  432. };
  433. b2Vec2.prototype.Normalize = function() {
  434. var length = Math.sqrt(this.x * this.x + this.y * this.y);
  435. if(length < Number.MIN_VALUE) {
  436. return 0
  437. }
  438. var invLength = 1 / length;
  439. this.x *= invLength;
  440. this.y *= invLength;
  441. return length
  442. };
  443. b2Vec2.prototype.IsValid = function() {
  444. return b2Math.IsValid(this.x) && b2Math.IsValid(this.y)
  445. };
  446. b2Vec2.prototype.x = 0;
  447. b2Vec2.prototype.y = 0;var b2Vec3 = function(x, y, z) {
  448. if(arguments.length == 3) {
  449. this.x = x;
  450. this.y = y;
  451. this.z = z
  452. }
  453. };
  454. b2Vec3.prototype.SetZero = function() {
  455. this.x = this.y = this.z = 0
  456. };
  457. b2Vec3.prototype.Set = function(x, y, z) {
  458. this.x = x;
  459. this.y = y;
  460. this.z = z
  461. };
  462. b2Vec3.prototype.SetV = function(v) {
  463. this.x = v.x;
  464. this.y = v.y;
  465. this.z = v.z
  466. };
  467. b2Vec3.prototype.GetNegative = function() {
  468. return new b2Vec3(-this.x, -this.y, -this.z)
  469. };
  470. b2Vec3.prototype.NegativeSelf = function() {
  471. this.x = -this.x;
  472. this.y = -this.y;
  473. this.z = -this.z
  474. };
  475. b2Vec3.prototype.Copy = function() {
  476. return new b2Vec3(this.x, this.y, this.z)
  477. };
  478. b2Vec3.prototype.Add = function(v) {
  479. this.x += v.x;
  480. this.y += v.y;
  481. this.z += v.z
  482. };
  483. b2Vec3.prototype.Subtract = function(v) {
  484. this.x -= v.x;
  485. this.y -= v.y;
  486. this.z -= v.z
  487. };
  488. b2Vec3.prototype.Multiply = function(a) {
  489. this.x *= a;
  490. this.y *= a;
  491. this.z *= a
  492. };
  493. b2Vec3.prototype.x = 0;
  494. b2Vec3.prototype.y = 0;
  495. b2Vec3.prototype.z = 0;var b2DistanceProxy = function() {
  496. this.__varz();
  497. this.__constructor.apply(this, arguments)
  498. };
  499. b2DistanceProxy.prototype.__constructor = function() {
  500. };
  501. b2DistanceProxy.prototype.__varz = function() {
  502. };
  503. b2DistanceProxy.prototype.Set = function(shape) {
  504. switch(shape.GetType()) {
  505. case b2Shape.e_circleShape:
  506. var circle = shape;
  507. this.m_vertices = new Array(1);
  508. this.m_vertices[0] = circle.m_p;
  509. this.m_count = 1;
  510. this.m_radius = circle.m_radius;
  511. break;
  512. case b2Shape.e_polygonShape:
  513. var polygon = shape;
  514. this.m_vertices = polygon.m_vertices;
  515. this.m_count = polygon.m_vertexCount;
  516. this.m_radius = polygon.m_radius;
  517. break;
  518. default:
  519. b2Settings.b2Assert(false)
  520. }
  521. };
  522. b2DistanceProxy.prototype.GetSupport = function(d) {
  523. var bestIndex = 0;
  524. var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
  525. for(var i = 1;i < this.m_count;++i) {
  526. var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
  527. if(value > bestValue) {
  528. bestIndex = i;
  529. bestValue = value
  530. }
  531. }
  532. return bestIndex
  533. };
  534. b2DistanceProxy.prototype.GetSupportVertex = function(d) {
  535. var bestIndex = 0;
  536. var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
  537. for(var i = 1;i < this.m_count;++i) {
  538. var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
  539. if(value > bestValue) {
  540. bestIndex = i;
  541. bestValue = value
  542. }
  543. }
  544. return this.m_vertices[bestIndex]
  545. };
  546. b2DistanceProxy.prototype.GetVertexCount = function() {
  547. return this.m_count
  548. };
  549. b2DistanceProxy.prototype.GetVertex = function(index) {
  550. b2Settings.b2Assert(0 <= index && index < this.m_count);
  551. return this.m_vertices[index]
  552. };
  553. b2DistanceProxy.prototype.m_vertices = null;
  554. b2DistanceProxy.prototype.m_count = 0;
  555. b2DistanceProxy.prototype.m_radius = null;var b2ContactFactory = function() {
  556. this.__varz();
  557. this.__constructor.apply(this, arguments)
  558. };
  559. b2ContactFactory.prototype.__constructor = function() {
  560. };
  561. b2ContactFactory.prototype.__varz = function() {
  562. this.InitializeRegisters()
  563. };
  564. b2ContactFactory.prototype.AddType = function(createFcn, destroyFcn, type1, type2) {
  565. this.m_registers[type1][type2].createFcn = createFcn;
  566. this.m_registers[type1][type2].destroyFcn = destroyFcn;
  567. this.m_registers[type1][type2].primary = true;
  568. if(type1 != type2) {
  569. this.m_registers[type2][type1].createFcn = createFcn;
  570. this.m_registers[type2][type1].destroyFcn = destroyFcn;
  571. this.m_registers[type2][type1].primary = false
  572. }
  573. };
  574. b2ContactFactory.prototype.InitializeRegisters = function() {
  575. this.m_registers = new Array(b2Shape.e_shapeTypeCount);
  576. for(var i = 0;i < b2Shape.e_shapeTypeCount;i++) {
  577. this.m_registers[i] = new Array(b2Shape.e_shapeTypeCount);
  578. for(var j = 0;j < b2Shape.e_shapeTypeCount;j++) {
  579. this.m_registers[i][j] = new b2ContactRegister
  580. }
  581. }
  582. this.AddType(b2CircleContact.Create, b2CircleContact.Destroy, b2Shape.e_circleShape, b2Shape.e_circleShape);
  583. this.AddType(b2PolyAndCircleContact.Create, b2PolyAndCircleContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_circleShape);
  584. this.AddType(b2PolygonContact.Create, b2PolygonContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_polygonShape);
  585. this.AddType(b2EdgeAndCircleContact.Create, b2EdgeAndCircleContact.Destroy, b2Shape.e_edgeShape, b2Shape.e_circleShape);
  586. this.AddType(b2PolyAndEdgeContact.Create, b2PolyAndEdgeContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_edgeShape)
  587. };
  588. b2ContactFactory.prototype.Create = function(fixtureA, fixtureB) {
  589. var type1 = fixtureA.GetType();
  590. var type2 = fixtureB.GetType();
  591. var reg = this.m_registers[type1][type2];
  592. var c;
  593. if(reg.pool) {
  594. c = reg.pool;
  595. reg.pool = c.m_next;
  596. reg.poolCount--;
  597. c.Reset(fixtureA, fixtureB);
  598. return c
  599. }
  600. var createFcn = reg.createFcn;
  601. if(createFcn != null) {
  602. if(reg.primary) {
  603. c = createFcn(this.m_allocator);
  604. c.Reset(fixtureA, fixtureB);
  605. return c
  606. }else {
  607. c = createFcn(this.m_allocator);
  608. c.Reset(fixtureB, fixtureA);
  609. return c
  610. }
  611. }else {
  612. return null
  613. }
  614. };
  615. b2ContactFactory.prototype.Destroy = function(contact) {
  616. if(contact.m_manifold.m_pointCount > 0) {
  617. contact.m_fixtureA.m_body.SetAwake(true);
  618. contact.m_fixtureB.m_body.SetAwake(true)
  619. }
  620. var type1 = contact.m_fixtureA.GetType();
  621. var type2 = contact.m_fixtureB.GetType();
  622. var reg = this.m_registers[type1][type2];
  623. if(true) {
  624. reg.poolCount++;
  625. contact.m_next = reg.pool;
  626. reg.pool = contact
  627. }
  628. var destroyFcn = reg.destroyFcn;
  629. destroyFcn(contact, this.m_allocator)
  630. };
  631. b2ContactFactory.prototype.m_registers = null;
  632. b2ContactFactory.prototype.m_allocator = null;var b2ConstantAccelController = function() {
  633. b2Controller.prototype.__varz.call(this);
  634. this.__varz();
  635. this.__constructor.apply(this, arguments)
  636. };
  637. extend(b2ConstantAccelController.prototype, b2Controller.prototype);
  638. b2ConstantAccelController.prototype._super = b2Controller.prototype;
  639. b2ConstantAccelController.prototype.__constructor = function() {
  640. this._super.__constructor.apply(this, arguments)
  641. };
  642. b2ConstantAccelController.prototype.__varz = function() {
  643. this.A = new b2Vec2(0, 0)
  644. };
  645. b2ConstantAccelController.prototype.Step = function(step) {
  646. var smallA = new b2Vec2(this.A.x * step.dt, this.A.y * step.dt);
  647. for(var i = m_bodyList;i;i = i.nextBody) {
  648. var body = i.body;
  649. if(!body.IsAwake()) {
  650. continue
  651. }
  652. body.SetLinearVelocity(new b2Vec2(body.GetLinearVelocity().x + smallA.x, body.GetLinearVelocity().y + smallA.y))
  653. }
  654. };
  655. b2ConstantAccelController.prototype.A = new b2Vec2(0, 0);var b2SeparationFunction = function() {
  656. this.__varz();
  657. this.__constructor.apply(this, arguments)
  658. };
  659. b2SeparationFunction.prototype.__constructor = function() {
  660. };
  661. b2SeparationFunction.prototype.__varz = function() {
  662. this.m_localPoint = new b2Vec2;
  663. this.m_axis = new b2Vec2
  664. };
  665. b2SeparationFunction.e_points = 1;
  666. b2SeparationFunction.e_faceA = 2;
  667. b2SeparationFunction.e_faceB = 4;
  668. b2SeparationFunction.prototype.Initialize = function(cache, proxyA, transformA, proxyB, transformB) {
  669. this.m_proxyA = proxyA;
  670. this.m_proxyB = proxyB;
  671. var count = cache.count;
  672. b2Settings.b2Assert(0 < count && count < 3);
  673. var localPointA;
  674. var localPointA1;
  675. var localPointA2;
  676. var localPointB;
  677. var localPointB1;
  678. var localPointB2;
  679. var pointAX;
  680. var pointAY;
  681. var pointBX;
  682. var pointBY;
  683. var normalX;
  684. var normalY;
  685. var tMat;
  686. var tVec;
  687. var s;
  688. var sgn;
  689. if(count == 1) {
  690. this.m_type = b2SeparationFunction.e_points;
  691. localPointA = this.m_proxyA.GetVertex(cache.indexA[0]);
  692. localPointB = this.m_proxyB.GetVertex(cache.indexB[0]);
  693. tVec = localPointA;
  694. tMat = transformA.R;
  695. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  696. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  697. tVec = localPointB;
  698. tMat = transformB.R;
  699. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  700. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  701. this.m_axis.x = pointBX - pointAX;
  702. this.m_axis.y = pointBY - pointAY;
  703. this.m_axis.Normalize()
  704. }else {
  705. if(cache.indexB[0] == cache.indexB[1]) {
  706. this.m_type = b2SeparationFunction.e_faceA;
  707. localPointA1 = this.m_proxyA.GetVertex(cache.indexA[0]);
  708. localPointA2 = this.m_proxyA.GetVertex(cache.indexA[1]);
  709. localPointB = this.m_proxyB.GetVertex(cache.indexB[0]);
  710. this.m_localPoint.x = 0.5 * (localPointA1.x + localPointA2.x);
  711. this.m_localPoint.y = 0.5 * (localPointA1.y + localPointA2.y);
  712. this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointA2, localPointA1), 1);
  713. this.m_axis.Normalize();
  714. tVec = this.m_axis;
  715. tMat = transformA.R;
  716. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  717. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  718. tVec = this.m_localPoint;
  719. tMat = transformA.R;
  720. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  721. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  722. tVec = localPointB;
  723. tMat = transformB.R;
  724. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  725. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  726. s = (pointBX - pointAX) * normalX + (pointBY - pointAY) * normalY;
  727. if(s < 0) {
  728. this.m_axis.NegativeSelf()
  729. }
  730. }else {
  731. if(cache.indexA[0] == cache.indexA[0]) {
  732. this.m_type = b2SeparationFunction.e_faceB;
  733. localPointB1 = this.m_proxyB.GetVertex(cache.indexB[0]);
  734. localPointB2 = this.m_proxyB.GetVertex(cache.indexB[1]);
  735. localPointA = this.m_proxyA.GetVertex(cache.indexA[0]);
  736. this.m_localPoint.x = 0.5 * (localPointB1.x + localPointB2.x);
  737. this.m_localPoint.y = 0.5 * (localPointB1.y + localPointB2.y);
  738. this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointB2, localPointB1), 1);
  739. this.m_axis.Normalize();
  740. tVec = this.m_axis;
  741. tMat = transformB.R;
  742. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  743. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  744. tVec = this.m_localPoint;
  745. tMat = transformB.R;
  746. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  747. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  748. tVec = localPointA;
  749. tMat = transformA.R;
  750. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  751. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  752. s = (pointAX - pointBX) * normalX + (pointAY - pointBY) * normalY;
  753. if(s < 0) {
  754. this.m_axis.NegativeSelf()
  755. }
  756. }else {
  757. localPointA1 = this.m_proxyA.GetVertex(cache.indexA[0]);
  758. localPointA2 = this.m_proxyA.GetVertex(cache.indexA[1]);
  759. localPointB1 = this.m_proxyB.GetVertex(cache.indexB[0]);
  760. localPointB2 = this.m_proxyB.GetVertex(cache.indexB[1]);
  761. var pA = b2Math.MulX(transformA, localPointA);
  762. var dA = b2Math.MulMV(transformA.R, b2Math.SubtractVV(localPointA2, localPointA1));
  763. var pB = b2Math.MulX(transformB, localPointB);
  764. var dB = b2Math.MulMV(transformB.R, b2Math.SubtractVV(localPointB2, localPointB1));
  765. var a = dA.x * dA.x + dA.y * dA.y;
  766. var e = dB.x * dB.x + dB.y * dB.y;
  767. var r = b2Math.SubtractVV(dB, dA);
  768. var c = dA.x * r.x + dA.y * r.y;
  769. var f = dB.x * r.x + dB.y * r.y;
  770. var b = dA.x * dB.x + dA.y * dB.y;
  771. var denom = a * e - b * b;
  772. s = 0;
  773. if(denom != 0) {
  774. s = b2Math.Clamp((b * f - c * e) / denom, 0, 1)
  775. }
  776. var t = (b * s + f) / e;
  777. if(t < 0) {
  778. t = 0;
  779. s = b2Math.Clamp((b - c) / a, 0, 1)
  780. }
  781. localPointA = new b2Vec2;
  782. localPointA.x = localPointA1.x + s * (localPointA2.x - localPointA1.x);
  783. localPointA.y = localPointA1.y + s * (localPointA2.y - localPointA1.y);
  784. localPointB = new b2Vec2;
  785. localPointB.x = localPointB1.x + s * (localPointB2.x - localPointB1.x);
  786. localPointB.y = localPointB1.y + s * (localPointB2.y - localPointB1.y);
  787. if(s == 0 || s == 1) {
  788. this.m_type = b2SeparationFunction.e_faceB;
  789. this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointB2, localPointB1), 1);
  790. this.m_axis.Normalize();
  791. this.m_localPoint = localPointB;
  792. tVec = this.m_axis;
  793. tMat = transformB.R;
  794. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  795. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  796. tVec = this.m_localPoint;
  797. tMat = transformB.R;
  798. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  799. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  800. tVec = localPointA;
  801. tMat = transformA.R;
  802. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  803. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  804. sgn = (pointAX - pointBX) * normalX + (pointAY - pointBY) * normalY;
  805. if(s < 0) {
  806. this.m_axis.NegativeSelf()
  807. }
  808. }else {
  809. this.m_type = b2SeparationFunction.e_faceA;
  810. this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointA2, localPointA1), 1);
  811. this.m_localPoint = localPointA;
  812. tVec = this.m_axis;
  813. tMat = transformA.R;
  814. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  815. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  816. tVec = this.m_localPoint;
  817. tMat = transformA.R;
  818. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  819. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  820. tVec = localPointB;
  821. tMat = transformB.R;
  822. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  823. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  824. sgn = (pointBX - pointAX) * normalX + (pointBY - pointAY) * normalY;
  825. if(s < 0) {
  826. this.m_axis.NegativeSelf()
  827. }
  828. }
  829. }
  830. }
  831. }
  832. };
  833. b2SeparationFunction.prototype.Evaluate = function(transformA, transformB) {
  834. var axisA;
  835. var axisB;
  836. var localPointA;
  837. var localPointB;
  838. var pointA;
  839. var pointB;
  840. var seperation;
  841. var normal;
  842. switch(this.m_type) {
  843. case b2SeparationFunction.e_points:
  844. axisA = b2Math.MulTMV(transformA.R, this.m_axis);
  845. axisB = b2Math.MulTMV(transformB.R, this.m_axis.GetNegative());
  846. localPointA = this.m_proxyA.GetSupportVertex(axisA);
  847. localPointB = this.m_proxyB.GetSupportVertex(axisB);
  848. pointA = b2Math.MulX(transformA, localPointA);
  849. pointB = b2Math.MulX(transformB, localPointB);
  850. seperation = (pointB.x - pointA.x) * this.m_axis.x + (pointB.y - pointA.y) * this.m_axis.y;
  851. return seperation;
  852. case b2SeparationFunction.e_faceA:
  853. normal = b2Math.MulMV(transformA.R, this.m_axis);
  854. pointA = b2Math.MulX(transformA, this.m_localPoint);
  855. axisB = b2Math.MulTMV(transformB.R, normal.GetNegative());
  856. localPointB = this.m_proxyB.GetSupportVertex(axisB);
  857. pointB = b2Math.MulX(transformB, localPointB);
  858. seperation = (pointB.x - pointA.x) * normal.x + (pointB.y - pointA.y) * normal.y;
  859. return seperation;
  860. case b2SeparationFunction.e_faceB:
  861. normal = b2Math.MulMV(transformB.R, this.m_axis);
  862. pointB = b2Math.MulX(transformB, this.m_localPoint);
  863. axisA = b2Math.MulTMV(transformA.R, normal.GetNegative());
  864. localPointA = this.m_proxyA.GetSupportVertex(axisA);
  865. pointA = b2Math.MulX(transformA, localPointA);
  866. seperation = (pointA.x - pointB.x) * normal.x + (pointA.y - pointB.y) * normal.y;
  867. return seperation;
  868. default:
  869. b2Settings.b2Assert(false);
  870. return 0
  871. }
  872. };
  873. b2SeparationFunction.prototype.m_proxyA = null;
  874. b2SeparationFunction.prototype.m_proxyB = null;
  875. b2SeparationFunction.prototype.m_type = 0;
  876. b2SeparationFunction.prototype.m_localPoint = new b2Vec2;
  877. b2SeparationFunction.prototype.m_axis = new b2Vec2;var b2DynamicTreePair = function() {
  878. this.__varz();
  879. this.__constructor.apply(this, arguments)
  880. };
  881. b2DynamicTreePair.prototype.__constructor = function() {
  882. };
  883. b2DynamicTreePair.prototype.__varz = function() {
  884. };
  885. b2DynamicTreePair.prototype.proxyA = null;
  886. b2DynamicTreePair.prototype.proxyB = null;var b2ContactConstraintPoint = function() {
  887. this.__varz();
  888. this.__constructor.apply(this, arguments)
  889. };
  890. b2ContactConstraintPoint.prototype.__constructor = function() {
  891. };
  892. b2ContactConstraintPoint.prototype.__varz = function() {
  893. this.localPoint = new b2Vec2;
  894. this.rA = new b2Vec2;
  895. this.rB = new b2Vec2
  896. };
  897. b2ContactConstraintPoint.prototype.localPoint = new b2Vec2;
  898. b2ContactConstraintPoint.prototype.rA = new b2Vec2;
  899. b2ContactConstraintPoint.prototype.rB = new b2Vec2;
  900. b2ContactConstraintPoint.prototype.normalImpulse = null;
  901. b2ContactConstraintPoint.prototype.tangentImpulse = null;
  902. b2ContactConstraintPoint.prototype.normalMass = null;
  903. b2ContactConstraintPoint.prototype.tangentMass = null;
  904. b2ContactConstraintPoint.prototype.equalizedMass = null;
  905. b2ContactConstraintPoint.prototype.velocityBias = null;var b2ControllerEdge = function() {
  906. this.__varz();
  907. this.__constructor.apply(this, arguments)
  908. };
  909. b2ControllerEdge.prototype.__constructor = function() {
  910. };
  911. b2ControllerEdge.prototype.__varz = function() {
  912. };
  913. b2ControllerEdge.prototype.controller = null;
  914. b2ControllerEdge.prototype.body = null;
  915. b2ControllerEdge.prototype.prevBody = null;
  916. b2ControllerEdge.prototype.nextBody = null;
  917. b2ControllerEdge.prototype.prevController = null;
  918. b2ControllerEdge.prototype.nextController = null;var b2DistanceInput = function() {
  919. this.__varz();
  920. this.__constructor.apply(this, arguments)
  921. };
  922. b2DistanceInput.prototype.__constructor = function() {
  923. };
  924. b2DistanceInput.prototype.__varz = function() {
  925. };
  926. b2DistanceInput.prototype.proxyA = null;
  927. b2DistanceInput.prototype.proxyB = null;
  928. b2DistanceInput.prototype.transformA = null;
  929. b2DistanceInput.prototype.transformB = null;
  930. b2DistanceInput.prototype.useRadii = null;var b2Settings = function() {
  931. this.__varz();
  932. this.__constructor.apply(this, arguments)
  933. };
  934. b2Settings.prototype.__constructor = function() {
  935. };
  936. b2Settings.prototype.__varz = function() {
  937. };
  938. b2Settings.b2MixFriction = function(friction1, friction2) {
  939. return Math.sqrt(friction1 * friction2)
  940. };
  941. b2Settings.b2MixRestitution = function(restitution1, restitution2) {
  942. return restitution1 > restitution2 ? restitution1 : restitution2
  943. };
  944. b2Settings.b2Assert = function(a) {
  945. if(!a) {
  946. throw"Assertion Failed";
  947. }
  948. };
  949. b2Settings.VERSION = "2.1alpha";
  950. b2Settings.USHRT_MAX = 65535;
  951. b2Settings.b2_pi = Math.PI;
  952. b2Settings.b2_maxManifoldPoints = 2;
  953. b2Settings.b2_aabbExtension = 0.1;
  954. b2Settings.b2_aabbMultiplier = 2;
  955. b2Settings.b2_polygonRadius = 2 * b2Settings.b2_linearSlop;
  956. b2Settings.b2_linearSlop = 0.0050;
  957. b2Settings.b2_angularSlop = 2 / 180 * b2Settings.b2_pi;
  958. b2Settings.b2_toiSlop = 8 * b2Settings.b2_linearSlop;
  959. b2Settings.b2_maxTOIContactsPerIsland = 32;
  960. b2Settings.b2_maxTOIJointsPerIsland = 32;
  961. b2Settings.b2_velocityThreshold = 1;
  962. b2Settings.b2_maxLinearCorrection = 0.2;
  963. b2Settings.b2_maxAngularCorrection = 8 / 180 * b2Settings.b2_pi;
  964. b2Settings.b2_maxTranslation = 2;
  965. b2Settings.b2_maxTranslationSquared = b2Settings.b2_maxTranslation * b2Settings.b2_maxTranslation;
  966. b2Settings.b2_maxRotation = 0.5 * b2Settings.b2_pi;
  967. b2Settings.b2_maxRotationSquared = b2Settings.b2_maxRotation * b2Settings.b2_maxRotation;
  968. b2Settings.b2_contactBaumgarte = 0.2;
  969. b2Settings.b2_timeToSleep = 0.5;
  970. b2Settings.b2_linearSleepTolerance = 0.01;
  971. b2Settings.b2_angularSleepTolerance = 2 / 180 * b2Settings.b2_pi;var b2Proxy = function() {
  972. this.__varz();
  973. this.__constructor.apply(this, arguments)
  974. };
  975. b2Proxy.prototype.__constructor = function() {
  976. };
  977. b2Proxy.prototype.__varz = function() {
  978. this.lowerBounds = new Array(2);
  979. this.upperBounds = new Array(2);
  980. this.pairs = new Object
  981. };
  982. b2Proxy.prototype.IsValid = function() {
  983. return this.overlapCount != b2BroadPhase.b2_invalid
  984. };
  985. b2Proxy.prototype.lowerBounds = new Array(2);
  986. b2Proxy.prototype.upperBounds = new Array(2);
  987. b2Proxy.prototype.overlapCount = 0;
  988. b2Proxy.prototype.timeStamp = 0;
  989. b2Proxy.prototype.pairs = new Object;
  990. b2Proxy.prototype.next = null;
  991. b2Proxy.prototype.userData = null;var b2Point = function() {
  992. this.__varz();
  993. this.__constructor.apply(this, arguments)
  994. };
  995. b2Point.prototype.__constructor = function() {
  996. };
  997. b2Point.prototype.__varz = function() {
  998. this.p = new b2Vec2
  999. };
  1000. b2Point.prototype.Support = function(xf, vX, vY) {
  1001. return this.p
  1002. };
  1003. b2Point.prototype.GetFirstVertex = function(xf) {
  1004. return this.p
  1005. };
  1006. b2Point.prototype.p = new b2Vec2;var b2WorldManifold = function() {
  1007. this.__varz();
  1008. this.__constructor.apply(this, arguments)
  1009. };
  1010. b2WorldManifold.prototype.__constructor = function() {
  1011. this.m_points = new Array(b2Settings.b2_maxManifoldPoints);
  1012. for(var i = 0;i < b2Settings.b2_maxManifoldPoints;i++) {
  1013. this.m_points[i] = new b2Vec2
  1014. }
  1015. };
  1016. b2WorldManifold.prototype.__varz = function() {
  1017. this.m_normal = new b2Vec2
  1018. };
  1019. b2WorldManifold.prototype.Initialize = function(manifold, xfA, radiusA, xfB, radiusB) {
  1020. if(manifold.m_pointCount == 0) {
  1021. return
  1022. }
  1023. var i = 0;
  1024. var tVec;
  1025. var tMat;
  1026. var normalX;
  1027. var normalY;
  1028. var planePointX;
  1029. var planePointY;
  1030. var clipPointX;
  1031. var clipPointY;
  1032. switch(manifold.m_type) {
  1033. case b2Manifold.e_circles:
  1034. tMat = xfA.R;
  1035. tVec = manifold.m_localPoint;
  1036. var pointAX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1037. var pointAY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1038. tMat = xfB.R;
  1039. tVec = manifold.m_points[0].m_localPoint;
  1040. var pointBX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1041. var pointBY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1042. var dX = pointBX - pointAX;
  1043. var dY = pointBY - pointAY;
  1044. var d2 = dX * dX + dY * dY;
  1045. if(d2 > Number.MIN_VALUE * Number.MIN_VALUE) {
  1046. var d = Math.sqrt(d2);
  1047. this.m_normal.x = dX / d;
  1048. this.m_normal.y = dY / d
  1049. }else {
  1050. this.m_normal.x = 1;
  1051. this.m_normal.y = 0
  1052. }
  1053. var cAX = pointAX + radiusA * this.m_normal.x;
  1054. var cAY = pointAY + radiusA * this.m_normal.y;
  1055. var cBX = pointBX - radiusB * this.m_normal.x;
  1056. var cBY = pointBY - radiusB * this.m_normal.y;
  1057. this.m_points[0].x = 0.5 * (cAX + cBX);
  1058. this.m_points[0].y = 0.5 * (cAY + cBY);
  1059. break;
  1060. case b2Manifold.e_faceA:
  1061. tMat = xfA.R;
  1062. tVec = manifold.m_localPlaneNormal;
  1063. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1064. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1065. tMat = xfA.R;
  1066. tVec = manifold.m_localPoint;
  1067. planePointX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1068. planePointY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1069. this.m_normal.x = normalX;
  1070. this.m_normal.y = normalY;
  1071. for(i = 0;i < manifold.m_pointCount;i++) {
  1072. tMat = xfB.R;
  1073. tVec = manifold.m_points[i].m_localPoint;
  1074. clipPointX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1075. clipPointY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1076. this.m_points[i].x = clipPointX + 0.5 * (radiusA - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusB) * normalX;
  1077. this.m_points[i].y = clipPointY + 0.5 * (radiusA - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusB) * normalY
  1078. }
  1079. break;
  1080. case b2Manifold.e_faceB:
  1081. tMat = xfB.R;
  1082. tVec = manifold.m_localPlaneNormal;
  1083. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1084. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1085. tMat = xfB.R;
  1086. tVec = manifold.m_localPoint;
  1087. planePointX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1088. planePointY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1089. this.m_normal.x = -normalX;
  1090. this.m_normal.y = -normalY;
  1091. for(i = 0;i < manifold.m_pointCount;i++) {
  1092. tMat = xfA.R;
  1093. tVec = manifold.m_points[i].m_localPoint;
  1094. clipPointX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1095. clipPointY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1096. this.m_points[i].x = clipPointX + 0.5 * (radiusB - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusA) * normalX;
  1097. this.m_points[i].y = clipPointY + 0.5 * (radiusB - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusA) * normalY
  1098. }
  1099. break
  1100. }
  1101. };
  1102. b2WorldManifold.prototype.m_normal = new b2Vec2;
  1103. b2WorldManifold.prototype.m_points = null;var b2RayCastOutput = function() {
  1104. this.__varz();
  1105. this.__constructor.apply(this, arguments)
  1106. };
  1107. b2RayCastOutput.prototype.__constructor = function() {
  1108. };
  1109. b2RayCastOutput.prototype.__varz = function() {
  1110. this.normal = new b2Vec2
  1111. };
  1112. b2RayCastOutput.prototype.normal = new b2Vec2;
  1113. b2RayCastOutput.prototype.fraction = null;var b2ConstantForceController = function() {
  1114. b2Controller.prototype.__varz.call(this);
  1115. this.__varz();
  1116. this.__constructor.apply(this, arguments)
  1117. };
  1118. extend(b2ConstantForceController.prototype, b2Controller.prototype);
  1119. b2ConstantForceController.prototype._super = b2Controller.prototype;
  1120. b2ConstantForceController.prototype.__constructor = function() {
  1121. this._super.__constructor.apply(this, arguments)
  1122. };
  1123. b2ConstantForceController.prototype.__varz = function() {
  1124. this.F = new b2Vec2(0, 0)
  1125. };
  1126. b2ConstantForceController.prototype.Step = function(step) {
  1127. for(var i = m_bodyList;i;i = i.nextBody) {
  1128. var body = i.body;
  1129. if(!body.IsAwake()) {
  1130. continue
  1131. }
  1132. body.ApplyForce(this.F, body.GetWorldCenter())
  1133. }
  1134. };
  1135. b2ConstantForceController.prototype.F = new b2Vec2(0, 0);var b2MassData = function() {
  1136. this.__varz();
  1137. this.__constructor.apply(this, arguments)
  1138. };
  1139. b2MassData.prototype.__constructor = function() {
  1140. };
  1141. b2MassData.prototype.__varz = function() {
  1142. this.center = new b2Vec2(0, 0)
  1143. };
  1144. b2MassData.prototype.mass = 0;
  1145. b2MassData.prototype.center = new b2Vec2(0, 0);
  1146. b2MassData.prototype.I = 0;var b2DynamicTree = function() {
  1147. this.__varz();
  1148. this.__constructor.apply(this, arguments)
  1149. };
  1150. b2DynamicTree.prototype.__constructor = function() {
  1151. this.m_root = null;
  1152. this.m_freeList = null;
  1153. this.m_path = 0;
  1154. this.m_insertionCount = 0
  1155. };
  1156. b2DynamicTree.prototype.__varz = function() {
  1157. };
  1158. b2DynamicTree.prototype.AllocateNode = function() {
  1159. if(this.m_freeList) {
  1160. var node = this.m_freeList;
  1161. this.m_freeList = node.parent;
  1162. node.parent = null;
  1163. node.child1 = null;
  1164. node.child2 = null;
  1165. return node
  1166. }
  1167. return new b2DynamicTreeNode
  1168. };
  1169. b2DynamicTree.prototype.FreeNode = function(node) {
  1170. node.parent = this.m_freeList;
  1171. this.m_freeList = node
  1172. };
  1173. b2DynamicTree.prototype.InsertLeaf = function(leaf) {
  1174. ++this.m_insertionCount;
  1175. if(this.m_root == null) {
  1176. this.m_root = leaf;
  1177. this.m_root.parent = null;
  1178. return
  1179. }
  1180. var center = leaf.aabb.GetCenter();
  1181. var sibling = this.m_root;
  1182. if(sibling.IsLeaf() == false) {
  1183. do {
  1184. var child1 = sibling.child1;
  1185. var child2 = sibling.child2;
  1186. 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);
  1187. 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);
  1188. if(norm1 < norm2) {
  1189. sibling = child1
  1190. }else {
  1191. sibling = child2
  1192. }
  1193. }while(sibling.IsLeaf() == false)
  1194. }
  1195. var node1 = sibling.parent;
  1196. var node2 = this.AllocateNode();
  1197. node2.parent = node1;
  1198. node2.userData = null;
  1199. node2.aabb.Combine(leaf.aabb, sibling.aabb);
  1200. if(node1) {
  1201. if(sibling.parent.child1 == sibling) {
  1202. node1.child1 = node2
  1203. }else {
  1204. node1.child2 = node2
  1205. }
  1206. node2.child1 = sibling;
  1207. node2.child2 = leaf;
  1208. sibling.parent = node2;
  1209. leaf.parent = node2;
  1210. do {
  1211. if(node1.aabb.Contains(node2.aabb)) {
  1212. break
  1213. }
  1214. node1.aabb.Combine(node1.child1.aabb, node1.child2.aabb);
  1215. node2 = node1;
  1216. node1 = node1.parent
  1217. }while(node1)
  1218. }else {
  1219. node2.child1 = sibling;
  1220. node2.child2 = leaf;
  1221. sibling.parent = node2;
  1222. leaf.parent = node2;
  1223. this.m_root = node2
  1224. }
  1225. };
  1226. b2DynamicTree.prototype.RemoveLeaf = function(leaf) {
  1227. if(leaf == this.m_root) {
  1228. this.m_root = null;
  1229. return
  1230. }
  1231. var node2 = leaf.parent;
  1232. var node1 = node2.parent;
  1233. var sibling;
  1234. if(node2.child1 == leaf) {
  1235. sibling = node2.child2
  1236. }else {
  1237. sibling = node2.child1
  1238. }
  1239. if(node1) {
  1240. if(node1.child1 == node2) {
  1241. node1.child1 = sibling
  1242. }else {
  1243. node1.child2 = sibling
  1244. }
  1245. sibling.parent = node1;
  1246. this.FreeNode(node2);
  1247. while(node1) {
  1248. var oldAABB = node1.aabb;
  1249. node1.aabb = b2AABB.Combine(node1.child1.aabb, node1.child2.aabb);
  1250. if(oldAABB.Contains(node1.aabb)) {
  1251. break
  1252. }
  1253. node1 = node1.parent
  1254. }
  1255. }else {
  1256. this.m_root = sibling;
  1257. sibling.parent = null;
  1258. this.FreeNode(node2)
  1259. }
  1260. };
  1261. b2DynamicTree.prototype.CreateProxy = function(aabb, userData) {
  1262. var node = this.AllocateNode();
  1263. var extendX = b2Settings.b2_aabbExtension;
  1264. var extendY = b2Settings.b2_aabbExtension;
  1265. node.aabb.lowerBound.x = aabb.lowerBound.x - extendX;
  1266. node.aabb.lowerBound.y = aabb.lowerBound.y - extendY;
  1267. node.aabb.upperBound.x = aabb.upperBound.x + extendX;
  1268. node.aabb.upperBound.y = aabb.upperBound.y + extendY;
  1269. node.userData = userData;
  1270. this.InsertLeaf(node);
  1271. return node
  1272. };
  1273. b2DynamicTree.prototype.DestroyProxy = function(proxy) {
  1274. this.RemoveLeaf(proxy);
  1275. this.FreeNode(proxy)
  1276. };
  1277. b2DynamicTree.prototype.MoveProxy = function(proxy, aabb, displacement) {
  1278. b2Settings.b2Assert(proxy.IsLeaf());
  1279. if(proxy.aabb.Contains(aabb)) {
  1280. return false
  1281. }
  1282. this.RemoveLeaf(proxy);
  1283. var extendX = b2Settings.b2_aabbExtension + b2Settings.b2_aabbMultiplier * (displacement.x > 0 ? displacement.x : -displacement.x);
  1284. var extendY = b2Settings.b2_aabbExtension + b2Settings.b2_aabbMultiplier * (displacement.y > 0 ? displacement.y : -displacement.y);
  1285. proxy.aabb.lowerBound.x = aabb.lowerBound.x - extendX;
  1286. proxy.aabb.lowerBound.y = aabb.lowerBound.y - extendY;
  1287. proxy.aabb.upperBound.x = aabb.upperBound.x + extendX;
  1288. proxy.aabb.upperBound.y = aabb.upperBound.y + extendY;
  1289. this.InsertLeaf(proxy);
  1290. return true
  1291. };
  1292. b2DynamicTree.prototype.Rebalance = function(iterations) {
  1293. if(this.m_root == null) {
  1294. return
  1295. }
  1296. for(var i = 0;i < iterations;i++) {
  1297. var node = this.m_root;
  1298. var bit = 0;
  1299. while(node.IsLeaf() == false) {
  1300. node = this.m_path >> bit & 1 ? node.child2 : node.child1;
  1301. bit = bit + 1 & 31
  1302. }
  1303. ++this.m_path;
  1304. this.RemoveLeaf(node);
  1305. this.InsertLeaf(node)
  1306. }
  1307. };
  1308. b2DynamicTree.prototype.GetFatAABB = function(proxy) {
  1309. return proxy.aabb
  1310. };
  1311. b2DynamicTree.prototype.GetUserData = function(proxy) {
  1312. return proxy.userData
  1313. };
  1314. b2DynamicTree.prototype.Query = function(callback, aabb) {
  1315. if(this.m_root == null) {
  1316. return
  1317. }
  1318. var stack = new Array;
  1319. var count = 0;
  1320. stack[count++] = this.m_root;
  1321. while(count > 0) {
  1322. var node = stack[--count];
  1323. if(node.aabb.TestOverlap(aabb)) {
  1324. if(node.IsLeaf()) {
  1325. var proceed = callback(node);
  1326. if(!proceed) {
  1327. return
  1328. }
  1329. }else {
  1330. stack[count++] = node.child1;
  1331. stack[count++] = node.child2
  1332. }
  1333. }
  1334. }
  1335. };
  1336. b2DynamicTree.prototype.RayCast = function(callback, input) {
  1337. if(this.m_root == null) {
  1338. return
  1339. }
  1340. var p1 = input.p1;
  1341. var p2 = input.p2;
  1342. var r = b2Math.SubtractVV(p1, p2);
  1343. r.Normalize();
  1344. var v = b2Math.CrossFV(1, r);
  1345. var abs_v = b2Math.AbsV(v);
  1346. var maxFraction = input.maxFraction;
  1347. var segmentAABB = new b2AABB;
  1348. var tX;
  1349. var tY;
  1350. tX = p1.x + maxFraction * (p2.x - p1.x);
  1351. tY = p1.y + maxFraction * (p2.y - p1.y);
  1352. segmentAABB.lowerBound.x = Math.min(p1.x, tX);
  1353. segmentAABB.lowerBound.y = Math.min(p1.y, tY);
  1354. segmentAABB.upperBound.x = Math.max(p1.x, tX);
  1355. segmentAABB.upperBound.y = Math.max(p1.y, tY);
  1356. var stack = new Array;
  1357. var count = 0;
  1358. stack[count++] = this.m_root;
  1359. while(count > 0) {
  1360. var node = stack[--count];
  1361. if(node.aabb.TestOverlap(segmentAABB) == false) {
  1362. continue
  1363. }
  1364. var c = node.aabb.GetCenter();
  1365. var h = node.aabb.GetExtents();
  1366. 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;
  1367. if(separation > 0) {
  1368. continue
  1369. }
  1370. if(node.IsLeaf()) {
  1371. var subInput = new b2RayCastInput;
  1372. subInput.p1 = input.p1;
  1373. subInput.p2 = input.p2;
  1374. subInput.maxFraction = input.maxFraction;
  1375. maxFraction = callback(subInput, node);
  1376. if(maxFraction == 0) {
  1377. return
  1378. }
  1379. tX = p1.x + maxFraction * (p2.x - p1.x);
  1380. tY = p1.y + maxFraction * (p2.y - p1.y);
  1381. segmentAABB.lowerBound.x = Math.min(p1.x, tX);
  1382. segmentAABB.lowerBound.y = Math.min(p1.y, tY);
  1383. segmentAABB.upperBound.x = Math.max(p1.x, tX);
  1384. segmentAABB.upperBound.y = Math.max(p1.y, tY)
  1385. }else {
  1386. stack[count++] = node.child1;
  1387. stack[count++] = node.child2
  1388. }
  1389. }
  1390. };
  1391. b2DynamicTree.prototype.m_root = null;
  1392. b2DynamicTree.prototype.m_freeList = null;
  1393. b2DynamicTree.prototype.m_path = 0;
  1394. b2DynamicTree.prototype.m_insertionCount = 0;var b2JointEdge = function() {
  1395. this.__varz();
  1396. this.__constructor.apply(this, arguments)
  1397. };
  1398. b2JointEdge.prototype.__constructor = function() {
  1399. };
  1400. b2JointEdge.prototype.__varz = function() {
  1401. };
  1402. b2JointEdge.prototype.other = null;
  1403. b2JointEdge.prototype.joint = null;
  1404. b2JointEdge.prototype.prev = null;
  1405. b2JointEdge.prototype.next = null;var b2RayCastInput = function() {
  1406. this.__varz();
  1407. this.__constructor.apply(this, arguments)
  1408. };
  1409. b2RayCastInput.prototype.__constructor = function() {
  1410. };
  1411. b2RayCastInput.prototype.__varz = function() {
  1412. this.p1 = new b2Vec2;
  1413. this.p2 = new b2Vec2
  1414. };
  1415. b2RayCastInput.prototype.p1 = new b2Vec2;
  1416. b2RayCastInput.prototype.p2 = new b2Vec2;
  1417. b2RayCastInput.prototype.maxFraction = null;var Features = function() {
  1418. this.__varz();
  1419. this.__constructor.apply(this, arguments)
  1420. };
  1421. Features.prototype.__constructor = function() {
  1422. };
  1423. Features.prototype.__varz = function() {
  1424. };
  1425. Features.prototype.__defineGetter__("referenceEdge", function() {
  1426. return this._referenceEdge
  1427. });
  1428. Features.prototype.__defineSetter__("referenceEdge", function(value) {
  1429. this._referenceEdge = value;
  1430. this._m_id._key = this._m_id._key & 4294967040 | this._referenceEdge & 255
  1431. });
  1432. Features.prototype.__defineGetter__("incidentEdge", function() {
  1433. return this._incidentEdge
  1434. });
  1435. Features.prototype.__defineSetter__("incidentEdge", function(value) {
  1436. this._incidentEdge = value;
  1437. this._m_id._key = this._m_id._key & 4294902015 | this._incidentEdge << 8 & 65280
  1438. });
  1439. Features.prototype.__defineGetter__("incidentVertex", function() {
  1440. return this._incidentVertex
  1441. });
  1442. Features.prototype.__defineSetter__("incidentVertex", function(value) {
  1443. this._incidentVertex = value;
  1444. this._m_id._key = this._m_id._key & 4278255615 | this._incidentVertex << 16 & 16711680
  1445. });
  1446. Features.prototype.__defineGetter__("flip", function() {
  1447. return this._flip
  1448. });
  1449. Features.prototype.__defineSetter__("flip", function(value) {
  1450. this._flip = value;
  1451. this._m_id._key = this._m_id._key & 16777215 | this._flip << 24 & 4278190080
  1452. });
  1453. Features.prototype._referenceEdge = 0;
  1454. Features.prototype._incidentEdge = 0;
  1455. Features.prototype._incidentVertex = 0;
  1456. Features.prototype._flip = 0;
  1457. Features.prototype._m_id = null;var b2FilterData = function() {
  1458. this.__varz();
  1459. this.__constructor.apply(this, arguments)
  1460. };
  1461. b2FilterData.prototype.__constructor = function() {
  1462. };
  1463. b2FilterData.prototype.__varz = function() {
  1464. this.categoryBits = 1;
  1465. this.maskBits = 65535
  1466. };
  1467. b2FilterData.prototype.Copy = function() {
  1468. var copy = new b2FilterData;
  1469. copy.categoryBits = this.categoryBits;
  1470. copy.maskBits = this.maskBits;
  1471. copy.groupIndex = this.groupIndex;
  1472. return copy
  1473. };
  1474. b2FilterData.prototype.categoryBits = 1;
  1475. b2FilterData.prototype.maskBits = 65535;
  1476. b2FilterData.prototype.groupIndex = 0;var b2AABB = function() {
  1477. this.__varz();
  1478. this.__constructor.apply(this, arguments)
  1479. };
  1480. b2AABB.prototype.__constructor = function() {
  1481. };
  1482. b2AABB.prototype.__varz = function() {
  1483. this.lowerBound = new b2Vec2;
  1484. this.upperBound = new b2Vec2
  1485. };
  1486. b2AABB.Combine = function(aabb1, aabb2) {
  1487. var aabb = new b2AABB;
  1488. aabb.Combine(aabb1, aabb2);
  1489. return aabb
  1490. };
  1491. b2AABB.prototype.IsValid = function() {
  1492. var dX = this.upperBound.x - this.lowerBound.x;
  1493. var dY = this.upperBound.y - this.lowerBound.y;
  1494. var valid = dX >= 0 && dY >= 0;
  1495. valid = valid && this.lowerBound.IsValid() && this.upperBound.IsValid();
  1496. return valid
  1497. };
  1498. b2AABB.prototype.GetCenter = function() {
  1499. return new b2Vec2((this.lowerBound.x + this.upperBound.x) / 2, (this.lowerBound.y + this.upperBound.y) / 2)
  1500. };
  1501. b2AABB.prototype.GetExtents = function() {
  1502. return new b2Vec2((this.upperBound.x - this.lowerBound.x) / 2, (this.upperBound.y - this.lowerBound.y) / 2)
  1503. };
  1504. b2AABB.prototype.Contains = function(aabb) {
  1505. 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;
  1506. return result
  1507. };
  1508. b2AABB.prototype.RayCast = function(output, input) {
  1509. var tmin = -Number.MAX_VALUE;
  1510. var tmax = Number.MAX_VALUE;
  1511. var pX = input.p1.x;
  1512. var pY = input.p1.y;
  1513. var dX = input.p2.x - input.p1.x;
  1514. var dY = input.p2.y - input.p1.y;
  1515. var absDX = Math.abs(dX);
  1516. var absDY = Math.abs(dY);
  1517. var normal = output.normal;
  1518. var inv_d;
  1519. var t1;
  1520. var t2;
  1521. var t3;
  1522. var s;
  1523. if(absDX < Number.MIN_VALUE) {
  1524. if(pX < this.lowerBound.x || this.upperBound.x < pX) {
  1525. return false
  1526. }
  1527. }else {
  1528. inv_d = 1 / dX;
  1529. t1 = (this.lowerBound.x - pX) * inv_d;
  1530. t2 = (this.upperBound.x - pX) * inv_d;
  1531. s = -1;
  1532. if(t1 > t2) {
  1533. t3 = t1;
  1534. t1 = t2;
  1535. t2 = t3;
  1536. s = 1
  1537. }
  1538. if(t1 > tmin) {
  1539. normal.x = s;
  1540. normal.y = 0;
  1541. tmin = t1
  1542. }
  1543. tmax = Math.min(tmax, t2);
  1544. if(tmin > tmax) {
  1545. return false
  1546. }
  1547. }
  1548. if(absDY < Number.MIN_VALUE) {
  1549. if(pY < this.lowerBound.y || this.upperBound.y < pY) {
  1550. return false
  1551. }
  1552. }else {
  1553. inv_d = 1 / dY;
  1554. t1 = (this.lowerBound.y - pY) * inv_d;
  1555. t2 = (this.upperBound.y - pY) * inv_d;
  1556. s = -1;
  1557. if(t1 > t2) {
  1558. t3 = t1;
  1559. t1 = t2;
  1560. t2 = t3;
  1561. s = 1
  1562. }
  1563. if(t1 > tmin) {
  1564. normal.y = s;
  1565. normal.x = 0;
  1566. tmin = t1
  1567. }
  1568. tmax = Math.min(tmax, t2);
  1569. if(tmin > tmax) {
  1570. return false
  1571. }
  1572. }
  1573. output.fraction = tmin;
  1574. return true
  1575. };
  1576. b2AABB.prototype.TestOverlap = function(other) {
  1577. var d1X = other.lowerBound.x - this.upperBound.x;
  1578. var d1Y = other.lowerBound.y - this.upperBound.y;
  1579. var d2X = this.lowerBound.x - other.upperBound.x;
  1580. var d2Y = this.lowerBound.y - other.upperBound.y;
  1581. if(d1X > 0 || d1Y > 0) {
  1582. return false
  1583. }
  1584. if(d2X > 0 || d2Y > 0) {
  1585. return false
  1586. }
  1587. return true
  1588. };
  1589. b2AABB.prototype.Combine = function(aabb1, aabb2) {
  1590. this.lowerBound.x = Math.min(aabb1.lowerBound.x, aabb2.lowerBound.x);
  1591. this.lowerBound.y = Math.min(aabb1.lowerBound.y, aabb2.lowerBound.y);
  1592. this.upperBound.x = Math.max(aabb1.upperBound.x, aabb2.upperBound.x);
  1593. this.upperBound.y = Math.max(aabb1.upperBound.y, aabb2.upperBound.y)
  1594. };
  1595. b2AABB.prototype.lowerBound = new b2Vec2;
  1596. b2AABB.prototype.upperBound = new b2Vec2;var b2Jacobian = function() {
  1597. this.__varz();
  1598. this.__constructor.apply(this, arguments)
  1599. };
  1600. b2Jacobian.prototype.__constructor = function() {
  1601. };
  1602. b2Jacobian.prototype.__varz = function() {
  1603. this.linearA = new b2Vec2;
  1604. this.linearB = new b2Vec2
  1605. };
  1606. b2Jacobian.prototype.SetZero = function() {
  1607. this.linearA.SetZero();
  1608. this.angularA = 0;
  1609. this.linearB.SetZero();
  1610. this.angularB = 0
  1611. };
  1612. b2Jacobian.prototype.Set = function(x1, a1, x2, a2) {
  1613. this.linearA.SetV(x1);
  1614. this.angularA = a1;
  1615. this.linearB.SetV(x2);
  1616. this.angularB = a2
  1617. };
  1618. b2Jacobian.prototype.Compute = function(x1, a1, x2, a2) {
  1619. 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
  1620. };
  1621. b2Jacobian.prototype.linearA = new b2Vec2;
  1622. b2Jacobian.prototype.angularA = null;
  1623. b2Jacobian.prototype.linearB = new b2Vec2;
  1624. b2Jacobian.prototype.angularB = null;var b2Bound = function() {
  1625. this.__varz();
  1626. this.__constructor.apply(this, arguments)
  1627. };
  1628. b2Bound.prototype.__constructor = function() {
  1629. };
  1630. b2Bound.prototype.__varz = function() {
  1631. };
  1632. b2Bound.prototype.IsLower = function() {
  1633. return(this.value & 1) == 0
  1634. };
  1635. b2Bound.prototype.IsUpper = function() {
  1636. return(this.value & 1) == 1
  1637. };
  1638. b2Bound.prototype.Swap = function(b) {
  1639. var tempValue = this.value;
  1640. var tempProxy = this.proxy;
  1641. var tempStabbingCount = this.stabbingCount;
  1642. this.value = b.value;
  1643. this.proxy = b.proxy;
  1644. this.stabbingCount = b.stabbingCount;
  1645. b.value = tempValue;
  1646. b.proxy = tempProxy;
  1647. b.stabbingCount = tempStabbingCount
  1648. };
  1649. b2Bound.prototype.value = 0;
  1650. b2Bound.prototype.proxy = null;
  1651. b2Bound.prototype.stabbingCount = 0;var b2SimplexVertex = function() {
  1652. this.__varz();
  1653. this.__constructor.apply(this, arguments)
  1654. };
  1655. b2SimplexVertex.prototype.__constructor = function() {
  1656. };
  1657. b2SimplexVertex.prototype.__varz = function() {
  1658. };
  1659. b2SimplexVertex.prototype.Set = function(other) {
  1660. this.wA.SetV(other.wA);
  1661. this.wB.SetV(other.wB);
  1662. this.w.SetV(other.w);
  1663. this.a = other.a;
  1664. this.indexA = other.indexA;
  1665. this.indexB = other.indexB
  1666. };
  1667. b2SimplexVertex.prototype.wA = null;
  1668. b2SimplexVertex.prototype.wB = null;
  1669. b2SimplexVertex.prototype.w = null;
  1670. b2SimplexVertex.prototype.a = null;
  1671. b2SimplexVertex.prototype.indexA = 0;
  1672. b2SimplexVertex.prototype.indexB = 0;var b2Mat22 = function() {
  1673. this.__varz();
  1674. this.__constructor.apply(this, arguments)
  1675. };
  1676. b2Mat22.prototype.__constructor = function() {
  1677. this.col1.x = this.col2.y = 1
  1678. };
  1679. b2Mat22.prototype.__varz = function() {
  1680. this.col1 = new b2Vec2;
  1681. this.col2 = new b2Vec2
  1682. };
  1683. b2Mat22.FromAngle = function(angle) {
  1684. var mat = new b2Mat22;
  1685. mat.Set(angle);
  1686. return mat
  1687. };
  1688. b2Mat22.FromVV = function(c1, c2) {
  1689. var mat = new b2Mat22;
  1690. mat.SetVV(c1, c2);
  1691. return mat
  1692. };
  1693. b2Mat22.prototype.Set = function(angle) {
  1694. var c = Math.cos(angle);
  1695. var s = Math.sin(angle);
  1696. this.col1.x = c;
  1697. this.col2.x = -s;
  1698. this.col1.y = s;
  1699. this.col2.y = c
  1700. };
  1701. b2Mat22.prototype.SetVV = function(c1, c2) {
  1702. this.col1.SetV(c1);
  1703. this.col2.SetV(c2)
  1704. };
  1705. b2Mat22.prototype.Copy = function() {
  1706. var mat = new b2Mat22;
  1707. mat.SetM(this);
  1708. return mat
  1709. };
  1710. b2Mat22.prototype.SetM = function(m) {
  1711. this.col1.SetV(m.col1);
  1712. this.col2.SetV(m.col2)
  1713. };
  1714. b2Mat22.prototype.AddM = function(m) {
  1715. this.col1.x += m.col1.x;
  1716. this.col1.y += m.col1.y;
  1717. this.col2.x += m.col2.x;
  1718. this.col2.y += m.col2.y
  1719. };
  1720. b2Mat22.prototype.SetIdentity = function() {
  1721. this.col1.x = 1;
  1722. this.col2.x = 0;
  1723. this.col1.y = 0;
  1724. this.col2.y = 1
  1725. };
  1726. b2Mat22.prototype.SetZero = function() {
  1727. this.col1.x = 0;
  1728. this.col2.x = 0;
  1729. this.col1.y = 0;
  1730. this.col2.y = 0
  1731. };
  1732. b2Mat22.prototype.GetAngle = function() {
  1733. return Math.atan2(this.col1.y, this.col1.x)
  1734. };
  1735. b2Mat22.prototype.GetInverse = function(out) {
  1736. var a = this.col1.x;
  1737. var b = this.col2.x;
  1738. var c = this.col1.y;
  1739. var d = this.col2.y;
  1740. var det = a * d - b * c;
  1741. if(det != 0) {
  1742. det = 1 / det
  1743. }
  1744. out.col1.x = det * d;
  1745. out.col2.x = -det * b;
  1746. out.col1.y = -det * c;
  1747. out.col2.y = det * a;
  1748. return out
  1749. };
  1750. b2Mat22.prototype.Solve = function(out, bX, bY) {
  1751. var a11 = this.col1.x;
  1752. var a12 = this.col2.x;
  1753. var a21 = this.col1.y;
  1754. var a22 = this.col2.y;
  1755. var det = a11 * a22 - a12 * a21;
  1756. if(det != 0) {
  1757. det = 1 / det
  1758. }
  1759. out.x = det * (a22 * bX - a12 * bY);
  1760. out.y = det * (a11 * bY - a21 * bX);
  1761. return out
  1762. };
  1763. b2Mat22.prototype.Abs = function() {
  1764. this.col1.Abs();
  1765. this.col2.Abs()
  1766. };
  1767. b2Mat22.prototype.col1 = new b2Vec2;
  1768. b2Mat22.prototype.col2 = new b2Vec2;var b2SimplexCache = function() {
  1769. this.__varz();
  1770. this.__constructor.apply(this, arguments)
  1771. };
  1772. b2SimplexCache.prototype.__constructor = function() {
  1773. };
  1774. b2SimplexCache.prototype.__varz = function() {
  1775. this.indexA = new Array(3);
  1776. this.indexB = new Array(3)
  1777. };
  1778. b2SimplexCache.prototype.metric = null;
  1779. b2SimplexCache.prototype.count = 0;
  1780. b2SimplexCache.prototype.indexA = new Array(3);
  1781. b2SimplexCache.prototype.indexB = new Array(3);var b2Shape = function() {
  1782. this.__varz();
  1783. this.__constructor.apply(this, arguments)
  1784. };
  1785. b2Shape.prototype.__constructor = function() {
  1786. this.m_type = b2Shape.e_unknownShape;
  1787. this.m_radius = b2Settings.b2_linearSlop
  1788. };
  1789. b2Shape.prototype.__varz = function() {
  1790. };
  1791. b2Shape.TestOverlap = function(shape1, transform1, shape2, transform2) {
  1792. var input = new b2DistanceInput;
  1793. input.proxyA = new b2DistanceProxy;
  1794. input.proxyA.Set(shape1);
  1795. input.proxyB = new b2DistanceProxy;
  1796. input.proxyB.Set(shape2);
  1797. input.transformA = transform1;
  1798. input.transformB = transform2;
  1799. input.useRadii = true;
  1800. var simplexCache = new b2SimplexCache;
  1801. simplexCache.count = 0;
  1802. var output = new b2DistanceOutput;
  1803. b2Distance.Distance(output, simplexCache, input);
  1804. return output.distance < 10 * Number.MIN_VALUE
  1805. };
  1806. b2Shape.e_hitCollide = 1;
  1807. b2Shape.e_missCollide = 0;
  1808. b2Shape.e_startsInsideCollide = -1;
  1809. b2Shape.e_unknownShape = -1;
  1810. b2Shape.e_circleShape = 0;
  1811. b2Shape.e_polygonShape = 1;
  1812. b2Shape.e_edgeShape = 2;
  1813. b2Shape.e_shapeTypeCount = 3;
  1814. b2Shape.prototype.Copy = function() {
  1815. return null
  1816. };
  1817. b2Shape.prototype.Set = function(other) {
  1818. this.m_radius = other.m_radius
  1819. };
  1820. b2Shape.prototype.GetType = function() {
  1821. return this.m_type
  1822. };
  1823. b2Shape.prototype.TestPoint = function(xf, p) {
  1824. return false
  1825. };
  1826. b2Shape.prototype.RayCast = function(output, input, transform) {
  1827. return false
  1828. };
  1829. b2Shape.prototype.ComputeAABB = function(aabb, xf) {
  1830. };
  1831. b2Shape.prototype.ComputeMass = function(massData, density) {
  1832. };
  1833. b2Shape.prototype.ComputeSubmergedArea = function(normal, offset, xf, c) {
  1834. return 0
  1835. };
  1836. b2Shape.prototype.m_type = 0;
  1837. b2Shape.prototype.m_radius = null;var b2Segment = function() {
  1838. this.__varz();
  1839. this.__constructor.apply(this, arguments)
  1840. };
  1841. b2Segment.prototype.__constructor = function() {
  1842. };
  1843. b2Segment.prototype.__varz = function() {
  1844. this.p1 = new b2Vec2;
  1845. this.p2 = new b2Vec2
  1846. };
  1847. b2Segment.prototype.TestSegment = function(lambda, normal, segment, maxLambda) {
  1848. var s = segment.p1;
  1849. var rX = segment.p2.x - s.x;
  1850. var rY = segment.p2.y - s.y;
  1851. var dX = this.p2.x - this.p1.x;
  1852. var dY = this.p2.y - this.p1.y;
  1853. var nX = dY;
  1854. var nY = -dX;
  1855. var k_slop = 100 * Number.MIN_VALUE;
  1856. var denom = -(rX * nX + rY * nY);
  1857. if(denom > k_slop) {
  1858. var bX = s.x - this.p1.x;
  1859. var bY = s.y - this.p1.y;
  1860. var a = bX * nX + bY * nY;
  1861. if(0 <= a && a <= maxLambda * denom) {
  1862. var mu2 = -rX * bY + rY * bX;
  1863. if(-k_slop * denom <= mu2 && mu2 <= denom * (1 + k_slop)) {
  1864. a /= denom;
  1865. var nLen = Math.sqrt(nX * nX + nY * nY);
  1866. nX /= nLen;
  1867. nY /= nLen;
  1868. lambda[0] = a;
  1869. normal.Set(nX, nY);
  1870. return true
  1871. }
  1872. }
  1873. }
  1874. return false
  1875. };
  1876. b2Segment.prototype.Extend = function(aabb) {
  1877. this.ExtendForward(aabb);
  1878. this.ExtendBackward(aabb)
  1879. };
  1880. b2Segment.prototype.ExtendForward = function(aabb) {
  1881. var dX = this.p2.x - this.p1.x;
  1882. var dY = this.p2.y - this.p1.y;
  1883. 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, dY > 0 ? (aabb.upperBound.y - this.p1.y) / dY : dY < 0 ? (aabb.lowerBound.y - this.p1.y) / dY : Number.POSITIVE_INFINITY);
  1884. this.p2.x = this.p1.x + dX * lambda;
  1885. this.p2.y = this.p1.y + dY * lambda
  1886. };
  1887. b2Segment.prototype.ExtendBackward = function(aabb) {
  1888. var dX = -this.p2.x + this.p1.x;
  1889. var dY = -this.p2.y + this.p1.y;
  1890. 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, dY > 0 ? (aabb.upperBound.y - this.p2.y) / dY : dY < 0 ? (aabb.lowerBound.y - this.p2.y) / dY : Number.POSITIVE_INFINITY);
  1891. this.p1.x = this.p2.x + dX * lambda;
  1892. this.p1.y = this.p2.y + dY * lambda
  1893. };
  1894. b2Segment.prototype.p1 = new b2Vec2;
  1895. b2Segment.prototype.p2 = new b2Vec2;var b2ContactRegister = function() {
  1896. this.__varz();
  1897. this.__constructor.apply(this, arguments)
  1898. };
  1899. b2ContactRegister.prototype.__constructor = function() {
  1900. };
  1901. b2ContactRegister.prototype.__varz = function() {
  1902. };
  1903. b2ContactRegister.prototype.createFcn = null;
  1904. b2ContactRegister.prototype.destroyFcn = null;
  1905. b2ContactRegister.prototype.primary = null;
  1906. b2ContactRegister.prototype.pool = null;
  1907. b2ContactRegister.prototype.poolCount = 0;var b2DebugDraw = function() {
  1908. this.__varz();
  1909. this.__constructor.apply(this, arguments)
  1910. };
  1911. b2DebugDraw.prototype.__constructor = function() {
  1912. this.m_drawFlags = 0
  1913. };
  1914. b2DebugDraw.prototype.__varz = function() {
  1915. };
  1916. b2DebugDraw.e_shapeBit = 1;
  1917. b2DebugDraw.e_jointBit = 2;
  1918. b2DebugDraw.e_aabbBit = 4;
  1919. b2DebugDraw.e_pairBit = 8;
  1920. b2DebugDraw.e_centerOfMassBit = 16;
  1921. b2DebugDraw.e_controllerBit = 32;
  1922. b2DebugDraw.prototype.SetFlags = function(flags) {
  1923. this.m_drawFlags = flags
  1924. };
  1925. b2DebugDraw.prototype.GetFlags = function() {
  1926. return this.m_drawFlags
  1927. };
  1928. b2DebugDraw.prototype.AppendFlags = function(flags) {
  1929. this.m_drawFlags |= flags
  1930. };
  1931. b2DebugDraw.prototype.ClearFlags = function(flags) {
  1932. this.m_drawFlags &= ~flags
  1933. };
  1934. b2DebugDraw.prototype.SetSprite = function(sprite) {
  1935. this.m_sprite = sprite
  1936. };
  1937. b2DebugDraw.prototype.GetSprite = function() {
  1938. return this.m_sprite
  1939. };
  1940. b2DebugDraw.prototype.SetDrawScale = function(drawScale) {
  1941. this.m_drawScale = drawScale
  1942. };
  1943. b2DebugDraw.prototype.GetDrawScale = function() {
  1944. return this.m_drawScale
  1945. };
  1946. b2DebugDraw.prototype.SetLineThickness = function(lineThickness) {
  1947. this.m_lineThickness = lineThickness
  1948. };
  1949. b2DebugDraw.prototype.GetLineThickness = function() {
  1950. return this.m_lineThickness
  1951. };
  1952. b2DebugDraw.prototype.SetAlpha = function(alpha) {
  1953. this.m_alpha = alpha
  1954. };
  1955. b2DebugDraw.prototype.GetAlpha = function() {
  1956. return this.m_alpha
  1957. };
  1958. b2DebugDraw.prototype.SetFillAlpha = function(alpha) {
  1959. this.m_fillAlpha = alpha
  1960. };
  1961. b2DebugDraw.prototype.GetFillAlpha = function() {
  1962. return this.m_fillAlpha
  1963. };
  1964. b2DebugDraw.prototype.SetXFormScale = function(xformScale) {
  1965. this.m_xformScale = xformScale
  1966. };
  1967. b2DebugDraw.prototype.GetXFormScale = function() {
  1968. return this.m_xformScale
  1969. };
  1970. b2DebugDraw.prototype.Clear = function() {
  1971. this.m_sprite.clearRect(0, 0, this.m_sprite.canvas.width, this.m_sprite.canvas.height)
  1972. };
  1973. b2DebugDraw.prototype.Y = function(y) {
  1974. return this.m_sprite.canvas.height - y
  1975. };
  1976. b2DebugDraw.prototype.ToWorldPoint = function(localPoint) {
  1977. return new b2Vec2(localPoint.x / this.m_drawScale, this.Y(localPoint.y) / this.m_drawScale)
  1978. };
  1979. b2DebugDraw.prototype.ColorStyle = function(color, alpha) {
  1980. return"rgba(" + color.r + ", " + color.g + ", " + color.b + ", " + alpha + ")"
  1981. };
  1982. b2DebugDraw.prototype.DrawPolygon = function(vertices, vertexCount, color) {
  1983. this.m_sprite.graphics.lineStyle(this.m_lineThickness, color.color, this.m_alpha);
  1984. this.m_sprite.graphics.moveTo(vertices[0].x * this.m_drawScale, vertices[0].y * this.m_drawScale);
  1985. for(var i = 1;i < vertexCount;i++) {
  1986. this.m_sprite.graphics.lineTo(vertices[i].x * this.m_drawScale, vertices[i].y * this.m_drawScale)
  1987. }
  1988. this.m_sprite.graphics.lineTo(vertices[0].x * this.m_drawScale, vertices[0].y * this.m_drawScale)
  1989. };
  1990. b2DebugDraw.prototype.DrawSolidPolygon = function(vertices, vertexCount, color) {
  1991. this.m_sprite.strokeSyle = this.ColorStyle(color, this.m_alpha);
  1992. this.m_sprite.lineWidth = this.m_lineThickness;
  1993. this.m_sprite.fillStyle = this.ColorStyle(color, this.m_fillAlpha);
  1994. this.m_sprite.beginPath();
  1995. this.m_sprite.moveTo(vertices[0].x * this.m_drawScale, this.Y(vertices[0].y * this.m_drawScale));
  1996. for(var i = 1;i < vertexCount;i++) {
  1997. this.m_sprite.lineTo(vertices[i].x * this.m_drawScale, this.Y(vertices[i].y * this.m_drawScale))
  1998. }
  1999. this.m_sprite.lineTo(vertices[0].x * this.m_drawScale, this.Y(vertices[0].y * this.m_drawScale));
  2000. this.m_sprite.fill();
  2001. this.m_sprite.stroke();
  2002. this.m_sprite.closePath()
  2003. };
  2004. b2DebugDraw.prototype.DrawCircle = function(center, radius, color) {
  2005. this.m_sprite.graphics.lineStyle(this.m_lineThickness, color.color, this.m_alpha);
  2006. this.m_sprite.graphics.drawCircle(center.x * this.m_drawScale, center.y * this.m_drawScale, radius * this.m_drawScale)
  2007. };
  2008. b2DebugDraw.prototype.DrawSolidCircle = function(center, radius, axis, color) {
  2009. this.m_sprite.strokeSyle = this.ColorStyle(color, this.m_alpha);
  2010. this.m_sprite.lineWidth = this.m_lineThickness;
  2011. this.m_sprite.fillStyle = this.ColorStyle(color, this.m_fillAlpha);
  2012. this.m_sprite.beginPath();
  2013. this.m_sprite.arc(center.x * this.m_drawScale, this.Y(center.y * this.m_drawScale), radius * this.m_drawScale, 0, Math.PI * 2, true);
  2014. this.m_sprite.fill();
  2015. this.m_sprite.stroke();
  2016. this.m_sprite.closePath()
  2017. };
  2018. b2DebugDraw.prototype.DrawSegment = function(p1, p2, color) {
  2019. this.m_sprite.lineWidth = this.m_lineThickness;
  2020. this.m_sprite.strokeSyle = this.ColorStyle(color, this.m_alpha);
  2021. this.m_sprite.beginPath();
  2022. this.m_sprite.moveTo(p1.x * this.m_drawScale, this.Y(p1.y * this.m_drawScale));
  2023. this.m_sprite.lineTo(p2.x * this.m_drawScale, this.Y(p2.y * this.m_drawScale));
  2024. this.m_sprite.stroke();
  2025. this.m_sprite.closePath()
  2026. };
  2027. b2DebugDraw.prototype.DrawTransform = function(xf) {
  2028. this.m_sprite.lineWidth = this.m_lineThickness;
  2029. this.m_sprite.strokeSyle = this.ColorStyle(new b2Color(255, 0, 0), this.m_alpha);
  2030. this.m_sprite.beginPath();
  2031. this.m_sprite.moveTo(xf.position.x * this.m_drawScale, this.Y(xf.position.y * this.m_drawScale));
  2032. this.m_sprite.lineTo((xf.position.x + this.m_xformScale * xf.R.col1.x) * this.m_drawScale, this.Y((xf.position.y + this.m_xformScale * xf.R.col1.y) * this.m_drawScale));
  2033. this.m_sprite.stroke();
  2034. this.m_sprite.closePath();
  2035. this.m_sprite.strokeSyle = this.ColorStyle(new b2Color(0, 255, 0), this.m_alpha);
  2036. this.m_sprite.beginPath();
  2037. this.m_sprite.moveTo(xf.position.x * this.m_drawScale, this.Y(xf.position.y * this.m_drawScale));
  2038. this.m_sprite.lineTo((xf.position.x + this.m_xformScale * xf.R.col2.x) * this.m_drawScale, this.Y((xf.position.y + this.m_xformScale * xf.R.col2.y) * this.m_drawScale));
  2039. this.m_sprite.stroke();
  2040. this.m_sprite.closePath()
  2041. };
  2042. b2DebugDraw.prototype.m_drawFlags = 0;
  2043. b2DebugDraw.prototype.m_sprite = null;
  2044. b2DebugDraw.prototype.m_drawScale = 1;
  2045. b2DebugDraw.prototype.m_lineThickness = 1;
  2046. b2DebugDraw.prototype.m_alpha = 1;
  2047. b2DebugDraw.prototype.m_fillAlpha = 1;
  2048. b2DebugDraw.prototype.m_xformScale = 1;var b2Sweep = function() {
  2049. this.__varz();
  2050. this.__constructor.apply(this, arguments)
  2051. };
  2052. b2Sweep.prototype.__constructor = function() {
  2053. };
  2054. b2Sweep.prototype.__varz = function() {
  2055. this.localCenter = new b2Vec2;
  2056. this.c0 = new b2Vec2;
  2057. this.c = new b2Vec2
  2058. };
  2059. b2Sweep.prototype.Set = function(other) {
  2060. this.localCenter.SetV(other.localCenter);
  2061. this.c0.SetV(other.c0);
  2062. this.c.SetV(other.c);
  2063. this.a0 = other.a0;
  2064. this.a = other.a;
  2065. this.t0 = other.t0
  2066. };
  2067. b2Sweep.prototype.Copy = function() {
  2068. var copy = new b2Sweep;
  2069. copy.localCenter.SetV(this.localCenter);
  2070. copy.c0.SetV(this.c0);
  2071. copy.c.SetV(this.c);
  2072. copy.a0 = this.a0;
  2073. copy.a = this.a;
  2074. copy.t0 = this.t0;
  2075. return copy
  2076. };
  2077. b2Sweep.prototype.GetTransform = function(xf, alpha) {
  2078. xf.position.x = (1 - alpha) * this.c0.x + alpha * this.c.x;
  2079. xf.position.y = (1 - alpha) * this.c0.y + alpha * this.c.y;
  2080. var angle = (1 - alpha) * this.a0 + alpha * this.a;
  2081. xf.R.Set(angle);
  2082. var tMat = xf.R;
  2083. xf.position.x -= tMat.col1.x * this.localCenter.x + tMat.col2.x * this.localCenter.y;
  2084. xf.position.y -= tMat.col1.y * this.localCenter.x + tMat.col2.y * this.localCenter.y
  2085. };
  2086. b2Sweep.prototype.Advance = function(t) {
  2087. if(this.t0 < t && 1 - this.t0 > Number.MIN_VALUE) {
  2088. var alpha = (t - this.t0) / (1 - this.t0);
  2089. this.c0.x = (1 - alpha) * this.c0.x + alpha * this.c.x;
  2090. this.c0.y = (1 - alpha) * this.c0.y + alpha * this.c.y;
  2091. this.a0 = (1 - alpha) * this.a0 + alpha * this.a;
  2092. this.t0 = t
  2093. }
  2094. };
  2095. b2Sweep.prototype.localCenter = new b2Vec2;
  2096. b2Sweep.prototype.c0 = new b2Vec2;
  2097. b2Sweep.prototype.c = new b2Vec2;
  2098. b2Sweep.prototype.a0 = null;
  2099. b2Sweep.prototype.a = null;
  2100. b2Sweep.prototype.t0 = null;var b2DistanceOutput = function() {
  2101. this.__varz();
  2102. this.__constructor.apply(this, arguments)
  2103. };
  2104. b2DistanceOutput.prototype.__constructor = function() {
  2105. };
  2106. b2DistanceOutput.prototype.__varz = function() {
  2107. this.pointA = new b2Vec2;
  2108. this.pointB = new b2Vec2
  2109. };
  2110. b2DistanceOutput.prototype.pointA = new b2Vec2;
  2111. b2DistanceOutput.prototype.pointB = new b2Vec2;
  2112. b2DistanceOutput.prototype.distance = null;
  2113. b2DistanceOutput.prototype.iterations = 0;var b2Mat33 = function() {
  2114. this.__varz();
  2115. this.__constructor.apply(this, arguments)
  2116. };
  2117. b2Mat33.prototype.__constructor = function(c1, c2, c3) {
  2118. if(!c1 && !c2 && !c3) {
  2119. this.col1.SetZero();
  2120. this.col2.SetZero();
  2121. this.col3.SetZero()
  2122. }else {
  2123. this.col1.SetV(c1);
  2124. this.col2.SetV(c2);
  2125. this.col3.SetV(c3)
  2126. }
  2127. };
  2128. b2Mat33.prototype.__varz = function() {
  2129. this.col1 = new b2Vec3;
  2130. this.col2 = new b2Vec3;
  2131. this.col3 = new b2Vec3
  2132. };
  2133. b2Mat33.prototype.SetVVV = function(c1, c2, c3) {
  2134. this.col1.SetV(c1);
  2135. this.col2.SetV(c2);
  2136. this.col3.SetV(c3)
  2137. };
  2138. b2Mat33.prototype.Copy = function() {
  2139. return new b2Mat33(this.col1, this.col2, this.col3)
  2140. };
  2141. b2Mat33.prototype.SetM = function(m) {
  2142. this.col1.SetV(m.col1);
  2143. this.col2.SetV(m.col2);
  2144. this.col3.SetV(m.col3)
  2145. };
  2146. b2Mat33.prototype.AddM = function(m) {
  2147. this.col1.x += m.col1.x;
  2148. this.col1.y += m.col1.y;
  2149. this.col1.z += m.col1.z;
  2150. this.col2.x += m.col2.x;
  2151. this.col2.y += m.col2.y;
  2152. this.col2.z += m.col2.z;
  2153. this.col3.x += m.col3.x;
  2154. this.col3.y += m.col3.y;
  2155. this.col3.z += m.col3.z
  2156. };
  2157. b2Mat33.prototype.SetIdentity = function() {
  2158. this.col1.x = 1;
  2159. this.col2.x = 0;
  2160. this.col3.x = 0;
  2161. this.col1.y = 0;
  2162. this.col2.y = 1;
  2163. this.col3.y = 0;
  2164. this.col1.z = 0;
  2165. this.col2.z = 0;
  2166. this.col3.z = 1
  2167. };
  2168. b2Mat33.prototype.SetZero = function() {
  2169. this.col1.x = 0;
  2170. this.col2.x = 0;
  2171. this.col3.x = 0;
  2172. this.col1.y = 0;
  2173. this.col2.y = 0;
  2174. this.col3.y = 0;
  2175. this.col1.z = 0;
  2176. this.col2.z = 0;
  2177. this.col3.z = 0
  2178. };
  2179. b2Mat33.prototype.Solve22 = function(out, bX, bY) {
  2180. var a11 = this.col1.x;
  2181. var a12 = this.col2.x;
  2182. var a21 = this.col1.y;
  2183. var a22 = this.col2.y;
  2184. var det = a11 * a22 - a12 * a21;
  2185. if(det != 0) {
  2186. det = 1 / det
  2187. }
  2188. out.x = det * (a22 * bX - a12 * bY);
  2189. out.y = det * (a11 * bY - a21 * bX);
  2190. return out
  2191. };
  2192. b2Mat33.prototype.Solve33 = function(out, bX, bY, bZ) {
  2193. var a11 = this.col1.x;
  2194. var a21 = this.col1.y;
  2195. var a31 = this.col1.z;
  2196. var a12 = this.col2.x;
  2197. var a22 = this.col2.y;
  2198. var a32 = this.col2.z;
  2199. var a13 = this.col3.x;
  2200. var a23 = this.col3.y;
  2201. var a33 = this.col3.z;
  2202. var det = a11 * (a22 * a33 - a32 * a23) + a21 * (a32 * a13 - a12 * a33) + a31 * (a12 * a23 - a22 * a13);
  2203. if(det != 0) {
  2204. det = 1 / det
  2205. }
  2206. out.x = det * (bX * (a22 * a33 - a32 * a23) + bY * (a32 * a13 - a12 * a33) + bZ * (a12 * a23 - a22 * a13));
  2207. out.y = det * (a11 * (bY * a33 - bZ * a23) + a21 * (bZ * a13 - bX * a33) + a31 * (bX * a23 - bY * a13));
  2208. out.z = det * (a11 * (a22 * bZ - a32 * bY) + a21 * (a32 * bX - a12 * bZ) + a31 * (a12 * bY - a22 * bX));
  2209. return out
  2210. };
  2211. b2Mat33.prototype.col1 = new b2Vec3;
  2212. b2Mat33.prototype.col2 = new b2Vec3;
  2213. b2Mat33.prototype.col3 = new b2Vec3;var b2PositionSolverManifold = function() {
  2214. this.__varz();
  2215. this.__constructor.apply(this, arguments)
  2216. };
  2217. b2PositionSolverManifold.prototype.__constructor = function() {
  2218. this.m_normal = new b2Vec2;
  2219. this.m_separations = new Array(b2Settings.b2_maxManifoldPoints);
  2220. this.m_points = new Array(b2Settings.b2_maxManifoldPoints);
  2221. for(var i = 0;i < b2Settings.b2_maxManifoldPoints;i++) {
  2222. this.m_points[i] = new b2Vec2
  2223. }
  2224. };
  2225. b2PositionSolverManifold.prototype.__varz = function() {
  2226. };
  2227. b2PositionSolverManifold.circlePointA = new b2Vec2;
  2228. b2PositionSolverManifold.circlePointB = new b2Vec2;
  2229. b2PositionSolverManifold.prototype.Initialize = function(cc) {
  2230. b2Settings.b2Assert(cc.pointCount > 0);
  2231. var i = 0;
  2232. var clipPointX;
  2233. var clipPointY;
  2234. var tMat;
  2235. var tVec;
  2236. var planePointX;
  2237. var planePointY;
  2238. switch(cc.type) {
  2239. case b2Manifold.e_circles:
  2240. tMat = cc.bodyA.m_xf.R;
  2241. tVec = cc.localPoint;
  2242. var pointAX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2243. var pointAY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2244. tMat = cc.bodyB.m_xf.R;
  2245. tVec = cc.points[0].localPoint;
  2246. var pointBX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2247. var pointBY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2248. var dX = pointBX - pointAX;
  2249. var dY = pointBY - pointAY;
  2250. var d2 = dX * dX + dY * dY;
  2251. if(d2 > Number.MIN_VALUE * Number.MIN_VALUE) {
  2252. var d = Math.sqrt(d2);
  2253. this.m_normal.x = dX / d;
  2254. this.m_normal.y = dY / d
  2255. }else {
  2256. this.m_normal.x = 1;
  2257. this.m_normal.y = 0
  2258. }
  2259. this.m_points[0].x = 0.5 * (pointAX + pointBX);
  2260. this.m_points[0].y = 0.5 * (pointAY + pointBY);
  2261. this.m_separations[0] = dX * this.m_normal.x + dY * this.m_normal.y - cc.radius;
  2262. break;
  2263. case b2Manifold.e_faceA:
  2264. tMat = cc.bodyA.m_xf.R;
  2265. tVec = cc.localPlaneNormal;
  2266. this.m_normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2267. this.m_normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2268. tMat = cc.bodyA.m_xf.R;
  2269. tVec = cc.localPoint;
  2270. planePointX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2271. planePointY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2272. tMat = cc.bodyB.m_xf.R;
  2273. for(i = 0;i < cc.pointCount;++i) {
  2274. tVec = cc.points[i].localPoint;
  2275. clipPointX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2276. clipPointY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2277. this.m_separations[i] = (clipPointX - planePointX) * this.m_normal.x + (clipPointY - planePointY) * this.m_normal.y - cc.radius;
  2278. this.m_points[i].x = clipPointX;
  2279. this.m_points[i].y = clipPointY
  2280. }
  2281. break;
  2282. case b2Manifold.e_faceB:
  2283. tMat = cc.bodyB.m_xf.R;
  2284. tVec = cc.localPlaneNormal;
  2285. this.m_normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2286. this.m_normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2287. tMat = cc.bodyB.m_xf.R;
  2288. tVec = cc.localPoint;
  2289. planePointX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2290. planePointY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2291. tMat = cc.bodyA.m_xf.R;
  2292. for(i = 0;i < cc.pointCount;++i) {
  2293. tVec = cc.points[i].localPoint;
  2294. clipPointX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2295. clipPointY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2296. this.m_separations[i] = (clipPointX - planePointX) * this.m_normal.x + (clipPointY - planePointY) * this.m_normal.y - cc.radius;
  2297. this.m_points[i].Set(clipPointX, clipPointY)
  2298. }
  2299. this.m_normal.x *= -1;
  2300. this.m_normal.y *= -1;
  2301. break
  2302. }
  2303. };
  2304. b2PositionSolverManifold.prototype.m_normal = null;
  2305. b2PositionSolverManifold.prototype.m_points = null;
  2306. b2PositionSolverManifold.prototype.m_separations = null;var b2OBB = function() {
  2307. this.__varz();
  2308. this.__constructor.apply(this, arguments)
  2309. };
  2310. b2OBB.prototype.__constructor = function() {
  2311. };
  2312. b2OBB.prototype.__varz = function() {
  2313. this.R = new b2Mat22;
  2314. this.center = new b2Vec2;
  2315. this.extents = new b2Vec2
  2316. };
  2317. b2OBB.prototype.R = new b2Mat22;
  2318. b2OBB.prototype.center = new b2Vec2;
  2319. b2OBB.prototype.extents = new b2Vec2;var b2Pair = function() {
  2320. this.__varz();
  2321. this.__constructor.apply(this, arguments)
  2322. };
  2323. b2Pair.prototype.__constructor = function() {
  2324. };
  2325. b2Pair.prototype.__varz = function() {
  2326. };
  2327. b2Pair.b2_nullProxy = b2Settings.USHRT_MAX;
  2328. b2Pair.e_pairBuffered = 1;
  2329. b2Pair.e_pairRemoved = 2;
  2330. b2Pair.e_pairFinal = 4;
  2331. b2Pair.prototype.SetBuffered = function() {
  2332. this.status |= b2Pair.e_pairBuffered
  2333. };
  2334. b2Pair.prototype.ClearBuffered = function() {
  2335. this.status &= ~b2Pair.e_pairBuffered
  2336. };
  2337. b2Pair.prototype.IsBuffered = function() {
  2338. return(this.status & b2Pair.e_pairBuffered) == b2Pair.e_pairBuffered
  2339. };
  2340. b2Pair.prototype.SetRemoved = function() {
  2341. this.status |= b2Pair.e_pairRemoved
  2342. };
  2343. b2Pair.prototype.ClearRemoved = function() {
  2344. this.status &= ~b2Pair.e_pairRemoved
  2345. };
  2346. b2Pair.prototype.IsRemoved = function() {
  2347. return(this.status & b2Pair.e_pairRemoved) == b2Pair.e_pairRemoved
  2348. };
  2349. b2Pair.prototype.SetFinal = function() {
  2350. this.status |= b2Pair.e_pairFinal
  2351. };
  2352. b2Pair.prototype.IsFinal = function() {
  2353. return(this.status & b2Pair.e_pairFinal) == b2Pair.e_pairFinal
  2354. };
  2355. b2Pair.prototype.userData = null;
  2356. b2Pair.prototype.proxy1 = null;
  2357. b2Pair.prototype.proxy2 = null;
  2358. b2Pair.prototype.next = null;
  2359. b2Pair.prototype.status = 0;var b2FixtureDef = function() {
  2360. this.__varz();
  2361. this.__constructor.apply(this, arguments)
  2362. };
  2363. b2FixtureDef.prototype.__constructor = function() {
  2364. this.shape = null;
  2365. this.userData = null;
  2366. this.friction = 0.2;
  2367. this.restitution = 0;
  2368. this.density = 0;
  2369. this.filter.categoryBits = 1;
  2370. this.filter.maskBits = 65535;
  2371. this.filter.groupIndex = 0;
  2372. this.isSensor = false
  2373. };
  2374. b2FixtureDef.prototype.__varz = function() {
  2375. this.filter = new b2FilterData
  2376. };
  2377. b2FixtureDef.prototype.shape = null;
  2378. b2FixtureDef.prototype.userData = null;
  2379. b2FixtureDef.prototype.friction = null;
  2380. b2FixtureDef.prototype.restitution = null;
  2381. b2FixtureDef.prototype.density = null;
  2382. b2FixtureDef.prototype.isSensor = null;
  2383. b2FixtureDef.prototype.filter = new b2FilterData;var b2ContactID = function() {
  2384. this.__varz();
  2385. this.__constructor.apply(this, arguments)
  2386. };
  2387. b2ContactID.prototype.__constructor = function() {
  2388. this.features._m_id = this
  2389. };
  2390. b2ContactID.prototype.__varz = function() {
  2391. this.features = new Features
  2392. };
  2393. b2ContactID.prototype.Set = function(id) {
  2394. key = id._key
  2395. };
  2396. b2ContactID.prototype.Copy = function() {
  2397. var id = new b2ContactID;
  2398. id.key = key;
  2399. return id
  2400. };
  2401. b2ContactID.prototype.__defineSetter__("key", function() {
  2402. return this._key
  2403. });
  2404. b2ContactID.prototype.__defineSetter__("key", function(value) {
  2405. this._key = value;
  2406. this.features._referenceEdge = this._key & 255;
  2407. this.features._incidentEdge = (this._key & 65280) >> 8 & 255;
  2408. this.features._incidentVertex = (this._key & 16711680) >> 16 & 255;
  2409. this.features._flip = (this._key & 4278190080) >> 24 & 255
  2410. });
  2411. b2ContactID.prototype._key = 0;
  2412. b2ContactID.prototype.features = new Features;var b2Transform = function() {
  2413. this.__varz();
  2414. this.__constructor.apply(this, arguments)
  2415. };
  2416. b2Transform.prototype.__constructor = function(pos, r) {
  2417. if(pos) {
  2418. this.position.SetV(pos);
  2419. this.R.SetM(r)
  2420. }
  2421. };
  2422. b2Transform.prototype.__varz = function() {
  2423. this.position = new b2Vec2;
  2424. this.R = new b2Mat22
  2425. };
  2426. b2Transform.prototype.Initialize = function(pos, r) {
  2427. this.position.SetV(pos);
  2428. this.R.SetM(r)
  2429. };
  2430. b2Transform.prototype.SetIdentity = function() {
  2431. this.position.SetZero();
  2432. this.R.SetIdentity()
  2433. };
  2434. b2Transform.prototype.Set = function(x) {
  2435. this.position.SetV(x.position);
  2436. this.R.SetM(x.R)
  2437. };
  2438. b2Transform.prototype.GetAngle = function() {
  2439. return Math.atan2(this.R.col1.y, this.R.col1.x)
  2440. };
  2441. b2Transform.prototype.position = new b2Vec2;
  2442. b2Transform.prototype.R = new b2Mat22;var b2EdgeShape = function() {
  2443. b2Shape.prototype.__varz.call(this);
  2444. this.__varz();
  2445. this.__constructor.apply(this, arguments)
  2446. };
  2447. extend(b2EdgeShape.prototype, b2Shape.prototype);
  2448. b2EdgeShape.prototype._super = b2Shape.prototype;
  2449. b2EdgeShape.prototype.__constructor = function(v1, v2) {
  2450. this._super.__constructor.apply(this, []);
  2451. this.m_type = b2Shape.e_edgeShape;
  2452. this.m_prevEdge = null;
  2453. this.m_nextEdge = null;
  2454. this.m_v1 = v1;
  2455. this.m_v2 = v2;
  2456. this.m_direction.Set(this.m_v2.x - this.m_v1.x, this.m_v2.y - this.m_v1.y);
  2457. this.m_length = this.m_direction.Normalize();
  2458. this.m_normal.Set(this.m_direction.y, -this.m_direction.x);
  2459. 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);
  2460. 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);
  2461. this.m_cornerDir1 = this.m_normal;
  2462. this.m_cornerDir2.Set(-this.m_normal.x, -this.m_normal.y)
  2463. };
  2464. b2EdgeShape.prototype.__varz = function() {
  2465. this.s_supportVec = new b2Vec2;
  2466. this.m_v1 = new b2Vec2;
  2467. this.m_v2 = new b2Vec2;
  2468. this.m_coreV1 = new b2Vec2;
  2469. this.m_coreV2 = new b2Vec2;
  2470. this.m_normal = new b2Vec2;
  2471. this.m_direction = new b2Vec2;
  2472. this.m_cornerDir1 = new b2Vec2;
  2473. this.m_cornerDir2 = new b2Vec2
  2474. };
  2475. b2EdgeShape.prototype.SetPrevEdge = function(edge, core, cornerDir, convex) {
  2476. this.m_prevEdge = edge;
  2477. this.m_coreV1 = core;
  2478. this.m_cornerDir1 = cornerDir;
  2479. this.m_cornerConvex1 = convex
  2480. };
  2481. b2EdgeShape.prototype.SetNextEdge = function(edge, core, cornerDir, convex) {
  2482. this.m_nextEdge = edge;
  2483. this.m_coreV2 = core;
  2484. this.m_cornerDir2 = cornerDir;
  2485. this.m_cornerConvex2 = convex
  2486. };
  2487. b2EdgeShape.prototype.TestPoint = function(transform, p) {
  2488. return false
  2489. };
  2490. b2EdgeShape.prototype.RayCast = function(output, input, transform) {
  2491. var tMat;
  2492. var rX = input.p2.x - input.p1.x;
  2493. var rY = input.p2.y - input.p1.y;
  2494. tMat = transform.R;
  2495. var v1X = transform.position.x + (tMat.col1.x * this.m_v1.x + tMat.col2.x * this.m_v1.y);
  2496. var v1Y = transform.position.y + (tMat.col1.y * this.m_v1.x + tMat.col2.y * this.m_v1.y);
  2497. var nX = transform.position.y + (tMat.col1.y * this.m_v2.x + tMat.col2.y * this.m_v2.y) - v1Y;
  2498. var nY = -(transform.position.x + (tMat.col1.x * this.m_v2.x + tMat.col2.x * this.m_v2.y) - v1X);
  2499. var k_slop = 100 * Number.MIN_VALUE;
  2500. var denom = -(rX * nX + rY * nY);
  2501. if(denom > k_slop) {
  2502. var bX = input.p1.x - v1X;
  2503. var bY = input.p1.y - v1Y;
  2504. var a = bX * nX + bY * nY;
  2505. if(0 <= a && a <= input.maxFraction * denom) {
  2506. var mu2 = -rX * bY + rY * bX;
  2507. if(-k_slop * denom <= mu2 && mu2 <= denom * (1 + k_slop)) {
  2508. a /= denom;
  2509. output.fraction = a;
  2510. var nLen = Math.sqrt(nX * nX + nY * nY);
  2511. output.normal.x = nX / nLen;
  2512. output.normal.y = nY / nLen;
  2513. return true
  2514. }
  2515. }
  2516. }
  2517. return false
  2518. };
  2519. b2EdgeShape.prototype.ComputeAABB = function(aabb, transform) {
  2520. var tMat = transform.R;
  2521. var v1X = transform.position.x + (tMat.col1.x * this.m_v1.x + tMat.col2.x * this.m_v1.y);
  2522. var v1Y = transform.position.y + (tMat.col1.y * this.m_v1.x + tMat.col2.y * this.m_v1.y);
  2523. var v2X = transform.position.x + (tMat.col1.x * this.m_v2.x + tMat.col2.x * this.m_v2.y);
  2524. var v2Y = transform.position.y + (tMat.col1.y * this.m_v2.x + tMat.col2.y * this.m_v2.y);
  2525. if(v1X < v2X) {
  2526. aabb.lowerBound.x = v1X;
  2527. aabb.upperBound.x = v2X
  2528. }else {
  2529. aabb.lowerBound.x = v2X;
  2530. aabb.upperBound.x = v1X
  2531. }
  2532. if(v1Y < v2Y) {
  2533. aabb.lowerBound.y = v1Y;
  2534. aabb.upperBound.y = v2Y
  2535. }else {
  2536. aabb.lowerBound.y = v2Y;
  2537. aabb.upperBound.y = v1Y
  2538. }
  2539. };
  2540. b2EdgeShape.prototype.ComputeMass = function(massData, density) {
  2541. massData.mass = 0;
  2542. massData.center.SetV(this.m_v1);
  2543. massData.I = 0
  2544. };
  2545. b2EdgeShape.prototype.ComputeSubmergedArea = function(normal, offset, xf, c) {
  2546. var v0 = new b2Vec2(normal.x * offset, normal.y * offset);
  2547. var v1 = b2Math.MulX(xf, this.m_v1);
  2548. var v2 = b2Math.MulX(xf, this.m_v2);
  2549. var d1 = b2Math.Dot(normal, v1) - offset;
  2550. var d2 = b2Math.Dot(normal, v2) - offset;
  2551. if(d1 > 0) {
  2552. if(d2 > 0) {
  2553. return 0
  2554. }else {
  2555. v1.x = -d2 / (d1 - d2) * v1.x + d1 / (d1 - d2) * v2.x;
  2556. v1.y = -d2 / (d1 - d2) * v1.y + d1 / (d1 - d2) * v2.y
  2557. }
  2558. }else {
  2559. if(d2 > 0) {
  2560. v2.x = -d2 / (d1 - d2) * v1.x + d1 / (d1 - d2) * v2.x;
  2561. v2.y = -d2 / (d1 - d2) * v1.y + d1 / (d1 - d2) * v2.y
  2562. }else {
  2563. }
  2564. }
  2565. c.x = (v0.x + v1.x + v2.x) / 3;
  2566. c.y = (v0.y + v1.y + v2.y) / 3;
  2567. return 0.5 * ((v1.x - v0.x) * (v2.y - v0.y) - (v1.y - v0.y) * (v2.x - v0.x))
  2568. };
  2569. b2EdgeShape.prototype.GetLength = function() {
  2570. return this.m_length
  2571. };
  2572. b2EdgeShape.prototype.GetVertex1 = function() {
  2573. return this.m_v1
  2574. };
  2575. b2EdgeShape.prototype.GetVertex2 = function() {
  2576. return this.m_v2
  2577. };
  2578. b2EdgeShape.prototype.GetCoreVertex1 = function() {
  2579. return this.m_coreV1
  2580. };
  2581. b2EdgeShape.prototype.GetCoreVertex2 = function() {
  2582. return this.m_coreV2
  2583. };
  2584. b2EdgeShape.prototype.GetNormalVector = function() {
  2585. return this.m_normal
  2586. };
  2587. b2EdgeShape.prototype.GetDirectionVector = function() {
  2588. return this.m_direction
  2589. };
  2590. b2EdgeShape.prototype.GetCorner1Vector = function() {
  2591. return this.m_cornerDir1
  2592. };
  2593. b2EdgeShape.prototype.GetCorner2Vector = function() {
  2594. return this.m_cornerDir2
  2595. };
  2596. b2EdgeShape.prototype.Corner1IsConvex = function() {
  2597. return this.m_cornerConvex1
  2598. };
  2599. b2EdgeShape.prototype.Corner2IsConvex = function() {
  2600. return this.m_cornerConvex2
  2601. };
  2602. b2EdgeShape.prototype.GetFirstVertex = function(xf) {
  2603. var tMat = xf.R;
  2604. 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))
  2605. };
  2606. b2EdgeShape.prototype.GetNextEdge = function() {
  2607. return this.m_nextEdge
  2608. };
  2609. b2EdgeShape.prototype.GetPrevEdge = function() {
  2610. return this.m_prevEdge
  2611. };
  2612. b2EdgeShape.prototype.Support = function(xf, dX, dY) {
  2613. var tMat = xf.R;
  2614. var v1X = xf.position.x + (tMat.col1.x * this.m_coreV1.x + tMat.col2.x * this.m_coreV1.y);
  2615. var v1Y = xf.position.y + (tMat.col1.y * this.m_coreV1.x + tMat.col2.y * this.m_coreV1.y);
  2616. var v2X = xf.position.x + (tMat.col1.x * this.m_coreV2.x + tMat.col2.x * this.m_coreV2.y);
  2617. var v2Y = xf.position.y + (tMat.col1.y * this.m_coreV2.x + tMat.col2.y * this.m_coreV2.y);
  2618. if(v1X * dX + v1Y * dY > v2X * dX + v2Y * dY) {
  2619. this.s_supportVec.x = v1X;
  2620. this.s_supportVec.y = v1Y
  2621. }else {
  2622. this.s_supportVec.x = v2X;
  2623. this.s_supportVec.y = v2Y
  2624. }
  2625. return this.s_supportVec
  2626. };
  2627. b2EdgeShape.prototype.s_supportVec = new b2Vec2;
  2628. b2EdgeShape.prototype.m_v1 = new b2Vec2;
  2629. b2EdgeShape.prototype.m_v2 = new b2Vec2;
  2630. b2EdgeShape.prototype.m_coreV1 = new b2Vec2;
  2631. b2EdgeShape.prototype.m_coreV2 = new b2Vec2;
  2632. b2EdgeShape.prototype.m_length = null;
  2633. b2EdgeShape.prototype.m_normal = new b2Vec2;
  2634. b2EdgeShape.prototype.m_direction = new b2Vec2;
  2635. b2EdgeShape.prototype.m_cornerDir1 = new b2Vec2;
  2636. b2EdgeShape.prototype.m_cornerDir2 = new b2Vec2;
  2637. b2EdgeShape.prototype.m_cornerConvex1 = null;
  2638. b2EdgeShape.prototype.m_cornerConvex2 = null;
  2639. b2EdgeShape.prototype.m_nextEdge = null;
  2640. b2EdgeShape.prototype.m_prevEdge = null;var b2BuoyancyController = function() {
  2641. b2Controller.prototype.__varz.call(this);
  2642. this.__varz();
  2643. this.__constructor.apply(this, arguments)
  2644. };
  2645. extend(b2BuoyancyController.prototype, b2Controller.prototype);
  2646. b2BuoyancyController.prototype._super = b2Controller.prototype;
  2647. b2BuoyancyController.prototype.__constructor = function() {
  2648. this._super.__constructor.apply(this, arguments)
  2649. };
  2650. b2BuoyancyController.prototype.__varz = function() {
  2651. this.normal = new b2Vec2(0, -1);
  2652. this.velocity = new b2Vec2(0, 0)
  2653. };
  2654. b2BuoyancyController.prototype.Step = function(step) {
  2655. if(!m_bodyList) {
  2656. return
  2657. }
  2658. if(this.useWorldGravity) {
  2659. this.gravity = this.GetWorld().GetGravity().Copy()
  2660. }
  2661. for(var i = m_bodyList;i;i = i.nextBody) {
  2662. var body = i.body;
  2663. if(body.IsAwake() == false) {
  2664. continue
  2665. }
  2666. var areac = new b2Vec2;
  2667. var massc = new b2Vec2;
  2668. var area = 0;
  2669. var mass = 0;
  2670. for(var fixture = body.GetFixtureList();fixture;fixture = fixture.GetNext()) {
  2671. var sc = new b2Vec2;
  2672. var sarea = fixture.GetShape().ComputeSubmergedArea(this.normal, this.offset, body.GetTransform(), sc);
  2673. area += sarea;
  2674. areac.x += sarea * sc.x;
  2675. areac.y += sarea * sc.y;
  2676. var shapeDensity;
  2677. if(this.useDensity) {
  2678. shapeDensity = 1
  2679. }else {
  2680. shapeDensity = 1
  2681. }
  2682. mass += sarea * shapeDensity;
  2683. massc.x += sarea * sc.x * shapeDensity;
  2684. massc.y += sarea * sc.y * shapeDensity
  2685. }
  2686. areac.x /= area;
  2687. areac.y /= area;
  2688. massc.x /= mass;
  2689. massc.y /= mass;
  2690. if(area < Number.MIN_VALUE) {
  2691. continue
  2692. }
  2693. var buoyancyForce = this.gravity.GetNegative();
  2694. buoyancyForce.Multiply(this.density * area);
  2695. body.ApplyForce(buoyancyForce, massc);
  2696. var dragForce = body.GetLinearVelocityFromWorldPoint(areac);
  2697. dragForce.Subtract(this.velocity);
  2698. dragForce.Multiply(-this.linearDrag * area);
  2699. body.ApplyForce(dragForce, areac);
  2700. body.ApplyTorque(-body.GetInertia() / body.GetMass() * area * body.GetAngularVelocity() * this.angularDrag)
  2701. }
  2702. };
  2703. b2BuoyancyController.prototype.Draw = function(debugDraw) {
  2704. var r = 1E3;
  2705. var p1 = new b2Vec2;
  2706. var p2 = new b2Vec2;
  2707. p1.x = this.normal.x * this.offset + this.normal.y * r;
  2708. p1.y = this.normal.y * this.offset - this.normal.x * r;
  2709. p2.x = this.normal.x * this.offset - this.normal.y * r;
  2710. p2.y = this.normal.y * this.offset + this.normal.x * r;
  2711. var color = new b2Color(0, 0, 1);
  2712. debugDraw.DrawSegment(p1, p2, color)
  2713. };
  2714. b2BuoyancyController.prototype.normal = new b2Vec2(0, -1);
  2715. b2BuoyancyController.prototype.offset = 0;
  2716. b2BuoyancyController.prototype.density = 0;
  2717. b2BuoyancyController.prototype.velocity = new b2Vec2(0, 0);
  2718. b2BuoyancyController.prototype.linearDrag = 2;
  2719. b2BuoyancyController.prototype.angularDrag = 1;
  2720. b2BuoyancyController.prototype.useDensity = false;
  2721. b2BuoyancyController.prototype.useWorldGravity = true;
  2722. b2BuoyancyController.prototype.gravity = null;var b2Body = function() {
  2723. this.__varz();
  2724. this.__constructor.apply(this, arguments)
  2725. };
  2726. b2Body.prototype.__constructor = function(bd, world) {
  2727. this.m_flags = 0;
  2728. if(bd.bullet) {
  2729. this.m_flags |= b2Body.e_bulletFlag
  2730. }
  2731. if(bd.fixedRotation) {
  2732. this.m_flags |= b2Body.e_fixedRotationFlag
  2733. }
  2734. if(bd.allowSleep) {
  2735. this.m_flags |= b2Body.e_allowSleepFlag
  2736. }
  2737. if(bd.awake) {
  2738. this.m_flags |= b2Body.e_awakeFlag
  2739. }
  2740. if(bd.active) {
  2741. this.m_flags |= b2Body.e_activeFlag
  2742. }
  2743. this.m_world = world;
  2744. this.m_xf.position.SetV(bd.position);
  2745. this.m_xf.R.Set(bd.angle);
  2746. this.m_sweep.localCenter.SetZero();
  2747. this.m_sweep.t0 = 1;
  2748. this.m_sweep.a0 = this.m_sweep.a = bd.angle;
  2749. var tMat = this.m_xf.R;
  2750. var tVec = this.m_sweep.localCenter;
  2751. this.m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2752. this.m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2753. this.m_sweep.c.x += this.m_xf.position.x;
  2754. this.m_sweep.c.y += this.m_xf.position.y;
  2755. this.m_sweep.c0.SetV(this.m_sweep.c);
  2756. this.m_jointList = null;
  2757. this.m_controllerList = null;
  2758. this.m_contactList = null;
  2759. this.m_controllerCount = 0;
  2760. this.m_prev = null;
  2761. this.m_next = null;
  2762. this.m_linearVelocity.SetV(bd.linearVelocity);
  2763. this.m_angularVelocity = bd.angularVelocity;
  2764. this.m_linearDamping = bd.linearDamping;
  2765. this.m_angularDamping = bd.angularDamping;
  2766. this.m_force.Set(0, 0);
  2767. this.m_torque = 0;
  2768. this.m_sleepTime = 0;
  2769. this.m_type = bd.type;
  2770. if(this.m_type == b2Body.b2_dynamicBody) {
  2771. this.m_mass = 1;
  2772. this.m_invMass = 1
  2773. }else {
  2774. this.m_mass = 0;
  2775. this.m_invMass = 0
  2776. }
  2777. this.m_I = 0;
  2778. this.m_invI = 0;
  2779. this.m_inertiaScale = bd.inertiaScale;
  2780. this.m_userData = bd.userData;
  2781. this.m_fixtureList = null;
  2782. this.m_fixtureCount = 0
  2783. };
  2784. b2Body.prototype.__varz = function() {
  2785. this.m_xf = new b2Transform;
  2786. this.m_sweep = new b2Sweep;
  2787. this.m_linearVelocity = new b2Vec2;
  2788. this.m_force = new b2Vec2
  2789. };
  2790. b2Body.b2_staticBody = 0;
  2791. b2Body.b2_kinematicBody = 1;
  2792. b2Body.b2_dynamicBody = 2;
  2793. b2Body.s_xf1 = new b2Transform;
  2794. b2Body.e_islandFlag = 1;
  2795. b2Body.e_awakeFlag = 2;
  2796. b2Body.e_allowSleepFlag = 4;
  2797. b2Body.e_bulletFlag = 8;
  2798. b2Body.e_fixedRotationFlag = 16;
  2799. b2Body.e_activeFlag = 32;
  2800. b2Body.prototype.connectEdges = function(s1, s2, angle1) {
  2801. var angle2 = Math.atan2(s2.GetDirectionVector().y, s2.GetDirectionVector().x);
  2802. var coreOffset = Math.tan((angle2 - angle1) * 0.5);
  2803. var core = b2Math.MulFV(coreOffset, s2.GetDirectionVector());
  2804. core = b2Math.SubtractVV(core, s2.GetNormalVector());
  2805. core = b2Math.MulFV(b2Settings.b2_toiSlop, core);
  2806. core = b2Math.AddVV(core, s2.GetVertex1());
  2807. var cornerDir = b2Math.AddVV(s1.GetDirectionVector(), s2.GetDirectionVector());
  2808. cornerDir.Normalize();
  2809. var convex = b2Math.Dot(s1.GetDirectionVector(), s2.GetNormalVector()) > 0;
  2810. s1.SetNextEdge(s2, core, cornerDir, convex);
  2811. s2.SetPrevEdge(s1, core, cornerDir, convex);
  2812. return angle2
  2813. };
  2814. b2Body.prototype.SynchronizeFixtures = function() {
  2815. var xf1 = b2Body.s_xf1;
  2816. xf1.R.Set(this.m_sweep.a0);
  2817. var tMat = xf1.R;
  2818. var tVec = this.m_sweep.localCenter;
  2819. xf1.position.x = this.m_sweep.c0.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2820. xf1.position.y = this.m_sweep.c0.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2821. var f;
  2822. var broadPhase = this.m_world.m_contactManager.m_broadPhase;
  2823. for(f = this.m_fixtureList;f;f = f.m_next) {
  2824. f.Synchronize(broadPhase, xf1, this.m_xf)
  2825. }
  2826. };
  2827. b2Body.prototype.SynchronizeTransform = function() {
  2828. this.m_xf.R.Set(this.m_sweep.a);
  2829. var tMat = this.m_xf.R;
  2830. var tVec = this.m_sweep.localCenter;
  2831. this.m_xf.position.x = this.m_sweep.c.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2832. this.m_xf.position.y = this.m_sweep.c.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y)
  2833. };
  2834. b2Body.prototype.ShouldCollide = function(other) {
  2835. if(this.m_type != b2Body.b2_dynamicBody && other.m_type != b2Body.b2_dynamicBody) {
  2836. return false
  2837. }
  2838. for(var jn = this.m_jointList;jn;jn = jn.next) {
  2839. if(jn.other == other) {
  2840. if(jn.joint.m_collideConnected == false) {
  2841. return false
  2842. }
  2843. }
  2844. }
  2845. return true
  2846. };
  2847. b2Body.prototype.Advance = function(t) {
  2848. this.m_sweep.Advance(t);
  2849. this.m_sweep.c.SetV(this.m_sweep.c0);
  2850. this.m_sweep.a = this.m_sweep.a0;
  2851. this.SynchronizeTransform()
  2852. };
  2853. b2Body.prototype.CreateFixture = function(def) {
  2854. if(this.m_world.IsLocked() == true) {
  2855. return null
  2856. }
  2857. var fixture = new b2Fixture;
  2858. fixture.Create(this, this.m_xf, def);
  2859. if(this.m_flags & b2Body.e_activeFlag) {
  2860. var broadPhase = this.m_world.m_contactManager.m_broadPhase;
  2861. fixture.CreateProxy(broadPhase, this.m_xf)
  2862. }
  2863. fixture.m_next = this.m_fixtureList;
  2864. this.m_fixtureList = fixture;
  2865. ++this.m_fixtureCount;
  2866. fixture.m_body = this;
  2867. if(fixture.m_density > 0) {
  2868. this.ResetMassData()
  2869. }
  2870. this.m_world.m_flags |= b2World.e_newFixture;
  2871. return fixture
  2872. };
  2873. b2Body.prototype.CreateFixture2 = function(shape, density) {
  2874. var def = new b2FixtureDef;
  2875. def.shape = shape;
  2876. def.density = density;
  2877. return this.CreateFixture(def)
  2878. };
  2879. b2Body.prototype.DestroyFixture = function(fixture) {
  2880. if(this.m_world.IsLocked() == true) {
  2881. return
  2882. }
  2883. var node = this.m_fixtureList;
  2884. var ppF = null;
  2885. var found = false;
  2886. while(node != null) {
  2887. if(node == fixture) {
  2888. if(ppF) {
  2889. ppF.m_next = fixture.m_next
  2890. }else {
  2891. this.m_fixtureList = fixture.m_next
  2892. }
  2893. found = true;
  2894. break
  2895. }
  2896. ppF = node;
  2897. node = node.m_next
  2898. }
  2899. var edge = this.m_contactList;
  2900. while(edge) {
  2901. var c = edge.contact;
  2902. edge = edge.next;
  2903. var fixtureA = c.GetFixtureA();
  2904. var fixtureB = c.GetFixtureB();
  2905. if(fixture == fixtureA || fixture == fixtureB) {
  2906. this.m_world.m_contactManager.Destroy(c)
  2907. }
  2908. }
  2909. if(this.m_flags & b2Body.e_activeFlag) {
  2910. var broadPhase = this.m_world.m_contactManager.m_broadPhase;
  2911. fixture.DestroyProxy(broadPhase)
  2912. }else {
  2913. }
  2914. fixture.Destroy();
  2915. fixture.m_body = null;
  2916. fixture.m_next = null;
  2917. --this.m_fixtureCount;
  2918. this.ResetMassData()
  2919. };
  2920. b2Body.prototype.SetPositionAndAngle = function(position, angle) {
  2921. var f;
  2922. if(this.m_world.IsLocked() == true) {
  2923. return
  2924. }
  2925. this.m_xf.R.Set(angle);
  2926. this.m_xf.position.SetV(position);
  2927. var tMat = this.m_xf.R;
  2928. var tVec = this.m_sweep.localCenter;
  2929. this.m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2930. this.m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2931. this.m_sweep.c.x += this.m_xf.position.x;
  2932. this.m_sweep.c.y += this.m_xf.position.y;
  2933. this.m_sweep.c0.SetV(this.m_sweep.c);
  2934. this.m_sweep.a0 = this.m_sweep.a = angle;
  2935. var broadPhase = this.m_world.m_contactManager.m_broadPhase;
  2936. for(f = this.m_fixtureList;f;f = f.m_next) {
  2937. f.Synchronize(broadPhase, this.m_xf, this.m_xf)
  2938. }
  2939. this.m_world.m_contactManager.FindNewContacts()
  2940. };
  2941. b2Body.prototype.SetTransform = function(xf) {
  2942. this.SetPositionAndAngle(xf.position, xf.GetAngle())
  2943. };
  2944. b2Body.prototype.GetTransform = function() {
  2945. return this.m_xf
  2946. };
  2947. b2Body.prototype.GetPosition = function() {
  2948. return this.m_xf.position
  2949. };
  2950. b2Body.prototype.SetPosition = function(position) {
  2951. this.SetPositionAndAngle(position, this.GetAngle())
  2952. };
  2953. b2Body.prototype.GetAngle = function() {
  2954. return this.m_sweep.a
  2955. };
  2956. b2Body.prototype.SetAngle = function(angle) {
  2957. this.SetPositionAndAngle(this.GetPosition(), angle)
  2958. };
  2959. b2Body.prototype.GetWorldCenter = function() {
  2960. return this.m_sweep.c
  2961. };
  2962. b2Body.prototype.GetLocalCenter = function() {
  2963. return this.m_sweep.localCenter
  2964. };
  2965. b2Body.prototype.SetLinearVelocity = function(v) {
  2966. if(this.m_type == b2Body.b2_staticBody) {
  2967. return
  2968. }
  2969. this.m_linearVelocity.SetV(v)
  2970. };
  2971. b2Body.prototype.GetLinearVelocity = function() {
  2972. return this.m_linearVelocity
  2973. };
  2974. b2Body.prototype.SetAngularVelocity = function(omega) {
  2975. if(this.m_type == b2Body.b2_staticBody) {
  2976. return
  2977. }
  2978. this.m_angularVelocity = omega
  2979. };
  2980. b2Body.prototype.GetAngularVelocity = function() {
  2981. return this.m_angularVelocity
  2982. };
  2983. b2Body.prototype.GetDefinition = function() {
  2984. var bd = new b2BodyDef;
  2985. bd.type = this.GetType();
  2986. bd.allowSleep = (this.m_flags & b2Body.e_allowSleepFlag) == b2Body.e_allowSleepFlag;
  2987. bd.angle = this.GetAngle();
  2988. bd.angularDamping = this.m_angularDamping;
  2989. bd.angularVelocity = this.m_angularVelocity;
  2990. bd.fixedRotation = (this.m_flags & b2Body.e_fixedRotationFlag) == b2Body.e_fixedRotationFlag;
  2991. bd.bullet = (this.m_flags & b2Body.e_bulletFlag) == b2Body.e_bulletFlag;
  2992. bd.awake = (this.m_flags & b2Body.e_awakeFlag) == b2Body.e_awakeFlag;
  2993. bd.linearDamping = this.m_linearDamping;
  2994. bd.linearVelocity.SetV(this.GetLinearVelocity());
  2995. bd.position = this.GetPosition();
  2996. bd.userData = this.GetUserData();
  2997. return bd
  2998. };
  2999. b2Body.prototype.ApplyForce = function(force, point) {
  3000. if(this.m_type != b2Body.b2_dynamicBody) {
  3001. return
  3002. }
  3003. if(this.IsAwake() == false) {
  3004. this.SetAwake(true)
  3005. }
  3006. this.m_force.x += force.x;
  3007. this.m_force.y += force.y;
  3008. this.m_torque += (point.x - this.m_sweep.c.x) * force.y - (point.y - this.m_sweep.c.y) * force.x
  3009. };
  3010. b2Body.prototype.ApplyTorque = function(torque) {
  3011. if(this.m_type != b2Body.b2_dynamicBody) {
  3012. return
  3013. }
  3014. if(this.IsAwake() == false) {
  3015. this.SetAwake(true)
  3016. }
  3017. this.m_torque += torque
  3018. };
  3019. b2Body.prototype.ApplyImpulse = function(impulse, point) {
  3020. if(this.m_type != b2Body.b2_dynamicBody) {
  3021. return
  3022. }
  3023. if(this.IsAwake() == false) {
  3024. this.SetAwake(true)
  3025. }
  3026. this.m_linearVelocity.x += this.m_invMass * impulse.x;
  3027. this.m_linearVelocity.y += this.m_invMass * impulse.y;
  3028. this.m_angularVelocity += this.m_invI * ((point.x - this.m_sweep.c.x) * impulse.y - (point.y - this.m_sweep.c.y) * impulse.x)
  3029. };
  3030. b2Body.prototype.Split = function(callback) {
  3031. var linearVelocity = this.GetLinearVelocity().Copy();
  3032. var angularVelocity = this.GetAngularVelocity();
  3033. var center = this.GetWorldCenter();
  3034. var body1 = this;
  3035. var body2 = this.m_world.CreateBody(this.GetDefinition());
  3036. var prev;
  3037. for(var f = body1.m_fixtureList;f;) {
  3038. if(callback(f)) {
  3039. var next = f.m_next;
  3040. if(prev) {
  3041. prev.m_next = next
  3042. }else {
  3043. body1.m_fixtureList = next
  3044. }
  3045. body1.m_fixtureCount--;
  3046. f.m_next = body2.m_fixtureList;
  3047. body2.m_fixtureList = f;
  3048. body2.m_fixtureCount++;
  3049. f.m_body = body2;
  3050. f = next
  3051. }else {
  3052. prev = f;
  3053. f = f.m_next
  3054. }
  3055. }
  3056. body1.ResetMassData();
  3057. body2.ResetMassData();
  3058. var center1 = body1.GetWorldCenter();
  3059. var center2 = body2.GetWorldCenter();
  3060. var velocity1 = b2Math.AddVV(linearVelocity, b2Math.CrossFV(angularVelocity, b2Math.SubtractVV(center1, center)));
  3061. var velocity2 = b2Math.AddVV(linearVelocity, b2Math.CrossFV(angularVelocity, b2Math.SubtractVV(center2, center)));
  3062. body1.SetLinearVelocity(velocity1);
  3063. body2.SetLinearVelocity(velocity2);
  3064. body1.SetAngularVelocity(angularVelocity);
  3065. body2.SetAngularVelocity(angularVelocity);
  3066. body1.SynchronizeFixtures();
  3067. body2.SynchronizeFixtures();
  3068. return body2
  3069. };
  3070. b2Body.prototype.Merge = function(other) {
  3071. var f;
  3072. for(f = other.m_fixtureList;f;) {
  3073. var next = f.m_next;
  3074. other.m_fixtureCount--;
  3075. f.m_next = this.m_fixtureList;
  3076. this.m_fixtureList = f;
  3077. this.m_fixtureCount++;
  3078. f.m_body = body2;
  3079. f = next
  3080. }
  3081. body1.m_fixtureCount = 0;
  3082. var body1 = this;
  3083. var body2 = other;
  3084. var center1 = body1.GetWorldCenter();
  3085. var center2 = body2.GetWorldCenter();
  3086. var velocity1 = body1.GetLinearVelocity().Copy();
  3087. var velocity2 = body2.GetLinearVelocity().Copy();
  3088. var angular1 = body1.GetAngularVelocity();
  3089. var angular = body2.GetAngularVelocity();
  3090. body1.ResetMassData();
  3091. this.SynchronizeFixtures()
  3092. };
  3093. b2Body.prototype.GetMass = function() {
  3094. return this.m_mass
  3095. };
  3096. b2Body.prototype.GetInertia = function() {
  3097. return this.m_I
  3098. };
  3099. b2Body.prototype.GetMassData = function(data) {
  3100. data.mass = this.m_mass;
  3101. data.I = this.m_I;
  3102. data.center.SetV(this.m_sweep.localCenter)
  3103. };
  3104. b2Body.prototype.SetMassData = function(massData) {
  3105. b2Settings.b2Assert(this.m_world.IsLocked() == false);
  3106. if(this.m_world.IsLocked() == true) {
  3107. return
  3108. }
  3109. if(this.m_type != b2Body.b2_dynamicBody) {
  3110. return
  3111. }
  3112. this.m_invMass = 0;
  3113. this.m_I = 0;
  3114. this.m_invI = 0;
  3115. this.m_mass = massData.mass;
  3116. if(this.m_mass <= 0) {
  3117. this.m_mass = 1
  3118. }
  3119. this.m_invMass = 1 / this.m_mass;
  3120. if(massData.I > 0 && (this.m_flags & b2Body.e_fixedRotationFlag) == 0) {
  3121. this.m_I = massData.I - this.m_mass * (massData.center.x * massData.center.x + massData.center.y * massData.center.y);
  3122. this.m_invI = 1 / this.m_I
  3123. }
  3124. var oldCenter = this.m_sweep.c.Copy();
  3125. this.m_sweep.localCenter.SetV(massData.center);
  3126. this.m_sweep.c0.SetV(b2Math.MulX(this.m_xf, this.m_sweep.localCenter));
  3127. this.m_sweep.c.SetV(this.m_sweep.c0);
  3128. this.m_linearVelocity.x += this.m_angularVelocity * -(this.m_sweep.c.y - oldCenter.y);
  3129. this.m_linearVelocity.y += this.m_angularVelocity * +(this.m_sweep.c.x - oldCenter.x)
  3130. };
  3131. b2Body.prototype.ResetMassData = function() {
  3132. this.m_mass = 0;
  3133. this.m_invMass = 0;
  3134. this.m_I = 0;
  3135. this.m_invI = 0;
  3136. this.m_sweep.localCenter.SetZero();
  3137. if(this.m_type == b2Body.b2_staticBody || this.m_type == b2Body.b2_kinematicBody) {
  3138. return
  3139. }
  3140. var center = b2Vec2.Make(0, 0);
  3141. for(var f = this.m_fixtureList;f;f = f.m_next) {
  3142. if(f.m_density == 0) {
  3143. continue
  3144. }
  3145. var massData = f.GetMassData();
  3146. this.m_mass += massData.mass;
  3147. center.x += massData.center.x * massData.mass;
  3148. center.y += massData.center.y * massData.mass;
  3149. this.m_I += massData.I
  3150. }
  3151. if(this.m_mass > 0) {
  3152. this.m_invMass = 1 / this.m_mass;
  3153. center.x *= this.m_invMass;
  3154. center.y *= this.m_invMass
  3155. }else {
  3156. this.m_mass = 1;
  3157. this.m_invMass = 1
  3158. }
  3159. if(this.m_I > 0 && (this.m_flags & b2Body.e_fixedRotationFlag) == 0) {
  3160. this.m_I -= this.m_mass * (center.x * center.x + center.y * center.y);
  3161. this.m_I *= this.m_inertiaScale;
  3162. b2Settings.b2Assert(this.m_I > 0);
  3163. this.m_invI = 1 / this.m_I
  3164. }else {
  3165. this.m_I = 0;
  3166. this.m_invI = 0
  3167. }
  3168. var oldCenter = this.m_sweep.c.Copy();
  3169. this.m_sweep.localCenter.SetV(center);
  3170. this.m_sweep.c0.SetV(b2Math.MulX(this.m_xf, this.m_sweep.localCenter));
  3171. this.m_sweep.c.SetV(this.m_sweep.c0);
  3172. this.m_linearVelocity.x += this.m_angularVelocity * -(this.m_sweep.c.y - oldCenter.y);
  3173. this.m_linearVelocity.y += this.m_angularVelocity * +(this.m_sweep.c.x - oldCenter.x)
  3174. };
  3175. b2Body.prototype.GetWorldPoint = function(localPoint) {
  3176. var A = this.m_xf.R;
  3177. var u = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y, A.col1.y * localPoint.x + A.col2.y * localPoint.y);
  3178. u.x += this.m_xf.position.x;
  3179. u.y += this.m_xf.position.y;
  3180. return u
  3181. };
  3182. b2Body.prototype.GetWorldVector = function(localVector) {
  3183. return b2Math.MulMV(this.m_xf.R, localVector)
  3184. };
  3185. b2Body.prototype.GetLocalPoint = function(worldPoint) {
  3186. return b2Math.MulXT(this.m_xf, worldPoint)
  3187. };
  3188. b2Body.prototype.GetLocalVector = function(worldVector) {
  3189. return b2Math.MulTMV(this.m_xf.R, worldVector)
  3190. };
  3191. b2Body.prototype.GetLinearVelocityFromWorldPoint = function(worldPoint) {
  3192. 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))
  3193. };
  3194. b2Body.prototype.GetLinearVelocityFromLocalPoint = function(localPoint) {
  3195. var A = this.m_xf.R;
  3196. var worldPoint = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y, A.col1.y * localPoint.x + A.col2.y * localPoint.y);
  3197. worldPoint.x += this.m_xf.position.x;
  3198. worldPoint.y += this.m_xf.position.y;
  3199. 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))
  3200. };
  3201. b2Body.prototype.GetLinearDamping = function() {
  3202. return this.m_linearDamping
  3203. };
  3204. b2Body.prototype.SetLinearDamping = function(linearDamping) {
  3205. this.m_linearDamping = linearDamping
  3206. };
  3207. b2Body.prototype.GetAngularDamping = function() {
  3208. return this.m_angularDamping
  3209. };
  3210. b2Body.prototype.SetAngularDamping = function(angularDamping) {
  3211. this.m_angularDamping = angularDamping
  3212. };
  3213. b2Body.prototype.SetType = function(type) {
  3214. if(this.m_type == type) {
  3215. return
  3216. }
  3217. this.m_type = type;
  3218. this.ResetMassData();
  3219. if(this.m_type == b2Body.b2_staticBody) {
  3220. this.m_linearVelocity.SetZero();
  3221. this.m_angularVelocity = 0
  3222. }
  3223. this.SetAwake(true);
  3224. this.m_force.SetZero();
  3225. this.m_torque = 0;
  3226. for(var ce = this.m_contactList;ce;ce = ce.next) {
  3227. ce.contact.FlagForFiltering()
  3228. }
  3229. };
  3230. b2Body.prototype.GetType = function() {
  3231. return this.m_type
  3232. };
  3233. b2Body.prototype.SetBullet = function(flag) {
  3234. if(flag) {
  3235. this.m_flags |= b2Body.e_bulletFlag
  3236. }else {
  3237. this.m_flags &= ~b2Body.e_bulletFlag
  3238. }
  3239. };
  3240. b2Body.prototype.IsBullet = function() {
  3241. return(this.m_flags & b2Body.e_bulletFlag) == b2Body.e_bulletFlag
  3242. };
  3243. b2Body.prototype.SetSleepingAllowed = function(flag) {
  3244. if(flag) {
  3245. this.m_flags |= b2Body.e_allowSleepFlag
  3246. }else {
  3247. this.m_flags &= ~b2Body.e_allowSleepFlag;
  3248. this.SetAwake(true)
  3249. }
  3250. };
  3251. b2Body.prototype.SetAwake = function(flag) {
  3252. if(flag) {
  3253. this.m_flags |= b2Body.e_awakeFlag;
  3254. this.m_sleepTime = 0
  3255. }else {
  3256. this.m_flags &= ~b2Body.e_awakeFlag;
  3257. this.m_sleepTime = 0;
  3258. this.m_linearVelocity.SetZero();
  3259. this.m_angularVelocity = 0;
  3260. this.m_force.SetZero();
  3261. this.m_torque = 0
  3262. }
  3263. };
  3264. b2Body.prototype.IsAwake = function() {
  3265. return(this.m_flags & b2Body.e_awakeFlag) == b2Body.e_awakeFlag
  3266. };
  3267. b2Body.prototype.SetFixedRotation = function(fixed) {
  3268. if(fixed) {
  3269. this.m_flags |= b2Body.e_fixedRotationFlag
  3270. }else {
  3271. this.m_flags &= ~b2Body.e_fixedRotationFlag
  3272. }
  3273. this.ResetMassData()
  3274. };
  3275. b2Body.prototype.IsFixedRotation = function() {
  3276. return(this.m_flags & b2Body.e_fixedRotationFlag) == b2Body.e_fixedRotationFlag
  3277. };
  3278. b2Body.prototype.SetActive = function(flag) {
  3279. if(flag == this.IsActive()) {
  3280. return
  3281. }
  3282. var broadPhase;
  3283. var f;
  3284. if(flag) {
  3285. this.m_flags |= b2Body.e_activeFlag;
  3286. broadPhase = this.m_world.m_contactManager.m_broadPhase;
  3287. for(f = this.m_fixtureList;f;f = f.m_next) {
  3288. f.CreateProxy(broadPhase, this.m_xf)
  3289. }
  3290. }else {
  3291. this.m_flags &= ~b2Body.e_activeFlag;
  3292. broadPhase = this.m_world.m_contactManager.m_broadPhase;
  3293. for(f = this.m_fixtureList;f;f = f.m_next) {
  3294. f.DestroyProxy(broadPhase)
  3295. }
  3296. var ce = this.m_contactList;
  3297. while(ce) {
  3298. var ce0 = ce;
  3299. ce = ce.next;
  3300. this.m_world.m_contactManager.Destroy(ce0.contact)
  3301. }
  3302. this.m_contactList = null
  3303. }
  3304. };
  3305. b2Body.prototype.IsActive = function() {
  3306. return(this.m_flags & b2Body.e_activeFlag) == b2Body.e_activeFlag
  3307. };
  3308. b2Body.prototype.IsSleepingAllowed = function() {
  3309. return(this.m_flags & b2Body.e_allowSleepFlag) == b2Body.e_allowSleepFlag
  3310. };
  3311. b2Body.prototype.GetFixtureList = function() {
  3312. return this.m_fixtureList
  3313. };
  3314. b2Body.prototype.GetJointList = function() {
  3315. return this.m_jointList
  3316. };
  3317. b2Body.prototype.GetControllerList = function() {
  3318. return this.m_controllerList
  3319. };
  3320. b2Body.prototype.GetContactList = function() {
  3321. return this.m_contactList
  3322. };
  3323. b2Body.prototype.GetNext = function() {
  3324. return this.m_next
  3325. };
  3326. b2Body.prototype.GetUserData = function() {
  3327. return this.m_userData
  3328. };
  3329. b2Body.prototype.SetUserData = function(data) {
  3330. this.m_userData = data
  3331. };
  3332. b2Body.prototype.GetWorld = function() {
  3333. return this.m_world
  3334. };
  3335. b2Body.prototype.m_flags = 0;
  3336. b2Body.prototype.m_type = 0;
  3337. b2Body.prototype.m_islandIndex = 0;
  3338. b2Body.prototype.m_xf = new b2Transform;
  3339. b2Body.prototype.m_sweep = new b2Sweep;
  3340. b2Body.prototype.m_linearVelocity = new b2Vec2;
  3341. b2Body.prototype.m_angularVelocity = null;
  3342. b2Body.prototype.m_force = new b2Vec2;
  3343. b2Body.prototype.m_torque = null;
  3344. b2Body.prototype.m_world = null;
  3345. b2Body.prototype.m_prev = null;
  3346. b2Body.prototype.m_next = null;
  3347. b2Body.prototype.m_fixtureList = null;
  3348. b2Body.prototype.m_fixtureCount = 0;
  3349. b2Body.prototype.m_controllerList = null;
  3350. b2Body.prototype.m_controllerCount = 0;
  3351. b2Body.prototype.m_jointList = null;
  3352. b2Body.prototype.m_contactList = null;
  3353. b2Body.prototype.m_mass = null;
  3354. b2Body.prototype.m_invMass = null;
  3355. b2Body.prototype.m_I = null;
  3356. b2Body.prototype.m_invI = null;
  3357. b2Body.prototype.m_inertiaScale = null;
  3358. b2Body.prototype.m_linearDamping = null;
  3359. b2Body.prototype.m_angularDamping = null;
  3360. b2Body.prototype.m_sleepTime = null;
  3361. b2Body.prototype.m_userData = null;var b2ContactImpulse = function() {
  3362. this.__varz();
  3363. this.__constructor.apply(this, arguments)
  3364. };
  3365. b2ContactImpulse.prototype.__constructor = function() {
  3366. };
  3367. b2ContactImpulse.prototype.__varz = function() {
  3368. this.normalImpulses = new Array(b2Settings.b2_maxManifoldPoints);
  3369. this.tangentImpulses = new Array(b2Settings.b2_maxManifoldPoints)
  3370. };
  3371. b2ContactImpulse.prototype.normalImpulses = new Array(b2Settings.b2_maxManifoldPoints);
  3372. b2ContactImpulse.prototype.tangentImpulses = new Array(b2Settings.b2_maxManifoldPoints);var b2TensorDampingController = function() {
  3373. b2Controller.prototype.__varz.call(this);
  3374. this.__varz();
  3375. this.__constructor.apply(this, arguments)
  3376. };
  3377. extend(b2TensorDampingController.prototype, b2Controller.prototype);
  3378. b2TensorDampingController.prototype._super = b2Controller.prototype;
  3379. b2TensorDampingController.prototype.__constructor = function() {
  3380. this._super.__constructor.apply(this, arguments)
  3381. };
  3382. b2TensorDampingController.prototype.__varz = function() {
  3383. this.T = new b2Mat22
  3384. };
  3385. b2TensorDampingController.prototype.SetAxisAligned = function(xDamping, yDamping) {
  3386. this.T.col1.x = -xDamping;
  3387. this.T.col1.y = 0;
  3388. this.T.col2.x = 0;
  3389. this.T.col2.y = -yDamping;
  3390. if(xDamping > 0 || yDamping > 0) {
  3391. this.maxTimestep = 1 / Math.max(xDamping, yDamping)
  3392. }else {
  3393. this.maxTimestep = 0
  3394. }
  3395. };
  3396. b2TensorDampingController.prototype.Step = function(step) {
  3397. var timestep = step.dt;
  3398. if(timestep <= Number.MIN_VALUE) {
  3399. return
  3400. }
  3401. if(timestep > this.maxTimestep && this.maxTimestep > 0) {
  3402. timestep = this.maxTimestep
  3403. }
  3404. for(var i = m_bodyList;i;i = i.nextBody) {
  3405. var body = i.body;
  3406. if(!body.IsAwake()) {
  3407. continue
  3408. }
  3409. var damping = body.GetWorldVector(b2Math.MulMV(this.T, body.GetLocalVector(body.GetLinearVelocity())));
  3410. body.SetLinearVelocity(new b2Vec2(body.GetLinearVelocity().x + damping.x * timestep, body.GetLinearVelocity().y + damping.y * timestep))
  3411. }
  3412. };
  3413. b2TensorDampingController.prototype.T = new b2Mat22;
  3414. b2TensorDampingController.prototype.maxTimestep = 0;var b2ManifoldPoint = function() {
  3415. this.__varz();
  3416. this.__constructor.apply(this, arguments)
  3417. };
  3418. b2ManifoldPoint.prototype.__constructor = function() {
  3419. this.Reset()
  3420. };
  3421. b2ManifoldPoint.prototype.__varz = function() {
  3422. this.m_localPoint = new b2Vec2;
  3423. this.m_id = new b2ContactID
  3424. };
  3425. b2ManifoldPoint.prototype.Reset = function() {
  3426. this.m_localPoint.SetZero();
  3427. this.m_normalImpulse = 0;
  3428. this.m_tangentImpulse = 0;
  3429. this.m_id.key = 0
  3430. };
  3431. b2ManifoldPoint.prototype.Set = function(m) {
  3432. this.m_localPoint.SetV(m.m_localPoint);
  3433. this.m_normalImpulse = m.m_normalImpulse;
  3434. this.m_tangentImpulse = m.m_tangentImpulse;
  3435. this.m_id.Set(m.m_id)
  3436. };
  3437. b2ManifoldPoint.prototype.m_localPoint = new b2Vec2;
  3438. b2ManifoldPoint.prototype.m_normalImpulse = null;
  3439. b2ManifoldPoint.prototype.m_tangentImpulse = null;
  3440. b2ManifoldPoint.prototype.m_id = new b2ContactID;var b2PolygonShape = function() {
  3441. b2Shape.prototype.__varz.call(this);
  3442. this.__varz();
  3443. this.__constructor.apply(this, arguments)
  3444. };
  3445. extend(b2PolygonShape.prototype, b2Shape.prototype);
  3446. b2PolygonShape.prototype._super = b2Shape.prototype;
  3447. b2PolygonShape.prototype.__constructor = function() {
  3448. this._super.__constructor.apply(this, arguments);
  3449. this.m_type = b2Shape.e_polygonShape;
  3450. this.m_centroid = new b2Vec2;
  3451. this.m_vertices = new Array;
  3452. this.m_normals = new Array
  3453. };
  3454. b2PolygonShape.prototype.__varz = function() {
  3455. };
  3456. b2PolygonShape.AsArray = function(vertices, vertexCount) {
  3457. var polygonShape = new b2PolygonShape;
  3458. polygonShape.SetAsArray(vertices, vertexCount);
  3459. return polygonShape
  3460. };
  3461. b2PolygonShape.AsVector = function(vertices, vertexCount) {
  3462. var polygonShape = new b2PolygonShape;
  3463. polygonShape.SetAsVector(vertices, vertexCount);
  3464. return polygonShape
  3465. };
  3466. b2PolygonShape.AsBox = function(hx, hy) {
  3467. var polygonShape = new b2PolygonShape;
  3468. polygonShape.SetAsBox(hx, hy);
  3469. return polygonShape
  3470. };
  3471. b2PolygonShape.AsOrientedBox = function(hx, hy, center, angle) {
  3472. var polygonShape = new b2PolygonShape;
  3473. polygonShape.SetAsOrientedBox(hx, hy, center, angle);
  3474. return polygonShape
  3475. };
  3476. b2PolygonShape.AsEdge = function(v1, v2) {
  3477. var polygonShape = new b2PolygonShape;
  3478. polygonShape.SetAsEdge(v1, v2);
  3479. return polygonShape
  3480. };
  3481. b2PolygonShape.ComputeCentroid = function(vs, count) {
  3482. var c = new b2Vec2;
  3483. var area = 0;
  3484. var p1X = 0;
  3485. var p1Y = 0;
  3486. var inv3 = 1 / 3;
  3487. for(var i = 0;i < count;++i) {
  3488. var p2 = vs[i];
  3489. var p3 = i + 1 < count ? vs[parseInt(i + 1)] : vs[0];
  3490. var e1X = p2.x - p1X;
  3491. var e1Y = p2.y - p1Y;
  3492. var e2X = p3.x - p1X;
  3493. var e2Y = p3.y - p1Y;
  3494. var D = e1X * e2Y - e1Y * e2X;
  3495. var triangleArea = 0.5 * D;
  3496. 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 / area;
  3501. c.y *= 1 / area;
  3502. return c
  3503. };
  3504. b2PolygonShape.ComputeOBB = function(obb, vs, count) {
  3505. var i = 0;
  3506. var p = new Array(count + 1);
  3507. for(i = 0;i < count;++i) {
  3508. p[i] = vs[i]
  3509. }
  3510. p[count] = p[0];
  3511. var minArea = Number.MAX_VALUE;
  3512. for(i = 1;i <= count;++i) {
  3513. var root = p[parseInt(i - 1)];
  3514. var uxX = p[i].x - root.x;
  3515. var uxY = p[i].y - root.y;
  3516. var length = Math.sqrt(uxX * uxX + uxY * uxY);
  3517. uxX /= length;
  3518. uxY /= length;
  3519. var uyX = -uxY;
  3520. var uyY = uxX;
  3521. var lowerX = Number.MAX_VALUE;
  3522. var lowerY = Number.MAX_VALUE;
  3523. var upperX = -Number.MAX_VALUE;
  3524. var upperY = -Number.MAX_VALUE;
  3525. for(var j = 0;j < count;++j) {
  3526. var dX = p[j].x - root.x;
  3527. var dY = p[j].y - root.y;
  3528. var rX = uxX * dX + uxY * dY;
  3529. var rY = uyX * dX + uyY * dY;
  3530. if(rX < lowerX) {
  3531. lowerX = rX
  3532. }
  3533. if(rY < lowerY) {
  3534. lowerY = rY
  3535. }
  3536. if(rX > upperX) {
  3537. upperX = rX
  3538. }
  3539. if(rY > upperY) {
  3540. upperY = rY
  3541. }
  3542. }
  3543. var area = (upperX - lowerX) * (upperY - lowerY);
  3544. if(area < 0.95 * minArea) {
  3545. minArea = area;
  3546. obb.R.col1.x = uxX;
  3547. obb.R.col1.y = uxY;
  3548. obb.R.col2.x = uyX;
  3549. obb.R.col2.y = uyY;
  3550. var centerX = 0.5 * (lowerX + upperX);
  3551. var centerY = 0.5 * (lowerY + upperY);
  3552. var tMat = obb.R;
  3553. obb.center.x = root.x + (tMat.col1.x * centerX + tMat.col2.x * centerY);
  3554. obb.center.y = root.y + (tMat.col1.y * centerX + tMat.col2.y * centerY);
  3555. obb.extents.x = 0.5 * (upperX - lowerX);
  3556. obb.extents.y = 0.5 * (upperY - lowerY)
  3557. }
  3558. }
  3559. };
  3560. b2PolygonShape.s_mat = new b2Mat22;
  3561. b2PolygonShape.prototype.Validate = function() {
  3562. return false
  3563. };
  3564. b2PolygonShape.prototype.Reserve = function(count) {
  3565. for(var i = this.m_vertices.length;i < count;i++) {
  3566. this.m_vertices[i] = new b2Vec2;
  3567. this.m_normals[i] = new b2Vec2
  3568. }
  3569. };
  3570. b2PolygonShape.prototype.Copy = function() {
  3571. var s = new b2PolygonShape;
  3572. s.Set(this);
  3573. return s
  3574. };
  3575. b2PolygonShape.prototype.Set = function(other) {
  3576. this._super.Set.apply(this, [other]);
  3577. if(isInstanceOf(other, b2PolygonShape)) {
  3578. var other2 = other;
  3579. this.m_centroid.SetV(other2.m_centroid);
  3580. this.m_vertexCount = other2.m_vertexCount;
  3581. this.Reserve(this.m_vertexCount);
  3582. for(var i = 0;i < this.m_vertexCount;i++) {
  3583. this.m_vertices[i].SetV(other2.m_vertices[i]);
  3584. this.m_normals[i].SetV(other2.m_normals[i])
  3585. }
  3586. }
  3587. };
  3588. b2PolygonShape.prototype.SetAsArray = function(vertices, vertexCount) {
  3589. var v = new Array;
  3590. for(var i = 0, tVec = null;i < vertices.length, tVec = vertices[i];i++) {
  3591. v.push(tVec)
  3592. }
  3593. this.SetAsVector(v, vertexCount)
  3594. };
  3595. b2PolygonShape.prototype.SetAsVector = function(vertices, vertexCount) {
  3596. if(typeof vertexCount == "undefined") {
  3597. vertexCount = vertices.length
  3598. }
  3599. b2Settings.b2Assert(2 <= vertexCount);
  3600. this.m_vertexCount = vertexCount;
  3601. this.Reserve(vertexCount);
  3602. var i = 0;
  3603. for(i = 0;i < this.m_vertexCount;i++) {
  3604. this.m_vertices[i].SetV(vertices[i])
  3605. }
  3606. for(i = 0;i < this.m_vertexCount;++i) {
  3607. var i1 = i;
  3608. var i2 = i + 1 < this.m_vertexCount ? i + 1 : 0;
  3609. var edge = b2Math.SubtractVV(this.m_vertices[i2], this.m_vertices[i1]);
  3610. b2Settings.b2Assert(edge.LengthSquared() > Number.MIN_VALUE);
  3611. this.m_normals[i].SetV(b2Math.CrossVF(edge, 1));
  3612. this.m_normals[i].Normalize()
  3613. }
  3614. this.m_centroid = b2PolygonShape.ComputeCentroid(this.m_vertices, this.m_vertexCount)
  3615. };
  3616. b2PolygonShape.prototype.SetAsBox = function(hx, hy) {
  3617. this.m_vertexCount = 4;
  3618. this.Reserve(4);
  3619. this.m_vertices[0].Set(-hx, -hy);
  3620. this.m_vertices[1].Set(hx, -hy);
  3621. this.m_vertices[2].Set(hx, hy);
  3622. this.m_vertices[3].Set(-hx, hy);
  3623. this.m_normals[0].Set(0, -1);
  3624. this.m_normals[1].Set(1, 0);
  3625. this.m_normals[2].Set(0, 1);
  3626. this.m_normals[3].Set(-1, 0);
  3627. this.m_centroid.SetZero()
  3628. };
  3629. b2PolygonShape.prototype.SetAsOrientedBox = function(hx, hy, center, angle) {
  3630. this.m_vertexCount = 4;
  3631. this.Reserve(4);
  3632. this.m_vertices[0].Set(-hx, -hy);
  3633. this.m_vertices[1].Set(hx, -hy);
  3634. this.m_vertices[2].Set(hx, hy);
  3635. this.m_vertices[3].Set(-hx, hy);
  3636. this.m_normals[0].Set(0, -1);
  3637. this.m_normals[1].Set(1, 0);
  3638. this.m_normals[2].Set(0, 1);
  3639. this.m_normals[3].Set(-1, 0);
  3640. this.m_centroid = center;
  3641. var xf = new b2Transform;
  3642. xf.position = center;
  3643. xf.R.Set(angle);
  3644. for(var i = 0;i < this.m_vertexCount;++i) {
  3645. this.m_vertices[i] = b2Math.MulX(xf, this.m_vertices[i]);
  3646. this.m_normals[i] = b2Math.MulMV(xf.R, this.m_normals[i])
  3647. }
  3648. };
  3649. b2PolygonShape.prototype.SetAsEdge = function(v1, v2) {
  3650. this.m_vertexCount = 2;
  3651. this.Reserve(2);
  3652. this.m_vertices[0].SetV(v1);
  3653. this.m_vertices[1].SetV(v2);
  3654. this.m_centroid.x = 0.5 * (v1.x + v2.x);
  3655. this.m_centroid.y = 0.5 * (v1.y + v2.y);
  3656. this.m_normals[0] = b2Math.CrossVF(b2Math.SubtractVV(v2, v1), 1);
  3657. this.m_normals[0].Normalize();
  3658. this.m_normals[1].x = -this.m_normals[0].x;
  3659. this.m_normals[1].y = -this.m_normals[0].y
  3660. };
  3661. b2PolygonShape.prototype.TestPoint = function(xf, p) {
  3662. var tVec;
  3663. var tMat = xf.R;
  3664. var tX = p.x - xf.position.x;
  3665. var tY = p.y - xf.position.y;
  3666. var pLocalX = tX * tMat.col1.x + tY * tMat.col1.y;
  3667. var pLocalY = tX * tMat.col2.x + tY * tMat.col2.y;
  3668. for(var i = 0;i < this.m_vertexCount;++i) {
  3669. tVec = this.m_vertices[i];
  3670. tX = pLocalX - tVec.x;
  3671. tY = pLocalY - tVec.y;
  3672. tVec = this.m_normals[i];
  3673. var dot = tVec.x * tX + tVec.y * tY;
  3674. if(dot > 0) {
  3675. return false
  3676. }
  3677. }
  3678. return true
  3679. };
  3680. b2PolygonShape.prototype.RayCast = function(output, input, transform) {
  3681. var lower = 0;
  3682. var upper = input.maxFraction;
  3683. var tX;
  3684. var tY;
  3685. var tMat;
  3686. var tVec;
  3687. tX = input.p1.x - transform.position.x;
  3688. tY = input.p1.y - transform.position.y;
  3689. tMat = transform.R;
  3690. var p1X = tX * tMat.col1.x + tY * tMat.col1.y;
  3691. var p1Y = tX * tMat.col2.x + tY * tMat.col2.y;
  3692. tX = input.p2.x - transform.position.x;
  3693. tY = input.p2.y - transform.position.y;
  3694. tMat = transform.R;
  3695. var p2X = tX * tMat.col1.x + tY * tMat.col1.y;
  3696. var p2Y = tX * tMat.col2.x + tY * tMat.col2.y;
  3697. var dX = p2X - p1X;
  3698. var dY = p2Y - p1Y;
  3699. var index = -1;
  3700. for(var i = 0;i < this.m_vertexCount;++i) {
  3701. tVec = this.m_vertices[i];
  3702. tX = tVec.x - p1X;
  3703. tY = tVec.y - p1Y;
  3704. tVec = this.m_normals[i];
  3705. var numerator = tVec.x * tX + tVec.y * tY;
  3706. var denominator = tVec.x * dX + tVec.y * dY;
  3707. if(denominator == 0) {
  3708. if(numerator < 0) {
  3709. return false
  3710. }
  3711. }else {
  3712. if(denominator < 0 && numerator < lower * denominator) {
  3713. lower = numerator / denominator;
  3714. index = i
  3715. }else {
  3716. if(denominator > 0 && numerator < upper * denominator) {
  3717. upper = numerator / denominator
  3718. }
  3719. }
  3720. }
  3721. if(upper < lower - Number.MIN_VALUE) {
  3722. return false
  3723. }
  3724. }
  3725. if(index >= 0) {
  3726. output.fraction = lower;
  3727. tMat = transform.R;
  3728. tVec = this.m_normals[index];
  3729. output.normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  3730. output.normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  3731. return true
  3732. }
  3733. return false
  3734. };
  3735. b2PolygonShape.prototype.ComputeAABB = function(aabb, xf) {
  3736. var tMat = xf.R;
  3737. var tVec = this.m_vertices[0];
  3738. var lowerX = xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  3739. var lowerY = xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  3740. var upperX = lowerX;
  3741. var upperY = lowerY;
  3742. for(var i = 1;i < this.m_vertexCount;++i) {
  3743. tVec = this.m_vertices[i];
  3744. var vX = xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  3745. var vY = xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  3746. lowerX = lowerX < vX ? lowerX : vX;
  3747. lowerY = lowerY < vY ? lowerY : vY;
  3748. upperX = upperX > vX ? upperX : vX;
  3749. upperY = upperY > vY ? upperY : vY
  3750. }
  3751. aabb.lowerBound.x = lowerX - this.m_radius;
  3752. aabb.lowerBound.y = lowerY - this.m_radius;
  3753. aabb.upperBound.x = upperX + this.m_radius;
  3754. aabb.upperBound.y = upperY + this.m_radius
  3755. };
  3756. b2PolygonShape.prototype.ComputeMass = function(massData, density) {
  3757. if(this.m_vertexCount == 2) {
  3758. massData.center.x = 0.5 * (this.m_vertices[0].x + this.m_vertices[1].x);
  3759. massData.center.y = 0.5 * (this.m_vertices[0].y + this.m_vertices[1].y);
  3760. massData.mass = 0;
  3761. massData.I = 0;
  3762. return
  3763. }
  3764. var centerX = 0;
  3765. var centerY = 0;
  3766. var area = 0;
  3767. var I = 0;
  3768. var p1X = 0;
  3769. var p1Y = 0;
  3770. var k_inv3 = 1 / 3;
  3771. for(var i = 0;i < this.m_vertexCount;++i) {
  3772. var p2 = this.m_vertices[i];
  3773. var p3 = i + 1 < this.m_vertexCount ? this.m_vertices[parseInt(i + 1)] : this.m_vertices[0];
  3774. var e1X = p2.x - p1X;
  3775. var e1Y = p2.y - p1Y;
  3776. var e2X = p3.x - p1X;
  3777. var e2Y = p3.y - p1Y;
  3778. var D = e1X * e2Y - e1Y * e2X;
  3779. var triangleArea = 0.5 * D;
  3780. area += triangleArea;
  3781. centerX += triangleArea * k_inv3 * (p1X + p2.x + p3.x);
  3782. centerY += triangleArea * k_inv3 * (p1Y + p2.y + p3.y);
  3783. var px = p1X;
  3784. var py = p1Y;
  3785. var ex1 = e1X;
  3786. var ey1 = e1Y;
  3787. var ex2 = e2X;
  3788. var ey2 = e2Y;
  3789. var intx2 = k_inv3 * (0.25 * (ex1 * ex1 + ex2 * ex1 + ex2 * ex2) + (px * ex1 + px * ex2)) + 0.5 * px * px;
  3790. var inty2 = k_inv3 * (0.25 * (ey1 * ey1 + ey2 * ey1 + ey2 * ey2) + (py * ey1 + py * ey2)) + 0.5 * py * py;
  3791. I += D * (intx2 + inty2)
  3792. }
  3793. massData.mass = density * area;
  3794. centerX *= 1 / area;
  3795. centerY *= 1 / area;
  3796. massData.center.Set(centerX, centerY);
  3797. massData.I = density * I
  3798. };
  3799. b2PolygonShape.prototype.ComputeSubmergedArea = function(normal, offset, xf, c) {
  3800. var normalL = b2Math.MulTMV(xf.R, normal);
  3801. var offsetL = offset - b2Math.Dot(normal, xf.position);
  3802. var depths = new Array;
  3803. var diveCount = 0;
  3804. var intoIndex = -1;
  3805. var outoIndex = -1;
  3806. var lastSubmerged = false;
  3807. var i = 0;
  3808. for(i = 0;i < this.m_vertexCount;++i) {
  3809. depths[i] = b2Math.Dot(normalL, this.m_vertices[i]) - offsetL;
  3810. var isSubmerged = depths[i] < -Number.MIN_VALUE;
  3811. if(i > 0) {
  3812. if(isSubmerged) {
  3813. if(!lastSubmerged) {
  3814. intoIndex = i - 1;
  3815. diveCount++
  3816. }
  3817. }else {
  3818. if(lastSubmerged) {
  3819. outoIndex = i - 1;
  3820. diveCount++
  3821. }
  3822. }
  3823. }
  3824. lastSubmerged = isSubmerged
  3825. }
  3826. switch(diveCount) {
  3827. case 0:
  3828. if(lastSubmerged) {
  3829. var md = new b2MassData;
  3830. this.ComputeMass(md, 1);
  3831. c.SetV(b2Math.MulX(xf, md.center));
  3832. return md.mass
  3833. }else {
  3834. return 0
  3835. }
  3836. break;
  3837. case 1:
  3838. if(intoIndex == -1) {
  3839. intoIndex = this.m_vertexCount - 1
  3840. }else {
  3841. outoIndex = this.m_vertexCount - 1
  3842. }
  3843. break
  3844. }
  3845. var intoIndex2 = (intoIndex + 1) % this.m_vertexCount;
  3846. var outoIndex2 = (outoIndex + 1) % this.m_vertexCount;
  3847. var intoLamdda = (0 - depths[intoIndex]) / (depths[intoIndex2] - depths[intoIndex]);
  3848. var outoLamdda = (0 - depths[outoIndex]) / (depths[outoIndex2] - depths[outoIndex]);
  3849. 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);
  3850. 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);
  3851. var area = 0;
  3852. var center = new b2Vec2;
  3853. var p2 = this.m_vertices[intoIndex2];
  3854. var p3;
  3855. i = intoIndex2;
  3856. while(i != outoIndex2) {
  3857. i = (i + 1) % this.m_vertexCount;
  3858. if(i == outoIndex2) {
  3859. p3 = outoVec
  3860. }else {
  3861. p3 = this.m_vertices[i]
  3862. }
  3863. var triangleArea = 0.5 * ((p2.x - intoVec.x) * (p3.y - intoVec.y) - (p2.y - intoVec.y) * (p3.x - intoVec.x));
  3864. area += triangleArea;
  3865. center.x += triangleArea * (intoVec.x + p2.x + p3.x) / 3;
  3866. center.y += triangleArea * (intoVec.y + p2.y + p3.y) / 3;
  3867. p2 = p3
  3868. }
  3869. center.Multiply(1 / area);
  3870. c.SetV(b2Math.MulX(xf, center));
  3871. return area
  3872. };
  3873. b2PolygonShape.prototype.GetVertexCount = function() {
  3874. return this.m_vertexCount
  3875. };
  3876. b2PolygonShape.prototype.GetVertices = function() {
  3877. return this.m_vertices
  3878. };
  3879. b2PolygonShape.prototype.GetNormals = function() {
  3880. return this.m_normals
  3881. };
  3882. b2PolygonShape.prototype.GetSupport = function(d) {
  3883. var bestIndex = 0;
  3884. var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
  3885. for(var i = 1;i < this.m_vertexCount;++i) {
  3886. var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
  3887. if(value > bestValue) {
  3888. bestIndex = i;
  3889. bestValue = value
  3890. }
  3891. }
  3892. return bestIndex
  3893. };
  3894. b2PolygonShape.prototype.GetSupportVertex = function(d) {
  3895. var bestIndex = 0;
  3896. var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
  3897. for(var i = 1;i < this.m_vertexCount;++i) {
  3898. var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
  3899. if(value > bestValue) {
  3900. bestIndex = i;
  3901. bestValue = value
  3902. }
  3903. }
  3904. return this.m_vertices[bestIndex]
  3905. };
  3906. b2PolygonShape.prototype.m_centroid = null;
  3907. b2PolygonShape.prototype.m_vertices = null;
  3908. b2PolygonShape.prototype.m_normals = null;
  3909. b2PolygonShape.prototype.m_vertexCount = 0;var b2Fixture = function() {
  3910. this.__varz();
  3911. this.__constructor.apply(this, arguments)
  3912. };
  3913. b2Fixture.prototype.__constructor = function() {
  3914. this.m_aabb = new b2AABB;
  3915. this.m_userData = null;
  3916. this.m_body = null;
  3917. this.m_next = null;
  3918. this.m_shape = null;
  3919. this.m_density = 0;
  3920. this.m_friction = 0;
  3921. this.m_restitution = 0
  3922. };
  3923. b2Fixture.prototype.__varz = function() {
  3924. this.m_filter = new b2FilterData
  3925. };
  3926. b2Fixture.prototype.Create = function(body, xf, def) {
  3927. this.m_userData = def.userData;
  3928. this.m_friction = def.friction;
  3929. this.m_restitution = def.restitution;
  3930. this.m_body = body;
  3931. this.m_next = null;
  3932. this.m_filter = def.filter.Copy();
  3933. this.m_isSensor = def.isSensor;
  3934. this.m_shape = def.shape.Copy();
  3935. this.m_density = def.density
  3936. };
  3937. b2Fixture.prototype.Destroy = function() {
  3938. this.m_shape = null
  3939. };
  3940. b2Fixture.prototype.CreateProxy = function(broadPhase, xf) {
  3941. this.m_shape.ComputeAABB(this.m_aabb, xf);
  3942. this.m_proxy = broadPhase.CreateProxy(this.m_aabb, this)
  3943. };
  3944. b2Fixture.prototype.DestroyProxy = function(broadPhase) {
  3945. if(this.m_proxy == null) {
  3946. return
  3947. }
  3948. broadPhase.DestroyProxy(this.m_proxy);
  3949. this.m_proxy = null
  3950. };
  3951. b2Fixture.prototype.Synchronize = function(broadPhase, transform1, transform2) {
  3952. if(!this.m_proxy) {
  3953. return
  3954. }
  3955. var aabb1 = new b2AABB;
  3956. var aabb2 = new b2AABB;
  3957. this.m_shape.ComputeAABB(aabb1, transform1);
  3958. this.m_shape.ComputeAABB(aabb2, transform2);
  3959. this.m_aabb.Combine(aabb1, aabb2);
  3960. var displacement = b2Math.SubtractVV(transform2.position, transform1.position);
  3961. broadPhase.MoveProxy(this.m_proxy, this.m_aabb, displacement)
  3962. };
  3963. b2Fixture.prototype.GetType = function() {
  3964. return this.m_shape.GetType()
  3965. };
  3966. b2Fixture.prototype.GetShape = function() {
  3967. return this.m_shape
  3968. };
  3969. b2Fixture.prototype.SetSensor = function(sensor) {
  3970. if(this.m_isSensor == sensor) {
  3971. return
  3972. }
  3973. this.m_isSensor = sensor;
  3974. if(this.m_body == null) {
  3975. return
  3976. }
  3977. var edge = this.m_body.GetContactList();
  3978. while(edge) {
  3979. var contact = edge.contact;
  3980. var fixtureA = contact.GetFixtureA();
  3981. var fixtureB = contact.GetFixtureB();
  3982. if(fixtureA == this || fixtureB == this) {
  3983. contact.SetSensor(fixtureA.IsSensor() || fixtureB.IsSensor())
  3984. }
  3985. edge = edge.next
  3986. }
  3987. };
  3988. b2Fixture.prototype.IsSensor = function() {
  3989. return this.m_isSensor
  3990. };
  3991. b2Fixture.prototype.SetFilterData = function(filter) {
  3992. this.m_filter = filter.Copy();
  3993. if(this.m_body) {
  3994. return
  3995. }
  3996. var edge = this.m_body.GetContactList();
  3997. while(edge) {
  3998. var contact = edge.contact;
  3999. var fixtureA = contact.GetFixtureA();
  4000. var fixtureB = contact.GetFixtureB();
  4001. if(fixtureA == this || fixtureB == this) {
  4002. contact.FlagForFiltering()
  4003. }
  4004. edge = edge.next
  4005. }
  4006. };
  4007. b2Fixture.prototype.GetFilterData = function() {
  4008. return this.m_filter.Copy()
  4009. };
  4010. b2Fixture.prototype.GetBody = function() {
  4011. return this.m_body
  4012. };
  4013. b2Fixture.prototype.GetNext = function() {
  4014. return this.m_next
  4015. };
  4016. b2Fixture.prototype.GetUserData = function() {
  4017. return this.m_userData
  4018. };
  4019. b2Fixture.prototype.SetUserData = function(data) {
  4020. this.m_userData = data
  4021. };
  4022. b2Fixture.prototype.TestPoint = function(p) {
  4023. return this.m_shape.TestPoint(this.m_body.GetTransform(), p)
  4024. };
  4025. b2Fixture.prototype.RayCast = function(output, input) {
  4026. return this.m_shape.RayCast(output, input, this.m_body.GetTransform())
  4027. };
  4028. b2Fixture.prototype.GetMassData = function(massData) {
  4029. if(massData == null) {
  4030. massData = new b2MassData
  4031. }
  4032. this.m_shape.ComputeMass(massData, this.m_density);
  4033. return massData
  4034. };
  4035. b2Fixture.prototype.SetDensity = function(density) {
  4036. this.m_density = density
  4037. };
  4038. b2Fixture.prototype.GetDensity = function() {
  4039. return this.m_density
  4040. };
  4041. b2Fixture.prototype.GetFriction = function() {
  4042. return this.m_friction
  4043. };
  4044. b2Fixture.prototype.SetFriction = function(friction) {
  4045. this.m_friction = friction
  4046. };
  4047. b2Fixture.prototype.GetRestitution = function() {
  4048. return this.m_restitution
  4049. };
  4050. b2Fixture.prototype.SetRestitution = function(restitution) {
  4051. this.m_restitution = restitution
  4052. };
  4053. b2Fixture.prototype.GetAABB = function() {
  4054. return this.m_aabb
  4055. };
  4056. b2Fixture.prototype.m_massData = null;
  4057. b2Fixture.prototype.m_aabb = null;
  4058. b2Fixture.prototype.m_density = null;
  4059. b2Fixture.prototype.m_next = null;
  4060. b2Fixture.prototype.m_body = null;
  4061. b2Fixture.prototype.m_shape = null;
  4062. b2Fixture.prototype.m_friction = null;
  4063. b2Fixture.prototype.m_restitution = null;
  4064. b2Fixture.prototype.m_proxy = null;
  4065. b2Fixture.prototype.m_filter = new b2FilterData;
  4066. b2Fixture.prototype.m_isSensor = null;
  4067. b2Fixture.prototype.m_userData = null;var b2DynamicTreeNode = function() {
  4068. this.__varz();
  4069. this.__constructor.apply(this, arguments)
  4070. };
  4071. b2DynamicTreeNode.prototype.__constructor = function() {
  4072. };
  4073. b2DynamicTreeNode.prototype.__varz = function() {
  4074. this.aabb = new b2AABB
  4075. };
  4076. b2DynamicTreeNode.prototype.IsLeaf = function() {
  4077. return this.child1 == null
  4078. };
  4079. b2DynamicTreeNode.prototype.userData = null;
  4080. b2DynamicTreeNode.prototype.aabb = new b2AABB;
  4081. b2DynamicTreeNode.prototype.parent = null;
  4082. b2DynamicTreeNode.prototype.child1 = null;
  4083. b2DynamicTreeNode.prototype.child2 = null;var b2BodyDef = function() {
  4084. this.__varz();
  4085. this.__constructor.apply(this, arguments)
  4086. };
  4087. b2BodyDef.prototype.__constructor = function() {
  4088. this.userData = null;
  4089. this.position.Set(0, 0);
  4090. this.angle = 0;
  4091. this.linearVelocity.Set(0, 0);
  4092. this.angularVelocity = 0;
  4093. this.linearDamping = 0;
  4094. this.angularDamping = 0;
  4095. this.allowSleep = true;
  4096. this.awake = true;
  4097. this.fixedRotation = false;
  4098. this.bullet = false;
  4099. this.type = b2Body.b2_staticBody;
  4100. this.active = true;
  4101. this.inertiaScale = 1
  4102. };
  4103. b2BodyDef.prototype.__varz = function() {
  4104. this.position = new b2Vec2;
  4105. this.linearVelocity = new b2Vec2
  4106. };
  4107. b2BodyDef.prototype.type = 0;
  4108. b2BodyDef.prototype.position = new b2Vec2;
  4109. b2BodyDef.prototype.angle = null;
  4110. b2BodyDef.prototype.linearVelocity = new b2Vec2;
  4111. b2BodyDef.prototype.angularVelocity = null;
  4112. b2BodyDef.prototype.linearDamping = null;
  4113. b2BodyDef.prototype.angularDamping = null;
  4114. b2BodyDef.prototype.allowSleep = null;
  4115. b2BodyDef.prototype.awake = null;
  4116. b2BodyDef.prototype.fixedRotation = null;
  4117. b2BodyDef.prototype.bullet = null;
  4118. b2BodyDef.prototype.active = null;
  4119. b2BodyDef.prototype.userData = null;
  4120. b2BodyDef.prototype.inertiaScale = null;var b2DynamicTreeBroadPhase = function() {
  4121. this.__varz();
  4122. this.__constructor.apply(this, arguments)
  4123. };
  4124. b2DynamicTreeBroadPhase.prototype.__constructor = function() {
  4125. };
  4126. b2DynamicTreeBroadPhase.prototype.__varz = function() {
  4127. this.m_tree = new b2DynamicTree;
  4128. this.m_moveBuffer = new Array;
  4129. this.m_pairBuffer = new Array
  4130. };
  4131. b2DynamicTreeBroadPhase.prototype.BufferMove = function(proxy) {
  4132. this.m_moveBuffer[this.m_moveBuffer.length] = proxy
  4133. };
  4134. b2DynamicTreeBroadPhase.prototype.UnBufferMove = function(proxy) {
  4135. var i = this.m_moveBuffer.indexOf(proxy);
  4136. this.m_moveBuffer.splice(i, 1)
  4137. };
  4138. b2DynamicTreeBroadPhase.prototype.ComparePairs = function(pair1, pair2) {
  4139. return 0
  4140. };
  4141. b2DynamicTreeBroadPhase.prototype.CreateProxy = function(aabb, userData) {
  4142. var proxy = this.m_tree.CreateProxy(aabb, userData);
  4143. ++this.m_proxyCount;
  4144. this.BufferMove(proxy);
  4145. return proxy
  4146. };
  4147. b2DynamicTreeBroadPhase.prototype.DestroyProxy = function(proxy) {
  4148. this.UnBufferMove(proxy);
  4149. --this.m_proxyCount;
  4150. this.m_tree.DestroyProxy(proxy)
  4151. };
  4152. b2DynamicTreeBroadPhase.prototype.MoveProxy = function(proxy, aabb, displacement) {
  4153. var buffer = this.m_tree.MoveProxy(proxy, aabb, displacement);
  4154. if(buffer) {
  4155. this.BufferMove(proxy)
  4156. }
  4157. };
  4158. b2DynamicTreeBroadPhase.prototype.TestOverlap = function(proxyA, proxyB) {
  4159. var aabbA = this.m_tree.GetFatAABB(proxyA);
  4160. var aabbB = this.m_tree.GetFatAABB(proxyB);
  4161. return aabbA.TestOverlap(aabbB)
  4162. };
  4163. b2DynamicTreeBroadPhase.prototype.GetUserData = function(proxy) {
  4164. return this.m_tree.GetUserData(proxy)
  4165. };
  4166. b2DynamicTreeBroadPhase.prototype.GetFatAABB = function(proxy) {
  4167. return this.m_tree.GetFatAABB(proxy)
  4168. };
  4169. b2DynamicTreeBroadPhase.prototype.GetProxyCount = function() {
  4170. return this.m_proxyCount
  4171. };
  4172. b2DynamicTreeBroadPhase.prototype.UpdatePairs = function(callback) {
  4173. this.m_pairCount = 0;
  4174. for(var i = 0, queryProxy = null;i < this.m_moveBuffer.length, queryProxy = this.m_moveBuffer[i];i++) {
  4175. var that = this;
  4176. function QueryCallback(proxy) {
  4177. if(proxy == queryProxy) {
  4178. return true
  4179. }
  4180. if(that.m_pairCount == that.m_pairBuffer.length) {
  4181. that.m_pairBuffer[that.m_pairCount] = new b2DynamicTreePair
  4182. }
  4183. var pair = that.m_pairBuffer[that.m_pairCount];
  4184. pair.proxyA = proxy < queryProxy ? proxy : queryProxy;
  4185. pair.proxyB = proxy >= queryProxy ? proxy : queryProxy;
  4186. ++that.m_pairCount;
  4187. return true
  4188. }
  4189. var fatAABB = this.m_tree.GetFatAABB(queryProxy);
  4190. this.m_tree.Query(QueryCallback, fatAABB)
  4191. }
  4192. this.m_moveBuffer.length = 0;
  4193. for(var i = 0;i < this.m_pairCount;) {
  4194. var primaryPair = this.m_pairBuffer[i];
  4195. var userDataA = this.m_tree.GetUserData(primaryPair.proxyA);
  4196. var userDataB = this.m_tree.GetUserData(primaryPair.proxyB);
  4197. callback(userDataA, userDataB);
  4198. ++i;
  4199. while(i < this.m_pairCount) {
  4200. var pair = this.m_pairBuffer[i];
  4201. if(pair.proxyA != primaryPair.proxyA || pair.proxyB != primaryPair.proxyB) {
  4202. break
  4203. }
  4204. ++i
  4205. }
  4206. }
  4207. };
  4208. b2DynamicTreeBroadPhase.prototype.Query = function(callback, aabb) {
  4209. this.m_tree.Query(callback, aabb)
  4210. };
  4211. b2DynamicTreeBroadPhase.prototype.RayCast = function(callback, input) {
  4212. this.m_tree.RayCast(callback, input)
  4213. };
  4214. b2DynamicTreeBroadPhase.prototype.Validate = function() {
  4215. };
  4216. b2DynamicTreeBroadPhase.prototype.Rebalance = function(iterations) {
  4217. this.m_tree.Rebalance(iterations)
  4218. };
  4219. b2DynamicTreeBroadPhase.prototype.m_tree = new b2DynamicTree;
  4220. b2DynamicTreeBroadPhase.prototype.m_proxyCount = 0;
  4221. b2DynamicTreeBroadPhase.prototype.m_moveBuffer = new Array;
  4222. b2DynamicTreeBroadPhase.prototype.m_pairBuffer = new Array;
  4223. b2DynamicTreeBroadPhase.prototype.m_pairCount = 0;var b2BroadPhase = function() {
  4224. this.__varz();
  4225. this.__constructor.apply(this, arguments)
  4226. };
  4227. b2BroadPhase.prototype.__constructor = function(worldAABB) {
  4228. var i = 0;
  4229. this.m_pairManager.Initialize(this);
  4230. this.m_worldAABB = worldAABB;
  4231. this.m_proxyCount = 0;
  4232. this.m_bounds = new Array;
  4233. for(i = 0;i < 2;i++) {
  4234. this.m_bounds[i] = new Array
  4235. }
  4236. var dX = worldAABB.upperBound.x - worldAABB.lowerBound.x;
  4237. var dY = worldAABB.upperBound.y - worldAABB.lowerBound.y;
  4238. this.m_quantizationFactor.x = b2Settings.USHRT_MAX / dX;
  4239. this.m_quantizationFactor.y = b2Settings.USHRT_MAX / dY;
  4240. this.m_timeStamp = 1;
  4241. this.m_queryResultCount = 0
  4242. };
  4243. b2BroadPhase.prototype.__varz = function() {
  4244. this.m_pairManager = new b2PairManager;
  4245. this.m_proxyPool = new Array;
  4246. this.m_querySortKeys = new Array;
  4247. this.m_queryResults = new Array;
  4248. this.m_quantizationFactor = new b2Vec2
  4249. };
  4250. b2BroadPhase.BinarySearch = function(bounds, count, value) {
  4251. var low = 0;
  4252. var high = count - 1;
  4253. while(low <= high) {
  4254. var mid = Math.round((low + high) / 2);
  4255. var bound = bounds[mid];
  4256. if(bound.value > value) {
  4257. high = mid - 1
  4258. }else {
  4259. if(bound.value < value) {
  4260. low = mid + 1
  4261. }else {
  4262. return parseInt(mid)
  4263. }
  4264. }
  4265. }
  4266. return parseInt(low)
  4267. };
  4268. b2BroadPhase.s_validate = false;
  4269. b2BroadPhase.b2_invalid = b2Settings.USHRT_MAX;
  4270. b2BroadPhase.b2_nullEdge = b2Settings.USHRT_MAX;
  4271. b2BroadPhase.prototype.ComputeBounds = function(lowerValues, upperValues, aabb) {
  4272. var minVertexX = aabb.lowerBound.x;
  4273. var minVertexY = aabb.lowerBound.y;
  4274. minVertexX = b2Math.Min(minVertexX, this.m_worldAABB.upperBound.x);
  4275. minVertexY = b2Math.Min(minVertexY, this.m_worldAABB.upperBound.y);
  4276. minVertexX = b2Math.Max(minVertexX, this.m_worldAABB.lowerBound.x);
  4277. minVertexY = b2Math.Max(minVertexY, this.m_worldAABB.lowerBound.y);
  4278. var maxVertexX = aabb.upperBound.x;
  4279. var maxVertexY = aabb.upperBound.y;
  4280. maxVertexX = b2Math.Min(maxVertexX, this.m_worldAABB.upperBound.x);
  4281. maxVertexY = b2Math.Min(maxVertexY, this.m_worldAABB.upperBound.y);
  4282. maxVertexX = b2Math.Max(maxVertexX, this.m_worldAABB.lowerBound.x);
  4283. maxVertexY = b2Math.Max(maxVertexY, this.m_worldAABB.lowerBound.y);
  4284. lowerValues[0] = parseInt(this.m_quantizationFactor.x * (minVertexX - this.m_worldAABB.lowerBound.x)) & b2Settings.USHRT_MAX - 1;
  4285. upperValues[0] = parseInt(this.m_quantizationFactor.x * (maxVertexX - this.m_worldAABB.lowerBound.x)) % 65535 | 1;
  4286. lowerValues[1] = parseInt(this.m_quantizationFactor.y * (minVertexY - this.m_worldAABB.lowerBound.y)) & b2Settings.USHRT_MAX - 1;
  4287. upperValues[1] = parseInt(this.m_quantizationFactor.y * (maxVertexY - this.m_worldAABB.lowerBound.y)) % 65535 | 1
  4288. };
  4289. b2BroadPhase.prototype.TestOverlapValidate = function(p1, p2) {
  4290. for(var axis = 0;axis < 2;++axis) {
  4291. var bounds = this.m_bounds[axis];
  4292. var bound1 = bounds[p1.lowerBounds[axis]];
  4293. var bound2 = bounds[p2.upperBounds[axis]];
  4294. if(bound1.value > bound2.value) {
  4295. return false
  4296. }
  4297. bound1 = bounds[p1.upperBounds[axis]];
  4298. bound2 = bounds[p2.lowerBounds[axis]];
  4299. if(bound1.value < bound2.value) {
  4300. return false
  4301. }
  4302. }
  4303. return true
  4304. };
  4305. b2BroadPhase.prototype.QueryAxis = function(lowerQueryOut, upperQueryOut, lowerValue, upperValue, bounds, boundCount, axis) {
  4306. var lowerQuery = b2BroadPhase.BinarySearch(bounds, boundCount, lowerValue);
  4307. var upperQuery = b2BroadPhase.BinarySearch(bounds, boundCount, upperValue);
  4308. var bound;
  4309. for(var j = lowerQuery;j < upperQuery;++j) {
  4310. bound = bounds[j];
  4311. if(bound.IsLower()) {
  4312. this.IncrementOverlapCount(bound.proxy)
  4313. }
  4314. }
  4315. if(lowerQuery > 0) {
  4316. var i = lowerQuery - 1;
  4317. bound = bounds[i];
  4318. var s = bound.stabbingCount;
  4319. while(s) {
  4320. bound = bounds[i];
  4321. if(bound.IsLower()) {
  4322. var proxy = bound.proxy;
  4323. if(lowerQuery <= proxy.upperBounds[axis]) {
  4324. this.IncrementOverlapCount(bound.proxy);
  4325. --s
  4326. }
  4327. }
  4328. --i
  4329. }
  4330. }
  4331. lowerQueryOut[0] = lowerQuery;
  4332. upperQueryOut[0] = upperQuery
  4333. };
  4334. b2BroadPhase.prototype.IncrementOverlapCount = function(proxy) {
  4335. if(proxy.timeStamp < this.m_timeStamp) {
  4336. proxy.timeStamp = this.m_timeStamp;
  4337. proxy.overlapCount = 1
  4338. }else {
  4339. proxy.overlapCount = 2;
  4340. this.m_queryResults[this.m_queryResultCount] = proxy;
  4341. ++this.m_queryResultCount
  4342. }
  4343. };
  4344. b2BroadPhase.prototype.IncrementTimeStamp = function() {
  4345. if(this.m_timeStamp == b2Settings.USHRT_MAX) {
  4346. for(var i = 0;i < this.m_proxyPool.length;++i) {
  4347. this.m_proxyPool[i].timeStamp = 0
  4348. }
  4349. this.m_timeStamp = 1
  4350. }else {
  4351. ++this.m_timeStamp
  4352. }
  4353. };
  4354. b2BroadPhase.prototype.InRange = function(aabb) {
  4355. var dX;
  4356. var dY;
  4357. var d2X;
  4358. var d2Y;
  4359. dX = aabb.lowerBound.x;
  4360. dY = aabb.lowerBound.y;
  4361. dX -= this.m_worldAABB.upperBound.x;
  4362. dY -= this.m_worldAABB.upperBound.y;
  4363. d2X = this.m_worldAABB.lowerBound.x;
  4364. d2Y = this.m_worldAABB.lowerBound.y;
  4365. d2X -= aabb.upperBound.x;
  4366. d2Y -= aabb.upperBound.y;
  4367. dX = b2Math.Max(dX, d2X);
  4368. dY = b2Math.Max(dY, d2Y);
  4369. return b2Math.Max(dX, dY) < 0
  4370. };
  4371. b2BroadPhase.prototype.CreateProxy = function(aabb, userData) {
  4372. var index = 0;
  4373. var proxy;
  4374. var i = 0;
  4375. var j = 0;
  4376. if(!this.m_freeProxy) {
  4377. this.m_freeProxy = this.m_proxyPool[this.m_proxyCount] = new b2Proxy;
  4378. this.m_freeProxy.next = null;
  4379. this.m_freeProxy.timeStamp = 0;
  4380. this.m_freeProxy.overlapCount = b2BroadPhase.b2_invalid;
  4381. this.m_freeProxy.userData = null;
  4382. for(i = 0;i < 2;i++) {
  4383. j = this.m_proxyCount * 2;
  4384. this.m_bounds[i][j++] = new b2Bound;
  4385. this.m_bounds[i][j] = new b2Bound
  4386. }
  4387. }
  4388. proxy = this.m_freeProxy;
  4389. this.m_freeProxy = proxy.next;
  4390. proxy.overlapCount = 0;
  4391. proxy.userData = userData;
  4392. var boundCount = 2 * this.m_proxyCount;
  4393. var lowerValues = new Array;
  4394. var upperValues = new Array;
  4395. this.ComputeBounds(lowerValues, upperValues, aabb);
  4396. for(var axis = 0;axis < 2;++axis) {
  4397. var bounds = this.m_bounds[axis];
  4398. var lowerIndex = 0;
  4399. var upperIndex = 0;
  4400. var lowerIndexOut = new Array;
  4401. lowerIndexOut.push(lowerIndex);
  4402. var upperIndexOut = new Array;
  4403. upperIndexOut.push(upperIndex);
  4404. this.QueryAxis(lowerIndexOut, upperIndexOut, lowerValues[axis], upperValues[axis], bounds, boundCount, axis);
  4405. lowerIndex = lowerIndexOut[0];
  4406. upperIndex = upperIndexOut[0];
  4407. bounds.splice(upperIndex, 0, bounds[bounds.length - 1]);
  4408. bounds.length--;
  4409. bounds.splice(lowerIndex, 0, bounds[bounds.length - 1]);
  4410. bounds.length--;
  4411. ++upperIndex;
  4412. var tBound1 = bounds[lowerIndex];
  4413. var tBound2 = bounds[upperIndex];
  4414. tBound1.value = lowerValues[axis];
  4415. tBound1.proxy = proxy;
  4416. tBound2.value = upperValues[axis];
  4417. tBound2.proxy = proxy;
  4418. var tBoundAS3 = bounds[parseInt(lowerIndex - 1)];
  4419. tBound1.stabbingCount = lowerIndex == 0 ? 0 : tBoundAS3.stabbingCount;
  4420. tBoundAS3 = bounds[parseInt(upperIndex - 1)];
  4421. tBound2.stabbingCount = tBoundAS3.stabbingCount;
  4422. for(index = lowerIndex;index < upperIndex;++index) {
  4423. tBoundAS3 = bounds[index];
  4424. tBoundAS3.stabbingCount++
  4425. }
  4426. for(index = lowerIndex;index < boundCount + 2;++index) {
  4427. tBound1 = bounds[index];
  4428. var proxy2 = tBound1.proxy;
  4429. if(tBound1.IsLower()) {
  4430. proxy2.lowerBounds[axis] = index
  4431. }else {
  4432. proxy2.upperBounds[axis] = index
  4433. }
  4434. }
  4435. }
  4436. ++this.m_proxyCount;
  4437. for(i = 0;i < this.m_queryResultCount;++i) {
  4438. this.m_pairManager.AddBufferedPair(proxy, this.m_queryResults[i])
  4439. }
  4440. this.m_queryResultCount = 0;
  4441. this.IncrementTimeStamp();
  4442. return proxy
  4443. };
  4444. b2BroadPhase.prototype.DestroyProxy = function(proxy_) {
  4445. var proxy = proxy_;
  4446. var tBound1;
  4447. var tBound2;
  4448. var boundCount = 2 * this.m_proxyCount;
  4449. for(var axis = 0;axis < 2;++axis) {
  4450. var bounds = this.m_bounds[axis];
  4451. var lowerIndex = proxy.lowerBounds[axis];
  4452. var upperIndex = proxy.upperBounds[axis];
  4453. tBound1 = bounds[lowerIndex];
  4454. var lowerValue = tBound1.value;
  4455. tBound2 = bounds[upperIndex];
  4456. var upperValue = tBound2.value;
  4457. bounds.splice(upperIndex, 1);
  4458. bounds.splice(lowerIndex, 1);
  4459. bounds.push(tBound1);
  4460. bounds.push(tBound2);
  4461. var tEnd = boundCount - 2;
  4462. for(var index = lowerIndex;index < tEnd;++index) {
  4463. tBound1 = bounds[index];
  4464. var proxy2 = tBound1.proxy;
  4465. if(tBound1.IsLower()) {
  4466. proxy2.lowerBounds[axis] = index
  4467. }else {
  4468. proxy2.upperBounds[axis] = index
  4469. }
  4470. }
  4471. tEnd = upperIndex - 1;
  4472. for(var index2 = lowerIndex;index2 < tEnd;++index2) {
  4473. tBound1 = bounds[index2];
  4474. tBound1.stabbingCount--
  4475. }
  4476. var ignore = new Array;
  4477. this.QueryAxis(ignore, ignore, lowerValue, upperValue, bounds, boundCount - 2, axis)
  4478. }
  4479. for(var i = 0;i < this.m_queryResultCount;++i) {
  4480. this.m_pairManager.RemoveBufferedPair(proxy, this.m_queryResults[i])
  4481. }
  4482. this.m_queryResultCount = 0;
  4483. this.IncrementTimeStamp();
  4484. proxy.userData = null;
  4485. proxy.overlapCount = b2BroadPhase.b2_invalid;
  4486. proxy.lowerBounds[0] = b2BroadPhase.b2_invalid;
  4487. proxy.lowerBounds[1] = b2BroadPhase.b2_invalid;
  4488. proxy.upperBounds[0] = b2BroadPhase.b2_invalid;
  4489. proxy.upperBounds[1] = b2BroadPhase.b2_invalid;
  4490. proxy.next = this.m_freeProxy;
  4491. this.m_freeProxy = proxy;
  4492. --this.m_proxyCount
  4493. };
  4494. b2BroadPhase.prototype.MoveProxy = function(proxy_, aabb, displacement) {
  4495. var proxy = proxy_;
  4496. var as3arr;
  4497. var as3int = 0;
  4498. var axis = 0;
  4499. var index = 0;
  4500. var bound;
  4501. var prevBound;
  4502. var nextBound;
  4503. var nextProxyId = 0;
  4504. var nextProxy;
  4505. if(proxy == null) {
  4506. return
  4507. }
  4508. if(aabb.IsValid() == false) {
  4509. return
  4510. }
  4511. var boundCount = 2 * this.m_proxyCount;
  4512. var newValues = new b2BoundValues;
  4513. this.ComputeBounds(newValues.lowerValues, newValues.upperValues, aabb);
  4514. var oldValues = new b2BoundValues;
  4515. for(axis = 0;axis < 2;++axis) {
  4516. bound = this.m_bounds[axis][proxy.lowerBounds[axis]];
  4517. oldValues.lowerValues[axis] = bound.value;
  4518. bound = this.m_bounds[axis][proxy.upperBounds[axis]];
  4519. oldValues.upperValues[axis] = bound.value
  4520. }
  4521. for(axis = 0;axis < 2;++axis) {
  4522. var bounds = this.m_bounds[axis];
  4523. var lowerIndex = proxy.lowerBounds[axis];
  4524. var upperIndex = proxy.upperBounds[axis];
  4525. var lowerValue = newValues.lowerValues[axis];
  4526. var upperValue = newValues.upperValues[axis];
  4527. bound = bounds[lowerIndex];
  4528. var deltaLower = lowerValue - bound.value;
  4529. bound.value = lowerValue;
  4530. bound = bounds[upperIndex];
  4531. var deltaUpper = upperValue - bound.value;
  4532. bound.value = upperValue;
  4533. if(deltaLower < 0) {
  4534. index = lowerIndex;
  4535. while(index > 0 && lowerValue < bounds[parseInt(index - 1)].value) {
  4536. bound = bounds[index];
  4537. prevBound = bounds[parseInt(index - 1)];
  4538. var prevProxy = prevBound.proxy;
  4539. prevBound.stabbingCount++;
  4540. if(prevBound.IsUpper() == true) {
  4541. if(this.TestOverlapBound(newValues, prevProxy)) {
  4542. this.m_pairManager.AddBufferedPair(proxy, prevProxy)
  4543. }
  4544. as3arr = prevProxy.upperBounds;
  4545. as3int = as3arr[axis];
  4546. as3int++;
  4547. as3arr[axis] = as3int;
  4548. bound.stabbingCount++
  4549. }else {
  4550. as3arr = prevProxy.lowerBounds;
  4551. as3int = as3arr[axis];
  4552. as3int++;
  4553. as3arr[axis] = as3int;
  4554. bound.stabbingCount--
  4555. }
  4556. as3arr = proxy.lowerBounds;
  4557. as3int = as3arr[axis];
  4558. as3int--;
  4559. as3arr[axis] = as3int;
  4560. bound.Swap(prevBound);
  4561. --index
  4562. }
  4563. }
  4564. if(deltaUpper > 0) {
  4565. index = upperIndex;
  4566. while(index < boundCount - 1 && bounds[parseInt(index + 1)].value <= upperValue) {
  4567. bound = bounds[index];
  4568. nextBound = bounds[parseInt(index + 1)];
  4569. nextProxy = nextBound.proxy;
  4570. nextBound.stabbingCount++;
  4571. if(nextBound.IsLower() == true) {
  4572. if(this.TestOverlapBound(newValues, nextProxy)) {
  4573. this.m_pairManager.AddBufferedPair(proxy, nextProxy)
  4574. }
  4575. as3arr = nextProxy.lowerBounds;
  4576. as3int = as3arr[axis];
  4577. as3int--;
  4578. as3arr[axis] = as3int;
  4579. bound.stabbingCount++
  4580. }else {
  4581. as3arr = nextProxy.upperBounds;
  4582. as3int = as3arr[axis];
  4583. as3int--;
  4584. as3arr[axis] = as3int;
  4585. bound.stabbingCount--
  4586. }
  4587. as3arr = proxy.upperBounds;
  4588. as3int = as3arr[axis];
  4589. as3int++;
  4590. as3arr[axis] = as3int;
  4591. bound.Swap(nextBound);
  4592. index++
  4593. }
  4594. }
  4595. if(deltaLower > 0) {
  4596. index = lowerIndex;
  4597. while(index < boundCount - 1 && bounds[parseInt(index + 1)].value <= lowerValue) {
  4598. bound = bounds[index];
  4599. nextBound = bounds[parseInt(index + 1)];
  4600. nextProxy = nextBound.proxy;
  4601. nextBound.stabbingCount--;
  4602. if(nextBound.IsUpper()) {
  4603. if(this.TestOverlapBound(oldValues, nextProxy)) {
  4604. this.m_pairManager.RemoveBufferedPair(proxy, nextProxy)
  4605. }
  4606. as3arr = nextProxy.upperBounds;
  4607. as3int = as3arr[axis];
  4608. as3int--;
  4609. as3arr[axis] = as3int;
  4610. bound.stabbingCount--
  4611. }else {
  4612. as3arr = nextProxy.lowerBounds;
  4613. as3int = as3arr[axis];
  4614. as3int--;
  4615. as3arr[axis] = as3int;
  4616. bound.stabbingCount++
  4617. }
  4618. as3arr = proxy.lowerBounds;
  4619. as3int = as3arr[axis];
  4620. as3int++;
  4621. as3arr[axis] = as3int;
  4622. bound.Swap(nextBound);
  4623. index++
  4624. }
  4625. }
  4626. if(deltaUpper < 0) {
  4627. index = upperIndex;
  4628. while(index > 0 && upperValue < bounds[parseInt(index - 1)].value) {
  4629. bound = bounds[index];
  4630. prevBound = bounds[parseInt(index - 1)];
  4631. prevProxy = prevBound.proxy;
  4632. prevBound.stabbingCount--;
  4633. if(prevBound.IsLower() == true) {
  4634. if(this.TestOverlapBound(oldValues, prevProxy)) {
  4635. this.m_pairManager.RemoveBufferedPair(proxy, prevProxy)
  4636. }
  4637. as3arr = prevProxy.lowerBounds;
  4638. as3int = as3arr[axis];
  4639. as3int++;
  4640. as3arr[axis] = as3int;
  4641. bound.stabbingCount--
  4642. }else {
  4643. as3arr = prevProxy.upperBounds;
  4644. as3int = as3arr[axis];
  4645. as3int++;
  4646. as3arr[axis] = as3int;
  4647. bound.stabbingCount++
  4648. }
  4649. as3arr = proxy.upperBounds;
  4650. as3int = as3arr[axis];
  4651. as3int--;
  4652. as3arr[axis] = as3int;
  4653. bound.Swap(prevBound);
  4654. index--
  4655. }
  4656. }
  4657. }
  4658. };
  4659. b2BroadPhase.prototype.UpdatePairs = function(callback) {
  4660. this.m_pairManager.Commit(callback)
  4661. };
  4662. b2BroadPhase.prototype.TestOverlap = function(proxyA, proxyB) {
  4663. var proxyA_ = proxyA;
  4664. var proxyB_ = proxyB;
  4665. if(proxyA_.lowerBounds[0] > proxyB_.upperBounds[0]) {
  4666. return false
  4667. }
  4668. if(proxyB_.lowerBounds[0] > proxyA_.upperBounds[0]) {
  4669. return false
  4670. }
  4671. if(proxyA_.lowerBounds[1] > proxyB_.upperBounds[1]) {
  4672. return false
  4673. }
  4674. if(proxyB_.lowerBounds[1] > proxyA_.upperBounds[1]) {
  4675. return false
  4676. }
  4677. return true
  4678. };
  4679. b2BroadPhase.prototype.GetUserData = function(proxy) {
  4680. return proxy.userData
  4681. };
  4682. b2BroadPhase.prototype.GetFatAABB = function(proxy_) {
  4683. var aabb = new b2AABB;
  4684. var proxy = proxy_;
  4685. aabb.lowerBound.x = this.m_worldAABB.lowerBound.x + this.m_bounds[0][proxy.lowerBounds[0]].value / this.m_quantizationFactor.x;
  4686. aabb.lowerBound.y = this.m_worldAABB.lowerBound.y + this.m_bounds[1][proxy.lowerBounds[1]].value / this.m_quantizationFactor.y;
  4687. aabb.upperBound.x = this.m_worldAABB.lowerBound.x + this.m_bounds[0][proxy.upperBounds[0]].value / this.m_quantizationFactor.x;
  4688. aabb.upperBound.y = this.m_worldAABB.lowerBound.y + this.m_bounds[1][proxy.upperBounds[1]].value / this.m_quantizationFactor.y;
  4689. return aabb
  4690. };
  4691. b2BroadPhase.prototype.GetProxyCount = function() {
  4692. return this.m_proxyCount
  4693. };
  4694. b2BroadPhase.prototype.Query = function(callback, aabb) {
  4695. var lowerValues = new Array;
  4696. var upperValues = new Array;
  4697. this.ComputeBounds(lowerValues, upperValues, aabb);
  4698. var lowerIndex = 0;
  4699. var upperIndex = 0;
  4700. var lowerIndexOut = new Array;
  4701. lowerIndexOut.push(lowerIndex);
  4702. var upperIndexOut = new Array;
  4703. upperIndexOut.push(upperIndex);
  4704. this.QueryAxis(lowerIndexOut, upperIndexOut, lowerValues[0], upperValues[0], this.m_bounds[0], 2 * this.m_proxyCount, 0);
  4705. this.QueryAxis(lowerIndexOut, upperIndexOut, lowerValues[1], upperValues[1], this.m_bounds[1], 2 * this.m_proxyCount, 1);
  4706. for(var i = 0;i < this.m_queryResultCount;++i) {
  4707. var proxy = this.m_queryResults[i];
  4708. if(!callback(proxy)) {
  4709. break
  4710. }
  4711. }
  4712. this.m_queryResultCount = 0;
  4713. this.IncrementTimeStamp()
  4714. };
  4715. b2BroadPhase.prototype.Validate = function() {
  4716. var pair;
  4717. var proxy1;
  4718. var proxy2;
  4719. var overlap;
  4720. for(var axis = 0;axis < 2;++axis) {
  4721. var bounds = this.m_bounds[axis];
  4722. var boundCount = 2 * this.m_proxyCount;
  4723. var stabbingCount = 0;
  4724. for(var i = 0;i < boundCount;++i) {
  4725. var bound = bounds[i];
  4726. if(bound.IsLower() == true) {
  4727. stabbingCount++
  4728. }else {
  4729. stabbingCount--
  4730. }
  4731. }
  4732. }
  4733. };
  4734. b2BroadPhase.prototype.Rebalance = function(iterations) {
  4735. };
  4736. b2BroadPhase.prototype.RayCast = function(callback, input) {
  4737. var subInput = new b2RayCastInput;
  4738. subInput.p1.SetV(input.p1);
  4739. subInput.p2.SetV(input.p2);
  4740. subInput.maxFraction = input.maxFraction;
  4741. var dx = (input.p2.x - input.p1.x) * this.m_quantizationFactor.x;
  4742. var dy = (input.p2.y - input.p1.y) * this.m_quantizationFactor.y;
  4743. var sx = dx < -Number.MIN_VALUE ? -1 : dx > Number.MIN_VALUE ? 1 : 0;
  4744. var sy = dy < -Number.MIN_VALUE ? -1 : dy > Number.MIN_VALUE ? 1 : 0;
  4745. var p1x = this.m_quantizationFactor.x * (input.p1.x - this.m_worldAABB.lowerBound.x);
  4746. var p1y = this.m_quantizationFactor.y * (input.p1.y - this.m_worldAABB.lowerBound.y);
  4747. var startValues = new Array;
  4748. var startValues2 = new Array;
  4749. startValues[0] = parseInt(p1x) & b2Settings.USHRT_MAX - 1;
  4750. startValues[1] = parseInt(p1y) & b2Settings.USHRT_MAX - 1;
  4751. startValues2[0] = startValues[0] + 1;
  4752. startValues2[1] = startValues[1] + 1;
  4753. var startIndices = new Array;
  4754. var xIndex = 0;
  4755. var yIndex = 0;
  4756. var proxy;
  4757. var lowerIndex = 0;
  4758. var upperIndex = 0;
  4759. var lowerIndexOut = new Array;
  4760. lowerIndexOut.push(lowerIndex);
  4761. var upperIndexOut = new Array;
  4762. upperIndexOut.push(upperIndex);
  4763. this.QueryAxis(lowerIndexOut, upperIndexOut, startValues[0], startValues2[0], this.m_bounds[0], 2 * this.m_proxyCount, 0);
  4764. if(sx >= 0) {
  4765. xIndex = upperIndexOut[0] - 1
  4766. }else {
  4767. xIndex = lowerIndexOut[0]
  4768. }
  4769. this.QueryAxis(lowerIndexOut, upperIndexOut, startValues[1], startValues2[1], this.m_bounds[1], 2 * this.m_proxyCount, 1);
  4770. if(sy >= 0) {
  4771. yIndex = upperIndexOut[0] - 1
  4772. }else {
  4773. yIndex = lowerIndexOut[0]
  4774. }
  4775. for(var i = 0;i < this.m_queryResultCount;i++) {
  4776. subInput.maxFraction = callback(this.m_queryResults[i], subInput)
  4777. }
  4778. for(;;) {
  4779. var xProgress = 0;
  4780. var yProgress = 0;
  4781. xIndex += sx >= 0 ? 1 : -1;
  4782. if(xIndex < 0 || xIndex >= this.m_proxyCount * 2) {
  4783. break
  4784. }
  4785. if(sx != 0) {
  4786. xProgress = (this.m_bounds[0][xIndex].value - p1x) / dx
  4787. }
  4788. yIndex += sy >= 0 ? 1 : -1;
  4789. if(yIndex < 0 || yIndex >= this.m_proxyCount * 2) {
  4790. break
  4791. }
  4792. if(sy != 0) {
  4793. yProgress = (this.m_bounds[1][yIndex].value - p1y) / dy
  4794. }
  4795. for(;;) {
  4796. if(sy == 0 || sx != 0 && xProgress < yProgress) {
  4797. if(xProgress > subInput.maxFraction) {
  4798. break
  4799. }
  4800. if(sx > 0 ? this.m_bounds[0][xIndex].IsLower() : this.m_bounds[0][xIndex].IsUpper()) {
  4801. proxy = this.m_bounds[0][xIndex].proxy;
  4802. if(sy >= 0) {
  4803. if(proxy.lowerBounds[1] <= yIndex - 1 && proxy.upperBounds[1] >= yIndex) {
  4804. subInput.maxFraction = callback(proxy, subInput)
  4805. }
  4806. }else {
  4807. if(proxy.lowerBounds[1] <= yIndex && proxy.upperBounds[1] >= yIndex + 1) {
  4808. subInput.maxFraction = callback(proxy, subInput)
  4809. }
  4810. }
  4811. }
  4812. if(subInput.maxFraction == 0) {
  4813. break
  4814. }
  4815. if(sx > 0) {
  4816. xIndex++;
  4817. if(xIndex == this.m_proxyCount * 2) {
  4818. break
  4819. }
  4820. }else {
  4821. xIndex--;
  4822. if(xIndex < 0) {
  4823. break
  4824. }
  4825. }
  4826. xProgress = (this.m_bounds[0][xIndex].value - p1x) / dx
  4827. }else {
  4828. if(yProgress > subInput.maxFraction) {
  4829. break
  4830. }
  4831. if(sy > 0 ? this.m_bounds[1][yIndex].IsLower() : this.m_bounds[1][yIndex].IsUpper()) {
  4832. proxy = this.m_bounds[1][yIndex].proxy;
  4833. if(sx >= 0) {
  4834. if(proxy.lowerBounds[0] <= xIndex - 1 && proxy.upperBounds[0] >= xIndex) {
  4835. subInput.maxFraction = callback(proxy, subInput)
  4836. }
  4837. }else {
  4838. if(proxy.lowerBounds[0] <= xIndex && proxy.upperBounds[0] >= xIndex + 1) {
  4839. subInput.maxFraction = callback(proxy, subInput)
  4840. }
  4841. }
  4842. }
  4843. if(subInput.maxFraction == 0) {
  4844. break
  4845. }
  4846. if(sy > 0) {
  4847. yIndex++;
  4848. if(yIndex == this.m_proxyCount * 2) {
  4849. break
  4850. }
  4851. }else {
  4852. yIndex--;
  4853. if(yIndex < 0) {
  4854. break
  4855. }
  4856. }
  4857. yProgress = (this.m_bounds[1][yIndex].value - p1y) / dy
  4858. }
  4859. }
  4860. break
  4861. }
  4862. this.m_queryResultCount = 0;
  4863. this.IncrementTimeStamp();
  4864. return
  4865. };
  4866. b2BroadPhase.prototype.TestOverlapBound = function(b, p) {
  4867. for(var axis = 0;axis < 2;++axis) {
  4868. var bounds = this.m_bounds[axis];
  4869. var bound = bounds[p.upperBounds[axis]];
  4870. if(b.lowerValues[axis] > bound.value) {
  4871. return false
  4872. }
  4873. bound = bounds[p.lowerBounds[axis]];
  4874. if(b.upperValues[axis] < bound.value) {
  4875. return false
  4876. }
  4877. }
  4878. return true
  4879. };
  4880. b2BroadPhase.prototype.m_pairManager = new b2PairManager;
  4881. b2BroadPhase.prototype.m_proxyPool = new Array;
  4882. b2BroadPhase.prototype.m_freeProxy = null;
  4883. b2BroadPhase.prototype.m_bounds = null;
  4884. b2BroadPhase.prototype.m_querySortKeys = new Array;
  4885. b2BroadPhase.prototype.m_queryResults = new Array;
  4886. b2BroadPhase.prototype.m_queryResultCount = 0;
  4887. b2BroadPhase.prototype.m_worldAABB = null;
  4888. b2BroadPhase.prototype.m_quantizationFactor = new b2Vec2;
  4889. b2BroadPhase.prototype.m_proxyCount = 0;
  4890. b2BroadPhase.prototype.m_timeStamp = 0;var b2Manifold = function() {
  4891. this.__varz();
  4892. this.__constructor.apply(this, arguments)
  4893. };
  4894. b2Manifold.prototype.__constructor = function() {
  4895. this.m_points = new Array(b2Settings.b2_maxManifoldPoints);
  4896. for(var i = 0;i < b2Settings.b2_maxManifoldPoints;i++) {
  4897. this.m_points[i] = new b2ManifoldPoint
  4898. }
  4899. this.m_localPlaneNormal = new b2Vec2;
  4900. this.m_localPoint = new b2Vec2
  4901. };
  4902. b2Manifold.prototype.__varz = function() {
  4903. };
  4904. b2Manifold.e_circles = 1;
  4905. b2Manifold.e_faceA = 2;
  4906. b2Manifold.e_faceB = 4;
  4907. b2Manifold.prototype.Reset = function() {
  4908. for(var i = 0;i < b2Settings.b2_maxManifoldPoints;i++) {
  4909. this.m_points[i].Reset()
  4910. }
  4911. this.m_localPlaneNormal.SetZero();
  4912. this.m_localPoint.SetZero();
  4913. this.m_type = 0;
  4914. this.m_pointCount = 0
  4915. };
  4916. b2Manifold.prototype.Set = function(m) {
  4917. this.m_pointCount = m.m_pointCount;
  4918. for(var i = 0;i < b2Settings.b2_maxManifoldPoints;i++) {
  4919. this.m_points[i].Set(m.m_points[i])
  4920. }
  4921. this.m_localPlaneNormal.SetV(m.m_localPlaneNormal);
  4922. this.m_localPoint.SetV(m.m_localPoint);
  4923. this.m_type = m.m_type
  4924. };
  4925. b2Manifold.prototype.Copy = function() {
  4926. var copy = new b2Manifold;
  4927. copy.Set(this);
  4928. return copy
  4929. };
  4930. b2Manifold.prototype.m_points = null;
  4931. b2Manifold.prototype.m_localPlaneNormal = null;
  4932. b2Manifold.prototype.m_localPoint = null;
  4933. b2Manifold.prototype.m_type = 0;
  4934. b2Manifold.prototype.m_pointCount = 0;var b2CircleShape = function() {
  4935. b2Shape.prototype.__varz.call(this);
  4936. this.__varz();
  4937. this.__constructor.apply(this, arguments)
  4938. };
  4939. extend(b2CircleShape.prototype, b2Shape.prototype);
  4940. b2CircleShape.prototype._super = b2Shape.prototype;
  4941. b2CircleShape.prototype.__constructor = function(radius) {
  4942. this._super.__constructor.apply(this, []);
  4943. this.m_type = b2Shape.e_circleShape;
  4944. this.m_radius = radius
  4945. };
  4946. b2CircleShape.prototype.__varz = function() {
  4947. this.m_p = new b2Vec2
  4948. };
  4949. b2CircleShape.prototype.Copy = function() {
  4950. var s = new b2CircleShape;
  4951. s.Set(this);
  4952. return s
  4953. };
  4954. b2CircleShape.prototype.Set = function(other) {
  4955. this._super.Set.apply(this, [other]);
  4956. if(isInstanceOf(other, b2CircleShape)) {
  4957. var other2 = other;
  4958. this.m_p.SetV(other2.m_p)
  4959. }
  4960. };
  4961. b2CircleShape.prototype.TestPoint = function(transform, p) {
  4962. var tMat = transform.R;
  4963. var dX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
  4964. var dY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
  4965. dX = p.x - dX;
  4966. dY = p.y - dY;
  4967. return dX * dX + dY * dY <= this.m_radius * this.m_radius
  4968. };
  4969. b2CircleShape.prototype.RayCast = function(output, input, transform) {
  4970. var tMat = transform.R;
  4971. var positionX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
  4972. var positionY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
  4973. var sX = input.p1.x - positionX;
  4974. var sY = input.p1.y - positionY;
  4975. var b = sX * sX + sY * sY - this.m_radius * this.m_radius;
  4976. var rX = input.p2.x - input.p1.x;
  4977. var rY = input.p2.y - input.p1.y;
  4978. var c = sX * rX + sY * rY;
  4979. var rr = rX * rX + rY * rY;
  4980. var sigma = c * c - rr * b;
  4981. if(sigma < 0 || rr < Number.MIN_VALUE) {
  4982. return false
  4983. }
  4984. var a = -(c + Math.sqrt(sigma));
  4985. if(0 <= a && a <= input.maxFraction * rr) {
  4986. a /= rr;
  4987. output.fraction = a;
  4988. output.normal.x = sX + a * rX;
  4989. output.normal.y = sY + a * rY;
  4990. output.normal.Normalize();
  4991. return true
  4992. }
  4993. return false
  4994. };
  4995. b2CircleShape.prototype.ComputeAABB = function(aabb, transform) {
  4996. var tMat = transform.R;
  4997. var pX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
  4998. var pY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
  4999. aabb.lowerBound.Set(pX - this.m_radius, pY - this.m_radius);
  5000. aabb.upperBound.Set(pX + this.m_radius, pY + this.m_radius)
  5001. };
  5002. b2CircleShape.prototype.ComputeMass = function(massData, density) {
  5003. massData.mass = density * b2Settings.b2_pi * this.m_radius * this.m_radius;
  5004. massData.center.SetV(this.m_p);
  5005. 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))
  5006. };
  5007. b2CircleShape.prototype.ComputeSubmergedArea = function(normal, offset, xf, c) {
  5008. var p = b2Math.MulX(xf, this.m_p);
  5009. var l = -(b2Math.Dot(normal, p) - offset);
  5010. if(l < -this.m_radius + Number.MIN_VALUE) {
  5011. return 0
  5012. }
  5013. if(l > this.m_radius) {
  5014. c.SetV(p);
  5015. return Math.PI * this.m_radius * this.m_radius
  5016. }
  5017. var r2 = this.m_radius * this.m_radius;
  5018. var l2 = l * l;
  5019. var area = r2 * (Math.asin(l / this.m_radius) + Math.PI / 2) + l * Math.sqrt(r2 - l2);
  5020. var com = -2 / 3 * Math.pow(r2 - l2, 1.5) / area;
  5021. c.x = p.x + normal.x * com;
  5022. c.y = p.y + normal.y * com;
  5023. return area
  5024. };
  5025. b2CircleShape.prototype.GetLocalPosition = function() {
  5026. return this.m_p
  5027. };
  5028. b2CircleShape.prototype.SetLocalPosition = function(position) {
  5029. this.m_p.SetV(position)
  5030. };
  5031. b2CircleShape.prototype.GetRadius = function() {
  5032. return this.m_radius
  5033. };
  5034. b2CircleShape.prototype.SetRadius = function(radius) {
  5035. this.m_radius = radius
  5036. };
  5037. b2CircleShape.prototype.m_p = new b2Vec2;var b2Joint = function() {
  5038. this.__varz();
  5039. this.__constructor.apply(this, arguments)
  5040. };
  5041. b2Joint.prototype.__constructor = function(def) {
  5042. b2Settings.b2Assert(def.bodyA != def.bodyB);
  5043. this.m_type = def.type;
  5044. this.m_prev = null;
  5045. this.m_next = null;
  5046. this.m_bodyA = def.bodyA;
  5047. this.m_bodyB = def.bodyB;
  5048. this.m_collideConnected = def.collideConnected;
  5049. this.m_islandFlag = false;
  5050. this.m_userData = def.userData
  5051. };
  5052. b2Joint.prototype.__varz = function() {
  5053. this.m_edgeA = new b2JointEdge;
  5054. this.m_edgeB = new b2JointEdge;
  5055. this.m_localCenterA = new b2Vec2;
  5056. this.m_localCenterB = new b2Vec2
  5057. };
  5058. b2Joint.Create = function(def, allocator) {
  5059. var joint = null;
  5060. switch(def.type) {
  5061. case b2Joint.e_distanceJoint:
  5062. joint = new b2DistanceJoint(def);
  5063. break;
  5064. case b2Joint.e_mouseJoint:
  5065. joint = new b2MouseJoint(def);
  5066. break;
  5067. case b2Joint.e_prismaticJoint:
  5068. joint = new b2PrismaticJoint(def);
  5069. break;
  5070. case b2Joint.e_revoluteJoint:
  5071. joint = new b2RevoluteJoint(def);
  5072. break;
  5073. case b2Joint.e_pulleyJoint:
  5074. joint = new b2PulleyJoint(def);
  5075. break;
  5076. case b2Joint.e_gearJoint:
  5077. joint = new b2GearJoint(def);
  5078. break;
  5079. case b2Joint.e_lineJoint:
  5080. joint = new b2LineJoint(def);
  5081. break;
  5082. case b2Joint.e_weldJoint:
  5083. joint = new b2WeldJoint(def);
  5084. break;
  5085. case b2Joint.e_frictionJoint:
  5086. joint = new b2FrictionJoint(def);
  5087. break;
  5088. default:
  5089. break
  5090. }
  5091. return joint
  5092. };
  5093. b2Joint.Destroy = function(joint, allocator) {
  5094. };
  5095. b2Joint.e_unknownJoint = 0;
  5096. b2Joint.e_revoluteJoint = 1;
  5097. b2Joint.e_prismaticJoint = 2;
  5098. b2Joint.e_distanceJoint = 3;
  5099. b2Joint.e_pulleyJoint = 4;
  5100. b2Joint.e_mouseJoint = 5;
  5101. b2Joint.e_gearJoint = 6;
  5102. b2Joint.e_lineJoint = 7;
  5103. b2Joint.e_weldJoint = 8;
  5104. b2Joint.e_frictionJoint = 9;
  5105. b2Joint.e_inactiveLimit = 0;
  5106. b2Joint.e_atLowerLimit = 1;
  5107. b2Joint.e_atUpperLimit = 2;
  5108. b2Joint.e_equalLimits = 3;
  5109. b2Joint.prototype.InitVelocityConstraints = function(step) {
  5110. };
  5111. b2Joint.prototype.SolveVelocityConstraints = function(step) {
  5112. };
  5113. b2Joint.prototype.FinalizeVelocityConstraints = function() {
  5114. };
  5115. b2Joint.prototype.SolvePositionConstraints = function(baumgarte) {
  5116. return false
  5117. };
  5118. b2Joint.prototype.GetType = function() {
  5119. return this.m_type
  5120. };
  5121. b2Joint.prototype.GetAnchorA = function() {
  5122. return null
  5123. };
  5124. b2Joint.prototype.GetAnchorB = function() {
  5125. return null
  5126. };
  5127. b2Joint.prototype.GetReactionForce = function(inv_dt) {
  5128. return null
  5129. };
  5130. b2Joint.prototype.GetReactionTorque = function(inv_dt) {
  5131. return 0
  5132. };
  5133. b2Joint.prototype.GetBodyA = function() {
  5134. return this.m_bodyA
  5135. };
  5136. b2Joint.prototype.GetBodyB = function() {
  5137. return this.m_bodyB
  5138. };
  5139. b2Joint.prototype.GetNext = function() {
  5140. return this.m_next
  5141. };
  5142. b2Joint.prototype.GetUserData = function() {
  5143. return this.m_userData
  5144. };
  5145. b2Joint.prototype.SetUserData = function(data) {
  5146. this.m_userData = data
  5147. };
  5148. b2Joint.prototype.IsActive = function() {
  5149. return this.m_bodyA.IsActive() && this.m_bodyB.IsActive()
  5150. };
  5151. b2Joint.prototype.m_type = 0;
  5152. b2Joint.prototype.m_prev = null;
  5153. b2Joint.prototype.m_next = null;
  5154. b2Joint.prototype.m_edgeA = new b2JointEdge;
  5155. b2Joint.prototype.m_edgeB = new b2JointEdge;
  5156. b2Joint.prototype.m_bodyA = null;
  5157. b2Joint.prototype.m_bodyB = null;
  5158. b2Joint.prototype.m_islandFlag = null;
  5159. b2Joint.prototype.m_collideConnected = null;
  5160. b2Joint.prototype.m_userData = null;
  5161. b2Joint.prototype.m_localCenterA = new b2Vec2;
  5162. b2Joint.prototype.m_localCenterB = new b2Vec2;
  5163. b2Joint.prototype.m_invMassA = null;
  5164. b2Joint.prototype.m_invMassB = null;
  5165. b2Joint.prototype.m_invIA = null;
  5166. b2Joint.prototype.m_invIB = null;var b2LineJoint = function() {
  5167. b2Joint.prototype.__varz.call(this);
  5168. this.__varz();
  5169. this.__constructor.apply(this, arguments)
  5170. };
  5171. extend(b2LineJoint.prototype, b2Joint.prototype);
  5172. b2LineJoint.prototype._super = b2Joint.prototype;
  5173. b2LineJoint.prototype.__constructor = function(def) {
  5174. this._super.__constructor.apply(this, [def]);
  5175. var tMat;
  5176. var tX;
  5177. var tY;
  5178. this.m_localAnchor1.SetV(def.localAnchorA);
  5179. this.m_localAnchor2.SetV(def.localAnchorB);
  5180. this.m_localXAxis1.SetV(def.localAxisA);
  5181. this.m_localYAxis1.x = -this.m_localXAxis1.y;
  5182. this.m_localYAxis1.y = this.m_localXAxis1.x;
  5183. this.m_impulse.SetZero();
  5184. this.m_motorMass = 0;
  5185. this.m_motorImpulse = 0;
  5186. this.m_lowerTranslation = def.lowerTranslation;
  5187. this.m_upperTranslation = def.upperTranslation;
  5188. this.m_maxMotorForce = def.maxMotorForce;
  5189. this.m_motorSpeed = def.motorSpeed;
  5190. this.m_enableLimit = def.enableLimit;
  5191. this.m_enableMotor = def.enableMotor;
  5192. this.m_limitState = b2Joint.e_inactiveLimit;
  5193. this.m_axis.SetZero();
  5194. this.m_perp.SetZero()
  5195. };
  5196. b2LineJoint.prototype.__varz = function() {
  5197. this.m_localAnchor1 = new b2Vec2;
  5198. this.m_localAnchor2 = new b2Vec2;
  5199. this.m_localXAxis1 = new b2Vec2;
  5200. this.m_localYAxis1 = new b2Vec2;
  5201. this.m_axis = new b2Vec2;
  5202. this.m_perp = new b2Vec2;
  5203. this.m_K = new b2Mat22;
  5204. this.m_impulse = new b2Vec2
  5205. };
  5206. b2LineJoint.prototype.InitVelocityConstraints = function(step) {
  5207. var bA = this.m_bodyA;
  5208. var bB = this.m_bodyB;
  5209. var tMat;
  5210. var tX;
  5211. this.m_localCenterA.SetV(bA.GetLocalCenter());
  5212. this.m_localCenterB.SetV(bB.GetLocalCenter());
  5213. var xf1 = bA.GetTransform();
  5214. var xf2 = bB.GetTransform();
  5215. tMat = bA.m_xf.R;
  5216. var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
  5217. var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
  5218. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  5219. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  5220. r1X = tX;
  5221. tMat = bB.m_xf.R;
  5222. var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
  5223. var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
  5224. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  5225. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  5226. r2X = tX;
  5227. var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  5228. var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  5229. this.m_invMassA = bA.m_invMass;
  5230. this.m_invMassB = bB.m_invMass;
  5231. this.m_invIA = bA.m_invI;
  5232. this.m_invIB = bB.m_invI;
  5233. this.m_axis.SetV(b2Math.MulMV(xf1.R, this.m_localXAxis1));
  5234. this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
  5235. this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
  5236. 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;
  5237. this.m_motorMass = this.m_motorMass > Number.MIN_VALUE ? 1 / this.m_motorMass : 0;
  5238. this.m_perp.SetV(b2Math.MulMV(xf1.R, this.m_localYAxis1));
  5239. this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
  5240. this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
  5241. var m1 = this.m_invMassA;
  5242. var m2 = this.m_invMassB;
  5243. var i1 = this.m_invIA;
  5244. var i2 = this.m_invIB;
  5245. this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  5246. this.m_K.col1.y = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
  5247. this.m_K.col2.x = this.m_K.col1.y;
  5248. this.m_K.col2.y = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
  5249. if(this.m_enableLimit) {
  5250. var jointTransition = this.m_axis.x * dX + this.m_axis.y * dY;
  5251. if(b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2 * b2Settings.b2_linearSlop) {
  5252. this.m_limitState = b2Joint.e_equalLimits
  5253. }else {
  5254. if(jointTransition <= this.m_lowerTranslation) {
  5255. if(this.m_limitState != b2Joint.e_atLowerLimit) {
  5256. this.m_limitState = b2Joint.e_atLowerLimit;
  5257. this.m_impulse.y = 0
  5258. }
  5259. }else {
  5260. if(jointTransition >= this.m_upperTranslation) {
  5261. if(this.m_limitState != b2Joint.e_atUpperLimit) {
  5262. this.m_limitState = b2Joint.e_atUpperLimit;
  5263. this.m_impulse.y = 0
  5264. }
  5265. }else {
  5266. this.m_limitState = b2Joint.e_inactiveLimit;
  5267. this.m_impulse.y = 0
  5268. }
  5269. }
  5270. }
  5271. }else {
  5272. this.m_limitState = b2Joint.e_inactiveLimit
  5273. }
  5274. if(this.m_enableMotor == false) {
  5275. this.m_motorImpulse = 0
  5276. }
  5277. if(step.warmStarting) {
  5278. this.m_impulse.x *= step.dtRatio;
  5279. this.m_impulse.y *= step.dtRatio;
  5280. this.m_motorImpulse *= step.dtRatio;
  5281. var PX = this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.x;
  5282. var PY = this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.y;
  5283. var L1 = this.m_impulse.x * this.m_s1 + (this.m_motorImpulse + this.m_impulse.y) * this.m_a1;
  5284. var L2 = this.m_impulse.x * this.m_s2 + (this.m_motorImpulse + this.m_impulse.y) * this.m_a2;
  5285. bA.m_linearVelocity.x -= this.m_invMassA * PX;
  5286. bA.m_linearVelocity.y -= this.m_invMassA * PY;
  5287. bA.m_angularVelocity -= this.m_invIA * L1;
  5288. bB.m_linearVelocity.x += this.m_invMassB * PX;
  5289. bB.m_linearVelocity.y += this.m_invMassB * PY;
  5290. bB.m_angularVelocity += this.m_invIB * L2
  5291. }else {
  5292. this.m_impulse.SetZero();
  5293. this.m_motorImpulse = 0
  5294. }
  5295. };
  5296. b2LineJoint.prototype.SolveVelocityConstraints = function(step) {
  5297. var bA = this.m_bodyA;
  5298. var bB = this.m_bodyB;
  5299. var v1 = bA.m_linearVelocity;
  5300. var w1 = bA.m_angularVelocity;
  5301. var v2 = bB.m_linearVelocity;
  5302. var w2 = bB.m_angularVelocity;
  5303. var PX;
  5304. var PY;
  5305. var L1;
  5306. var L2;
  5307. if(this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
  5308. 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;
  5309. var impulse = this.m_motorMass * (this.m_motorSpeed - Cdot);
  5310. var oldImpulse = this.m_motorImpulse;
  5311. var maxImpulse = step.dt * this.m_maxMotorForce;
  5312. this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, -maxImpulse, maxImpulse);
  5313. impulse = this.m_motorImpulse - oldImpulse;
  5314. PX = impulse * this.m_axis.x;
  5315. PY = impulse * this.m_axis.y;
  5316. L1 = impulse * this.m_a1;
  5317. L2 = impulse * this.m_a2;
  5318. v1.x -= this.m_invMassA * PX;
  5319. v1.y -= this.m_invMassA * PY;
  5320. w1 -= this.m_invIA * L1;
  5321. v2.x += this.m_invMassB * PX;
  5322. v2.y += this.m_invMassB * PY;
  5323. w2 += this.m_invIB * L2
  5324. }
  5325. 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;
  5326. if(this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  5327. 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;
  5328. var f1 = this.m_impulse.Copy();
  5329. var df = this.m_K.Solve(new b2Vec2, -Cdot1, -Cdot2);
  5330. this.m_impulse.Add(df);
  5331. if(this.m_limitState == b2Joint.e_atLowerLimit) {
  5332. this.m_impulse.y = b2Math.Max(this.m_impulse.y, 0)
  5333. }else {
  5334. if(this.m_limitState == b2Joint.e_atUpperLimit) {
  5335. this.m_impulse.y = b2Math.Min(this.m_impulse.y, 0)
  5336. }
  5337. }
  5338. var b = -Cdot1 - (this.m_impulse.y - f1.y) * this.m_K.col2.x;
  5339. var f2r;
  5340. if(this.m_K.col1.x != 0) {
  5341. f2r = b / this.m_K.col1.x + f1.x
  5342. }else {
  5343. f2r = f1.x
  5344. }
  5345. this.m_impulse.x = f2r;
  5346. df.x = this.m_impulse.x - f1.x;
  5347. df.y = this.m_impulse.y - f1.y;
  5348. PX = df.x * this.m_perp.x + df.y * this.m_axis.x;
  5349. PY = df.x * this.m_perp.y + df.y * this.m_axis.y;
  5350. L1 = df.x * this.m_s1 + df.y * this.m_a1;
  5351. L2 = df.x * this.m_s2 + df.y * this.m_a2;
  5352. v1.x -= this.m_invMassA * PX;
  5353. v1.y -= this.m_invMassA * PY;
  5354. w1 -= this.m_invIA * L1;
  5355. v2.x += this.m_invMassB * PX;
  5356. v2.y += this.m_invMassB * PY;
  5357. w2 += this.m_invIB * L2
  5358. }else {
  5359. var df2;
  5360. if(this.m_K.col1.x != 0) {
  5361. df2 = -Cdot1 / this.m_K.col1.x
  5362. }else {
  5363. df2 = 0
  5364. }
  5365. this.m_impulse.x += df2;
  5366. PX = df2 * this.m_perp.x;
  5367. PY = df2 * this.m_perp.y;
  5368. L1 = df2 * this.m_s1;
  5369. L2 = df2 * this.m_s2;
  5370. v1.x -= this.m_invMassA * PX;
  5371. v1.y -= this.m_invMassA * PY;
  5372. w1 -= this.m_invIA * L1;
  5373. v2.x += this.m_invMassB * PX;
  5374. v2.y += this.m_invMassB * PY;
  5375. w2 += this.m_invIB * L2
  5376. }
  5377. bA.m_linearVelocity.SetV(v1);
  5378. bA.m_angularVelocity = w1;
  5379. bB.m_linearVelocity.SetV(v2);
  5380. bB.m_angularVelocity = w2
  5381. };
  5382. b2LineJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  5383. var limitC;
  5384. var oldLimitImpulse;
  5385. var bA = this.m_bodyA;
  5386. var bB = this.m_bodyB;
  5387. var c1 = bA.m_sweep.c;
  5388. var a1 = bA.m_sweep.a;
  5389. var c2 = bB.m_sweep.c;
  5390. var a2 = bB.m_sweep.a;
  5391. var tMat;
  5392. var tX;
  5393. var m1;
  5394. var m2;
  5395. var i1;
  5396. var i2;
  5397. var linearError = 0;
  5398. var angularError = 0;
  5399. var active = false;
  5400. var C2 = 0;
  5401. var R1 = b2Mat22.FromAngle(a1);
  5402. var R2 = b2Mat22.FromAngle(a2);
  5403. tMat = R1;
  5404. var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
  5405. var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
  5406. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  5407. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  5408. r1X = tX;
  5409. tMat = R2;
  5410. var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
  5411. var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
  5412. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  5413. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  5414. r2X = tX;
  5415. var dX = c2.x + r2X - c1.x - r1X;
  5416. var dY = c2.y + r2Y - c1.y - r1Y;
  5417. if(this.m_enableLimit) {
  5418. this.m_axis = b2Math.MulMV(R1, this.m_localXAxis1);
  5419. this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
  5420. this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
  5421. var translation = this.m_axis.x * dX + this.m_axis.y * dY;
  5422. if(b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2 * b2Settings.b2_linearSlop) {
  5423. C2 = b2Math.Clamp(translation, -b2Settings.b2_maxLinearCorrection, b2Settings.b2_maxLinearCorrection);
  5424. linearError = b2Math.Abs(translation);
  5425. active = true
  5426. }else {
  5427. if(translation <= this.m_lowerTranslation) {
  5428. C2 = b2Math.Clamp(translation - this.m_lowerTranslation + b2Settings.b2_linearSlop, -b2Settings.b2_maxLinearCorrection, 0);
  5429. linearError = this.m_lowerTranslation - translation;
  5430. active = true
  5431. }else {
  5432. if(translation >= this.m_upperTranslation) {
  5433. C2 = b2Math.Clamp(translation - this.m_upperTranslation + b2Settings.b2_linearSlop, 0, b2Settings.b2_maxLinearCorrection);
  5434. linearError = translation - this.m_upperTranslation;
  5435. active = true
  5436. }
  5437. }
  5438. }
  5439. }
  5440. this.m_perp = b2Math.MulMV(R1, this.m_localYAxis1);
  5441. this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
  5442. this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
  5443. var impulse = new b2Vec2;
  5444. var C1 = this.m_perp.x * dX + this.m_perp.y * dY;
  5445. linearError = b2Math.Max(linearError, b2Math.Abs(C1));
  5446. angularError = 0;
  5447. if(active) {
  5448. m1 = this.m_invMassA;
  5449. m2 = this.m_invMassB;
  5450. i1 = this.m_invIA;
  5451. i2 = this.m_invIB;
  5452. this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  5453. this.m_K.col1.y = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
  5454. this.m_K.col2.x = this.m_K.col1.y;
  5455. this.m_K.col2.y = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
  5456. this.m_K.Solve(impulse, -C1, -C2)
  5457. }else {
  5458. m1 = this.m_invMassA;
  5459. m2 = this.m_invMassB;
  5460. i1 = this.m_invIA;
  5461. i2 = this.m_invIB;
  5462. var k11 = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  5463. var impulse1;
  5464. if(k11 != 0) {
  5465. impulse1 = -C1 / k11
  5466. }else {
  5467. impulse1 = 0
  5468. }
  5469. impulse.x = impulse1;
  5470. impulse.y = 0
  5471. }
  5472. var PX = impulse.x * this.m_perp.x + impulse.y * this.m_axis.x;
  5473. var PY = impulse.x * this.m_perp.y + impulse.y * this.m_axis.y;
  5474. var L1 = impulse.x * this.m_s1 + impulse.y * this.m_a1;
  5475. var L2 = impulse.x * this.m_s2 + impulse.y * this.m_a2;
  5476. c1.x -= this.m_invMassA * PX;
  5477. c1.y -= this.m_invMassA * PY;
  5478. a1 -= this.m_invIA * L1;
  5479. c2.x += this.m_invMassB * PX;
  5480. c2.y += this.m_invMassB * PY;
  5481. a2 += this.m_invIB * L2;
  5482. bA.m_sweep.a = a1;
  5483. bB.m_sweep.a = a2;
  5484. bA.SynchronizeTransform();
  5485. bB.SynchronizeTransform();
  5486. return linearError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop
  5487. };
  5488. b2LineJoint.prototype.GetAnchorA = function() {
  5489. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1)
  5490. };
  5491. b2LineJoint.prototype.GetAnchorB = function() {
  5492. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2)
  5493. };
  5494. b2LineJoint.prototype.GetReactionForce = function(inv_dt) {
  5495. 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))
  5496. };
  5497. b2LineJoint.prototype.GetReactionTorque = function(inv_dt) {
  5498. return inv_dt * this.m_impulse.y
  5499. };
  5500. b2LineJoint.prototype.GetJointTranslation = function() {
  5501. var bA = this.m_bodyA;
  5502. var bB = this.m_bodyB;
  5503. var tMat;
  5504. var p1 = bA.GetWorldPoint(this.m_localAnchor1);
  5505. var p2 = bB.GetWorldPoint(this.m_localAnchor2);
  5506. var dX = p2.x - p1.x;
  5507. var dY = p2.y - p1.y;
  5508. var axis = bA.GetWorldVector(this.m_localXAxis1);
  5509. var translation = axis.x * dX + axis.y * dY;
  5510. return translation
  5511. };
  5512. b2LineJoint.prototype.GetJointSpeed = function() {
  5513. var bA = this.m_bodyA;
  5514. var bB = this.m_bodyB;
  5515. var tMat;
  5516. tMat = bA.m_xf.R;
  5517. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  5518. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  5519. var tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  5520. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  5521. r1X = tX;
  5522. tMat = bB.m_xf.R;
  5523. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  5524. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  5525. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  5526. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  5527. r2X = tX;
  5528. var p1X = bA.m_sweep.c.x + r1X;
  5529. var p1Y = bA.m_sweep.c.y + r1Y;
  5530. var p2X = bB.m_sweep.c.x + r2X;
  5531. var p2Y = bB.m_sweep.c.y + r2Y;
  5532. var dX = p2X - p1X;
  5533. var dY = p2Y - p1Y;
  5534. var axis = bA.GetWorldVector(this.m_localXAxis1);
  5535. var v1 = bA.m_linearVelocity;
  5536. var v2 = bB.m_linearVelocity;
  5537. var w1 = bA.m_angularVelocity;
  5538. var w2 = bB.m_angularVelocity;
  5539. 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));
  5540. return speed
  5541. };
  5542. b2LineJoint.prototype.IsLimitEnabled = function() {
  5543. return this.m_enableLimit
  5544. };
  5545. b2LineJoint.prototype.EnableLimit = function(flag) {
  5546. this.m_bodyA.SetAwake(true);
  5547. this.m_bodyB.SetAwake(true);
  5548. this.m_enableLimit = flag
  5549. };
  5550. b2LineJoint.prototype.GetLowerLimit = function() {
  5551. return this.m_lowerTranslation
  5552. };
  5553. b2LineJoint.prototype.GetUpperLimit = function() {
  5554. return this.m_upperTranslation
  5555. };
  5556. b2LineJoint.prototype.SetLimits = function(lower, upper) {
  5557. this.m_bodyA.SetAwake(true);
  5558. this.m_bodyB.SetAwake(true);
  5559. this.m_lowerTranslation = lower;
  5560. this.m_upperTranslation = upper
  5561. };
  5562. b2LineJoint.prototype.IsMotorEnabled = function() {
  5563. return this.m_enableMotor
  5564. };
  5565. b2LineJoint.prototype.EnableMotor = function(flag) {
  5566. this.m_bodyA.SetAwake(true);
  5567. this.m_bodyB.SetAwake(true);
  5568. this.m_enableMotor = flag
  5569. };
  5570. b2LineJoint.prototype.SetMotorSpeed = function(speed) {
  5571. this.m_bodyA.SetAwake(true);
  5572. this.m_bodyB.SetAwake(true);
  5573. this.m_motorSpeed = speed
  5574. };
  5575. b2LineJoint.prototype.GetMotorSpeed = function() {
  5576. return this.m_motorSpeed
  5577. };
  5578. b2LineJoint.prototype.SetMaxMotorForce = function(force) {
  5579. this.m_bodyA.SetAwake(true);
  5580. this.m_bodyB.SetAwake(true);
  5581. this.m_maxMotorForce = force
  5582. };
  5583. b2LineJoint.prototype.GetMaxMotorForce = function() {
  5584. return this.m_maxMotorForce
  5585. };
  5586. b2LineJoint.prototype.GetMotorForce = function() {
  5587. return this.m_motorImpulse
  5588. };
  5589. b2LineJoint.prototype.m_localAnchor1 = new b2Vec2;
  5590. b2LineJoint.prototype.m_localAnchor2 = new b2Vec2;
  5591. b2LineJoint.prototype.m_localXAxis1 = new b2Vec2;
  5592. b2LineJoint.prototype.m_localYAxis1 = new b2Vec2;
  5593. b2LineJoint.prototype.m_axis = new b2Vec2;
  5594. b2LineJoint.prototype.m_perp = new b2Vec2;
  5595. b2LineJoint.prototype.m_s1 = null;
  5596. b2LineJoint.prototype.m_s2 = null;
  5597. b2LineJoint.prototype.m_a1 = null;
  5598. b2LineJoint.prototype.m_a2 = null;
  5599. b2LineJoint.prototype.m_K = new b2Mat22;
  5600. b2LineJoint.prototype.m_impulse = new b2Vec2;
  5601. b2LineJoint.prototype.m_motorMass = null;
  5602. b2LineJoint.prototype.m_motorImpulse = null;
  5603. b2LineJoint.prototype.m_lowerTranslation = null;
  5604. b2LineJoint.prototype.m_upperTranslation = null;
  5605. b2LineJoint.prototype.m_maxMotorForce = null;
  5606. b2LineJoint.prototype.m_motorSpeed = null;
  5607. b2LineJoint.prototype.m_enableLimit = null;
  5608. b2LineJoint.prototype.m_enableMotor = null;
  5609. b2LineJoint.prototype.m_limitState = 0;var b2ContactSolver = function() {
  5610. this.__varz();
  5611. this.__constructor.apply(this, arguments)
  5612. };
  5613. b2ContactSolver.prototype.__constructor = function() {
  5614. };
  5615. b2ContactSolver.prototype.__varz = function() {
  5616. this.m_step = new b2TimeStep;
  5617. this.m_constraints = new Array
  5618. };
  5619. b2ContactSolver.s_worldManifold = new b2WorldManifold;
  5620. b2ContactSolver.s_psm = new b2PositionSolverManifold;
  5621. b2ContactSolver.prototype.Initialize = function(step, contacts, contactCount, allocator) {
  5622. var contact;
  5623. this.m_step.Set(step);
  5624. this.m_allocator = allocator;
  5625. var i = 0;
  5626. var tVec;
  5627. var tMat;
  5628. this.m_constraintCount = contactCount;
  5629. while(this.m_constraints.length < this.m_constraintCount) {
  5630. this.m_constraints[this.m_constraints.length] = new b2ContactConstraint
  5631. }
  5632. for(i = 0;i < contactCount;++i) {
  5633. contact = contacts[i];
  5634. var fixtureA = contact.m_fixtureA;
  5635. var fixtureB = contact.m_fixtureB;
  5636. var shapeA = fixtureA.m_shape;
  5637. var shapeB = fixtureB.m_shape;
  5638. var radiusA = shapeA.m_radius;
  5639. var radiusB = shapeB.m_radius;
  5640. var bodyA = fixtureA.m_body;
  5641. var bodyB = fixtureB.m_body;
  5642. var manifold = contact.GetManifold();
  5643. var friction = b2Settings.b2MixFriction(fixtureA.GetFriction(), fixtureB.GetFriction());
  5644. var restitution = b2Settings.b2MixRestitution(fixtureA.GetRestitution(), fixtureB.GetRestitution());
  5645. var vAX = bodyA.m_linearVelocity.x;
  5646. var vAY = bodyA.m_linearVelocity.y;
  5647. var vBX = bodyB.m_linearVelocity.x;
  5648. var vBY = bodyB.m_linearVelocity.y;
  5649. var wA = bodyA.m_angularVelocity;
  5650. var wB = bodyB.m_angularVelocity;
  5651. b2Settings.b2Assert(manifold.m_pointCount > 0);
  5652. b2ContactSolver.s_worldManifold.Initialize(manifold, bodyA.m_xf, radiusA, bodyB.m_xf, radiusB);
  5653. var normalX = b2ContactSolver.s_worldManifold.m_normal.x;
  5654. var normalY = b2ContactSolver.s_worldManifold.m_normal.y;
  5655. var cc = this.m_constraints[i];
  5656. cc.bodyA = bodyA;
  5657. cc.bodyB = bodyB;
  5658. cc.manifold = manifold;
  5659. cc.normal.x = normalX;
  5660. cc.normal.y = normalY;
  5661. cc.pointCount = manifold.m_pointCount;
  5662. cc.friction = friction;
  5663. cc.restitution = restitution;
  5664. cc.localPlaneNormal.x = manifold.m_localPlaneNormal.x;
  5665. cc.localPlaneNormal.y = manifold.m_localPlaneNormal.y;
  5666. cc.localPoint.x = manifold.m_localPoint.x;
  5667. cc.localPoint.y = manifold.m_localPoint.y;
  5668. cc.radius = radiusA + radiusB;
  5669. cc.type = manifold.m_type;
  5670. for(var k = 0;k < cc.pointCount;++k) {
  5671. var cp = manifold.m_points[k];
  5672. var ccp = cc.points[k];
  5673. ccp.normalImpulse = cp.m_normalImpulse;
  5674. ccp.tangentImpulse = cp.m_tangentImpulse;
  5675. ccp.localPoint.SetV(cp.m_localPoint);
  5676. var rAX = ccp.rA.x = b2ContactSolver.s_worldManifold.m_points[k].x - bodyA.m_sweep.c.x;
  5677. var rAY = ccp.rA.y = b2ContactSolver.s_worldManifold.m_points[k].y - bodyA.m_sweep.c.y;
  5678. var rBX = ccp.rB.x = b2ContactSolver.s_worldManifold.m_points[k].x - bodyB.m_sweep.c.x;
  5679. var rBY = ccp.rB.y = b2ContactSolver.s_worldManifold.m_points[k].y - bodyB.m_sweep.c.y;
  5680. var rnA = rAX * normalY - rAY * normalX;
  5681. var rnB = rBX * normalY - rBY * normalX;
  5682. rnA *= rnA;
  5683. rnB *= rnB;
  5684. var kNormal = bodyA.m_invMass + bodyB.m_invMass + bodyA.m_invI * rnA + bodyB.m_invI * rnB;
  5685. ccp.normalMass = 1 / kNormal;
  5686. var kEqualized = bodyA.m_mass * bodyA.m_invMass + bodyB.m_mass * bodyB.m_invMass;
  5687. kEqualized += bodyA.m_mass * bodyA.m_invI * rnA + bodyB.m_mass * bodyB.m_invI * rnB;
  5688. ccp.equalizedMass = 1 / kEqualized;
  5689. var tangentX = normalY;
  5690. var tangentY = -normalX;
  5691. var rtA = rAX * tangentY - rAY * tangentX;
  5692. var rtB = rBX * tangentY - rBY * tangentX;
  5693. rtA *= rtA;
  5694. rtB *= rtB;
  5695. var kTangent = bodyA.m_invMass + bodyB.m_invMass + bodyA.m_invI * rtA + bodyB.m_invI * rtB;
  5696. ccp.tangentMass = 1 / kTangent;
  5697. ccp.velocityBias = 0;
  5698. var tX = vBX + -wB * rBY - vAX - -wA * rAY;
  5699. var tY = vBY + wB * rBX - vAY - wA * rAX;
  5700. var vRel = cc.normal.x * tX + cc.normal.y * tY;
  5701. if(vRel < -b2Settings.b2_velocityThreshold) {
  5702. ccp.velocityBias += -cc.restitution * vRel
  5703. }
  5704. }
  5705. if(cc.pointCount == 2) {
  5706. var ccp1 = cc.points[0];
  5707. var ccp2 = cc.points[1];
  5708. var invMassA = bodyA.m_invMass;
  5709. var invIA = bodyA.m_invI;
  5710. var invMassB = bodyB.m_invMass;
  5711. var invIB = bodyB.m_invI;
  5712. var rn1A = ccp1.rA.x * normalY - ccp1.rA.y * normalX;
  5713. var rn1B = ccp1.rB.x * normalY - ccp1.rB.y * normalX;
  5714. var rn2A = ccp2.rA.x * normalY - ccp2.rA.y * normalX;
  5715. var rn2B = ccp2.rB.x * normalY - ccp2.rB.y * normalX;
  5716. var k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
  5717. var k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
  5718. var k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;
  5719. var k_maxConditionNumber = 100;
  5720. if(k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12)) {
  5721. cc.K.col1.Set(k11, k12);
  5722. cc.K.col2.Set(k12, k22);
  5723. cc.K.GetInverse(cc.normalMass)
  5724. }else {
  5725. cc.pointCount = 1
  5726. }
  5727. }
  5728. }
  5729. };
  5730. b2ContactSolver.prototype.InitVelocityConstraints = function(step) {
  5731. var tVec;
  5732. var tVec2;
  5733. var tMat;
  5734. for(var i = 0;i < this.m_constraintCount;++i) {
  5735. var c = this.m_constraints[i];
  5736. var bodyA = c.bodyA;
  5737. var bodyB = c.bodyB;
  5738. var invMassA = bodyA.m_invMass;
  5739. var invIA = bodyA.m_invI;
  5740. var invMassB = bodyB.m_invMass;
  5741. var invIB = bodyB.m_invI;
  5742. var normalX = c.normal.x;
  5743. var normalY = c.normal.y;
  5744. var tangentX = normalY;
  5745. var tangentY = -normalX;
  5746. var tX;
  5747. var j = 0;
  5748. var tCount = 0;
  5749. if(step.warmStarting) {
  5750. tCount = c.pointCount;
  5751. for(j = 0;j < tCount;++j) {
  5752. var ccp = c.points[j];
  5753. ccp.normalImpulse *= step.dtRatio;
  5754. ccp.tangentImpulse *= step.dtRatio;
  5755. var PX = ccp.normalImpulse * normalX + ccp.tangentImpulse * tangentX;
  5756. var PY = ccp.normalImpulse * normalY + ccp.tangentImpulse * tangentY;
  5757. bodyA.m_angularVelocity -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
  5758. bodyA.m_linearVelocity.x -= invMassA * PX;
  5759. bodyA.m_linearVelocity.y -= invMassA * PY;
  5760. bodyB.m_angularVelocity += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
  5761. bodyB.m_linearVelocity.x += invMassB * PX;
  5762. bodyB.m_linearVelocity.y += invMassB * PY
  5763. }
  5764. }else {
  5765. tCount = c.pointCount;
  5766. for(j = 0;j < tCount;++j) {
  5767. var ccp2 = c.points[j];
  5768. ccp2.normalImpulse = 0;
  5769. ccp2.tangentImpulse = 0
  5770. }
  5771. }
  5772. }
  5773. };
  5774. b2ContactSolver.prototype.SolveVelocityConstraints = function() {
  5775. var j = 0;
  5776. var ccp;
  5777. var rAX;
  5778. var rAY;
  5779. var rBX;
  5780. var rBY;
  5781. var dvX;
  5782. var dvY;
  5783. var vn;
  5784. var vt;
  5785. var lambda;
  5786. var maxFriction;
  5787. var newImpulse;
  5788. var PX;
  5789. var PY;
  5790. var dX;
  5791. var dY;
  5792. var P1X;
  5793. var P1Y;
  5794. var P2X;
  5795. var P2Y;
  5796. var tMat;
  5797. var tVec;
  5798. for(var i = 0;i < this.m_constraintCount;++i) {
  5799. var c = this.m_constraints[i];
  5800. var bodyA = c.bodyA;
  5801. var bodyB = c.bodyB;
  5802. var wA = bodyA.m_angularVelocity;
  5803. var wB = bodyB.m_angularVelocity;
  5804. var vA = bodyA.m_linearVelocity;
  5805. var vB = bodyB.m_linearVelocity;
  5806. var invMassA = bodyA.m_invMass;
  5807. var invIA = bodyA.m_invI;
  5808. var invMassB = bodyB.m_invMass;
  5809. var invIB = bodyB.m_invI;
  5810. var normalX = c.normal.x;
  5811. var normalY = c.normal.y;
  5812. var tangentX = normalY;
  5813. var tangentY = -normalX;
  5814. var friction = c.friction;
  5815. var tX;
  5816. for(j = 0;j < c.pointCount;j++) {
  5817. ccp = c.points[j];
  5818. dvX = vB.x - wB * ccp.rB.y - vA.x + wA * ccp.rA.y;
  5819. dvY = vB.y + wB * ccp.rB.x - vA.y - wA * ccp.rA.x;
  5820. vt = dvX * tangentX + dvY * tangentY;
  5821. lambda = ccp.tangentMass * -vt;
  5822. maxFriction = friction * ccp.normalImpulse;
  5823. newImpulse = b2Math.Clamp(ccp.tangentImpulse + lambda, -maxFriction, maxFriction);
  5824. lambda = newImpulse - ccp.tangentImpulse;
  5825. PX = lambda * tangentX;
  5826. PY = lambda * tangentY;
  5827. vA.x -= invMassA * PX;
  5828. vA.y -= invMassA * PY;
  5829. wA -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
  5830. vB.x += invMassB * PX;
  5831. vB.y += invMassB * PY;
  5832. wB += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
  5833. ccp.tangentImpulse = newImpulse
  5834. }
  5835. var tCount = c.pointCount;
  5836. if(c.pointCount == 1) {
  5837. ccp = c.points[0];
  5838. dvX = vB.x + -wB * ccp.rB.y - vA.x - -wA * ccp.rA.y;
  5839. dvY = vB.y + wB * ccp.rB.x - vA.y - wA * ccp.rA.x;
  5840. vn = dvX * normalX + dvY * normalY;
  5841. lambda = -ccp.normalMass * (vn - ccp.velocityBias);
  5842. newImpulse = ccp.normalImpulse + lambda;
  5843. newImpulse = newImpulse > 0 ? newImpulse : 0;
  5844. lambda = newImpulse - ccp.normalImpulse;
  5845. PX = lambda * normalX;
  5846. PY = lambda * normalY;
  5847. vA.x -= invMassA * PX;
  5848. vA.y -= invMassA * PY;
  5849. wA -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
  5850. vB.x += invMassB * PX;
  5851. vB.y += invMassB * PY;
  5852. wB += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
  5853. ccp.normalImpulse = newImpulse
  5854. }else {
  5855. var cp1 = c.points[0];
  5856. var cp2 = c.points[1];
  5857. var aX = cp1.normalImpulse;
  5858. var aY = cp2.normalImpulse;
  5859. var dv1X = vB.x - wB * cp1.rB.y - vA.x + wA * cp1.rA.y;
  5860. var dv1Y = vB.y + wB * cp1.rB.x - vA.y - wA * cp1.rA.x;
  5861. var dv2X = vB.x - wB * cp2.rB.y - vA.x + wA * cp2.rA.y;
  5862. var dv2Y = vB.y + wB * cp2.rB.x - vA.y - wA * cp2.rA.x;
  5863. var vn1 = dv1X * normalX + dv1Y * normalY;
  5864. var vn2 = dv2X * normalX + dv2Y * normalY;
  5865. var bX = vn1 - cp1.velocityBias;
  5866. var bY = vn2 - cp2.velocityBias;
  5867. tMat = c.K;
  5868. bX -= tMat.col1.x * aX + tMat.col2.x * aY;
  5869. bY -= tMat.col1.y * aX + tMat.col2.y * aY;
  5870. var k_errorTol = 0.0010;
  5871. for(;;) {
  5872. tMat = c.normalMass;
  5873. var xX = -(tMat.col1.x * bX + tMat.col2.x * bY);
  5874. var xY = -(tMat.col1.y * bX + tMat.col2.y * bY);
  5875. if(xX >= 0 && xY >= 0) {
  5876. dX = xX - aX;
  5877. dY = xY - aY;
  5878. P1X = dX * normalX;
  5879. P1Y = dX * normalY;
  5880. P2X = dY * normalX;
  5881. P2Y = dY * normalY;
  5882. vA.x -= invMassA * (P1X + P2X);
  5883. vA.y -= invMassA * (P1Y + P2Y);
  5884. wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
  5885. vB.x += invMassB * (P1X + P2X);
  5886. vB.y += invMassB * (P1Y + P2Y);
  5887. wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
  5888. cp1.normalImpulse = xX;
  5889. cp2.normalImpulse = xY;
  5890. break
  5891. }
  5892. xX = -cp1.normalMass * bX;
  5893. xY = 0;
  5894. vn1 = 0;
  5895. vn2 = c.K.col1.y * xX + bY;
  5896. if(xX >= 0 && vn2 >= 0) {
  5897. dX = xX - aX;
  5898. dY = xY - aY;
  5899. P1X = dX * normalX;
  5900. P1Y = dX * normalY;
  5901. P2X = dY * normalX;
  5902. P2Y = dY * normalY;
  5903. vA.x -= invMassA * (P1X + P2X);
  5904. vA.y -= invMassA * (P1Y + P2Y);
  5905. wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
  5906. vB.x += invMassB * (P1X + P2X);
  5907. vB.y += invMassB * (P1Y + P2Y);
  5908. wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
  5909. cp1.normalImpulse = xX;
  5910. cp2.normalImpulse = xY;
  5911. break
  5912. }
  5913. xX = 0;
  5914. xY = -cp2.normalMass * bY;
  5915. vn1 = c.K.col2.x * xY + bX;
  5916. vn2 = 0;
  5917. if(xY >= 0 && vn1 >= 0) {
  5918. dX = xX - aX;
  5919. dY = xY - aY;
  5920. P1X = dX * normalX;
  5921. P1Y = dX * normalY;
  5922. P2X = dY * normalX;
  5923. P2Y = dY * normalY;
  5924. vA.x -= invMassA * (P1X + P2X);
  5925. vA.y -= invMassA * (P1Y + P2Y);
  5926. wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
  5927. vB.x += invMassB * (P1X + P2X);
  5928. vB.y += invMassB * (P1Y + P2Y);
  5929. wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
  5930. cp1.normalImpulse = xX;
  5931. cp2.normalImpulse = xY;
  5932. break
  5933. }
  5934. xX = 0;
  5935. xY = 0;
  5936. vn1 = bX;
  5937. vn2 = bY;
  5938. if(vn1 >= 0 && vn2 >= 0) {
  5939. dX = xX - aX;
  5940. dY = xY - aY;
  5941. P1X = dX * normalX;
  5942. P1Y = dX * normalY;
  5943. P2X = dY * normalX;
  5944. P2Y = dY * normalY;
  5945. vA.x -= invMassA * (P1X + P2X);
  5946. vA.y -= invMassA * (P1Y + P2Y);
  5947. wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
  5948. vB.x += invMassB * (P1X + P2X);
  5949. vB.y += invMassB * (P1Y + P2Y);
  5950. wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
  5951. cp1.normalImpulse = xX;
  5952. cp2.normalImpulse = xY;
  5953. break
  5954. }
  5955. break
  5956. }
  5957. }
  5958. bodyA.m_angularVelocity = wA;
  5959. bodyB.m_angularVelocity = wB
  5960. }
  5961. };
  5962. b2ContactSolver.prototype.FinalizeVelocityConstraints = function() {
  5963. for(var i = 0;i < this.m_constraintCount;++i) {
  5964. var c = this.m_constraints[i];
  5965. var m = c.manifold;
  5966. for(var j = 0;j < c.pointCount;++j) {
  5967. var point1 = m.m_points[j];
  5968. var point2 = c.points[j];
  5969. point1.m_normalImpulse = point2.normalImpulse;
  5970. point1.m_tangentImpulse = point2.tangentImpulse
  5971. }
  5972. }
  5973. };
  5974. b2ContactSolver.prototype.SolvePositionConstraints = function(baumgarte) {
  5975. var minSeparation = 0;
  5976. for(var i = 0;i < this.m_constraintCount;i++) {
  5977. var c = this.m_constraints[i];
  5978. var bodyA = c.bodyA;
  5979. var bodyB = c.bodyB;
  5980. var invMassA = bodyA.m_mass * bodyA.m_invMass;
  5981. var invIA = bodyA.m_mass * bodyA.m_invI;
  5982. var invMassB = bodyB.m_mass * bodyB.m_invMass;
  5983. var invIB = bodyB.m_mass * bodyB.m_invI;
  5984. b2ContactSolver.s_psm.Initialize(c);
  5985. var normal = b2ContactSolver.s_psm.m_normal;
  5986. for(var j = 0;j < c.pointCount;j++) {
  5987. var ccp = c.points[j];
  5988. var point = b2ContactSolver.s_psm.m_points[j];
  5989. var separation = b2ContactSolver.s_psm.m_separations[j];
  5990. var rAX = point.x - bodyA.m_sweep.c.x;
  5991. var rAY = point.y - bodyA.m_sweep.c.y;
  5992. var rBX = point.x - bodyB.m_sweep.c.x;
  5993. var rBY = point.y - bodyB.m_sweep.c.y;
  5994. minSeparation = minSeparation < separation ? minSeparation : separation;
  5995. var C = b2Math.Clamp(baumgarte * (separation + b2Settings.b2_linearSlop), -b2Settings.b2_maxLinearCorrection, 0);
  5996. var impulse = -ccp.equalizedMass * C;
  5997. var PX = impulse * normal.x;
  5998. var PY = impulse * normal.y;
  5999. bodyA.m_sweep.c.x -= invMassA * PX;
  6000. bodyA.m_sweep.c.y -= invMassA * PY;
  6001. bodyA.m_sweep.a -= invIA * (rAX * PY - rAY * PX);
  6002. bodyA.SynchronizeTransform();
  6003. bodyB.m_sweep.c.x += invMassB * PX;
  6004. bodyB.m_sweep.c.y += invMassB * PY;
  6005. bodyB.m_sweep.a += invIB * (rBX * PY - rBY * PX);
  6006. bodyB.SynchronizeTransform()
  6007. }
  6008. }
  6009. return minSeparation > -1.5 * b2Settings.b2_linearSlop
  6010. };
  6011. b2ContactSolver.prototype.m_step = new b2TimeStep;
  6012. b2ContactSolver.prototype.m_allocator = null;
  6013. b2ContactSolver.prototype.m_constraints = new Array;
  6014. b2ContactSolver.prototype.m_constraintCount = 0;var b2Simplex = function() {
  6015. this.__varz();
  6016. this.__constructor.apply(this, arguments)
  6017. };
  6018. b2Simplex.prototype.__constructor = function() {
  6019. this.m_vertices[0] = this.m_v1;
  6020. this.m_vertices[1] = this.m_v2;
  6021. this.m_vertices[2] = this.m_v3
  6022. };
  6023. b2Simplex.prototype.__varz = function() {
  6024. this.m_v1 = new b2SimplexVertex;
  6025. this.m_v2 = new b2SimplexVertex;
  6026. this.m_v3 = new b2SimplexVertex;
  6027. this.m_vertices = new Array(3)
  6028. };
  6029. b2Simplex.prototype.ReadCache = function(cache, proxyA, transformA, proxyB, transformB) {
  6030. b2Settings.b2Assert(0 <= cache.count && cache.count <= 3);
  6031. var wALocal;
  6032. var wBLocal;
  6033. this.m_count = cache.count;
  6034. var vertices = this.m_vertices;
  6035. for(var i = 0;i < this.m_count;i++) {
  6036. var v = vertices[i];
  6037. v.indexA = cache.indexA[i];
  6038. v.indexB = cache.indexB[i];
  6039. wALocal = proxyA.GetVertex(v.indexA);
  6040. wBLocal = proxyB.GetVertex(v.indexB);
  6041. v.wA = b2Math.MulX(transformA, wALocal);
  6042. v.wB = b2Math.MulX(transformB, wBLocal);
  6043. v.w = b2Math.SubtractVV(v.wB, v.wA);
  6044. v.a = 0
  6045. }
  6046. if(this.m_count > 1) {
  6047. var metric1 = cache.metric;
  6048. var metric2 = this.GetMetric();
  6049. if(metric2 < 0.5 * metric1 || 2 * metric1 < metric2 || metric2 < Number.MIN_VALUE) {
  6050. this.m_count = 0
  6051. }
  6052. }
  6053. if(this.m_count == 0) {
  6054. v = vertices[0];
  6055. v.indexA = 0;
  6056. v.indexB = 0;
  6057. wALocal = proxyA.GetVertex(0);
  6058. wBLocal = proxyB.GetVertex(0);
  6059. v.wA = b2Math.MulX(transformA, wALocal);
  6060. v.wB = b2Math.MulX(transformB, wBLocal);
  6061. v.w = b2Math.SubtractVV(v.wB, v.wA);
  6062. this.m_count = 1
  6063. }
  6064. };
  6065. b2Simplex.prototype.WriteCache = function(cache) {
  6066. cache.metric = this.GetMetric();
  6067. cache.count = parseInt(this.m_count);
  6068. var vertices = this.m_vertices;
  6069. for(var i = 0;i < this.m_count;i++) {
  6070. cache.indexA[i] = parseInt(vertices[i].indexA);
  6071. cache.indexB[i] = parseInt(vertices[i].indexB)
  6072. }
  6073. };
  6074. b2Simplex.prototype.GetSearchDirection = function() {
  6075. switch(this.m_count) {
  6076. case 1:
  6077. return this.m_v1.w.GetNegative();
  6078. case 2:
  6079. var e12 = b2Math.SubtractVV(this.m_v2.w, this.m_v1.w);
  6080. var sgn = b2Math.CrossVV(e12, this.m_v1.w.GetNegative());
  6081. if(sgn > 0) {
  6082. return b2Math.CrossFV(1, e12)
  6083. }else {
  6084. return b2Math.CrossVF(e12, 1)
  6085. }
  6086. ;
  6087. default:
  6088. b2Settings.b2Assert(false);
  6089. return new b2Vec2
  6090. }
  6091. };
  6092. b2Simplex.prototype.GetClosestPoint = function() {
  6093. switch(this.m_count) {
  6094. case 0:
  6095. b2Settings.b2Assert(false);
  6096. return new b2Vec2;
  6097. case 1:
  6098. return this.m_v1.w;
  6099. case 2:
  6100. 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);
  6101. default:
  6102. b2Settings.b2Assert(false);
  6103. return new b2Vec2
  6104. }
  6105. };
  6106. b2Simplex.prototype.GetWitnessPoints = function(pA, pB) {
  6107. switch(this.m_count) {
  6108. case 0:
  6109. b2Settings.b2Assert(false);
  6110. break;
  6111. case 1:
  6112. pA.SetV(this.m_v1.wA);
  6113. pB.SetV(this.m_v1.wB);
  6114. break;
  6115. case 2:
  6116. pA.x = this.m_v1.a * this.m_v1.wA.x + this.m_v2.a * this.m_v2.wA.x;
  6117. pA.y = this.m_v1.a * this.m_v1.wA.y + this.m_v2.a * this.m_v2.wA.y;
  6118. pB.x = this.m_v1.a * this.m_v1.wB.x + this.m_v2.a * this.m_v2.wB.x;
  6119. pB.y = this.m_v1.a * this.m_v1.wB.y + this.m_v2.a * this.m_v2.wB.y;
  6120. break;
  6121. case 3:
  6122. 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;
  6123. 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;
  6124. break;
  6125. default:
  6126. b2Settings.b2Assert(false);
  6127. break
  6128. }
  6129. };
  6130. b2Simplex.prototype.GetMetric = function() {
  6131. switch(this.m_count) {
  6132. case 0:
  6133. b2Settings.b2Assert(false);
  6134. return 0;
  6135. case 1:
  6136. return 0;
  6137. case 2:
  6138. return b2Math.SubtractVV(this.m_v1.w, this.m_v2.w).Length();
  6139. case 3:
  6140. return b2Math.CrossVV(b2Math.SubtractVV(this.m_v2.w, this.m_v1.w), b2Math.SubtractVV(this.m_v3.w, this.m_v1.w));
  6141. default:
  6142. b2Settings.b2Assert(false);
  6143. return 0
  6144. }
  6145. };
  6146. b2Simplex.prototype.Solve2 = function() {
  6147. var w1 = this.m_v1.w;
  6148. var w2 = this.m_v2.w;
  6149. var e12 = b2Math.SubtractVV(w2, w1);
  6150. var d12_2 = -(w1.x * e12.x + w1.y * e12.y);
  6151. if(d12_2 <= 0) {
  6152. this.m_v1.a = 1;
  6153. this.m_count = 1;
  6154. return
  6155. }
  6156. var d12_1 = w2.x * e12.x + w2.y * e12.y;
  6157. if(d12_1 <= 0) {
  6158. this.m_v2.a = 1;
  6159. this.m_count = 1;
  6160. this.m_v1.Set(this.m_v2);
  6161. return
  6162. }
  6163. var inv_d12 = 1 / (d12_1 + d12_2);
  6164. this.m_v1.a = d12_1 * inv_d12;
  6165. this.m_v2.a = d12_2 * inv_d12;
  6166. this.m_count = 2
  6167. };
  6168. b2Simplex.prototype.Solve3 = function() {
  6169. var w1 = this.m_v1.w;
  6170. var w2 = this.m_v2.w;
  6171. var w3 = this.m_v3.w;
  6172. var e12 = b2Math.SubtractVV(w2, w1);
  6173. var w1e12 = b2Math.Dot(w1, e12);
  6174. var w2e12 = b2Math.Dot(w2, e12);
  6175. var d12_1 = w2e12;
  6176. var d12_2 = -w1e12;
  6177. var e13 = b2Math.SubtractVV(w3, w1);
  6178. var w1e13 = b2Math.Dot(w1, e13);
  6179. var w3e13 = b2Math.Dot(w3, e13);
  6180. var d13_1 = w3e13;
  6181. var d13_2 = -w1e13;
  6182. var e23 = b2Math.SubtractVV(w3, w2);
  6183. var w2e23 = b2Math.Dot(w2, e23);
  6184. var w3e23 = b2Math.Dot(w3, e23);
  6185. var d23_1 = w3e23;
  6186. var d23_2 = -w2e23;
  6187. var n123 = b2Math.CrossVV(e12, e13);
  6188. var d123_1 = n123 * b2Math.CrossVV(w2, w3);
  6189. var d123_2 = n123 * b2Math.CrossVV(w3, w1);
  6190. var d123_3 = n123 * b2Math.CrossVV(w1, w2);
  6191. if(d12_2 <= 0 && d13_2 <= 0) {
  6192. this.m_v1.a = 1;
  6193. this.m_count = 1;
  6194. return
  6195. }
  6196. if(d12_1 > 0 && d12_2 > 0 && d123_3 <= 0) {
  6197. var inv_d12 = 1 / (d12_1 + d12_2);
  6198. this.m_v1.a = d12_1 * inv_d12;
  6199. this.m_v2.a = d12_2 * inv_d12;
  6200. this.m_count = 2;
  6201. return
  6202. }
  6203. if(d13_1 > 0 && d13_2 > 0 && d123_2 <= 0) {
  6204. var inv_d13 = 1 / (d13_1 + d13_2);
  6205. this.m_v1.a = d13_1 * inv_d13;
  6206. this.m_v3.a = d13_2 * inv_d13;
  6207. this.m_count = 2;
  6208. this.m_v2.Set(this.m_v3);
  6209. return
  6210. }
  6211. if(d12_1 <= 0 && d23_2 <= 0) {
  6212. this.m_v2.a = 1;
  6213. this.m_count = 1;
  6214. this.m_v1.Set(this.m_v2);
  6215. return
  6216. }
  6217. if(d13_1 <= 0 && d23_1 <= 0) {
  6218. this.m_v3.a = 1;
  6219. this.m_count = 1;
  6220. this.m_v1.Set(this.m_v3);
  6221. return
  6222. }
  6223. if(d23_1 > 0 && d23_2 > 0 && d123_1 <= 0) {
  6224. var inv_d23 = 1 / (d23_1 + d23_2);
  6225. this.m_v2.a = d23_1 * inv_d23;
  6226. this.m_v3.a = d23_2 * inv_d23;
  6227. this.m_count = 2;
  6228. this.m_v1.Set(this.m_v3);
  6229. return
  6230. }
  6231. var inv_d123 = 1 / (d123_1 + d123_2 + d123_3);
  6232. this.m_v1.a = d123_1 * inv_d123;
  6233. this.m_v2.a = d123_2 * inv_d123;
  6234. this.m_v3.a = d123_3 * inv_d123;
  6235. this.m_count = 3
  6236. };
  6237. b2Simplex.prototype.m_v1 = new b2SimplexVertex;
  6238. b2Simplex.prototype.m_v2 = new b2SimplexVertex;
  6239. b2Simplex.prototype.m_v3 = new b2SimplexVertex;
  6240. b2Simplex.prototype.m_vertices = new Array(3);
  6241. b2Simplex.prototype.m_count = 0;var b2WeldJoint = function() {
  6242. b2Joint.prototype.__varz.call(this);
  6243. this.__varz();
  6244. this.__constructor.apply(this, arguments)
  6245. };
  6246. extend(b2WeldJoint.prototype, b2Joint.prototype);
  6247. b2WeldJoint.prototype._super = b2Joint.prototype;
  6248. b2WeldJoint.prototype.__constructor = function(def) {
  6249. this._super.__constructor.apply(this, [def]);
  6250. this.m_localAnchorA.SetV(def.localAnchorA);
  6251. this.m_localAnchorB.SetV(def.localAnchorB);
  6252. this.m_referenceAngle = def.referenceAngle;
  6253. this.m_impulse.SetZero();
  6254. this.m_mass = new b2Mat33
  6255. };
  6256. b2WeldJoint.prototype.__varz = function() {
  6257. this.m_localAnchorA = new b2Vec2;
  6258. this.m_localAnchorB = new b2Vec2;
  6259. this.m_impulse = new b2Vec3;
  6260. this.m_mass = new b2Mat33
  6261. };
  6262. b2WeldJoint.prototype.InitVelocityConstraints = function(step) {
  6263. var tMat;
  6264. var tX;
  6265. var bA = this.m_bodyA;
  6266. var bB = this.m_bodyB;
  6267. tMat = bA.m_xf.R;
  6268. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  6269. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  6270. tX = tMat.col1.x * rAX + tMat.col2.x * rAY;
  6271. rAY = tMat.col1.y * rAX + tMat.col2.y * rAY;
  6272. rAX = tX;
  6273. tMat = bB.m_xf.R;
  6274. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  6275. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  6276. tX = tMat.col1.x * rBX + tMat.col2.x * rBY;
  6277. rBY = tMat.col1.y * rBX + tMat.col2.y * rBY;
  6278. rBX = tX;
  6279. var mA = bA.m_invMass;
  6280. var mB = bB.m_invMass;
  6281. var iA = bA.m_invI;
  6282. var iB = bB.m_invI;
  6283. this.m_mass.col1.x = mA + mB + rAY * rAY * iA + rBY * rBY * iB;
  6284. this.m_mass.col2.x = -rAY * rAX * iA - rBY * rBX * iB;
  6285. this.m_mass.col3.x = -rAY * iA - rBY * iB;
  6286. this.m_mass.col1.y = this.m_mass.col2.x;
  6287. this.m_mass.col2.y = mA + mB + rAX * rAX * iA + rBX * rBX * iB;
  6288. this.m_mass.col3.y = rAX * iA + rBX * iB;
  6289. this.m_mass.col1.z = this.m_mass.col3.x;
  6290. this.m_mass.col2.z = this.m_mass.col3.y;
  6291. this.m_mass.col3.z = iA + iB;
  6292. if(step.warmStarting) {
  6293. this.m_impulse.x *= step.dtRatio;
  6294. this.m_impulse.y *= step.dtRatio;
  6295. this.m_impulse.z *= step.dtRatio;
  6296. bA.m_linearVelocity.x -= mA * this.m_impulse.x;
  6297. bA.m_linearVelocity.y -= mA * this.m_impulse.y;
  6298. bA.m_angularVelocity -= iA * (rAX * this.m_impulse.y - rAY * this.m_impulse.x + this.m_impulse.z);
  6299. bB.m_linearVelocity.x += mB * this.m_impulse.x;
  6300. bB.m_linearVelocity.y += mB * this.m_impulse.y;
  6301. bB.m_angularVelocity += iB * (rBX * this.m_impulse.y - rBY * this.m_impulse.x + this.m_impulse.z)
  6302. }else {
  6303. this.m_impulse.SetZero()
  6304. }
  6305. };
  6306. b2WeldJoint.prototype.SolveVelocityConstraints = function(step) {
  6307. var tMat;
  6308. var tX;
  6309. var bA = this.m_bodyA;
  6310. var bB = this.m_bodyB;
  6311. var vA = bA.m_linearVelocity;
  6312. var wA = bA.m_angularVelocity;
  6313. var vB = bB.m_linearVelocity;
  6314. var wB = bB.m_angularVelocity;
  6315. var mA = bA.m_invMass;
  6316. var mB = bB.m_invMass;
  6317. var iA = bA.m_invI;
  6318. var iB = bB.m_invI;
  6319. tMat = bA.m_xf.R;
  6320. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  6321. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  6322. tX = tMat.col1.x * rAX + tMat.col2.x * rAY;
  6323. rAY = tMat.col1.y * rAX + tMat.col2.y * rAY;
  6324. rAX = tX;
  6325. tMat = bB.m_xf.R;
  6326. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  6327. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  6328. tX = tMat.col1.x * rBX + tMat.col2.x * rBY;
  6329. rBY = tMat.col1.y * rBX + tMat.col2.y * rBY;
  6330. rBX = tX;
  6331. var Cdot1X = vB.x - wB * rBY - vA.x + wA * rAY;
  6332. var Cdot1Y = vB.y + wB * rBX - vA.y - wA * rAX;
  6333. var Cdot2 = wB - wA;
  6334. var impulse = new b2Vec3;
  6335. this.m_mass.Solve33(impulse, -Cdot1X, -Cdot1Y, -Cdot2);
  6336. this.m_impulse.Add(impulse);
  6337. vA.x -= mA * impulse.x;
  6338. vA.y -= mA * impulse.y;
  6339. wA -= iA * (rAX * impulse.y - rAY * impulse.x + impulse.z);
  6340. vB.x += mB * impulse.x;
  6341. vB.y += mB * impulse.y;
  6342. wB += iB * (rBX * impulse.y - rBY * impulse.x + impulse.z);
  6343. bA.m_angularVelocity = wA;
  6344. bB.m_angularVelocity = wB
  6345. };
  6346. b2WeldJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  6347. var tMat;
  6348. var tX;
  6349. var bA = this.m_bodyA;
  6350. var bB = this.m_bodyB;
  6351. tMat = bA.m_xf.R;
  6352. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  6353. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  6354. tX = tMat.col1.x * rAX + tMat.col2.x * rAY;
  6355. rAY = tMat.col1.y * rAX + tMat.col2.y * rAY;
  6356. rAX = tX;
  6357. tMat = bB.m_xf.R;
  6358. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  6359. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  6360. tX = tMat.col1.x * rBX + tMat.col2.x * rBY;
  6361. rBY = tMat.col1.y * rBX + tMat.col2.y * rBY;
  6362. rBX = tX;
  6363. var mA = bA.m_invMass;
  6364. var mB = bB.m_invMass;
  6365. var iA = bA.m_invI;
  6366. var iB = bB.m_invI;
  6367. var C1X = bB.m_sweep.c.x + rBX - bA.m_sweep.c.x - rAX;
  6368. var C1Y = bB.m_sweep.c.y + rBY - bA.m_sweep.c.y - rAY;
  6369. var C2 = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  6370. var k_allowedStretch = 10 * b2Settings.b2_linearSlop;
  6371. var positionError = Math.sqrt(C1X * C1X + C1Y * C1Y);
  6372. var angularError = b2Math.Abs(C2);
  6373. if(positionError > k_allowedStretch) {
  6374. iA *= 1;
  6375. iB *= 1
  6376. }
  6377. this.m_mass.col1.x = mA + mB + rAY * rAY * iA + rBY * rBY * iB;
  6378. this.m_mass.col2.x = -rAY * rAX * iA - rBY * rBX * iB;
  6379. this.m_mass.col3.x = -rAY * iA - rBY * iB;
  6380. this.m_mass.col1.y = this.m_mass.col2.x;
  6381. this.m_mass.col2.y = mA + mB + rAX * rAX * iA + rBX * rBX * iB;
  6382. this.m_mass.col3.y = rAX * iA + rBX * iB;
  6383. this.m_mass.col1.z = this.m_mass.col3.x;
  6384. this.m_mass.col2.z = this.m_mass.col3.y;
  6385. this.m_mass.col3.z = iA + iB;
  6386. var impulse = new b2Vec3;
  6387. this.m_mass.Solve33(impulse, -C1X, -C1Y, -C2);
  6388. bA.m_sweep.c.x -= mA * impulse.x;
  6389. bA.m_sweep.c.y -= mA * impulse.y;
  6390. bA.m_sweep.a -= iA * (rAX * impulse.y - rAY * impulse.x + impulse.z);
  6391. bB.m_sweep.c.x += mB * impulse.x;
  6392. bB.m_sweep.c.y += mB * impulse.y;
  6393. bB.m_sweep.a += iB * (rBX * impulse.y - rBY * impulse.x + impulse.z);
  6394. bA.SynchronizeTransform();
  6395. bB.SynchronizeTransform();
  6396. return positionError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop
  6397. };
  6398. b2WeldJoint.prototype.GetAnchorA = function() {
  6399. return this.m_bodyA.GetWorldPoint(this.m_localAnchorA)
  6400. };
  6401. b2WeldJoint.prototype.GetAnchorB = function() {
  6402. return this.m_bodyB.GetWorldPoint(this.m_localAnchorB)
  6403. };
  6404. b2WeldJoint.prototype.GetReactionForce = function(inv_dt) {
  6405. return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y)
  6406. };
  6407. b2WeldJoint.prototype.GetReactionTorque = function(inv_dt) {
  6408. return inv_dt * this.m_impulse.z
  6409. };
  6410. b2WeldJoint.prototype.m_localAnchorA = new b2Vec2;
  6411. b2WeldJoint.prototype.m_localAnchorB = new b2Vec2;
  6412. b2WeldJoint.prototype.m_referenceAngle = null;
  6413. b2WeldJoint.prototype.m_impulse = new b2Vec3;
  6414. b2WeldJoint.prototype.m_mass = new b2Mat33;var b2Math = function() {
  6415. this.__varz();
  6416. this.__constructor.apply(this, arguments)
  6417. };
  6418. b2Math.prototype.__constructor = function() {
  6419. };
  6420. b2Math.prototype.__varz = function() {
  6421. };
  6422. b2Math.IsValid = function(x) {
  6423. return isFinite(x)
  6424. };
  6425. b2Math.Dot = function(a, b) {
  6426. return a.x * b.x + a.y * b.y
  6427. };
  6428. b2Math.CrossVV = function(a, b) {
  6429. return a.x * b.y - a.y * b.x
  6430. };
  6431. b2Math.CrossVF = function(a, s) {
  6432. var v = new b2Vec2(s * a.y, -s * a.x);
  6433. return v
  6434. };
  6435. b2Math.CrossFV = function(s, a) {
  6436. var v = new b2Vec2(-s * a.y, s * a.x);
  6437. return v
  6438. };
  6439. b2Math.MulMV = function(A, v) {
  6440. var u = new b2Vec2(A.col1.x * v.x + A.col2.x * v.y, A.col1.y * v.x + A.col2.y * v.y);
  6441. return u
  6442. };
  6443. b2Math.MulTMV = function(A, v) {
  6444. var u = new b2Vec2(b2Math.Dot(v, A.col1), b2Math.Dot(v, A.col2));
  6445. return u
  6446. };
  6447. b2Math.MulX = function(T, v) {
  6448. var a = b2Math.MulMV(T.R, v);
  6449. a.x += T.position.x;
  6450. a.y += T.position.y;
  6451. return a
  6452. };
  6453. b2Math.MulXT = function(T, v) {
  6454. var a = b2Math.SubtractVV(v, T.position);
  6455. var tX = a.x * T.R.col1.x + a.y * T.R.col1.y;
  6456. a.y = a.x * T.R.col2.x + a.y * T.R.col2.y;
  6457. a.x = tX;
  6458. return a
  6459. };
  6460. b2Math.AddVV = function(a, b) {
  6461. var v = new b2Vec2(a.x + b.x, a.y + b.y);
  6462. return v
  6463. };
  6464. b2Math.SubtractVV = function(a, b) {
  6465. var v = new b2Vec2(a.x - b.x, a.y - b.y);
  6466. return v
  6467. };
  6468. b2Math.Distance = function(a, b) {
  6469. var cX = a.x - b.x;
  6470. var cY = a.y - b.y;
  6471. return Math.sqrt(cX * cX + cY * cY)
  6472. };
  6473. b2Math.DistanceSquared = function(a, b) {
  6474. var cX = a.x - b.x;
  6475. var cY = a.y - b.y;
  6476. return cX * cX + cY * cY
  6477. };
  6478. b2Math.MulFV = function(s, a) {
  6479. var v = new b2Vec2(s * a.x, s * a.y);
  6480. return v
  6481. };
  6482. b2Math.AddMM = function(A, B) {
  6483. var C = b2Mat22.FromVV(b2Math.AddVV(A.col1, B.col1), b2Math.AddVV(A.col2, B.col2));
  6484. return C
  6485. };
  6486. b2Math.MulMM = function(A, B) {
  6487. var C = b2Mat22.FromVV(b2Math.MulMV(A, B.col1), b2Math.MulMV(A, B.col2));
  6488. return C
  6489. };
  6490. b2Math.MulTMM = function(A, B) {
  6491. var c1 = new b2Vec2(b2Math.Dot(A.col1, B.col1), b2Math.Dot(A.col2, B.col1));
  6492. var c2 = new b2Vec2(b2Math.Dot(A.col1, B.col2), b2Math.Dot(A.col2, B.col2));
  6493. var C = b2Mat22.FromVV(c1, c2);
  6494. return C
  6495. };
  6496. b2Math.Abs = function(a) {
  6497. return a > 0 ? a : -a
  6498. };
  6499. b2Math.AbsV = function(a) {
  6500. var b = new b2Vec2(b2Math.Abs(a.x), b2Math.Abs(a.y));
  6501. return b
  6502. };
  6503. b2Math.AbsM = function(A) {
  6504. var B = b2Mat22.FromVV(b2Math.AbsV(A.col1), b2Math.AbsV(A.col2));
  6505. return B
  6506. };
  6507. b2Math.Min = function(a, b) {
  6508. return a < b ? a : b
  6509. };
  6510. b2Math.MinV = function(a, b) {
  6511. var c = new b2Vec2(b2Math.Min(a.x, b.x), b2Math.Min(a.y, b.y));
  6512. return c
  6513. };
  6514. b2Math.Max = function(a, b) {
  6515. return a > b ? a : b
  6516. };
  6517. b2Math.MaxV = function(a, b) {
  6518. var c = new b2Vec2(b2Math.Max(a.x, b.x), b2Math.Max(a.y, b.y));
  6519. return c
  6520. };
  6521. b2Math.Clamp = function(a, low, high) {
  6522. return a < low ? low : a > high ? high : a
  6523. };
  6524. b2Math.ClampV = function(a, low, high) {
  6525. return b2Math.MaxV(low, b2Math.MinV(a, high))
  6526. };
  6527. b2Math.Swap = function(a, b) {
  6528. var tmp = a[0];
  6529. a[0] = b[0];
  6530. b[0] = tmp
  6531. };
  6532. b2Math.Random = function() {
  6533. return Math.random() * 2 - 1
  6534. };
  6535. b2Math.RandomRange = function(lo, hi) {
  6536. var r = Math.random();
  6537. r = (hi - lo) * r + lo;
  6538. return r
  6539. };
  6540. b2Math.NextPowerOfTwo = function(x) {
  6541. x |= x >> 1 & 2147483647;
  6542. x |= x >> 2 & 1073741823;
  6543. x |= x >> 4 & 268435455;
  6544. x |= x >> 8 & 16777215;
  6545. x |= x >> 16 & 65535;
  6546. return x + 1
  6547. };
  6548. b2Math.IsPowerOfTwo = function(x) {
  6549. var result = x > 0 && (x & x - 1) == 0;
  6550. return result
  6551. };
  6552. b2Math.b2Vec2_zero = new b2Vec2(0, 0);
  6553. b2Math.b2Mat22_identity = b2Mat22.FromVV(new b2Vec2(1, 0), new b2Vec2(0, 1));
  6554. b2Math.b2Transform_identity = new b2Transform(b2Math.b2Vec2_zero, b2Math.b2Mat22_identity);var b2PulleyJoint = function() {
  6555. b2Joint.prototype.__varz.call(this);
  6556. this.__varz();
  6557. this.__constructor.apply(this, arguments)
  6558. };
  6559. extend(b2PulleyJoint.prototype, b2Joint.prototype);
  6560. b2PulleyJoint.prototype._super = b2Joint.prototype;
  6561. b2PulleyJoint.prototype.__constructor = function(def) {
  6562. this._super.__constructor.apply(this, [def]);
  6563. var tMat;
  6564. var tX;
  6565. var tY;
  6566. this.m_ground = this.m_bodyA.m_world.m_groundBody;
  6567. this.m_groundAnchor1.x = def.groundAnchorA.x - this.m_ground.m_xf.position.x;
  6568. this.m_groundAnchor1.y = def.groundAnchorA.y - this.m_ground.m_xf.position.y;
  6569. this.m_groundAnchor2.x = def.groundAnchorB.x - this.m_ground.m_xf.position.x;
  6570. this.m_groundAnchor2.y = def.groundAnchorB.y - this.m_ground.m_xf.position.y;
  6571. this.m_localAnchor1.SetV(def.localAnchorA);
  6572. this.m_localAnchor2.SetV(def.localAnchorB);
  6573. this.m_ratio = def.ratio;
  6574. this.m_constant = def.lengthA + this.m_ratio * def.lengthB;
  6575. this.m_maxLength1 = b2Math.Min(def.maxLengthA, this.m_constant - this.m_ratio * b2PulleyJoint.b2_minPulleyLength);
  6576. this.m_maxLength2 = b2Math.Min(def.maxLengthB, (this.m_constant - b2PulleyJoint.b2_minPulleyLength) / this.m_ratio);
  6577. this.m_impulse = 0;
  6578. this.m_limitImpulse1 = 0;
  6579. this.m_limitImpulse2 = 0
  6580. };
  6581. b2PulleyJoint.prototype.__varz = function() {
  6582. this.m_groundAnchor1 = new b2Vec2;
  6583. this.m_groundAnchor2 = new b2Vec2;
  6584. this.m_localAnchor1 = new b2Vec2;
  6585. this.m_localAnchor2 = new b2Vec2;
  6586. this.m_u1 = new b2Vec2;
  6587. this.m_u2 = new b2Vec2
  6588. };
  6589. b2PulleyJoint.b2_minPulleyLength = 2;
  6590. b2PulleyJoint.prototype.InitVelocityConstraints = function(step) {
  6591. var bA = this.m_bodyA;
  6592. var bB = this.m_bodyB;
  6593. var tMat;
  6594. tMat = bA.m_xf.R;
  6595. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  6596. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  6597. var tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  6598. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  6599. r1X = tX;
  6600. tMat = bB.m_xf.R;
  6601. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  6602. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  6603. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  6604. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  6605. r2X = tX;
  6606. var p1X = bA.m_sweep.c.x + r1X;
  6607. var p1Y = bA.m_sweep.c.y + r1Y;
  6608. var p2X = bB.m_sweep.c.x + r2X;
  6609. var p2Y = bB.m_sweep.c.y + r2Y;
  6610. var s1X = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
  6611. var s1Y = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
  6612. var s2X = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
  6613. var s2Y = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
  6614. this.m_u1.Set(p1X - s1X, p1Y - s1Y);
  6615. this.m_u2.Set(p2X - s2X, p2Y - s2Y);
  6616. var length1 = this.m_u1.Length();
  6617. var length2 = this.m_u2.Length();
  6618. if(length1 > b2Settings.b2_linearSlop) {
  6619. this.m_u1.Multiply(1 / length1)
  6620. }else {
  6621. this.m_u1.SetZero()
  6622. }
  6623. if(length2 > b2Settings.b2_linearSlop) {
  6624. this.m_u2.Multiply(1 / length2)
  6625. }else {
  6626. this.m_u2.SetZero()
  6627. }
  6628. var C = this.m_constant - length1 - this.m_ratio * length2;
  6629. if(C > 0) {
  6630. this.m_state = b2Joint.e_inactiveLimit;
  6631. this.m_impulse = 0
  6632. }else {
  6633. this.m_state = b2Joint.e_atUpperLimit
  6634. }
  6635. if(length1 < this.m_maxLength1) {
  6636. this.m_limitState1 = b2Joint.e_inactiveLimit;
  6637. this.m_limitImpulse1 = 0
  6638. }else {
  6639. this.m_limitState1 = b2Joint.e_atUpperLimit
  6640. }
  6641. if(length2 < this.m_maxLength2) {
  6642. this.m_limitState2 = b2Joint.e_inactiveLimit;
  6643. this.m_limitImpulse2 = 0
  6644. }else {
  6645. this.m_limitState2 = b2Joint.e_atUpperLimit
  6646. }
  6647. var cr1u1 = r1X * this.m_u1.y - r1Y * this.m_u1.x;
  6648. var cr2u2 = r2X * this.m_u2.y - r2Y * this.m_u2.x;
  6649. this.m_limitMass1 = bA.m_invMass + bA.m_invI * cr1u1 * cr1u1;
  6650. this.m_limitMass2 = bB.m_invMass + bB.m_invI * cr2u2 * cr2u2;
  6651. this.m_pulleyMass = this.m_limitMass1 + this.m_ratio * this.m_ratio * this.m_limitMass2;
  6652. this.m_limitMass1 = 1 / this.m_limitMass1;
  6653. this.m_limitMass2 = 1 / this.m_limitMass2;
  6654. this.m_pulleyMass = 1 / this.m_pulleyMass;
  6655. if(step.warmStarting) {
  6656. this.m_impulse *= step.dtRatio;
  6657. this.m_limitImpulse1 *= step.dtRatio;
  6658. this.m_limitImpulse2 *= step.dtRatio;
  6659. var P1X = (-this.m_impulse - this.m_limitImpulse1) * this.m_u1.x;
  6660. var P1Y = (-this.m_impulse - this.m_limitImpulse1) * this.m_u1.y;
  6661. var P2X = (-this.m_ratio * this.m_impulse - this.m_limitImpulse2) * this.m_u2.x;
  6662. var P2Y = (-this.m_ratio * this.m_impulse - this.m_limitImpulse2) * this.m_u2.y;
  6663. bA.m_linearVelocity.x += bA.m_invMass * P1X;
  6664. bA.m_linearVelocity.y += bA.m_invMass * P1Y;
  6665. bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X);
  6666. bB.m_linearVelocity.x += bB.m_invMass * P2X;
  6667. bB.m_linearVelocity.y += bB.m_invMass * P2Y;
  6668. bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X)
  6669. }else {
  6670. this.m_impulse = 0;
  6671. this.m_limitImpulse1 = 0;
  6672. this.m_limitImpulse2 = 0
  6673. }
  6674. };
  6675. b2PulleyJoint.prototype.SolveVelocityConstraints = function(step) {
  6676. var bA = this.m_bodyA;
  6677. var bB = this.m_bodyB;
  6678. var tMat;
  6679. tMat = bA.m_xf.R;
  6680. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  6681. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  6682. var tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  6683. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  6684. r1X = tX;
  6685. tMat = bB.m_xf.R;
  6686. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  6687. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  6688. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  6689. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  6690. r2X = tX;
  6691. var v1X;
  6692. var v1Y;
  6693. var v2X;
  6694. var v2Y;
  6695. var P1X;
  6696. var P1Y;
  6697. var P2X;
  6698. var P2Y;
  6699. var Cdot;
  6700. var impulse;
  6701. var oldImpulse;
  6702. if(this.m_state == b2Joint.e_atUpperLimit) {
  6703. v1X = bA.m_linearVelocity.x + -bA.m_angularVelocity * r1Y;
  6704. v1Y = bA.m_linearVelocity.y + bA.m_angularVelocity * r1X;
  6705. v2X = bB.m_linearVelocity.x + -bB.m_angularVelocity * r2Y;
  6706. v2Y = bB.m_linearVelocity.y + bB.m_angularVelocity * r2X;
  6707. Cdot = -(this.m_u1.x * v1X + this.m_u1.y * v1Y) - this.m_ratio * (this.m_u2.x * v2X + this.m_u2.y * v2Y);
  6708. impulse = this.m_pulleyMass * -Cdot;
  6709. oldImpulse = this.m_impulse;
  6710. this.m_impulse = b2Math.Max(0, this.m_impulse + impulse);
  6711. impulse = this.m_impulse - oldImpulse;
  6712. P1X = -impulse * this.m_u1.x;
  6713. P1Y = -impulse * this.m_u1.y;
  6714. P2X = -this.m_ratio * impulse * this.m_u2.x;
  6715. P2Y = -this.m_ratio * impulse * this.m_u2.y;
  6716. bA.m_linearVelocity.x += bA.m_invMass * P1X;
  6717. bA.m_linearVelocity.y += bA.m_invMass * P1Y;
  6718. bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X);
  6719. bB.m_linearVelocity.x += bB.m_invMass * P2X;
  6720. bB.m_linearVelocity.y += bB.m_invMass * P2Y;
  6721. bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X)
  6722. }
  6723. if(this.m_limitState1 == b2Joint.e_atUpperLimit) {
  6724. v1X = bA.m_linearVelocity.x + -bA.m_angularVelocity * r1Y;
  6725. v1Y = bA.m_linearVelocity.y + bA.m_angularVelocity * r1X;
  6726. Cdot = -(this.m_u1.x * v1X + this.m_u1.y * v1Y);
  6727. impulse = -this.m_limitMass1 * Cdot;
  6728. oldImpulse = this.m_limitImpulse1;
  6729. this.m_limitImpulse1 = b2Math.Max(0, this.m_limitImpulse1 + impulse);
  6730. impulse = this.m_limitImpulse1 - oldImpulse;
  6731. P1X = -impulse * this.m_u1.x;
  6732. P1Y = -impulse * this.m_u1.y;
  6733. bA.m_linearVelocity.x += bA.m_invMass * P1X;
  6734. bA.m_linearVelocity.y += bA.m_invMass * P1Y;
  6735. bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X)
  6736. }
  6737. if(this.m_limitState2 == b2Joint.e_atUpperLimit) {
  6738. v2X = bB.m_linearVelocity.x + -bB.m_angularVelocity * r2Y;
  6739. v2Y = bB.m_linearVelocity.y + bB.m_angularVelocity * r2X;
  6740. Cdot = -(this.m_u2.x * v2X + this.m_u2.y * v2Y);
  6741. impulse = -this.m_limitMass2 * Cdot;
  6742. oldImpulse = this.m_limitImpulse2;
  6743. this.m_limitImpulse2 = b2Math.Max(0, this.m_limitImpulse2 + impulse);
  6744. impulse = this.m_limitImpulse2 - oldImpulse;
  6745. P2X = -impulse * this.m_u2.x;
  6746. P2Y = -impulse * this.m_u2.y;
  6747. bB.m_linearVelocity.x += bB.m_invMass * P2X;
  6748. bB.m_linearVelocity.y += bB.m_invMass * P2Y;
  6749. bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X)
  6750. }
  6751. };
  6752. b2PulleyJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  6753. var bA = this.m_bodyA;
  6754. var bB = this.m_bodyB;
  6755. var tMat;
  6756. var s1X = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
  6757. var s1Y = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
  6758. var s2X = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
  6759. var s2Y = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
  6760. var r1X;
  6761. var r1Y;
  6762. var r2X;
  6763. var r2Y;
  6764. var p1X;
  6765. var p1Y;
  6766. var p2X;
  6767. var p2Y;
  6768. var length1;
  6769. var length2;
  6770. var C;
  6771. var impulse;
  6772. var oldImpulse;
  6773. var oldLimitPositionImpulse;
  6774. var tX;
  6775. var linearError = 0;
  6776. if(this.m_state == b2Joint.e_atUpperLimit) {
  6777. tMat = bA.m_xf.R;
  6778. r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  6779. r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  6780. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  6781. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  6782. r1X = tX;
  6783. tMat = bB.m_xf.R;
  6784. r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  6785. r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  6786. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  6787. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  6788. r2X = tX;
  6789. p1X = bA.m_sweep.c.x + r1X;
  6790. p1Y = bA.m_sweep.c.y + r1Y;
  6791. p2X = bB.m_sweep.c.x + r2X;
  6792. p2Y = bB.m_sweep.c.y + r2Y;
  6793. this.m_u1.Set(p1X - s1X, p1Y - s1Y);
  6794. this.m_u2.Set(p2X - s2X, p2Y - s2Y);
  6795. length1 = this.m_u1.Length();
  6796. length2 = this.m_u2.Length();
  6797. if(length1 > b2Settings.b2_linearSlop) {
  6798. this.m_u1.Multiply(1 / length1)
  6799. }else {
  6800. this.m_u1.SetZero()
  6801. }
  6802. if(length2 > b2Settings.b2_linearSlop) {
  6803. this.m_u2.Multiply(1 / length2)
  6804. }else {
  6805. this.m_u2.SetZero()
  6806. }
  6807. C = this.m_constant - length1 - this.m_ratio * length2;
  6808. linearError = b2Math.Max(linearError, -C);
  6809. C = b2Math.Clamp(C + b2Settings.b2_linearSlop, -b2Settings.b2_maxLinearCorrection, 0);
  6810. impulse = -this.m_pulleyMass * C;
  6811. p1X = -impulse * this.m_u1.x;
  6812. p1Y = -impulse * this.m_u1.y;
  6813. p2X = -this.m_ratio * impulse * this.m_u2.x;
  6814. p2Y = -this.m_ratio * impulse * this.m_u2.y;
  6815. bA.m_sweep.c.x += bA.m_invMass * p1X;
  6816. bA.m_sweep.c.y += bA.m_invMass * p1Y;
  6817. bA.m_sweep.a += bA.m_invI * (r1X * p1Y - r1Y * p1X);
  6818. bB.m_sweep.c.x += bB.m_invMass * p2X;
  6819. bB.m_sweep.c.y += bB.m_invMass * p2Y;
  6820. bB.m_sweep.a += bB.m_invI * (r2X * p2Y - r2Y * p2X);
  6821. bA.SynchronizeTransform();
  6822. bB.SynchronizeTransform()
  6823. }
  6824. if(this.m_limitState1 == b2Joint.e_atUpperLimit) {
  6825. tMat = bA.m_xf.R;
  6826. r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  6827. r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  6828. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  6829. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  6830. r1X = tX;
  6831. p1X = bA.m_sweep.c.x + r1X;
  6832. p1Y = bA.m_sweep.c.y + r1Y;
  6833. this.m_u1.Set(p1X - s1X, p1Y - s1Y);
  6834. length1 = this.m_u1.Length();
  6835. if(length1 > b2Settings.b2_linearSlop) {
  6836. this.m_u1.x *= 1 / length1;
  6837. this.m_u1.y *= 1 / length1
  6838. }else {
  6839. this.m_u1.SetZero()
  6840. }
  6841. C = this.m_maxLength1 - length1;
  6842. linearError = b2Math.Max(linearError, -C);
  6843. C = b2Math.Clamp(C + b2Settings.b2_linearSlop, -b2Settings.b2_maxLinearCorrection, 0);
  6844. impulse = -this.m_limitMass1 * C;
  6845. p1X = -impulse * this.m_u1.x;
  6846. p1Y = -impulse * this.m_u1.y;
  6847. bA.m_sweep.c.x += bA.m_invMass * p1X;
  6848. bA.m_sweep.c.y += bA.m_invMass * p1Y;
  6849. bA.m_sweep.a += bA.m_invI * (r1X * p1Y - r1Y * p1X);
  6850. bA.SynchronizeTransform()
  6851. }
  6852. if(this.m_limitState2 == b2Joint.e_atUpperLimit) {
  6853. tMat = bB.m_xf.R;
  6854. r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  6855. r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  6856. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  6857. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  6858. r2X = tX;
  6859. p2X = bB.m_sweep.c.x + r2X;
  6860. p2Y = bB.m_sweep.c.y + r2Y;
  6861. this.m_u2.Set(p2X - s2X, p2Y - s2Y);
  6862. length2 = this.m_u2.Length();
  6863. if(length2 > b2Settings.b2_linearSlop) {
  6864. this.m_u2.x *= 1 / length2;
  6865. this.m_u2.y *= 1 / length2
  6866. }else {
  6867. this.m_u2.SetZero()
  6868. }
  6869. C = this.m_maxLength2 - length2;
  6870. linearError = b2Math.Max(linearError, -C);
  6871. C = b2Math.Clamp(C + b2Settings.b2_linearSlop, -b2Settings.b2_maxLinearCorrection, 0);
  6872. impulse = -this.m_limitMass2 * C;
  6873. p2X = -impulse * this.m_u2.x;
  6874. p2Y = -impulse * this.m_u2.y;
  6875. bB.m_sweep.c.x += bB.m_invMass * p2X;
  6876. bB.m_sweep.c.y += bB.m_invMass * p2Y;
  6877. bB.m_sweep.a += bB.m_invI * (r2X * p2Y - r2Y * p2X);
  6878. bB.SynchronizeTransform()
  6879. }
  6880. return linearError < b2Settings.b2_linearSlop
  6881. };
  6882. b2PulleyJoint.prototype.GetAnchorA = function() {
  6883. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1)
  6884. };
  6885. b2PulleyJoint.prototype.GetAnchorB = function() {
  6886. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2)
  6887. };
  6888. b2PulleyJoint.prototype.GetReactionForce = function(inv_dt) {
  6889. return new b2Vec2(inv_dt * this.m_impulse * this.m_u2.x, inv_dt * this.m_impulse * this.m_u2.y)
  6890. };
  6891. b2PulleyJoint.prototype.GetReactionTorque = function(inv_dt) {
  6892. return 0
  6893. };
  6894. b2PulleyJoint.prototype.GetGroundAnchorA = function() {
  6895. var a = this.m_ground.m_xf.position.Copy();
  6896. a.Add(this.m_groundAnchor1);
  6897. return a
  6898. };
  6899. b2PulleyJoint.prototype.GetGroundAnchorB = function() {
  6900. var a = this.m_ground.m_xf.position.Copy();
  6901. a.Add(this.m_groundAnchor2);
  6902. return a
  6903. };
  6904. b2PulleyJoint.prototype.GetLength1 = function() {
  6905. var p = this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  6906. var sX = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
  6907. var sY = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
  6908. var dX = p.x - sX;
  6909. var dY = p.y - sY;
  6910. return Math.sqrt(dX * dX + dY * dY)
  6911. };
  6912. b2PulleyJoint.prototype.GetLength2 = function() {
  6913. var p = this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  6914. var sX = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
  6915. var sY = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
  6916. var dX = p.x - sX;
  6917. var dY = p.y - sY;
  6918. return Math.sqrt(dX * dX + dY * dY)
  6919. };
  6920. b2PulleyJoint.prototype.GetRatio = function() {
  6921. return this.m_ratio
  6922. };
  6923. b2PulleyJoint.prototype.m_ground = null;
  6924. b2PulleyJoint.prototype.m_groundAnchor1 = new b2Vec2;
  6925. b2PulleyJoint.prototype.m_groundAnchor2 = new b2Vec2;
  6926. b2PulleyJoint.prototype.m_localAnchor1 = new b2Vec2;
  6927. b2PulleyJoint.prototype.m_localAnchor2 = new b2Vec2;
  6928. b2PulleyJoint.prototype.m_u1 = new b2Vec2;
  6929. b2PulleyJoint.prototype.m_u2 = new b2Vec2;
  6930. b2PulleyJoint.prototype.m_constant = null;
  6931. b2PulleyJoint.prototype.m_ratio = null;
  6932. b2PulleyJoint.prototype.m_maxLength1 = null;
  6933. b2PulleyJoint.prototype.m_maxLength2 = null;
  6934. b2PulleyJoint.prototype.m_pulleyMass = null;
  6935. b2PulleyJoint.prototype.m_limitMass1 = null;
  6936. b2PulleyJoint.prototype.m_limitMass2 = null;
  6937. b2PulleyJoint.prototype.m_impulse = null;
  6938. b2PulleyJoint.prototype.m_limitImpulse1 = null;
  6939. b2PulleyJoint.prototype.m_limitImpulse2 = null;
  6940. b2PulleyJoint.prototype.m_state = 0;
  6941. b2PulleyJoint.prototype.m_limitState1 = 0;
  6942. b2PulleyJoint.prototype.m_limitState2 = 0;var b2PrismaticJoint = function() {
  6943. b2Joint.prototype.__varz.call(this);
  6944. this.__varz();
  6945. this.__constructor.apply(this, arguments)
  6946. };
  6947. extend(b2PrismaticJoint.prototype, b2Joint.prototype);
  6948. b2PrismaticJoint.prototype._super = b2Joint.prototype;
  6949. b2PrismaticJoint.prototype.__constructor = function(def) {
  6950. this._super.__constructor.apply(this, [def]);
  6951. var tMat;
  6952. var tX;
  6953. var tY;
  6954. this.m_localAnchor1.SetV(def.localAnchorA);
  6955. this.m_localAnchor2.SetV(def.localAnchorB);
  6956. this.m_localXAxis1.SetV(def.localAxisA);
  6957. this.m_localYAxis1.x = -this.m_localXAxis1.y;
  6958. this.m_localYAxis1.y = this.m_localXAxis1.x;
  6959. this.m_refAngle = def.referenceAngle;
  6960. this.m_impulse.SetZero();
  6961. this.m_motorMass = 0;
  6962. this.m_motorImpulse = 0;
  6963. this.m_lowerTranslation = def.lowerTranslation;
  6964. this.m_upperTranslation = def.upperTranslation;
  6965. this.m_maxMotorForce = def.maxMotorForce;
  6966. this.m_motorSpeed = def.motorSpeed;
  6967. this.m_enableLimit = def.enableLimit;
  6968. this.m_enableMotor = def.enableMotor;
  6969. this.m_limitState = b2Joint.e_inactiveLimit;
  6970. this.m_axis.SetZero();
  6971. this.m_perp.SetZero()
  6972. };
  6973. b2PrismaticJoint.prototype.__varz = function() {
  6974. this.m_localAnchor1 = new b2Vec2;
  6975. this.m_localAnchor2 = new b2Vec2;
  6976. this.m_localXAxis1 = new b2Vec2;
  6977. this.m_localYAxis1 = new b2Vec2;
  6978. this.m_axis = new b2Vec2;
  6979. this.m_perp = new b2Vec2;
  6980. this.m_K = new b2Mat33;
  6981. this.m_impulse = new b2Vec3
  6982. };
  6983. b2PrismaticJoint.prototype.InitVelocityConstraints = function(step) {
  6984. var bA = this.m_bodyA;
  6985. var bB = this.m_bodyB;
  6986. var tMat;
  6987. var tX;
  6988. this.m_localCenterA.SetV(bA.GetLocalCenter());
  6989. this.m_localCenterB.SetV(bB.GetLocalCenter());
  6990. var xf1 = bA.GetTransform();
  6991. var xf2 = bB.GetTransform();
  6992. tMat = bA.m_xf.R;
  6993. var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
  6994. var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
  6995. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  6996. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  6997. r1X = tX;
  6998. tMat = bB.m_xf.R;
  6999. var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
  7000. var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
  7001. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7002. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7003. r2X = tX;
  7004. var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  7005. var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  7006. this.m_invMassA = bA.m_invMass;
  7007. this.m_invMassB = bB.m_invMass;
  7008. this.m_invIA = bA.m_invI;
  7009. this.m_invIB = bB.m_invI;
  7010. this.m_axis.SetV(b2Math.MulMV(xf1.R, this.m_localXAxis1));
  7011. this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
  7012. this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
  7013. 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;
  7014. if(this.m_motorMass > Number.MIN_VALUE) {
  7015. this.m_motorMass = 1 / this.m_motorMass
  7016. }
  7017. this.m_perp.SetV(b2Math.MulMV(xf1.R, this.m_localYAxis1));
  7018. this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
  7019. this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
  7020. var m1 = this.m_invMassA;
  7021. var m2 = this.m_invMassB;
  7022. var i1 = this.m_invIA;
  7023. var i2 = this.m_invIB;
  7024. this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  7025. this.m_K.col1.y = i1 * this.m_s1 + i2 * this.m_s2;
  7026. this.m_K.col1.z = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
  7027. this.m_K.col2.x = this.m_K.col1.y;
  7028. this.m_K.col2.y = i1 + i2;
  7029. this.m_K.col2.z = i1 * this.m_a1 + i2 * this.m_a2;
  7030. this.m_K.col3.x = this.m_K.col1.z;
  7031. this.m_K.col3.y = this.m_K.col2.z;
  7032. this.m_K.col3.z = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
  7033. if(this.m_enableLimit) {
  7034. var jointTransition = this.m_axis.x * dX + this.m_axis.y * dY;
  7035. if(b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2 * b2Settings.b2_linearSlop) {
  7036. this.m_limitState = b2Joint.e_equalLimits
  7037. }else {
  7038. if(jointTransition <= this.m_lowerTranslation) {
  7039. if(this.m_limitState != b2Joint.e_atLowerLimit) {
  7040. this.m_limitState = b2Joint.e_atLowerLimit;
  7041. this.m_impulse.z = 0
  7042. }
  7043. }else {
  7044. if(jointTransition >= this.m_upperTranslation) {
  7045. if(this.m_limitState != b2Joint.e_atUpperLimit) {
  7046. this.m_limitState = b2Joint.e_atUpperLimit;
  7047. this.m_impulse.z = 0
  7048. }
  7049. }else {
  7050. this.m_limitState = b2Joint.e_inactiveLimit;
  7051. this.m_impulse.z = 0
  7052. }
  7053. }
  7054. }
  7055. }else {
  7056. this.m_limitState = b2Joint.e_inactiveLimit
  7057. }
  7058. if(this.m_enableMotor == false) {
  7059. this.m_motorImpulse = 0
  7060. }
  7061. if(step.warmStarting) {
  7062. this.m_impulse.x *= step.dtRatio;
  7063. this.m_impulse.y *= step.dtRatio;
  7064. this.m_motorImpulse *= step.dtRatio;
  7065. var PX = this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.x;
  7066. var PY = this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.y;
  7067. var L1 = this.m_impulse.x * this.m_s1 + this.m_impulse.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_a1;
  7068. var L2 = this.m_impulse.x * this.m_s2 + this.m_impulse.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_a2;
  7069. bA.m_linearVelocity.x -= this.m_invMassA * PX;
  7070. bA.m_linearVelocity.y -= this.m_invMassA * PY;
  7071. bA.m_angularVelocity -= this.m_invIA * L1;
  7072. bB.m_linearVelocity.x += this.m_invMassB * PX;
  7073. bB.m_linearVelocity.y += this.m_invMassB * PY;
  7074. bB.m_angularVelocity += this.m_invIB * L2
  7075. }else {
  7076. this.m_impulse.SetZero();
  7077. this.m_motorImpulse = 0
  7078. }
  7079. };
  7080. b2PrismaticJoint.prototype.SolveVelocityConstraints = function(step) {
  7081. var bA = this.m_bodyA;
  7082. var bB = this.m_bodyB;
  7083. var v1 = bA.m_linearVelocity;
  7084. var w1 = bA.m_angularVelocity;
  7085. var v2 = bB.m_linearVelocity;
  7086. var w2 = bB.m_angularVelocity;
  7087. var PX;
  7088. var PY;
  7089. var L1;
  7090. var L2;
  7091. if(this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
  7092. 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;
  7093. var impulse = this.m_motorMass * (this.m_motorSpeed - Cdot);
  7094. var oldImpulse = this.m_motorImpulse;
  7095. var maxImpulse = step.dt * this.m_maxMotorForce;
  7096. this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, -maxImpulse, maxImpulse);
  7097. impulse = this.m_motorImpulse - oldImpulse;
  7098. PX = impulse * this.m_axis.x;
  7099. PY = impulse * this.m_axis.y;
  7100. L1 = impulse * this.m_a1;
  7101. L2 = impulse * this.m_a2;
  7102. v1.x -= this.m_invMassA * PX;
  7103. v1.y -= this.m_invMassA * PY;
  7104. w1 -= this.m_invIA * L1;
  7105. v2.x += this.m_invMassB * PX;
  7106. v2.y += this.m_invMassB * PY;
  7107. w2 += this.m_invIB * L2
  7108. }
  7109. 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;
  7110. var Cdot1Y = w2 - w1;
  7111. if(this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  7112. 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;
  7113. var f1 = this.m_impulse.Copy();
  7114. var df = this.m_K.Solve33(new b2Vec3, -Cdot1X, -Cdot1Y, -Cdot2);
  7115. this.m_impulse.Add(df);
  7116. if(this.m_limitState == b2Joint.e_atLowerLimit) {
  7117. this.m_impulse.z = b2Math.Max(this.m_impulse.z, 0)
  7118. }else {
  7119. if(this.m_limitState == b2Joint.e_atUpperLimit) {
  7120. this.m_impulse.z = b2Math.Min(this.m_impulse.z, 0)
  7121. }
  7122. }
  7123. var bX = -Cdot1X - (this.m_impulse.z - f1.z) * this.m_K.col3.x;
  7124. var bY = -Cdot1Y - (this.m_impulse.z - f1.z) * this.m_K.col3.y;
  7125. var f2r = this.m_K.Solve22(new b2Vec2, bX, bY);
  7126. f2r.x += f1.x;
  7127. f2r.y += f1.y;
  7128. this.m_impulse.x = f2r.x;
  7129. this.m_impulse.y = f2r.y;
  7130. df.x = this.m_impulse.x - f1.x;
  7131. df.y = this.m_impulse.y - f1.y;
  7132. df.z = this.m_impulse.z - f1.z;
  7133. PX = df.x * this.m_perp.x + df.z * this.m_axis.x;
  7134. PY = df.x * this.m_perp.y + df.z * this.m_axis.y;
  7135. L1 = df.x * this.m_s1 + df.y + df.z * this.m_a1;
  7136. L2 = df.x * this.m_s2 + df.y + df.z * this.m_a2;
  7137. v1.x -= this.m_invMassA * PX;
  7138. v1.y -= this.m_invMassA * PY;
  7139. w1 -= this.m_invIA * L1;
  7140. v2.x += this.m_invMassB * PX;
  7141. v2.y += this.m_invMassB * PY;
  7142. w2 += this.m_invIB * L2
  7143. }else {
  7144. var df2 = this.m_K.Solve22(new b2Vec2, -Cdot1X, -Cdot1Y);
  7145. this.m_impulse.x += df2.x;
  7146. this.m_impulse.y += df2.y;
  7147. PX = df2.x * this.m_perp.x;
  7148. PY = df2.x * this.m_perp.y;
  7149. L1 = df2.x * this.m_s1 + df2.y;
  7150. L2 = df2.x * this.m_s2 + df2.y;
  7151. v1.x -= this.m_invMassA * PX;
  7152. v1.y -= this.m_invMassA * PY;
  7153. w1 -= this.m_invIA * L1;
  7154. v2.x += this.m_invMassB * PX;
  7155. v2.y += this.m_invMassB * PY;
  7156. w2 += this.m_invIB * L2
  7157. }
  7158. bA.m_linearVelocity.SetV(v1);
  7159. bA.m_angularVelocity = w1;
  7160. bB.m_linearVelocity.SetV(v2);
  7161. bB.m_angularVelocity = w2
  7162. };
  7163. b2PrismaticJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  7164. var limitC;
  7165. var oldLimitImpulse;
  7166. var bA = this.m_bodyA;
  7167. var bB = this.m_bodyB;
  7168. var c1 = bA.m_sweep.c;
  7169. var a1 = bA.m_sweep.a;
  7170. var c2 = bB.m_sweep.c;
  7171. var a2 = bB.m_sweep.a;
  7172. var tMat;
  7173. var tX;
  7174. var m1;
  7175. var m2;
  7176. var i1;
  7177. var i2;
  7178. var linearError = 0;
  7179. var angularError = 0;
  7180. var active = false;
  7181. var C2 = 0;
  7182. var R1 = b2Mat22.FromAngle(a1);
  7183. var R2 = b2Mat22.FromAngle(a2);
  7184. tMat = R1;
  7185. var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
  7186. var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
  7187. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7188. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7189. r1X = tX;
  7190. tMat = R2;
  7191. var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
  7192. var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
  7193. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7194. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7195. r2X = tX;
  7196. var dX = c2.x + r2X - c1.x - r1X;
  7197. var dY = c2.y + r2Y - c1.y - r1Y;
  7198. if(this.m_enableLimit) {
  7199. this.m_axis = b2Math.MulMV(R1, this.m_localXAxis1);
  7200. this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
  7201. this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
  7202. var translation = this.m_axis.x * dX + this.m_axis.y * dY;
  7203. if(b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2 * b2Settings.b2_linearSlop) {
  7204. C2 = b2Math.Clamp(translation, -b2Settings.b2_maxLinearCorrection, b2Settings.b2_maxLinearCorrection);
  7205. linearError = b2Math.Abs(translation);
  7206. active = true
  7207. }else {
  7208. if(translation <= this.m_lowerTranslation) {
  7209. C2 = b2Math.Clamp(translation - this.m_lowerTranslation + b2Settings.b2_linearSlop, -b2Settings.b2_maxLinearCorrection, 0);
  7210. linearError = this.m_lowerTranslation - translation;
  7211. active = true
  7212. }else {
  7213. if(translation >= this.m_upperTranslation) {
  7214. C2 = b2Math.Clamp(translation - this.m_upperTranslation + b2Settings.b2_linearSlop, 0, b2Settings.b2_maxLinearCorrection);
  7215. linearError = translation - this.m_upperTranslation;
  7216. active = true
  7217. }
  7218. }
  7219. }
  7220. }
  7221. this.m_perp = b2Math.MulMV(R1, this.m_localYAxis1);
  7222. this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
  7223. this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
  7224. var impulse = new b2Vec3;
  7225. var C1X = this.m_perp.x * dX + this.m_perp.y * dY;
  7226. var C1Y = a2 - a1 - this.m_refAngle;
  7227. linearError = b2Math.Max(linearError, b2Math.Abs(C1X));
  7228. angularError = b2Math.Abs(C1Y);
  7229. if(active) {
  7230. m1 = this.m_invMassA;
  7231. m2 = this.m_invMassB;
  7232. i1 = this.m_invIA;
  7233. i2 = this.m_invIB;
  7234. this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  7235. this.m_K.col1.y = i1 * this.m_s1 + i2 * this.m_s2;
  7236. this.m_K.col1.z = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
  7237. this.m_K.col2.x = this.m_K.col1.y;
  7238. this.m_K.col2.y = i1 + i2;
  7239. this.m_K.col2.z = i1 * this.m_a1 + i2 * this.m_a2;
  7240. this.m_K.col3.x = this.m_K.col1.z;
  7241. this.m_K.col3.y = this.m_K.col2.z;
  7242. this.m_K.col3.z = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
  7243. this.m_K.Solve33(impulse, -C1X, -C1Y, -C2)
  7244. }else {
  7245. m1 = this.m_invMassA;
  7246. m2 = this.m_invMassB;
  7247. i1 = this.m_invIA;
  7248. i2 = this.m_invIB;
  7249. var k11 = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  7250. var k12 = i1 * this.m_s1 + i2 * this.m_s2;
  7251. var k22 = i1 + i2;
  7252. this.m_K.col1.Set(k11, k12, 0);
  7253. this.m_K.col2.Set(k12, k22, 0);
  7254. var impulse1 = this.m_K.Solve22(new b2Vec2, -C1X, -C1Y);
  7255. impulse.x = impulse1.x;
  7256. impulse.y = impulse1.y;
  7257. impulse.z = 0
  7258. }
  7259. var PX = impulse.x * this.m_perp.x + impulse.z * this.m_axis.x;
  7260. var PY = impulse.x * this.m_perp.y + impulse.z * this.m_axis.y;
  7261. var L1 = impulse.x * this.m_s1 + impulse.y + impulse.z * this.m_a1;
  7262. var L2 = impulse.x * this.m_s2 + impulse.y + impulse.z * this.m_a2;
  7263. c1.x -= this.m_invMassA * PX;
  7264. c1.y -= this.m_invMassA * PY;
  7265. a1 -= this.m_invIA * L1;
  7266. c2.x += this.m_invMassB * PX;
  7267. c2.y += this.m_invMassB * PY;
  7268. a2 += this.m_invIB * L2;
  7269. bA.m_sweep.a = a1;
  7270. bB.m_sweep.a = a2;
  7271. bA.SynchronizeTransform();
  7272. bB.SynchronizeTransform();
  7273. return linearError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop
  7274. };
  7275. b2PrismaticJoint.prototype.GetAnchorA = function() {
  7276. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1)
  7277. };
  7278. b2PrismaticJoint.prototype.GetAnchorB = function() {
  7279. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2)
  7280. };
  7281. b2PrismaticJoint.prototype.GetReactionForce = function(inv_dt) {
  7282. 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))
  7283. };
  7284. b2PrismaticJoint.prototype.GetReactionTorque = function(inv_dt) {
  7285. return inv_dt * this.m_impulse.y
  7286. };
  7287. b2PrismaticJoint.prototype.GetJointTranslation = function() {
  7288. var bA = this.m_bodyA;
  7289. var bB = this.m_bodyB;
  7290. var tMat;
  7291. var p1 = bA.GetWorldPoint(this.m_localAnchor1);
  7292. var p2 = bB.GetWorldPoint(this.m_localAnchor2);
  7293. var dX = p2.x - p1.x;
  7294. var dY = p2.y - p1.y;
  7295. var axis = bA.GetWorldVector(this.m_localXAxis1);
  7296. var translation = axis.x * dX + axis.y * dY;
  7297. return translation
  7298. };
  7299. b2PrismaticJoint.prototype.GetJointSpeed = function() {
  7300. var bA = this.m_bodyA;
  7301. var bB = this.m_bodyB;
  7302. var tMat;
  7303. tMat = bA.m_xf.R;
  7304. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7305. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7306. var tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7307. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7308. r1X = tX;
  7309. tMat = bB.m_xf.R;
  7310. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7311. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7312. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7313. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7314. r2X = tX;
  7315. var p1X = bA.m_sweep.c.x + r1X;
  7316. var p1Y = bA.m_sweep.c.y + r1Y;
  7317. var p2X = bB.m_sweep.c.x + r2X;
  7318. var p2Y = bB.m_sweep.c.y + r2Y;
  7319. var dX = p2X - p1X;
  7320. var dY = p2Y - p1Y;
  7321. var axis = bA.GetWorldVector(this.m_localXAxis1);
  7322. var v1 = bA.m_linearVelocity;
  7323. var v2 = bB.m_linearVelocity;
  7324. var w1 = bA.m_angularVelocity;
  7325. var w2 = bB.m_angularVelocity;
  7326. 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));
  7327. return speed
  7328. };
  7329. b2PrismaticJoint.prototype.IsLimitEnabled = function() {
  7330. return this.m_enableLimit
  7331. };
  7332. b2PrismaticJoint.prototype.EnableLimit = function(flag) {
  7333. this.m_bodyA.SetAwake(true);
  7334. this.m_bodyB.SetAwake(true);
  7335. this.m_enableLimit = flag
  7336. };
  7337. b2PrismaticJoint.prototype.GetLowerLimit = function() {
  7338. return this.m_lowerTranslation
  7339. };
  7340. b2PrismaticJoint.prototype.GetUpperLimit = function() {
  7341. return this.m_upperTranslation
  7342. };
  7343. b2PrismaticJoint.prototype.SetLimits = function(lower, upper) {
  7344. this.m_bodyA.SetAwake(true);
  7345. this.m_bodyB.SetAwake(true);
  7346. this.m_lowerTranslation = lower;
  7347. this.m_upperTranslation = upper
  7348. };
  7349. b2PrismaticJoint.prototype.IsMotorEnabled = function() {
  7350. return this.m_enableMotor
  7351. };
  7352. b2PrismaticJoint.prototype.EnableMotor = function(flag) {
  7353. this.m_bodyA.SetAwake(true);
  7354. this.m_bodyB.SetAwake(true);
  7355. this.m_enableMotor = flag
  7356. };
  7357. b2PrismaticJoint.prototype.SetMotorSpeed = function(speed) {
  7358. this.m_bodyA.SetAwake(true);
  7359. this.m_bodyB.SetAwake(true);
  7360. this.m_motorSpeed = speed
  7361. };
  7362. b2PrismaticJoint.prototype.GetMotorSpeed = function() {
  7363. return this.m_motorSpeed
  7364. };
  7365. b2PrismaticJoint.prototype.SetMaxMotorForce = function(force) {
  7366. this.m_bodyA.SetAwake(true);
  7367. this.m_bodyB.SetAwake(true);
  7368. this.m_maxMotorForce = force
  7369. };
  7370. b2PrismaticJoint.prototype.GetMotorForce = function() {
  7371. return this.m_motorImpulse
  7372. };
  7373. b2PrismaticJoint.prototype.m_localAnchor1 = new b2Vec2;
  7374. b2PrismaticJoint.prototype.m_localAnchor2 = new b2Vec2;
  7375. b2PrismaticJoint.prototype.m_localXAxis1 = new b2Vec2;
  7376. b2PrismaticJoint.prototype.m_localYAxis1 = new b2Vec2;
  7377. b2PrismaticJoint.prototype.m_refAngle = null;
  7378. b2PrismaticJoint.prototype.m_axis = new b2Vec2;
  7379. b2PrismaticJoint.prototype.m_perp = new b2Vec2;
  7380. b2PrismaticJoint.prototype.m_s1 = null;
  7381. b2PrismaticJoint.prototype.m_s2 = null;
  7382. b2PrismaticJoint.prototype.m_a1 = null;
  7383. b2PrismaticJoint.prototype.m_a2 = null;
  7384. b2PrismaticJoint.prototype.m_K = new b2Mat33;
  7385. b2PrismaticJoint.prototype.m_impulse = new b2Vec3;
  7386. b2PrismaticJoint.prototype.m_motorMass = null;
  7387. b2PrismaticJoint.prototype.m_motorImpulse = null;
  7388. b2PrismaticJoint.prototype.m_lowerTranslation = null;
  7389. b2PrismaticJoint.prototype.m_upperTranslation = null;
  7390. b2PrismaticJoint.prototype.m_maxMotorForce = null;
  7391. b2PrismaticJoint.prototype.m_motorSpeed = null;
  7392. b2PrismaticJoint.prototype.m_enableLimit = null;
  7393. b2PrismaticJoint.prototype.m_enableMotor = null;
  7394. b2PrismaticJoint.prototype.m_limitState = 0;var b2RevoluteJoint = function() {
  7395. b2Joint.prototype.__varz.call(this);
  7396. this.__varz();
  7397. this.__constructor.apply(this, arguments)
  7398. };
  7399. extend(b2RevoluteJoint.prototype, b2Joint.prototype);
  7400. b2RevoluteJoint.prototype._super = b2Joint.prototype;
  7401. b2RevoluteJoint.prototype.__constructor = function(def) {
  7402. this._super.__constructor.apply(this, [def]);
  7403. this.m_localAnchor1.SetV(def.localAnchorA);
  7404. this.m_localAnchor2.SetV(def.localAnchorB);
  7405. this.m_referenceAngle = def.referenceAngle;
  7406. this.m_impulse.SetZero();
  7407. this.m_motorImpulse = 0;
  7408. this.m_lowerAngle = def.lowerAngle;
  7409. this.m_upperAngle = def.upperAngle;
  7410. this.m_maxMotorTorque = def.maxMotorTorque;
  7411. this.m_motorSpeed = def.motorSpeed;
  7412. this.m_enableLimit = def.enableLimit;
  7413. this.m_enableMotor = def.enableMotor;
  7414. this.m_limitState = b2Joint.e_inactiveLimit
  7415. };
  7416. b2RevoluteJoint.prototype.__varz = function() {
  7417. this.K = new b2Mat22;
  7418. this.K1 = new b2Mat22;
  7419. this.K2 = new b2Mat22;
  7420. this.K3 = new b2Mat22;
  7421. this.impulse3 = new b2Vec3;
  7422. this.impulse2 = new b2Vec2;
  7423. this.reduced = new b2Vec2;
  7424. this.m_localAnchor1 = new b2Vec2;
  7425. this.m_localAnchor2 = new b2Vec2;
  7426. this.m_impulse = new b2Vec3;
  7427. this.m_mass = new b2Mat33
  7428. };
  7429. b2RevoluteJoint.tImpulse = new b2Vec2;
  7430. b2RevoluteJoint.prototype.InitVelocityConstraints = function(step) {
  7431. var bA = this.m_bodyA;
  7432. var bB = this.m_bodyB;
  7433. var tMat;
  7434. var tX;
  7435. if(this.m_enableMotor || this.m_enableLimit) {
  7436. }
  7437. tMat = bA.m_xf.R;
  7438. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7439. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7440. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7441. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7442. r1X = tX;
  7443. tMat = bB.m_xf.R;
  7444. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7445. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7446. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7447. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7448. r2X = tX;
  7449. var m1 = bA.m_invMass;
  7450. var m2 = bB.m_invMass;
  7451. var i1 = bA.m_invI;
  7452. var i2 = bB.m_invI;
  7453. this.m_mass.col1.x = m1 + m2 + r1Y * r1Y * i1 + r2Y * r2Y * i2;
  7454. this.m_mass.col2.x = -r1Y * r1X * i1 - r2Y * r2X * i2;
  7455. this.m_mass.col3.x = -r1Y * i1 - r2Y * i2;
  7456. this.m_mass.col1.y = this.m_mass.col2.x;
  7457. this.m_mass.col2.y = m1 + m2 + r1X * r1X * i1 + r2X * r2X * i2;
  7458. this.m_mass.col3.y = r1X * i1 + r2X * i2;
  7459. this.m_mass.col1.z = this.m_mass.col3.x;
  7460. this.m_mass.col2.z = this.m_mass.col3.y;
  7461. this.m_mass.col3.z = i1 + i2;
  7462. this.m_motorMass = 1 / (i1 + i2);
  7463. if(this.m_enableMotor == false) {
  7464. this.m_motorImpulse = 0
  7465. }
  7466. if(this.m_enableLimit) {
  7467. var jointAngle = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  7468. if(b2Math.Abs(this.m_upperAngle - this.m_lowerAngle) < 2 * b2Settings.b2_angularSlop) {
  7469. this.m_limitState = b2Joint.e_equalLimits
  7470. }else {
  7471. if(jointAngle <= this.m_lowerAngle) {
  7472. if(this.m_limitState != b2Joint.e_atLowerLimit) {
  7473. this.m_impulse.z = 0
  7474. }
  7475. this.m_limitState = b2Joint.e_atLowerLimit
  7476. }else {
  7477. if(jointAngle >= this.m_upperAngle) {
  7478. if(this.m_limitState != b2Joint.e_atUpperLimit) {
  7479. this.m_impulse.z = 0
  7480. }
  7481. this.m_limitState = b2Joint.e_atUpperLimit
  7482. }else {
  7483. this.m_limitState = b2Joint.e_inactiveLimit;
  7484. this.m_impulse.z = 0
  7485. }
  7486. }
  7487. }
  7488. }else {
  7489. this.m_limitState = b2Joint.e_inactiveLimit
  7490. }
  7491. if(step.warmStarting) {
  7492. this.m_impulse.x *= step.dtRatio;
  7493. this.m_impulse.y *= step.dtRatio;
  7494. this.m_motorImpulse *= step.dtRatio;
  7495. var PX = this.m_impulse.x;
  7496. var PY = this.m_impulse.y;
  7497. bA.m_linearVelocity.x -= m1 * PX;
  7498. bA.m_linearVelocity.y -= m1 * PY;
  7499. bA.m_angularVelocity -= i1 * (r1X * PY - r1Y * PX + this.m_motorImpulse + this.m_impulse.z);
  7500. bB.m_linearVelocity.x += m2 * PX;
  7501. bB.m_linearVelocity.y += m2 * PY;
  7502. bB.m_angularVelocity += i2 * (r2X * PY - r2Y * PX + this.m_motorImpulse + this.m_impulse.z)
  7503. }else {
  7504. this.m_impulse.SetZero();
  7505. this.m_motorImpulse = 0
  7506. }
  7507. };
  7508. b2RevoluteJoint.prototype.SolveVelocityConstraints = function(step) {
  7509. var bA = this.m_bodyA;
  7510. var bB = this.m_bodyB;
  7511. var tMat;
  7512. var tX;
  7513. var newImpulse;
  7514. var r1X;
  7515. var r1Y;
  7516. var r2X;
  7517. var r2Y;
  7518. var v1 = bA.m_linearVelocity;
  7519. var w1 = bA.m_angularVelocity;
  7520. var v2 = bB.m_linearVelocity;
  7521. var w2 = bB.m_angularVelocity;
  7522. var m1 = bA.m_invMass;
  7523. var m2 = bB.m_invMass;
  7524. var i1 = bA.m_invI;
  7525. var i2 = bB.m_invI;
  7526. if(this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
  7527. var Cdot = w2 - w1 - this.m_motorSpeed;
  7528. var impulse = this.m_motorMass * -Cdot;
  7529. var oldImpulse = this.m_motorImpulse;
  7530. var maxImpulse = step.dt * this.m_maxMotorTorque;
  7531. this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, -maxImpulse, maxImpulse);
  7532. impulse = this.m_motorImpulse - oldImpulse;
  7533. w1 -= i1 * impulse;
  7534. w2 += i2 * impulse
  7535. }
  7536. if(this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  7537. tMat = bA.m_xf.R;
  7538. r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7539. r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7540. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7541. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7542. r1X = tX;
  7543. tMat = bB.m_xf.R;
  7544. r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7545. r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7546. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7547. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7548. r2X = tX;
  7549. var Cdot1X = v2.x + -w2 * r2Y - v1.x - -w1 * r1Y;
  7550. var Cdot1Y = v2.y + w2 * r2X - v1.y - w1 * r1X;
  7551. var Cdot2 = w2 - w1;
  7552. this.m_mass.Solve33(this.impulse3, -Cdot1X, -Cdot1Y, -Cdot2);
  7553. if(this.m_limitState == b2Joint.e_equalLimits) {
  7554. this.m_impulse.Add(this.impulse3)
  7555. }else {
  7556. if(this.m_limitState == b2Joint.e_atLowerLimit) {
  7557. newImpulse = this.m_impulse.z + this.impulse3.z;
  7558. if(newImpulse < 0) {
  7559. this.m_mass.Solve22(this.reduced, -Cdot1X, -Cdot1Y);
  7560. this.impulse3.x = this.reduced.x;
  7561. this.impulse3.y = this.reduced.y;
  7562. this.impulse3.z = -this.m_impulse.z;
  7563. this.m_impulse.x += this.reduced.x;
  7564. this.m_impulse.y += this.reduced.y;
  7565. this.m_impulse.z = 0
  7566. }
  7567. }else {
  7568. if(this.m_limitState == b2Joint.e_atUpperLimit) {
  7569. newImpulse = this.m_impulse.z + this.impulse3.z;
  7570. if(newImpulse > 0) {
  7571. this.m_mass.Solve22(this.reduced, -Cdot1X, -Cdot1Y);
  7572. this.impulse3.x = this.reduced.x;
  7573. this.impulse3.y = this.reduced.y;
  7574. this.impulse3.z = -this.m_impulse.z;
  7575. this.m_impulse.x += this.reduced.x;
  7576. this.m_impulse.y += this.reduced.y;
  7577. this.m_impulse.z = 0
  7578. }
  7579. }
  7580. }
  7581. }
  7582. v1.x -= m1 * this.impulse3.x;
  7583. v1.y -= m1 * this.impulse3.y;
  7584. w1 -= i1 * (r1X * this.impulse3.y - r1Y * this.impulse3.x + this.impulse3.z);
  7585. v2.x += m2 * this.impulse3.x;
  7586. v2.y += m2 * this.impulse3.y;
  7587. w2 += i2 * (r2X * this.impulse3.y - r2Y * this.impulse3.x + this.impulse3.z)
  7588. }else {
  7589. tMat = bA.m_xf.R;
  7590. r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7591. r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7592. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7593. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7594. r1X = tX;
  7595. tMat = bB.m_xf.R;
  7596. r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7597. r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7598. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7599. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7600. r2X = tX;
  7601. var CdotX = v2.x + -w2 * r2Y - v1.x - -w1 * r1Y;
  7602. var CdotY = v2.y + w2 * r2X - v1.y - w1 * r1X;
  7603. this.m_mass.Solve22(this.impulse2, -CdotX, -CdotY);
  7604. this.m_impulse.x += this.impulse2.x;
  7605. this.m_impulse.y += this.impulse2.y;
  7606. v1.x -= m1 * this.impulse2.x;
  7607. v1.y -= m1 * this.impulse2.y;
  7608. w1 -= i1 * (r1X * this.impulse2.y - r1Y * this.impulse2.x);
  7609. v2.x += m2 * this.impulse2.x;
  7610. v2.y += m2 * this.impulse2.y;
  7611. w2 += i2 * (r2X * this.impulse2.y - r2Y * this.impulse2.x)
  7612. }
  7613. bA.m_linearVelocity.SetV(v1);
  7614. bA.m_angularVelocity = w1;
  7615. bB.m_linearVelocity.SetV(v2);
  7616. bB.m_angularVelocity = w2
  7617. };
  7618. b2RevoluteJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  7619. var oldLimitImpulse;
  7620. var C;
  7621. var tMat;
  7622. var bA = this.m_bodyA;
  7623. var bB = this.m_bodyB;
  7624. var angularError = 0;
  7625. var positionError = 0;
  7626. var tX;
  7627. var impulseX;
  7628. var impulseY;
  7629. if(this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  7630. var angle = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  7631. var limitImpulse = 0;
  7632. if(this.m_limitState == b2Joint.e_equalLimits) {
  7633. C = b2Math.Clamp(angle - this.m_lowerAngle, -b2Settings.b2_maxAngularCorrection, b2Settings.b2_maxAngularCorrection);
  7634. limitImpulse = -this.m_motorMass * C;
  7635. angularError = b2Math.Abs(C)
  7636. }else {
  7637. if(this.m_limitState == b2Joint.e_atLowerLimit) {
  7638. C = angle - this.m_lowerAngle;
  7639. angularError = -C;
  7640. C = b2Math.Clamp(C + b2Settings.b2_angularSlop, -b2Settings.b2_maxAngularCorrection, 0);
  7641. limitImpulse = -this.m_motorMass * C
  7642. }else {
  7643. if(this.m_limitState == b2Joint.e_atUpperLimit) {
  7644. C = angle - this.m_upperAngle;
  7645. angularError = C;
  7646. C = b2Math.Clamp(C - b2Settings.b2_angularSlop, 0, b2Settings.b2_maxAngularCorrection);
  7647. limitImpulse = -this.m_motorMass * C
  7648. }
  7649. }
  7650. }
  7651. bA.m_sweep.a -= bA.m_invI * limitImpulse;
  7652. bB.m_sweep.a += bB.m_invI * limitImpulse;
  7653. bA.SynchronizeTransform();
  7654. bB.SynchronizeTransform()
  7655. }
  7656. tMat = bA.m_xf.R;
  7657. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7658. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7659. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7660. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7661. r1X = tX;
  7662. tMat = bB.m_xf.R;
  7663. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7664. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7665. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7666. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7667. r2X = tX;
  7668. var CX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  7669. var CY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  7670. var CLengthSquared = CX * CX + CY * CY;
  7671. var CLength = Math.sqrt(CLengthSquared);
  7672. positionError = CLength;
  7673. var invMass1 = bA.m_invMass;
  7674. var invMass2 = bB.m_invMass;
  7675. var invI1 = bA.m_invI;
  7676. var invI2 = bB.m_invI;
  7677. var k_allowedStretch = 10 * b2Settings.b2_linearSlop;
  7678. if(CLengthSquared > k_allowedStretch * k_allowedStretch) {
  7679. var uX = CX / CLength;
  7680. var uY = CY / CLength;
  7681. var k = invMass1 + invMass2;
  7682. var m = 1 / k;
  7683. impulseX = m * -CX;
  7684. impulseY = m * -CY;
  7685. var k_beta = 0.5;
  7686. bA.m_sweep.c.x -= k_beta * invMass1 * impulseX;
  7687. bA.m_sweep.c.y -= k_beta * invMass1 * impulseY;
  7688. bB.m_sweep.c.x += k_beta * invMass2 * impulseX;
  7689. bB.m_sweep.c.y += k_beta * invMass2 * impulseY;
  7690. CX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  7691. CY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y
  7692. }
  7693. this.K1.col1.x = invMass1 + invMass2;
  7694. this.K1.col2.x = 0;
  7695. this.K1.col1.y = 0;
  7696. this.K1.col2.y = invMass1 + invMass2;
  7697. this.K2.col1.x = invI1 * r1Y * r1Y;
  7698. this.K2.col2.x = -invI1 * r1X * r1Y;
  7699. this.K2.col1.y = -invI1 * r1X * r1Y;
  7700. this.K2.col2.y = invI1 * r1X * r1X;
  7701. this.K3.col1.x = invI2 * r2Y * r2Y;
  7702. this.K3.col2.x = -invI2 * r2X * r2Y;
  7703. this.K3.col1.y = -invI2 * r2X * r2Y;
  7704. this.K3.col2.y = invI2 * r2X * r2X;
  7705. this.K.SetM(this.K1);
  7706. this.K.AddM(this.K2);
  7707. this.K.AddM(this.K3);
  7708. this.K.Solve(b2RevoluteJoint.tImpulse, -CX, -CY);
  7709. impulseX = b2RevoluteJoint.tImpulse.x;
  7710. impulseY = b2RevoluteJoint.tImpulse.y;
  7711. bA.m_sweep.c.x -= bA.m_invMass * impulseX;
  7712. bA.m_sweep.c.y -= bA.m_invMass * impulseY;
  7713. bA.m_sweep.a -= bA.m_invI * (r1X * impulseY - r1Y * impulseX);
  7714. bB.m_sweep.c.x += bB.m_invMass * impulseX;
  7715. bB.m_sweep.c.y += bB.m_invMass * impulseY;
  7716. bB.m_sweep.a += bB.m_invI * (r2X * impulseY - r2Y * impulseX);
  7717. bA.SynchronizeTransform();
  7718. bB.SynchronizeTransform();
  7719. return positionError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop
  7720. };
  7721. b2RevoluteJoint.prototype.GetAnchorA = function() {
  7722. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1)
  7723. };
  7724. b2RevoluteJoint.prototype.GetAnchorB = function() {
  7725. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2)
  7726. };
  7727. b2RevoluteJoint.prototype.GetReactionForce = function(inv_dt) {
  7728. return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y)
  7729. };
  7730. b2RevoluteJoint.prototype.GetReactionTorque = function(inv_dt) {
  7731. return inv_dt * this.m_impulse.z
  7732. };
  7733. b2RevoluteJoint.prototype.GetJointAngle = function() {
  7734. return this.m_bodyB.m_sweep.a - this.m_bodyA.m_sweep.a - this.m_referenceAngle
  7735. };
  7736. b2RevoluteJoint.prototype.GetJointSpeed = function() {
  7737. return this.m_bodyB.m_angularVelocity - this.m_bodyA.m_angularVelocity
  7738. };
  7739. b2RevoluteJoint.prototype.IsLimitEnabled = function() {
  7740. return this.m_enableLimit
  7741. };
  7742. b2RevoluteJoint.prototype.EnableLimit = function(flag) {
  7743. this.m_enableLimit = flag
  7744. };
  7745. b2RevoluteJoint.prototype.GetLowerLimit = function() {
  7746. return this.m_lowerAngle
  7747. };
  7748. b2RevoluteJoint.prototype.GetUpperLimit = function() {
  7749. return this.m_upperAngle
  7750. };
  7751. b2RevoluteJoint.prototype.SetLimits = function(lower, upper) {
  7752. this.m_lowerAngle = lower;
  7753. this.m_upperAngle = upper
  7754. };
  7755. b2RevoluteJoint.prototype.IsMotorEnabled = function() {
  7756. this.m_bodyA.SetAwake(true);
  7757. this.m_bodyB.SetAwake(true);
  7758. return this.m_enableMotor
  7759. };
  7760. b2RevoluteJoint.prototype.EnableMotor = function(flag) {
  7761. this.m_enableMotor = flag
  7762. };
  7763. b2RevoluteJoint.prototype.SetMotorSpeed = function(speed) {
  7764. this.m_bodyA.SetAwake(true);
  7765. this.m_bodyB.SetAwake(true);
  7766. this.m_motorSpeed = speed
  7767. };
  7768. b2RevoluteJoint.prototype.GetMotorSpeed = function() {
  7769. return this.m_motorSpeed
  7770. };
  7771. b2RevoluteJoint.prototype.SetMaxMotorTorque = function(torque) {
  7772. this.m_maxMotorTorque = torque
  7773. };
  7774. b2RevoluteJoint.prototype.GetMotorTorque = function() {
  7775. return this.m_maxMotorTorque
  7776. };
  7777. b2RevoluteJoint.prototype.K = new b2Mat22;
  7778. b2RevoluteJoint.prototype.K1 = new b2Mat22;
  7779. b2RevoluteJoint.prototype.K2 = new b2Mat22;
  7780. b2RevoluteJoint.prototype.K3 = new b2Mat22;
  7781. b2RevoluteJoint.prototype.impulse3 = new b2Vec3;
  7782. b2RevoluteJoint.prototype.impulse2 = new b2Vec2;
  7783. b2RevoluteJoint.prototype.reduced = new b2Vec2;
  7784. b2RevoluteJoint.prototype.m_localAnchor1 = new b2Vec2;
  7785. b2RevoluteJoint.prototype.m_localAnchor2 = new b2Vec2;
  7786. b2RevoluteJoint.prototype.m_impulse = new b2Vec3;
  7787. b2RevoluteJoint.prototype.m_motorImpulse = null;
  7788. b2RevoluteJoint.prototype.m_mass = new b2Mat33;
  7789. b2RevoluteJoint.prototype.m_motorMass = null;
  7790. b2RevoluteJoint.prototype.m_enableMotor = null;
  7791. b2RevoluteJoint.prototype.m_maxMotorTorque = null;
  7792. b2RevoluteJoint.prototype.m_motorSpeed = null;
  7793. b2RevoluteJoint.prototype.m_enableLimit = null;
  7794. b2RevoluteJoint.prototype.m_referenceAngle = null;
  7795. b2RevoluteJoint.prototype.m_lowerAngle = null;
  7796. b2RevoluteJoint.prototype.m_upperAngle = null;
  7797. b2RevoluteJoint.prototype.m_limitState = 0;var b2JointDef = function() {
  7798. this.__varz();
  7799. this.__constructor.apply(this, arguments)
  7800. };
  7801. b2JointDef.prototype.__constructor = function() {
  7802. this.type = b2Joint.e_unknownJoint;
  7803. this.userData = null;
  7804. this.bodyA = null;
  7805. this.bodyB = null;
  7806. this.collideConnected = false
  7807. };
  7808. b2JointDef.prototype.__varz = function() {
  7809. };
  7810. b2JointDef.prototype.type = 0;
  7811. b2JointDef.prototype.userData = null;
  7812. b2JointDef.prototype.bodyA = null;
  7813. b2JointDef.prototype.bodyB = null;
  7814. b2JointDef.prototype.collideConnected = null;var b2LineJointDef = function() {
  7815. b2JointDef.prototype.__varz.call(this);
  7816. this.__varz();
  7817. this.__constructor.apply(this, arguments)
  7818. };
  7819. extend(b2LineJointDef.prototype, b2JointDef.prototype);
  7820. b2LineJointDef.prototype._super = b2JointDef.prototype;
  7821. b2LineJointDef.prototype.__constructor = function() {
  7822. this._super.__constructor.apply(this, arguments);
  7823. this.type = b2Joint.e_lineJoint;
  7824. this.localAxisA.Set(1, 0);
  7825. this.enableLimit = false;
  7826. this.lowerTranslation = 0;
  7827. this.upperTranslation = 0;
  7828. this.enableMotor = false;
  7829. this.maxMotorForce = 0;
  7830. this.motorSpeed = 0
  7831. };
  7832. b2LineJointDef.prototype.__varz = function() {
  7833. this.localAnchorA = new b2Vec2;
  7834. this.localAnchorB = new b2Vec2;
  7835. this.localAxisA = new b2Vec2
  7836. };
  7837. b2LineJointDef.prototype.Initialize = function(bA, bB, anchor, axis) {
  7838. this.bodyA = bA;
  7839. this.bodyB = bB;
  7840. this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
  7841. this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
  7842. this.localAxisA = this.bodyA.GetLocalVector(axis)
  7843. };
  7844. b2LineJointDef.prototype.localAnchorA = new b2Vec2;
  7845. b2LineJointDef.prototype.localAnchorB = new b2Vec2;
  7846. b2LineJointDef.prototype.localAxisA = new b2Vec2;
  7847. b2LineJointDef.prototype.enableLimit = null;
  7848. b2LineJointDef.prototype.lowerTranslation = null;
  7849. b2LineJointDef.prototype.upperTranslation = null;
  7850. b2LineJointDef.prototype.enableMotor = null;
  7851. b2LineJointDef.prototype.maxMotorForce = null;
  7852. b2LineJointDef.prototype.motorSpeed = null;var b2DistanceJoint = function() {
  7853. b2Joint.prototype.__varz.call(this);
  7854. this.__varz();
  7855. this.__constructor.apply(this, arguments)
  7856. };
  7857. extend(b2DistanceJoint.prototype, b2Joint.prototype);
  7858. b2DistanceJoint.prototype._super = b2Joint.prototype;
  7859. b2DistanceJoint.prototype.__constructor = function(def) {
  7860. this._super.__constructor.apply(this, [def]);
  7861. var tMat;
  7862. var tX;
  7863. var tY;
  7864. this.m_localAnchor1.SetV(def.localAnchorA);
  7865. this.m_localAnchor2.SetV(def.localAnchorB);
  7866. this.m_length = def.length;
  7867. this.m_frequencyHz = def.frequencyHz;
  7868. this.m_dampingRatio = def.dampingRatio;
  7869. this.m_impulse = 0;
  7870. this.m_gamma = 0;
  7871. this.m_bias = 0
  7872. };
  7873. b2DistanceJoint.prototype.__varz = function() {
  7874. this.m_localAnchor1 = new b2Vec2;
  7875. this.m_localAnchor2 = new b2Vec2;
  7876. this.m_u = new b2Vec2
  7877. };
  7878. b2DistanceJoint.prototype.InitVelocityConstraints = function(step) {
  7879. var tMat;
  7880. var tX;
  7881. var bA = this.m_bodyA;
  7882. var bB = this.m_bodyB;
  7883. tMat = bA.m_xf.R;
  7884. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7885. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7886. tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7887. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7888. r1X = tX;
  7889. tMat = bB.m_xf.R;
  7890. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7891. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7892. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7893. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7894. r2X = tX;
  7895. this.m_u.x = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  7896. this.m_u.y = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  7897. var length = Math.sqrt(this.m_u.x * this.m_u.x + this.m_u.y * this.m_u.y);
  7898. if(length > b2Settings.b2_linearSlop) {
  7899. this.m_u.Multiply(1 / length)
  7900. }else {
  7901. this.m_u.SetZero()
  7902. }
  7903. var cr1u = r1X * this.m_u.y - r1Y * this.m_u.x;
  7904. var cr2u = r2X * this.m_u.y - r2Y * this.m_u.x;
  7905. var invMass = bA.m_invMass + bA.m_invI * cr1u * cr1u + bB.m_invMass + bB.m_invI * cr2u * cr2u;
  7906. this.m_mass = invMass != 0 ? 1 / invMass : 0;
  7907. if(this.m_frequencyHz > 0) {
  7908. var C = length - this.m_length;
  7909. var omega = 2 * Math.PI * this.m_frequencyHz;
  7910. var d = 2 * this.m_mass * this.m_dampingRatio * omega;
  7911. var k = this.m_mass * omega * omega;
  7912. this.m_gamma = step.dt * (d + step.dt * k);
  7913. this.m_gamma = this.m_gamma != 0 ? 1 / this.m_gamma : 0;
  7914. this.m_bias = C * step.dt * k * this.m_gamma;
  7915. this.m_mass = invMass + this.m_gamma;
  7916. this.m_mass = this.m_mass != 0 ? 1 / this.m_mass : 0
  7917. }
  7918. if(step.warmStarting) {
  7919. this.m_impulse *= step.dtRatio;
  7920. var PX = this.m_impulse * this.m_u.x;
  7921. var PY = this.m_impulse * this.m_u.y;
  7922. bA.m_linearVelocity.x -= bA.m_invMass * PX;
  7923. bA.m_linearVelocity.y -= bA.m_invMass * PY;
  7924. bA.m_angularVelocity -= bA.m_invI * (r1X * PY - r1Y * PX);
  7925. bB.m_linearVelocity.x += bB.m_invMass * PX;
  7926. bB.m_linearVelocity.y += bB.m_invMass * PY;
  7927. bB.m_angularVelocity += bB.m_invI * (r2X * PY - r2Y * PX)
  7928. }else {
  7929. this.m_impulse = 0
  7930. }
  7931. };
  7932. b2DistanceJoint.prototype.SolveVelocityConstraints = function(step) {
  7933. var tMat;
  7934. var bA = this.m_bodyA;
  7935. var bB = this.m_bodyB;
  7936. tMat = bA.m_xf.R;
  7937. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7938. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7939. var tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7940. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7941. r1X = tX;
  7942. tMat = bB.m_xf.R;
  7943. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7944. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7945. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7946. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7947. r2X = tX;
  7948. var v1X = bA.m_linearVelocity.x + -bA.m_angularVelocity * r1Y;
  7949. var v1Y = bA.m_linearVelocity.y + bA.m_angularVelocity * r1X;
  7950. var v2X = bB.m_linearVelocity.x + -bB.m_angularVelocity * r2Y;
  7951. var v2Y = bB.m_linearVelocity.y + bB.m_angularVelocity * r2X;
  7952. var Cdot = this.m_u.x * (v2X - v1X) + this.m_u.y * (v2Y - v1Y);
  7953. var impulse = -this.m_mass * (Cdot + this.m_bias + this.m_gamma * this.m_impulse);
  7954. this.m_impulse += impulse;
  7955. var PX = impulse * this.m_u.x;
  7956. var PY = impulse * this.m_u.y;
  7957. bA.m_linearVelocity.x -= bA.m_invMass * PX;
  7958. bA.m_linearVelocity.y -= bA.m_invMass * PY;
  7959. bA.m_angularVelocity -= bA.m_invI * (r1X * PY - r1Y * PX);
  7960. bB.m_linearVelocity.x += bB.m_invMass * PX;
  7961. bB.m_linearVelocity.y += bB.m_invMass * PY;
  7962. bB.m_angularVelocity += bB.m_invI * (r2X * PY - r2Y * PX)
  7963. };
  7964. b2DistanceJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  7965. var tMat;
  7966. if(this.m_frequencyHz > 0) {
  7967. return true
  7968. }
  7969. var bA = this.m_bodyA;
  7970. var bB = this.m_bodyB;
  7971. tMat = bA.m_xf.R;
  7972. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7973. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7974. var tX = tMat.col1.x * r1X + tMat.col2.x * r1Y;
  7975. r1Y = tMat.col1.y * r1X + tMat.col2.y * r1Y;
  7976. r1X = tX;
  7977. tMat = bB.m_xf.R;
  7978. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7979. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7980. tX = tMat.col1.x * r2X + tMat.col2.x * r2Y;
  7981. r2Y = tMat.col1.y * r2X + tMat.col2.y * r2Y;
  7982. r2X = tX;
  7983. var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  7984. var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  7985. var length = Math.sqrt(dX * dX + dY * dY);
  7986. dX /= length;
  7987. dY /= length;
  7988. var C = length - this.m_length;
  7989. C = b2Math.Clamp(C, -b2Settings.b2_maxLinearCorrection, b2Settings.b2_maxLinearCorrection);
  7990. var impulse = -this.m_mass * C;
  7991. this.m_u.Set(dX, dY);
  7992. var PX = impulse * this.m_u.x;
  7993. var PY = impulse * this.m_u.y;
  7994. bA.m_sweep.c.x -= bA.m_invMass * PX;
  7995. bA.m_sweep.c.y -= bA.m_invMass * PY;
  7996. bA.m_sweep.a -= bA.m_invI * (r1X * PY - r1Y * PX);
  7997. bB.m_sweep.c.x += bB.m_invMass * PX;
  7998. bB.m_sweep.c.y += bB.m_invMass * PY;
  7999. bB.m_sweep.a += bB.m_invI * (r2X * PY - r2Y * PX);
  8000. bA.SynchronizeTransform();
  8001. bB.SynchronizeTransform();
  8002. return b2Math.Abs(C) < b2Settings.b2_linearSlop
  8003. };
  8004. b2DistanceJoint.prototype.GetAnchorA = function() {
  8005. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1)
  8006. };
  8007. b2DistanceJoint.prototype.GetAnchorB = function() {
  8008. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2)
  8009. };
  8010. b2DistanceJoint.prototype.GetReactionForce = function(inv_dt) {
  8011. return new b2Vec2(inv_dt * this.m_impulse * this.m_u.x, inv_dt * this.m_impulse * this.m_u.y)
  8012. };
  8013. b2DistanceJoint.prototype.GetReactionTorque = function(inv_dt) {
  8014. return 0
  8015. };
  8016. b2DistanceJoint.prototype.GetLength = function() {
  8017. return this.m_length
  8018. };
  8019. b2DistanceJoint.prototype.SetLength = function(length) {
  8020. this.m_length = length
  8021. };
  8022. b2DistanceJoint.prototype.GetFrequency = function() {
  8023. return this.m_frequencyHz
  8024. };
  8025. b2DistanceJoint.prototype.SetFrequency = function(hz) {
  8026. this.m_frequencyHz = hz
  8027. };
  8028. b2DistanceJoint.prototype.GetDampingRatio = function() {
  8029. return this.m_dampingRatio
  8030. };
  8031. b2DistanceJoint.prototype.SetDampingRatio = function(ratio) {
  8032. this.m_dampingRatio = ratio
  8033. };
  8034. b2DistanceJoint.prototype.m_localAnchor1 = new b2Vec2;
  8035. b2DistanceJoint.prototype.m_localAnchor2 = new b2Vec2;
  8036. b2DistanceJoint.prototype.m_u = new b2Vec2;
  8037. b2DistanceJoint.prototype.m_frequencyHz = null;
  8038. b2DistanceJoint.prototype.m_dampingRatio = null;
  8039. b2DistanceJoint.prototype.m_gamma = null;
  8040. b2DistanceJoint.prototype.m_bias = null;
  8041. b2DistanceJoint.prototype.m_impulse = null;
  8042. b2DistanceJoint.prototype.m_mass = null;
  8043. b2DistanceJoint.prototype.m_length = null;var b2PulleyJointDef = function() {
  8044. b2JointDef.prototype.__varz.call(this);
  8045. this.__varz();
  8046. this.__constructor.apply(this, arguments)
  8047. };
  8048. extend(b2PulleyJointDef.prototype, b2JointDef.prototype);
  8049. b2PulleyJointDef.prototype._super = b2JointDef.prototype;
  8050. b2PulleyJointDef.prototype.__constructor = function() {
  8051. this._super.__constructor.apply(this, arguments);
  8052. this.type = b2Joint.e_pulleyJoint;
  8053. this.groundAnchorA.Set(-1, 1);
  8054. this.groundAnchorB.Set(1, 1);
  8055. this.localAnchorA.Set(-1, 0);
  8056. this.localAnchorB.Set(1, 0);
  8057. this.lengthA = 0;
  8058. this.maxLengthA = 0;
  8059. this.lengthB = 0;
  8060. this.maxLengthB = 0;
  8061. this.ratio = 1;
  8062. this.collideConnected = true
  8063. };
  8064. b2PulleyJointDef.prototype.__varz = function() {
  8065. this.groundAnchorA = new b2Vec2;
  8066. this.groundAnchorB = new b2Vec2;
  8067. this.localAnchorA = new b2Vec2;
  8068. this.localAnchorB = new b2Vec2
  8069. };
  8070. b2PulleyJointDef.prototype.Initialize = function(bA, bB, gaA, gaB, anchorA, anchorB, r) {
  8071. this.bodyA = bA;
  8072. this.bodyB = bB;
  8073. this.groundAnchorA.SetV(gaA);
  8074. this.groundAnchorB.SetV(gaB);
  8075. this.localAnchorA = this.bodyA.GetLocalPoint(anchorA);
  8076. this.localAnchorB = this.bodyB.GetLocalPoint(anchorB);
  8077. var d1X = anchorA.x - gaA.x;
  8078. var d1Y = anchorA.y - gaA.y;
  8079. this.lengthA = Math.sqrt(d1X * d1X + d1Y * d1Y);
  8080. var d2X = anchorB.x - gaB.x;
  8081. var d2Y = anchorB.y - gaB.y;
  8082. this.lengthB = Math.sqrt(d2X * d2X + d2Y * d2Y);
  8083. this.ratio = r;
  8084. var C = this.lengthA + this.ratio * this.lengthB;
  8085. this.maxLengthA = C - this.ratio * b2PulleyJoint.b2_minPulleyLength;
  8086. this.maxLengthB = (C - b2PulleyJoint.b2_minPulleyLength) / this.ratio
  8087. };
  8088. b2PulleyJointDef.prototype.groundAnchorA = new b2Vec2;
  8089. b2PulleyJointDef.prototype.groundAnchorB = new b2Vec2;
  8090. b2PulleyJointDef.prototype.localAnchorA = new b2Vec2;
  8091. b2PulleyJointDef.prototype.localAnchorB = new b2Vec2;
  8092. b2PulleyJointDef.prototype.lengthA = null;
  8093. b2PulleyJointDef.prototype.maxLengthA = null;
  8094. b2PulleyJointDef.prototype.lengthB = null;
  8095. b2PulleyJointDef.prototype.maxLengthB = null;
  8096. b2PulleyJointDef.prototype.ratio = null;var b2DistanceJointDef = function() {
  8097. b2JointDef.prototype.__varz.call(this);
  8098. this.__varz();
  8099. this.__constructor.apply(this, arguments)
  8100. };
  8101. extend(b2DistanceJointDef.prototype, b2JointDef.prototype);
  8102. b2DistanceJointDef.prototype._super = b2JointDef.prototype;
  8103. b2DistanceJointDef.prototype.__constructor = function() {
  8104. this._super.__constructor.apply(this, arguments);
  8105. this.type = b2Joint.e_distanceJoint;
  8106. this.length = 1;
  8107. this.frequencyHz = 0;
  8108. this.dampingRatio = 0
  8109. };
  8110. b2DistanceJointDef.prototype.__varz = function() {
  8111. this.localAnchorA = new b2Vec2;
  8112. this.localAnchorB = new b2Vec2
  8113. };
  8114. b2DistanceJointDef.prototype.Initialize = function(bA, bB, anchorA, anchorB) {
  8115. this.bodyA = bA;
  8116. this.bodyB = bB;
  8117. this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchorA));
  8118. this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchorB));
  8119. var dX = anchorB.x - anchorA.x;
  8120. var dY = anchorB.y - anchorA.y;
  8121. this.length = Math.sqrt(dX * dX + dY * dY);
  8122. this.frequencyHz = 0;
  8123. this.dampingRatio = 0
  8124. };
  8125. b2DistanceJointDef.prototype.localAnchorA = new b2Vec2;
  8126. b2DistanceJointDef.prototype.localAnchorB = new b2Vec2;
  8127. b2DistanceJointDef.prototype.length = null;
  8128. b2DistanceJointDef.prototype.frequencyHz = null;
  8129. b2DistanceJointDef.prototype.dampingRatio = null;var b2FrictionJointDef = function() {
  8130. b2JointDef.prototype.__varz.call(this);
  8131. this.__varz();
  8132. this.__constructor.apply(this, arguments)
  8133. };
  8134. extend(b2FrictionJointDef.prototype, b2JointDef.prototype);
  8135. b2FrictionJointDef.prototype._super = b2JointDef.prototype;
  8136. b2FrictionJointDef.prototype.__constructor = function() {
  8137. this._super.__constructor.apply(this, arguments);
  8138. this.type = b2Joint.e_frictionJoint;
  8139. this.maxForce = 0;
  8140. this.maxTorque = 0
  8141. };
  8142. b2FrictionJointDef.prototype.__varz = function() {
  8143. this.localAnchorA = new b2Vec2;
  8144. this.localAnchorB = new b2Vec2
  8145. };
  8146. b2FrictionJointDef.prototype.Initialize = function(bA, bB, anchor) {
  8147. this.bodyA = bA;
  8148. this.bodyB = bB;
  8149. this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchor));
  8150. this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchor))
  8151. };
  8152. b2FrictionJointDef.prototype.localAnchorA = new b2Vec2;
  8153. b2FrictionJointDef.prototype.localAnchorB = new b2Vec2;
  8154. b2FrictionJointDef.prototype.maxForce = null;
  8155. b2FrictionJointDef.prototype.maxTorque = null;var b2WeldJointDef = function() {
  8156. b2JointDef.prototype.__varz.call(this);
  8157. this.__varz();
  8158. this.__constructor.apply(this, arguments)
  8159. };
  8160. extend(b2WeldJointDef.prototype, b2JointDef.prototype);
  8161. b2WeldJointDef.prototype._super = b2JointDef.prototype;
  8162. b2WeldJointDef.prototype.__constructor = function() {
  8163. this._super.__constructor.apply(this, arguments);
  8164. this.type = b2Joint.e_weldJoint;
  8165. this.referenceAngle = 0
  8166. };
  8167. b2WeldJointDef.prototype.__varz = function() {
  8168. this.localAnchorA = new b2Vec2;
  8169. this.localAnchorB = new b2Vec2
  8170. };
  8171. b2WeldJointDef.prototype.Initialize = function(bA, bB, anchor) {
  8172. this.bodyA = bA;
  8173. this.bodyB = bB;
  8174. this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchor));
  8175. this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchor));
  8176. this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle()
  8177. };
  8178. b2WeldJointDef.prototype.localAnchorA = new b2Vec2;
  8179. b2WeldJointDef.prototype.localAnchorB = new b2Vec2;
  8180. b2WeldJointDef.prototype.referenceAngle = null;var b2GearJointDef = function() {
  8181. b2JointDef.prototype.__varz.call(this);
  8182. this.__varz();
  8183. this.__constructor.apply(this, arguments)
  8184. };
  8185. extend(b2GearJointDef.prototype, b2JointDef.prototype);
  8186. b2GearJointDef.prototype._super = b2JointDef.prototype;
  8187. b2GearJointDef.prototype.__constructor = function() {
  8188. this._super.__constructor.apply(this, arguments);
  8189. this.type = b2Joint.e_gearJoint;
  8190. this.joint1 = null;
  8191. this.joint2 = null;
  8192. this.ratio = 1
  8193. };
  8194. b2GearJointDef.prototype.__varz = function() {
  8195. };
  8196. b2GearJointDef.prototype.joint1 = null;
  8197. b2GearJointDef.prototype.joint2 = null;
  8198. b2GearJointDef.prototype.ratio = null;var b2Color = function() {
  8199. this.__varz();
  8200. this.__constructor.apply(this, arguments)
  8201. };
  8202. b2Color.prototype.__constructor = function(rr, gg, bb) {
  8203. this._r = parseInt(255 * b2Math.Clamp(rr, 0, 1));
  8204. this._g = parseInt(255 * b2Math.Clamp(gg, 0, 1));
  8205. this._b = parseInt(255 * b2Math.Clamp(bb, 0, 1))
  8206. };
  8207. b2Color.prototype.__varz = function() {
  8208. };
  8209. b2Color.prototype.Set = function(rr, gg, bb) {
  8210. this._r = parseInt(255 * b2Math.Clamp(rr, 0, 1));
  8211. this._g = parseInt(255 * b2Math.Clamp(gg, 0, 1));
  8212. this._b = parseInt(255 * b2Math.Clamp(bb, 0, 1))
  8213. };
  8214. b2Color.prototype.__defineGetter__("r", function() {
  8215. return this._r
  8216. });
  8217. b2Color.prototype.__defineSetter__("r", function(rr) {
  8218. this._r = parseInt(255 * b2Math.Clamp(rr, 0, 1))
  8219. });
  8220. b2Color.prototype.__defineGetter__("g", function() {
  8221. return this._g
  8222. });
  8223. b2Color.prototype.__defineSetter__("g", function(gg) {
  8224. this._g = parseInt(255 * b2Math.Clamp(gg, 0, 1))
  8225. });
  8226. b2Color.prototype.__defineGetter__("b", function() {
  8227. return this._b
  8228. });
  8229. b2Color.prototype.__defineSetter__("b", function(bb) {
  8230. this._b = parseInt(255 * b2Math.Clamp(bb, 0, 1))
  8231. });
  8232. b2Color.prototype.__defineGetter__("color", function() {
  8233. return this._r << 16 | this._g << 8 | this._b
  8234. });
  8235. b2Color.prototype._r = 0;
  8236. b2Color.prototype._g = 0;
  8237. b2Color.prototype._b = 0;var b2FrictionJoint = function() {
  8238. b2Joint.prototype.__varz.call(this);
  8239. this.__varz();
  8240. this.__constructor.apply(this, arguments)
  8241. };
  8242. extend(b2FrictionJoint.prototype, b2Joint.prototype);
  8243. b2FrictionJoint.prototype._super = b2Joint.prototype;
  8244. b2FrictionJoint.prototype.__constructor = function(def) {
  8245. this._super.__constructor.apply(this, [def]);
  8246. this.m_localAnchorA.SetV(def.localAnchorA);
  8247. this.m_localAnchorB.SetV(def.localAnchorB);
  8248. this.m_linearMass.SetZero();
  8249. this.m_angularMass = 0;
  8250. this.m_linearImpulse.SetZero();
  8251. this.m_angularImpulse = 0;
  8252. this.m_maxForce = def.maxForce;
  8253. this.m_maxTorque = def.maxTorque
  8254. };
  8255. b2FrictionJoint.prototype.__varz = function() {
  8256. this.m_localAnchorA = new b2Vec2;
  8257. this.m_localAnchorB = new b2Vec2;
  8258. this.m_linearImpulse = new b2Vec2;
  8259. this.m_linearMass = new b2Mat22
  8260. };
  8261. b2FrictionJoint.prototype.InitVelocityConstraints = function(step) {
  8262. var tMat;
  8263. var tX;
  8264. var bA = this.m_bodyA;
  8265. var bB = this.m_bodyB;
  8266. tMat = bA.m_xf.R;
  8267. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  8268. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  8269. tX = tMat.col1.x * rAX + tMat.col2.x * rAY;
  8270. rAY = tMat.col1.y * rAX + tMat.col2.y * rAY;
  8271. rAX = tX;
  8272. tMat = bB.m_xf.R;
  8273. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  8274. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  8275. tX = tMat.col1.x * rBX + tMat.col2.x * rBY;
  8276. rBY = tMat.col1.y * rBX + tMat.col2.y * rBY;
  8277. rBX = tX;
  8278. var mA = bA.m_invMass;
  8279. var mB = bB.m_invMass;
  8280. var iA = bA.m_invI;
  8281. var iB = bB.m_invI;
  8282. var K = new b2Mat22;
  8283. K.col1.x = mA + mB;
  8284. K.col2.x = 0;
  8285. K.col1.y = 0;
  8286. K.col2.y = mA + mB;
  8287. K.col1.x += iA * rAY * rAY;
  8288. K.col2.x += -iA * rAX * rAY;
  8289. K.col1.y += -iA * rAX * rAY;
  8290. K.col2.y += iA * rAX * rAX;
  8291. K.col1.x += iB * rBY * rBY;
  8292. K.col2.x += -iB * rBX * rBY;
  8293. K.col1.y += -iB * rBX * rBY;
  8294. K.col2.y += iB * rBX * rBX;
  8295. K.GetInverse(this.m_linearMass);
  8296. this.m_angularMass = iA + iB;
  8297. if(this.m_angularMass > 0) {
  8298. this.m_angularMass = 1 / this.m_angularMass
  8299. }
  8300. if(step.warmStarting) {
  8301. this.m_linearImpulse.x *= step.dtRatio;
  8302. this.m_linearImpulse.y *= step.dtRatio;
  8303. this.m_angularImpulse *= step.dtRatio;
  8304. var P = this.m_linearImpulse;
  8305. bA.m_linearVelocity.x -= mA * P.x;
  8306. bA.m_linearVelocity.y -= mA * P.y;
  8307. bA.m_angularVelocity -= iA * (rAX * P.y - rAY * P.x + this.m_angularImpulse);
  8308. bB.m_linearVelocity.x += mB * P.x;
  8309. bB.m_linearVelocity.y += mB * P.y;
  8310. bB.m_angularVelocity += iB * (rBX * P.y - rBY * P.x + this.m_angularImpulse)
  8311. }else {
  8312. this.m_linearImpulse.SetZero();
  8313. this.m_angularImpulse = 0
  8314. }
  8315. };
  8316. b2FrictionJoint.prototype.SolveVelocityConstraints = function(step) {
  8317. var tMat;
  8318. var tX;
  8319. var bA = this.m_bodyA;
  8320. var bB = this.m_bodyB;
  8321. var vA = bA.m_linearVelocity;
  8322. var wA = bA.m_angularVelocity;
  8323. var vB = bB.m_linearVelocity;
  8324. var wB = bB.m_angularVelocity;
  8325. var mA = bA.m_invMass;
  8326. var mB = bB.m_invMass;
  8327. var iA = bA.m_invI;
  8328. var iB = bB.m_invI;
  8329. tMat = bA.m_xf.R;
  8330. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  8331. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  8332. tX = tMat.col1.x * rAX + tMat.col2.x * rAY;
  8333. rAY = tMat.col1.y * rAX + tMat.col2.y * rAY;
  8334. rAX = tX;
  8335. tMat = bB.m_xf.R;
  8336. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  8337. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  8338. tX = tMat.col1.x * rBX + tMat.col2.x * rBY;
  8339. rBY = tMat.col1.y * rBX + tMat.col2.y * rBY;
  8340. rBX = tX;
  8341. var maxImpulse;
  8342. var Cdot = wB - wA;
  8343. var impulse = -this.m_angularMass * Cdot;
  8344. var oldImpulse = this.m_angularImpulse;
  8345. maxImpulse = step.dt * this.m_maxTorque;
  8346. this.m_angularImpulse = b2Math.Clamp(this.m_angularImpulse + impulse, -maxImpulse, maxImpulse);
  8347. impulse = this.m_angularImpulse - oldImpulse;
  8348. wA -= iA * impulse;
  8349. wB += iB * impulse;
  8350. var CdotX = vB.x - wB * rBY - vA.x + wA * rAY;
  8351. var CdotY = vB.y + wB * rBX - vA.y - wA * rAX;
  8352. var impulseV = b2Math.MulMV(this.m_linearMass, new b2Vec2(-CdotX, -CdotY));
  8353. var oldImpulseV = this.m_linearImpulse.Copy();
  8354. this.m_linearImpulse.Add(impulseV);
  8355. maxImpulse = step.dt * this.m_maxForce;
  8356. if(this.m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse) {
  8357. this.m_linearImpulse.Normalize();
  8358. this.m_linearImpulse.Multiply(maxImpulse)
  8359. }
  8360. impulseV = b2Math.SubtractVV(this.m_linearImpulse, oldImpulseV);
  8361. vA.x -= mA * impulseV.x;
  8362. vA.y -= mA * impulseV.y;
  8363. wA -= iA * (rAX * impulseV.y - rAY * impulseV.x);
  8364. vB.x += mB * impulseV.x;
  8365. vB.y += mB * impulseV.y;
  8366. wB += iB * (rBX * impulseV.y - rBY * impulseV.x);
  8367. bA.m_angularVelocity = wA;
  8368. bB.m_angularVelocity = wB
  8369. };
  8370. b2FrictionJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  8371. return true
  8372. };
  8373. b2FrictionJoint.prototype.GetAnchorA = function() {
  8374. return this.m_bodyA.GetWorldPoint(this.m_localAnchorA)
  8375. };
  8376. b2FrictionJoint.prototype.GetAnchorB = function() {
  8377. return this.m_bodyB.GetWorldPoint(this.m_localAnchorB)
  8378. };
  8379. b2FrictionJoint.prototype.GetReactionForce = function(inv_dt) {
  8380. return new b2Vec2(inv_dt * this.m_linearImpulse.x, inv_dt * this.m_linearImpulse.y)
  8381. };
  8382. b2FrictionJoint.prototype.GetReactionTorque = function(inv_dt) {
  8383. return inv_dt * this.m_angularImpulse
  8384. };
  8385. b2FrictionJoint.prototype.SetMaxForce = function(force) {
  8386. this.m_maxForce = force
  8387. };
  8388. b2FrictionJoint.prototype.GetMaxForce = function() {
  8389. return this.m_maxForce
  8390. };
  8391. b2FrictionJoint.prototype.SetMaxTorque = function(torque) {
  8392. this.m_maxTorque = torque
  8393. };
  8394. b2FrictionJoint.prototype.GetMaxTorque = function() {
  8395. return this.m_maxTorque
  8396. };
  8397. b2FrictionJoint.prototype.m_localAnchorA = new b2Vec2;
  8398. b2FrictionJoint.prototype.m_localAnchorB = new b2Vec2;
  8399. b2FrictionJoint.prototype.m_linearImpulse = new b2Vec2;
  8400. b2FrictionJoint.prototype.m_angularImpulse = null;
  8401. b2FrictionJoint.prototype.m_maxForce = null;
  8402. b2FrictionJoint.prototype.m_maxTorque = null;
  8403. b2FrictionJoint.prototype.m_linearMass = new b2Mat22;
  8404. b2FrictionJoint.prototype.m_angularMass = null;var b2Distance = function() {
  8405. this.__varz();
  8406. this.__constructor.apply(this, arguments)
  8407. };
  8408. b2Distance.prototype.__constructor = function() {
  8409. };
  8410. b2Distance.prototype.__varz = function() {
  8411. };
  8412. b2Distance.Distance = function(output, cache, input) {
  8413. ++b2Distance.b2_gjkCalls;
  8414. var proxyA = input.proxyA;
  8415. var proxyB = input.proxyB;
  8416. var transformA = input.transformA;
  8417. var transformB = input.transformB;
  8418. var simplex = b2Distance.s_simplex;
  8419. simplex.ReadCache(cache, proxyA, transformA, proxyB, transformB);
  8420. var vertices = simplex.m_vertices;
  8421. var k_maxIters = 20;
  8422. var saveA = b2Distance.s_saveA;
  8423. var saveB = b2Distance.s_saveB;
  8424. var saveCount = 0;
  8425. var closestPoint = simplex.GetClosestPoint();
  8426. var distanceSqr1 = closestPoint.LengthSquared();
  8427. var distanceSqr2 = distanceSqr1;
  8428. var i = 0;
  8429. var p;
  8430. var iter = 0;
  8431. while(iter < k_maxIters) {
  8432. saveCount = simplex.m_count;
  8433. for(i = 0;i < saveCount;i++) {
  8434. saveA[i] = vertices[i].indexA;
  8435. saveB[i] = vertices[i].indexB
  8436. }
  8437. switch(simplex.m_count) {
  8438. case 1:
  8439. break;
  8440. case 2:
  8441. simplex.Solve2();
  8442. break;
  8443. case 3:
  8444. simplex.Solve3();
  8445. break;
  8446. default:
  8447. b2Settings.b2Assert(false)
  8448. }
  8449. if(simplex.m_count == 3) {
  8450. break
  8451. }
  8452. p = simplex.GetClosestPoint();
  8453. distanceSqr2 = p.LengthSquared();
  8454. if(distanceSqr2 > distanceSqr1) {
  8455. }
  8456. distanceSqr1 = distanceSqr2;
  8457. var d = simplex.GetSearchDirection();
  8458. if(d.LengthSquared() < Number.MIN_VALUE * Number.MIN_VALUE) {
  8459. break
  8460. }
  8461. var vertex = vertices[simplex.m_count];
  8462. vertex.indexA = proxyA.GetSupport(b2Math.MulTMV(transformA.R, d.GetNegative()));
  8463. vertex.wA = b2Math.MulX(transformA, proxyA.GetVertex(vertex.indexA));
  8464. vertex.indexB = proxyB.GetSupport(b2Math.MulTMV(transformB.R, d));
  8465. vertex.wB = b2Math.MulX(transformB, proxyB.GetVertex(vertex.indexB));
  8466. vertex.w = b2Math.SubtractVV(vertex.wB, vertex.wA);
  8467. ++iter;
  8468. ++b2Distance.b2_gjkIters;
  8469. var duplicate = false;
  8470. for(i = 0;i < saveCount;i++) {
  8471. if(vertex.indexA == saveA[i] && vertex.indexB == saveB[i]) {
  8472. duplicate = true;
  8473. break
  8474. }
  8475. }
  8476. if(duplicate) {
  8477. break
  8478. }
  8479. ++simplex.m_count
  8480. }
  8481. b2Distance.b2_gjkMaxIters = b2Math.Max(b2Distance.b2_gjkMaxIters, iter);
  8482. simplex.GetWitnessPoints(output.pointA, output.pointB);
  8483. output.distance = b2Math.SubtractVV(output.pointA, output.pointB).Length();
  8484. output.iterations = iter;
  8485. simplex.WriteCache(cache);
  8486. if(input.useRadii) {
  8487. var rA = proxyA.m_radius;
  8488. var rB = proxyB.m_radius;
  8489. if(output.distance > rA + rB && output.distance > Number.MIN_VALUE) {
  8490. output.distance -= rA + rB;
  8491. var normal = b2Math.SubtractVV(output.pointB, output.pointA);
  8492. normal.Normalize();
  8493. output.pointA.x += rA * normal.x;
  8494. output.pointA.y += rA * normal.y;
  8495. output.pointB.x -= rB * normal.x;
  8496. output.pointB.y -= rB * normal.y
  8497. }else {
  8498. p = new b2Vec2;
  8499. p.x = 0.5 * (output.pointA.x + output.pointB.x);
  8500. p.y = 0.5 * (output.pointA.y + output.pointB.y);
  8501. output.pointA.x = output.pointB.x = p.x;
  8502. output.pointA.y = output.pointB.y = p.y;
  8503. output.distance = 0
  8504. }
  8505. }
  8506. };
  8507. b2Distance.b2_gjkCalls = 0;
  8508. b2Distance.b2_gjkIters = 0;
  8509. b2Distance.b2_gjkMaxIters = 0;
  8510. b2Distance.s_simplex = new b2Simplex;
  8511. b2Distance.s_saveA = new Array(3);
  8512. b2Distance.s_saveB = new Array(3);var b2MouseJoint = function() {
  8513. b2Joint.prototype.__varz.call(this);
  8514. this.__varz();
  8515. this.__constructor.apply(this, arguments)
  8516. };
  8517. extend(b2MouseJoint.prototype, b2Joint.prototype);
  8518. b2MouseJoint.prototype._super = b2Joint.prototype;
  8519. b2MouseJoint.prototype.__constructor = function(def) {
  8520. this._super.__constructor.apply(this, [def]);
  8521. this.m_target.SetV(def.target);
  8522. var tX = this.m_target.x - this.m_bodyB.m_xf.position.x;
  8523. var tY = this.m_target.y - this.m_bodyB.m_xf.position.y;
  8524. var tMat = this.m_bodyB.m_xf.R;
  8525. this.m_localAnchor.x = tX * tMat.col1.x + tY * tMat.col1.y;
  8526. this.m_localAnchor.y = tX * tMat.col2.x + tY * tMat.col2.y;
  8527. this.m_maxForce = def.maxForce;
  8528. this.m_impulse.SetZero();
  8529. this.m_frequencyHz = def.frequencyHz;
  8530. this.m_dampingRatio = def.dampingRatio;
  8531. this.m_beta = 0;
  8532. this.m_gamma = 0
  8533. };
  8534. b2MouseJoint.prototype.__varz = function() {
  8535. this.K = new b2Mat22;
  8536. this.K1 = new b2Mat22;
  8537. this.K2 = new b2Mat22;
  8538. this.m_localAnchor = new b2Vec2;
  8539. this.m_target = new b2Vec2;
  8540. this.m_impulse = new b2Vec2;
  8541. this.m_mass = new b2Mat22;
  8542. this.m_C = new b2Vec2
  8543. };
  8544. b2MouseJoint.prototype.InitVelocityConstraints = function(step) {
  8545. var b = this.m_bodyB;
  8546. var mass = b.GetMass();
  8547. var omega = 2 * Math.PI * this.m_frequencyHz;
  8548. var d = 2 * mass * this.m_dampingRatio * omega;
  8549. var k = mass * omega * omega;
  8550. this.m_gamma = step.dt * (d + step.dt * k);
  8551. this.m_gamma = this.m_gamma != 0 ? 1 / this.m_gamma : 0;
  8552. this.m_beta = step.dt * k * this.m_gamma;
  8553. var tMat;
  8554. tMat = b.m_xf.R;
  8555. var rX = this.m_localAnchor.x - b.m_sweep.localCenter.x;
  8556. var rY = this.m_localAnchor.y - b.m_sweep.localCenter.y;
  8557. var tX = tMat.col1.x * rX + tMat.col2.x * rY;
  8558. rY = tMat.col1.y * rX + tMat.col2.y * rY;
  8559. rX = tX;
  8560. var invMass = b.m_invMass;
  8561. var invI = b.m_invI;
  8562. this.K1.col1.x = invMass;
  8563. this.K1.col2.x = 0;
  8564. this.K1.col1.y = 0;
  8565. this.K1.col2.y = invMass;
  8566. this.K2.col1.x = invI * rY * rY;
  8567. this.K2.col2.x = -invI * rX * rY;
  8568. this.K2.col1.y = -invI * rX * rY;
  8569. this.K2.col2.y = invI * rX * rX;
  8570. this.K.SetM(this.K1);
  8571. this.K.AddM(this.K2);
  8572. this.K.col1.x += this.m_gamma;
  8573. this.K.col2.y += this.m_gamma;
  8574. this.K.GetInverse(this.m_mass);
  8575. this.m_C.x = b.m_sweep.c.x + rX - this.m_target.x;
  8576. this.m_C.y = b.m_sweep.c.y + rY - this.m_target.y;
  8577. b.m_angularVelocity *= 0.98;
  8578. this.m_impulse.x *= step.dtRatio;
  8579. this.m_impulse.y *= step.dtRatio;
  8580. b.m_linearVelocity.x += invMass * this.m_impulse.x;
  8581. b.m_linearVelocity.y += invMass * this.m_impulse.y;
  8582. b.m_angularVelocity += invI * (rX * this.m_impulse.y - rY * this.m_impulse.x)
  8583. };
  8584. b2MouseJoint.prototype.SolveVelocityConstraints = function(step) {
  8585. var b = this.m_bodyB;
  8586. var tMat;
  8587. var tX;
  8588. var tY;
  8589. tMat = b.m_xf.R;
  8590. var rX = this.m_localAnchor.x - b.m_sweep.localCenter.x;
  8591. var rY = this.m_localAnchor.y - b.m_sweep.localCenter.y;
  8592. tX = tMat.col1.x * rX + tMat.col2.x * rY;
  8593. rY = tMat.col1.y * rX + tMat.col2.y * rY;
  8594. rX = tX;
  8595. var CdotX = b.m_linearVelocity.x + -b.m_angularVelocity * rY;
  8596. var CdotY = b.m_linearVelocity.y + b.m_angularVelocity * rX;
  8597. tMat = this.m_mass;
  8598. tX = CdotX + this.m_beta * this.m_C.x + this.m_gamma * this.m_impulse.x;
  8599. tY = CdotY + this.m_beta * this.m_C.y + this.m_gamma * this.m_impulse.y;
  8600. var impulseX = -(tMat.col1.x * tX + tMat.col2.x * tY);
  8601. var impulseY = -(tMat.col1.y * tX + tMat.col2.y * tY);
  8602. var oldImpulseX = this.m_impulse.x;
  8603. var oldImpulseY = this.m_impulse.y;
  8604. this.m_impulse.x += impulseX;
  8605. this.m_impulse.y += impulseY;
  8606. var maxImpulse = step.dt * this.m_maxForce;
  8607. if(this.m_impulse.LengthSquared() > maxImpulse * maxImpulse) {
  8608. this.m_impulse.Multiply(maxImpulse / this.m_impulse.Length())
  8609. }
  8610. impulseX = this.m_impulse.x - oldImpulseX;
  8611. impulseY = this.m_impulse.y - oldImpulseY;
  8612. b.m_linearVelocity.x += b.m_invMass * impulseX;
  8613. b.m_linearVelocity.y += b.m_invMass * impulseY;
  8614. b.m_angularVelocity += b.m_invI * (rX * impulseY - rY * impulseX)
  8615. };
  8616. b2MouseJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  8617. return true
  8618. };
  8619. b2MouseJoint.prototype.GetAnchorA = function() {
  8620. return this.m_target
  8621. };
  8622. b2MouseJoint.prototype.GetAnchorB = function() {
  8623. return this.m_bodyB.GetWorldPoint(this.m_localAnchor)
  8624. };
  8625. b2MouseJoint.prototype.GetReactionForce = function(inv_dt) {
  8626. return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y)
  8627. };
  8628. b2MouseJoint.prototype.GetReactionTorque = function(inv_dt) {
  8629. return 0
  8630. };
  8631. b2MouseJoint.prototype.GetTarget = function() {
  8632. return this.m_target
  8633. };
  8634. b2MouseJoint.prototype.SetTarget = function(target) {
  8635. if(this.m_bodyB.IsAwake() == false) {
  8636. this.m_bodyB.SetAwake(true)
  8637. }
  8638. this.m_target = target
  8639. };
  8640. b2MouseJoint.prototype.GetMaxForce = function() {
  8641. return this.m_maxForce
  8642. };
  8643. b2MouseJoint.prototype.SetMaxForce = function(maxForce) {
  8644. this.m_maxForce = maxForce
  8645. };
  8646. b2MouseJoint.prototype.GetFrequency = function() {
  8647. return this.m_frequencyHz
  8648. };
  8649. b2MouseJoint.prototype.SetFrequency = function(hz) {
  8650. this.m_frequencyHz = hz
  8651. };
  8652. b2MouseJoint.prototype.GetDampingRatio = function() {
  8653. return this.m_dampingRatio
  8654. };
  8655. b2MouseJoint.prototype.SetDampingRatio = function(ratio) {
  8656. this.m_dampingRatio = ratio
  8657. };
  8658. b2MouseJoint.prototype.K = new b2Mat22;
  8659. b2MouseJoint.prototype.K1 = new b2Mat22;
  8660. b2MouseJoint.prototype.K2 = new b2Mat22;
  8661. b2MouseJoint.prototype.m_localAnchor = new b2Vec2;
  8662. b2MouseJoint.prototype.m_target = new b2Vec2;
  8663. b2MouseJoint.prototype.m_impulse = new b2Vec2;
  8664. b2MouseJoint.prototype.m_mass = new b2Mat22;
  8665. b2MouseJoint.prototype.m_C = new b2Vec2;
  8666. b2MouseJoint.prototype.m_maxForce = null;
  8667. b2MouseJoint.prototype.m_frequencyHz = null;
  8668. b2MouseJoint.prototype.m_dampingRatio = null;
  8669. b2MouseJoint.prototype.m_beta = null;
  8670. b2MouseJoint.prototype.m_gamma = null;var b2PrismaticJointDef = function() {
  8671. b2JointDef.prototype.__varz.call(this);
  8672. this.__varz();
  8673. this.__constructor.apply(this, arguments)
  8674. };
  8675. extend(b2PrismaticJointDef.prototype, b2JointDef.prototype);
  8676. b2PrismaticJointDef.prototype._super = b2JointDef.prototype;
  8677. b2PrismaticJointDef.prototype.__constructor = function() {
  8678. this._super.__constructor.apply(this, arguments);
  8679. this.type = b2Joint.e_prismaticJoint;
  8680. this.localAxisA.Set(1, 0);
  8681. this.referenceAngle = 0;
  8682. this.enableLimit = false;
  8683. this.lowerTranslation = 0;
  8684. this.upperTranslation = 0;
  8685. this.enableMotor = false;
  8686. this.maxMotorForce = 0;
  8687. this.motorSpeed = 0
  8688. };
  8689. b2PrismaticJointDef.prototype.__varz = function() {
  8690. this.localAnchorA = new b2Vec2;
  8691. this.localAnchorB = new b2Vec2;
  8692. this.localAxisA = new b2Vec2
  8693. };
  8694. b2PrismaticJointDef.prototype.Initialize = function(bA, bB, anchor, axis) {
  8695. this.bodyA = bA;
  8696. this.bodyB = bB;
  8697. this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
  8698. this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
  8699. this.localAxisA = this.bodyA.GetLocalVector(axis);
  8700. this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle()
  8701. };
  8702. b2PrismaticJointDef.prototype.localAnchorA = new b2Vec2;
  8703. b2PrismaticJointDef.prototype.localAnchorB = new b2Vec2;
  8704. b2PrismaticJointDef.prototype.localAxisA = new b2Vec2;
  8705. b2PrismaticJointDef.prototype.referenceAngle = null;
  8706. b2PrismaticJointDef.prototype.enableLimit = null;
  8707. b2PrismaticJointDef.prototype.lowerTranslation = null;
  8708. b2PrismaticJointDef.prototype.upperTranslation = null;
  8709. b2PrismaticJointDef.prototype.enableMotor = null;
  8710. b2PrismaticJointDef.prototype.maxMotorForce = null;
  8711. b2PrismaticJointDef.prototype.motorSpeed = null;var b2TimeOfImpact = function() {
  8712. this.__varz();
  8713. this.__constructor.apply(this, arguments)
  8714. };
  8715. b2TimeOfImpact.prototype.__constructor = function() {
  8716. };
  8717. b2TimeOfImpact.prototype.__varz = function() {
  8718. };
  8719. b2TimeOfImpact.TimeOfImpact = function(input) {
  8720. ++b2TimeOfImpact.b2_toiCalls;
  8721. var proxyA = input.proxyA;
  8722. var proxyB = input.proxyB;
  8723. var sweepA = input.sweepA;
  8724. var sweepB = input.sweepB;
  8725. b2Settings.b2Assert(sweepA.t0 == sweepB.t0);
  8726. b2Settings.b2Assert(1 - sweepA.t0 > Number.MIN_VALUE);
  8727. var radius = proxyA.m_radius + proxyB.m_radius;
  8728. var tolerance = input.tolerance;
  8729. var alpha = 0;
  8730. var k_maxIterations = 1E3;
  8731. var iter = 0;
  8732. var target = 0;
  8733. b2TimeOfImpact.s_cache.count = 0;
  8734. b2TimeOfImpact.s_distanceInput.useRadii = false;
  8735. for(;;) {
  8736. sweepA.GetTransform(b2TimeOfImpact.s_xfA, alpha);
  8737. sweepB.GetTransform(b2TimeOfImpact.s_xfB, alpha);
  8738. b2TimeOfImpact.s_distanceInput.proxyA = proxyA;
  8739. b2TimeOfImpact.s_distanceInput.proxyB = proxyB;
  8740. b2TimeOfImpact.s_distanceInput.transformA = b2TimeOfImpact.s_xfA;
  8741. b2TimeOfImpact.s_distanceInput.transformB = b2TimeOfImpact.s_xfB;
  8742. b2Distance.Distance(b2TimeOfImpact.s_distanceOutput, b2TimeOfImpact.s_cache, b2TimeOfImpact.s_distanceInput);
  8743. if(b2TimeOfImpact.s_distanceOutput.distance <= 0) {
  8744. alpha = 1;
  8745. break
  8746. }
  8747. b2TimeOfImpact.s_fcn.Initialize(b2TimeOfImpact.s_cache, proxyA, b2TimeOfImpact.s_xfA, proxyB, b2TimeOfImpact.s_xfB);
  8748. var separation = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
  8749. if(separation <= 0) {
  8750. alpha = 1;
  8751. break
  8752. }
  8753. if(iter == 0) {
  8754. if(separation > radius) {
  8755. target = b2Math.Max(radius - tolerance, 0.75 * radius)
  8756. }else {
  8757. target = b2Math.Max(separation - tolerance, 0.02 * radius)
  8758. }
  8759. }
  8760. if(separation - target < 0.5 * tolerance) {
  8761. if(iter == 0) {
  8762. alpha = 1;
  8763. break
  8764. }
  8765. break
  8766. }
  8767. var newAlpha = alpha;
  8768. var x1 = alpha;
  8769. var x2 = 1;
  8770. var f1 = separation;
  8771. sweepA.GetTransform(b2TimeOfImpact.s_xfA, x2);
  8772. sweepB.GetTransform(b2TimeOfImpact.s_xfB, x2);
  8773. var f2 = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
  8774. if(f2 >= target) {
  8775. alpha = 1;
  8776. break
  8777. }
  8778. var rootIterCount = 0;
  8779. for(;;) {
  8780. var x;
  8781. if(rootIterCount & 1) {
  8782. x = x1 + (target - f1) * (x2 - x1) / (f2 - f1)
  8783. }else {
  8784. x = 0.5 * (x1 + x2)
  8785. }
  8786. sweepA.GetTransform(b2TimeOfImpact.s_xfA, x);
  8787. sweepB.GetTransform(b2TimeOfImpact.s_xfB, x);
  8788. var f = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
  8789. if(b2Math.Abs(f - target) < 0.025 * tolerance) {
  8790. newAlpha = x;
  8791. break
  8792. }
  8793. if(f > target) {
  8794. x1 = x;
  8795. f1 = f
  8796. }else {
  8797. x2 = x;
  8798. f2 = f
  8799. }
  8800. ++rootIterCount;
  8801. ++b2TimeOfImpact.b2_toiRootIters;
  8802. if(rootIterCount == 50) {
  8803. break
  8804. }
  8805. }
  8806. b2TimeOfImpact.b2_toiMaxRootIters = b2Math.Max(b2TimeOfImpact.b2_toiMaxRootIters, rootIterCount);
  8807. if(newAlpha < (1 + 100 * Number.MIN_VALUE) * alpha) {
  8808. break
  8809. }
  8810. alpha = newAlpha;
  8811. iter++;
  8812. ++b2TimeOfImpact.b2_toiIters;
  8813. if(iter == k_maxIterations) {
  8814. break
  8815. }
  8816. }
  8817. b2TimeOfImpact.b2_toiMaxIters = b2Math.Max(b2TimeOfImpact.b2_toiMaxIters, iter);
  8818. return alpha
  8819. };
  8820. b2TimeOfImpact.b2_toiCalls = 0;
  8821. b2TimeOfImpact.b2_toiIters = 0;
  8822. b2TimeOfImpact.b2_toiMaxIters = 0;
  8823. b2TimeOfImpact.b2_toiRootIters = 0;
  8824. b2TimeOfImpact.b2_toiMaxRootIters = 0;
  8825. b2TimeOfImpact.s_cache = new b2SimplexCache;
  8826. b2TimeOfImpact.s_distanceInput = new b2DistanceInput;
  8827. b2TimeOfImpact.s_xfA = new b2Transform;
  8828. b2TimeOfImpact.s_xfB = new b2Transform;
  8829. b2TimeOfImpact.s_fcn = new b2SeparationFunction;
  8830. b2TimeOfImpact.s_distanceOutput = new b2DistanceOutput;var b2GearJoint = function() {
  8831. b2Joint.prototype.__varz.call(this);
  8832. this.__varz();
  8833. this.__constructor.apply(this, arguments)
  8834. };
  8835. extend(b2GearJoint.prototype, b2Joint.prototype);
  8836. b2GearJoint.prototype._super = b2Joint.prototype;
  8837. b2GearJoint.prototype.__constructor = function(def) {
  8838. this._super.__constructor.apply(this, [def]);
  8839. var type1 = def.joint1.m_type;
  8840. var type2 = def.joint2.m_type;
  8841. this.m_revolute1 = null;
  8842. this.m_prismatic1 = null;
  8843. this.m_revolute2 = null;
  8844. this.m_prismatic2 = null;
  8845. var coordinate1;
  8846. var coordinate2;
  8847. this.m_ground1 = def.joint1.GetBodyA();
  8848. this.m_bodyA = def.joint1.GetBodyB();
  8849. if(type1 == b2Joint.e_revoluteJoint) {
  8850. this.m_revolute1 = def.joint1;
  8851. this.m_groundAnchor1.SetV(this.m_revolute1.m_localAnchor1);
  8852. this.m_localAnchor1.SetV(this.m_revolute1.m_localAnchor2);
  8853. coordinate1 = this.m_revolute1.GetJointAngle()
  8854. }else {
  8855. this.m_prismatic1 = def.joint1;
  8856. this.m_groundAnchor1.SetV(this.m_prismatic1.m_localAnchor1);
  8857. this.m_localAnchor1.SetV(this.m_prismatic1.m_localAnchor2);
  8858. coordinate1 = this.m_prismatic1.GetJointTranslation()
  8859. }
  8860. this.m_ground2 = def.joint2.GetBodyA();
  8861. this.m_bodyB = def.joint2.GetBodyB();
  8862. if(type2 == b2Joint.e_revoluteJoint) {
  8863. this.m_revolute2 = def.joint2;
  8864. this.m_groundAnchor2.SetV(this.m_revolute2.m_localAnchor1);
  8865. this.m_localAnchor2.SetV(this.m_revolute2.m_localAnchor2);
  8866. coordinate2 = this.m_revolute2.GetJointAngle()
  8867. }else {
  8868. this.m_prismatic2 = def.joint2;
  8869. this.m_groundAnchor2.SetV(this.m_prismatic2.m_localAnchor1);
  8870. this.m_localAnchor2.SetV(this.m_prismatic2.m_localAnchor2);
  8871. coordinate2 = this.m_prismatic2.GetJointTranslation()
  8872. }
  8873. this.m_ratio = def.ratio;
  8874. this.m_constant = coordinate1 + this.m_ratio * coordinate2;
  8875. this.m_impulse = 0
  8876. };
  8877. b2GearJoint.prototype.__varz = function() {
  8878. this.m_groundAnchor1 = new b2Vec2;
  8879. this.m_groundAnchor2 = new b2Vec2;
  8880. this.m_localAnchor1 = new b2Vec2;
  8881. this.m_localAnchor2 = new b2Vec2;
  8882. this.m_J = new b2Jacobian
  8883. };
  8884. b2GearJoint.prototype.InitVelocityConstraints = function(step) {
  8885. var g1 = this.m_ground1;
  8886. var g2 = this.m_ground2;
  8887. var bA = this.m_bodyA;
  8888. var bB = this.m_bodyB;
  8889. var ugX;
  8890. var ugY;
  8891. var rX;
  8892. var rY;
  8893. var tMat;
  8894. var tVec;
  8895. var crug;
  8896. var tX;
  8897. var K = 0;
  8898. this.m_J.SetZero();
  8899. if(this.m_revolute1) {
  8900. this.m_J.angularA = -1;
  8901. K += bA.m_invI
  8902. }else {
  8903. tMat = g1.m_xf.R;
  8904. tVec = this.m_prismatic1.m_localXAxis1;
  8905. ugX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  8906. ugY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  8907. tMat = bA.m_xf.R;
  8908. rX = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  8909. rY = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  8910. tX = tMat.col1.x * rX + tMat.col2.x * rY;
  8911. rY = tMat.col1.y * rX + tMat.col2.y * rY;
  8912. rX = tX;
  8913. crug = rX * ugY - rY * ugX;
  8914. this.m_J.linearA.Set(-ugX, -ugY);
  8915. this.m_J.angularA = -crug;
  8916. K += bA.m_invMass + bA.m_invI * crug * crug
  8917. }
  8918. if(this.m_revolute2) {
  8919. this.m_J.angularB = -this.m_ratio;
  8920. K += this.m_ratio * this.m_ratio * bB.m_invI
  8921. }else {
  8922. tMat = g2.m_xf.R;
  8923. tVec = this.m_prismatic2.m_localXAxis1;
  8924. ugX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  8925. ugY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  8926. tMat = bB.m_xf.R;
  8927. rX = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  8928. rY = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  8929. tX = tMat.col1.x * rX + tMat.col2.x * rY;
  8930. rY = tMat.col1.y * rX + tMat.col2.y * rY;
  8931. rX = tX;
  8932. crug = rX * ugY - rY * ugX;
  8933. this.m_J.linearB.Set(-this.m_ratio * ugX, -this.m_ratio * ugY);
  8934. this.m_J.angularB = -this.m_ratio * crug;
  8935. K += this.m_ratio * this.m_ratio * (bB.m_invMass + bB.m_invI * crug * crug)
  8936. }
  8937. this.m_mass = K > 0 ? 1 / K : 0;
  8938. if(step.warmStarting) {
  8939. bA.m_linearVelocity.x += bA.m_invMass * this.m_impulse * this.m_J.linearA.x;
  8940. bA.m_linearVelocity.y += bA.m_invMass * this.m_impulse * this.m_J.linearA.y;
  8941. bA.m_angularVelocity += bA.m_invI * this.m_impulse * this.m_J.angularA;
  8942. bB.m_linearVelocity.x += bB.m_invMass * this.m_impulse * this.m_J.linearB.x;
  8943. bB.m_linearVelocity.y += bB.m_invMass * this.m_impulse * this.m_J.linearB.y;
  8944. bB.m_angularVelocity += bB.m_invI * this.m_impulse * this.m_J.angularB
  8945. }else {
  8946. this.m_impulse = 0
  8947. }
  8948. };
  8949. b2GearJoint.prototype.SolveVelocityConstraints = function(step) {
  8950. var bA = this.m_bodyA;
  8951. var bB = this.m_bodyB;
  8952. var Cdot = this.m_J.Compute(bA.m_linearVelocity, bA.m_angularVelocity, bB.m_linearVelocity, bB.m_angularVelocity);
  8953. var impulse = -this.m_mass * Cdot;
  8954. this.m_impulse += impulse;
  8955. bA.m_linearVelocity.x += bA.m_invMass * impulse * this.m_J.linearA.x;
  8956. bA.m_linearVelocity.y += bA.m_invMass * impulse * this.m_J.linearA.y;
  8957. bA.m_angularVelocity += bA.m_invI * impulse * this.m_J.angularA;
  8958. bB.m_linearVelocity.x += bB.m_invMass * impulse * this.m_J.linearB.x;
  8959. bB.m_linearVelocity.y += bB.m_invMass * impulse * this.m_J.linearB.y;
  8960. bB.m_angularVelocity += bB.m_invI * impulse * this.m_J.angularB
  8961. };
  8962. b2GearJoint.prototype.SolvePositionConstraints = function(baumgarte) {
  8963. var linearError = 0;
  8964. var bA = this.m_bodyA;
  8965. var bB = this.m_bodyB;
  8966. var coordinate1;
  8967. var coordinate2;
  8968. if(this.m_revolute1) {
  8969. coordinate1 = this.m_revolute1.GetJointAngle()
  8970. }else {
  8971. coordinate1 = this.m_prismatic1.GetJointTranslation()
  8972. }
  8973. if(this.m_revolute2) {
  8974. coordinate2 = this.m_revolute2.GetJointAngle()
  8975. }else {
  8976. coordinate2 = this.m_prismatic2.GetJointTranslation()
  8977. }
  8978. var C = this.m_constant - (coordinate1 + this.m_ratio * coordinate2);
  8979. var impulse = -this.m_mass * C;
  8980. bA.m_sweep.c.x += bA.m_invMass * impulse * this.m_J.linearA.x;
  8981. bA.m_sweep.c.y += bA.m_invMass * impulse * this.m_J.linearA.y;
  8982. bA.m_sweep.a += bA.m_invI * impulse * this.m_J.angularA;
  8983. bB.m_sweep.c.x += bB.m_invMass * impulse * this.m_J.linearB.x;
  8984. bB.m_sweep.c.y += bB.m_invMass * impulse * this.m_J.linearB.y;
  8985. bB.m_sweep.a += bB.m_invI * impulse * this.m_J.angularB;
  8986. bA.SynchronizeTransform();
  8987. bB.SynchronizeTransform();
  8988. return linearError < b2Settings.b2_linearSlop
  8989. };
  8990. b2GearJoint.prototype.GetAnchorA = function() {
  8991. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1)
  8992. };
  8993. b2GearJoint.prototype.GetAnchorB = function() {
  8994. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2)
  8995. };
  8996. b2GearJoint.prototype.GetReactionForce = function(inv_dt) {
  8997. return new b2Vec2(inv_dt * this.m_impulse * this.m_J.linearB.x, inv_dt * this.m_impulse * this.m_J.linearB.y)
  8998. };
  8999. b2GearJoint.prototype.GetReactionTorque = function(inv_dt) {
  9000. var tMat = this.m_bodyB.m_xf.R;
  9001. var rX = this.m_localAnchor1.x - this.m_bodyB.m_sweep.localCenter.x;
  9002. var rY = this.m_localAnchor1.y - this.m_bodyB.m_sweep.localCenter.y;
  9003. var tX = tMat.col1.x * rX + tMat.col2.x * rY;
  9004. rY = tMat.col1.y * rX + tMat.col2.y * rY;
  9005. rX = tX;
  9006. var PX = this.m_impulse * this.m_J.linearB.x;
  9007. var PY = this.m_impulse * this.m_J.linearB.y;
  9008. return inv_dt * (this.m_impulse * this.m_J.angularB - rX * PY + rY * PX)
  9009. };
  9010. b2GearJoint.prototype.GetRatio = function() {
  9011. return this.m_ratio
  9012. };
  9013. b2GearJoint.prototype.SetRatio = function(ratio) {
  9014. this.m_ratio = ratio
  9015. };
  9016. b2GearJoint.prototype.m_ground1 = null;
  9017. b2GearJoint.prototype.m_ground2 = null;
  9018. b2GearJoint.prototype.m_revolute1 = null;
  9019. b2GearJoint.prototype.m_prismatic1 = null;
  9020. b2GearJoint.prototype.m_revolute2 = null;
  9021. b2GearJoint.prototype.m_prismatic2 = null;
  9022. b2GearJoint.prototype.m_groundAnchor1 = new b2Vec2;
  9023. b2GearJoint.prototype.m_groundAnchor2 = new b2Vec2;
  9024. b2GearJoint.prototype.m_localAnchor1 = new b2Vec2;
  9025. b2GearJoint.prototype.m_localAnchor2 = new b2Vec2;
  9026. b2GearJoint.prototype.m_J = new b2Jacobian;
  9027. b2GearJoint.prototype.m_constant = null;
  9028. b2GearJoint.prototype.m_ratio = null;
  9029. b2GearJoint.prototype.m_mass = null;
  9030. b2GearJoint.prototype.m_impulse = null;var b2TOIInput = function() {
  9031. this.__varz();
  9032. this.__constructor.apply(this, arguments)
  9033. };
  9034. b2TOIInput.prototype.__constructor = function() {
  9035. };
  9036. b2TOIInput.prototype.__varz = function() {
  9037. this.proxyA = new b2DistanceProxy;
  9038. this.proxyB = new b2DistanceProxy;
  9039. this.sweepA = new b2Sweep;
  9040. this.sweepB = new b2Sweep
  9041. };
  9042. b2TOIInput.prototype.proxyA = new b2DistanceProxy;
  9043. b2TOIInput.prototype.proxyB = new b2DistanceProxy;
  9044. b2TOIInput.prototype.sweepA = new b2Sweep;
  9045. b2TOIInput.prototype.sweepB = new b2Sweep;
  9046. b2TOIInput.prototype.tolerance = null;var b2RevoluteJointDef = function() {
  9047. b2JointDef.prototype.__varz.call(this);
  9048. this.__varz();
  9049. this.__constructor.apply(this, arguments)
  9050. };
  9051. extend(b2RevoluteJointDef.prototype, b2JointDef.prototype);
  9052. b2RevoluteJointDef.prototype._super = b2JointDef.prototype;
  9053. b2RevoluteJointDef.prototype.__constructor = function() {
  9054. this._super.__constructor.apply(this, arguments);
  9055. this.type = b2Joint.e_revoluteJoint;
  9056. this.localAnchorA.Set(0, 0);
  9057. this.localAnchorB.Set(0, 0);
  9058. this.referenceAngle = 0;
  9059. this.lowerAngle = 0;
  9060. this.upperAngle = 0;
  9061. this.maxMotorTorque = 0;
  9062. this.motorSpeed = 0;
  9063. this.enableLimit = false;
  9064. this.enableMotor = false
  9065. };
  9066. b2RevoluteJointDef.prototype.__varz = function() {
  9067. this.localAnchorA = new b2Vec2;
  9068. this.localAnchorB = new b2Vec2
  9069. };
  9070. b2RevoluteJointDef.prototype.Initialize = function(bA, bB, anchor) {
  9071. this.bodyA = bA;
  9072. this.bodyB = bB;
  9073. this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
  9074. this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
  9075. this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle()
  9076. };
  9077. b2RevoluteJointDef.prototype.localAnchorA = new b2Vec2;
  9078. b2RevoluteJointDef.prototype.localAnchorB = new b2Vec2;
  9079. b2RevoluteJointDef.prototype.referenceAngle = null;
  9080. b2RevoluteJointDef.prototype.enableLimit = null;
  9081. b2RevoluteJointDef.prototype.lowerAngle = null;
  9082. b2RevoluteJointDef.prototype.upperAngle = null;
  9083. b2RevoluteJointDef.prototype.enableMotor = null;
  9084. b2RevoluteJointDef.prototype.motorSpeed = null;
  9085. b2RevoluteJointDef.prototype.maxMotorTorque = null;var b2MouseJointDef = function() {
  9086. b2JointDef.prototype.__varz.call(this);
  9087. this.__varz();
  9088. this.__constructor.apply(this, arguments)
  9089. };
  9090. extend(b2MouseJointDef.prototype, b2JointDef.prototype);
  9091. b2MouseJointDef.prototype._super = b2JointDef.prototype;
  9092. b2MouseJointDef.prototype.__constructor = function() {
  9093. this._super.__constructor.apply(this, arguments);
  9094. this.type = b2Joint.e_mouseJoint;
  9095. this.maxForce = 0;
  9096. this.frequencyHz = 5;
  9097. this.dampingRatio = 0.7
  9098. };
  9099. b2MouseJointDef.prototype.__varz = function() {
  9100. this.target = new b2Vec2
  9101. };
  9102. b2MouseJointDef.prototype.target = new b2Vec2;
  9103. b2MouseJointDef.prototype.maxForce = null;
  9104. b2MouseJointDef.prototype.frequencyHz = null;
  9105. b2MouseJointDef.prototype.dampingRatio = null;var b2Contact = function() {
  9106. this.__varz();
  9107. this.__constructor.apply(this, arguments)
  9108. };
  9109. b2Contact.prototype.__constructor = function() {
  9110. };
  9111. b2Contact.prototype.__varz = function() {
  9112. this.m_nodeA = new b2ContactEdge;
  9113. this.m_nodeB = new b2ContactEdge;
  9114. this.m_manifold = new b2Manifold;
  9115. this.m_oldManifold = new b2Manifold
  9116. };
  9117. b2Contact.s_input = new b2TOIInput;
  9118. b2Contact.e_sensorFlag = 1;
  9119. b2Contact.e_continuousFlag = 2;
  9120. b2Contact.e_islandFlag = 4;
  9121. b2Contact.e_toiFlag = 8;
  9122. b2Contact.e_touchingFlag = 16;
  9123. b2Contact.e_enabledFlag = 32;
  9124. b2Contact.e_filterFlag = 64;
  9125. b2Contact.prototype.Reset = function(fixtureA, fixtureB) {
  9126. this.m_flags = b2Contact.e_enabledFlag;
  9127. if(!fixtureA || !fixtureB) {
  9128. this.m_fixtureA = null;
  9129. this.m_fixtureB = null;
  9130. return
  9131. }
  9132. if(fixtureA.IsSensor() || fixtureB.IsSensor()) {
  9133. this.m_flags |= b2Contact.e_sensorFlag
  9134. }
  9135. var bodyA = fixtureA.GetBody();
  9136. var bodyB = fixtureB.GetBody();
  9137. if(bodyA.GetType() != b2Body.b2_dynamicBody || bodyA.IsBullet() || bodyB.GetType() != b2Body.b2_dynamicBody || bodyB.IsBullet()) {
  9138. this.m_flags |= b2Contact.e_continuousFlag
  9139. }
  9140. this.m_fixtureA = fixtureA;
  9141. this.m_fixtureB = fixtureB;
  9142. this.m_manifold.m_pointCount = 0;
  9143. this.m_prev = null;
  9144. this.m_next = null;
  9145. this.m_nodeA.contact = null;
  9146. this.m_nodeA.prev = null;
  9147. this.m_nodeA.next = null;
  9148. this.m_nodeA.other = null;
  9149. this.m_nodeB.contact = null;
  9150. this.m_nodeB.prev = null;
  9151. this.m_nodeB.next = null;
  9152. this.m_nodeB.other = null
  9153. };
  9154. b2Contact.prototype.Update = function(listener) {
  9155. var tManifold = this.m_oldManifold;
  9156. this.m_oldManifold = this.m_manifold;
  9157. this.m_manifold = tManifold;
  9158. this.m_flags |= b2Contact.e_enabledFlag;
  9159. var touching = false;
  9160. var wasTouching = (this.m_flags & b2Contact.e_touchingFlag) == b2Contact.e_touchingFlag;
  9161. var bodyA = this.m_fixtureA.m_body;
  9162. var bodyB = this.m_fixtureB.m_body;
  9163. var aabbOverlap = this.m_fixtureA.m_aabb.TestOverlap(this.m_fixtureB.m_aabb);
  9164. if(this.m_flags & b2Contact.e_sensorFlag) {
  9165. if(aabbOverlap) {
  9166. var shapeA = this.m_fixtureA.GetShape();
  9167. var shapeB = this.m_fixtureB.GetShape();
  9168. var xfA = bodyA.GetTransform();
  9169. var xfB = bodyB.GetTransform();
  9170. touching = b2Shape.TestOverlap(shapeA, xfA, shapeB, xfB)
  9171. }
  9172. this.m_manifold.m_pointCount = 0
  9173. }else {
  9174. if(bodyA.GetType() != b2Body.b2_dynamicBody || bodyA.IsBullet() || bodyB.GetType() != b2Body.b2_dynamicBody || bodyB.IsBullet()) {
  9175. this.m_flags |= b2Contact.e_continuousFlag
  9176. }else {
  9177. this.m_flags &= ~b2Contact.e_continuousFlag
  9178. }
  9179. if(aabbOverlap) {
  9180. this.Evaluate();
  9181. touching = this.m_manifold.m_pointCount > 0;
  9182. for(var i = 0;i < this.m_manifold.m_pointCount;++i) {
  9183. var mp2 = this.m_manifold.m_points[i];
  9184. mp2.m_normalImpulse = 0;
  9185. mp2.m_tangentImpulse = 0;
  9186. var id2 = mp2.m_id;
  9187. for(var j = 0;j < this.m_oldManifold.m_pointCount;++j) {
  9188. var mp1 = this.m_oldManifold.m_points[j];
  9189. if(mp1.m_id.key == id2.key) {
  9190. mp2.m_normalImpulse = mp1.m_normalImpulse;
  9191. mp2.m_tangentImpulse = mp1.m_tangentImpulse;
  9192. break
  9193. }
  9194. }
  9195. }
  9196. }else {
  9197. this.m_manifold.m_pointCount = 0
  9198. }
  9199. if(touching != wasTouching) {
  9200. bodyA.SetAwake(true);
  9201. bodyB.SetAwake(true)
  9202. }
  9203. }
  9204. if(touching) {
  9205. this.m_flags |= b2Contact.e_touchingFlag
  9206. }else {
  9207. this.m_flags &= ~b2Contact.e_touchingFlag
  9208. }
  9209. if(wasTouching == false && touching == true) {
  9210. listener.BeginContact(this)
  9211. }
  9212. if(wasTouching == true && touching == false) {
  9213. listener.EndContact(this)
  9214. }
  9215. if((this.m_flags & b2Contact.e_sensorFlag) == 0) {
  9216. listener.PreSolve(this, this.m_oldManifold)
  9217. }
  9218. };
  9219. b2Contact.prototype.Evaluate = function() {
  9220. };
  9221. b2Contact.prototype.ComputeTOI = function(sweepA, sweepB) {
  9222. b2Contact.s_input.proxyA.Set(this.m_fixtureA.GetShape());
  9223. b2Contact.s_input.proxyB.Set(this.m_fixtureB.GetShape());
  9224. b2Contact.s_input.sweepA = sweepA;
  9225. b2Contact.s_input.sweepB = sweepB;
  9226. b2Contact.s_input.tolerance = b2Settings.b2_linearSlop;
  9227. return b2TimeOfImpact.TimeOfImpact(b2Contact.s_input)
  9228. };
  9229. b2Contact.prototype.GetManifold = function() {
  9230. return this.m_manifold
  9231. };
  9232. b2Contact.prototype.GetWorldManifold = function(worldManifold) {
  9233. var bodyA = this.m_fixtureA.GetBody();
  9234. var bodyB = this.m_fixtureB.GetBody();
  9235. var shapeA = this.m_fixtureA.GetShape();
  9236. var shapeB = this.m_fixtureB.GetShape();
  9237. worldManifold.Initialize(this.m_manifold, bodyA.GetTransform(), shapeA.m_radius, bodyB.GetTransform(), shapeB.m_radius)
  9238. };
  9239. b2Contact.prototype.IsTouching = function() {
  9240. return(this.m_flags & b2Contact.e_touchingFlag) == b2Contact.e_touchingFlag
  9241. };
  9242. b2Contact.prototype.IsContinuous = function() {
  9243. return(this.m_flags & b2Contact.e_continuousFlag) == b2Contact.e_continuousFlag
  9244. };
  9245. b2Contact.prototype.SetSensor = function(sensor) {
  9246. if(sensor) {
  9247. this.m_flags |= b2Contact.e_sensorFlag
  9248. }else {
  9249. this.m_flags &= ~b2Contact.e_sensorFlag
  9250. }
  9251. };
  9252. b2Contact.prototype.IsSensor = function() {
  9253. return(this.m_flags & b2Contact.e_sensorFlag) == b2Contact.e_sensorFlag
  9254. };
  9255. b2Contact.prototype.SetEnabled = function(flag) {
  9256. if(flag) {
  9257. this.m_flags |= b2Contact.e_enabledFlag
  9258. }else {
  9259. this.m_flags &= ~b2Contact.e_enabledFlag
  9260. }
  9261. };
  9262. b2Contact.prototype.IsEnabled = function() {
  9263. return(this.m_flags & b2Contact.e_enabledFlag) == b2Contact.e_enabledFlag
  9264. };
  9265. b2Contact.prototype.GetNext = function() {
  9266. return this.m_next
  9267. };
  9268. b2Contact.prototype.GetFixtureA = function() {
  9269. return this.m_fixtureA
  9270. };
  9271. b2Contact.prototype.GetFixtureB = function() {
  9272. return this.m_fixtureB
  9273. };
  9274. b2Contact.prototype.FlagForFiltering = function() {
  9275. this.m_flags |= b2Contact.e_filterFlag
  9276. };
  9277. b2Contact.prototype.m_flags = 0;
  9278. b2Contact.prototype.m_prev = null;
  9279. b2Contact.prototype.m_next = null;
  9280. b2Contact.prototype.m_nodeA = new b2ContactEdge;
  9281. b2Contact.prototype.m_nodeB = new b2ContactEdge;
  9282. b2Contact.prototype.m_fixtureA = null;
  9283. b2Contact.prototype.m_fixtureB = null;
  9284. b2Contact.prototype.m_manifold = new b2Manifold;
  9285. b2Contact.prototype.m_oldManifold = new b2Manifold;
  9286. b2Contact.prototype.m_toi = null;var b2ContactConstraint = function() {
  9287. this.__varz();
  9288. this.__constructor.apply(this, arguments)
  9289. };
  9290. b2ContactConstraint.prototype.__constructor = function() {
  9291. this.points = new Array(b2Settings.b2_maxManifoldPoints);
  9292. for(var i = 0;i < b2Settings.b2_maxManifoldPoints;i++) {
  9293. this.points[i] = new b2ContactConstraintPoint
  9294. }
  9295. };
  9296. b2ContactConstraint.prototype.__varz = function() {
  9297. this.localPlaneNormal = new b2Vec2;
  9298. this.localPoint = new b2Vec2;
  9299. this.normal = new b2Vec2;
  9300. this.normalMass = new b2Mat22;
  9301. this.K = new b2Mat22
  9302. };
  9303. b2ContactConstraint.prototype.points = null;
  9304. b2ContactConstraint.prototype.localPlaneNormal = new b2Vec2;
  9305. b2ContactConstraint.prototype.localPoint = new b2Vec2;
  9306. b2ContactConstraint.prototype.normal = new b2Vec2;
  9307. b2ContactConstraint.prototype.normalMass = new b2Mat22;
  9308. b2ContactConstraint.prototype.K = new b2Mat22;
  9309. b2ContactConstraint.prototype.bodyA = null;
  9310. b2ContactConstraint.prototype.bodyB = null;
  9311. b2ContactConstraint.prototype.type = 0;
  9312. b2ContactConstraint.prototype.radius = null;
  9313. b2ContactConstraint.prototype.friction = null;
  9314. b2ContactConstraint.prototype.restitution = null;
  9315. b2ContactConstraint.prototype.pointCount = 0;
  9316. b2ContactConstraint.prototype.manifold = null;var b2ContactResult = function() {
  9317. this.__varz();
  9318. this.__constructor.apply(this, arguments)
  9319. };
  9320. b2ContactResult.prototype.__constructor = function() {
  9321. };
  9322. b2ContactResult.prototype.__varz = function() {
  9323. this.position = new b2Vec2;
  9324. this.normal = new b2Vec2;
  9325. this.id = new b2ContactID
  9326. };
  9327. b2ContactResult.prototype.shape1 = null;
  9328. b2ContactResult.prototype.shape2 = null;
  9329. b2ContactResult.prototype.position = new b2Vec2;
  9330. b2ContactResult.prototype.normal = new b2Vec2;
  9331. b2ContactResult.prototype.normalImpulse = null;
  9332. b2ContactResult.prototype.tangentImpulse = null;
  9333. b2ContactResult.prototype.id = new b2ContactID;var b2PolygonContact = function() {
  9334. b2Contact.prototype.__varz.call(this);
  9335. this.__varz();
  9336. this.__constructor.apply(this, arguments)
  9337. };
  9338. extend(b2PolygonContact.prototype, b2Contact.prototype);
  9339. b2PolygonContact.prototype._super = b2Contact.prototype;
  9340. b2PolygonContact.prototype.__constructor = function() {
  9341. this._super.__constructor.apply(this, arguments)
  9342. };
  9343. b2PolygonContact.prototype.__varz = function() {
  9344. };
  9345. b2PolygonContact.Create = function(allocator) {
  9346. return new b2PolygonContact
  9347. };
  9348. b2PolygonContact.Destroy = function(contact, allocator) {
  9349. };
  9350. b2PolygonContact.prototype.Evaluate = function() {
  9351. var bA = this.m_fixtureA.GetBody();
  9352. var bB = this.m_fixtureB.GetBody();
  9353. b2Collision.CollidePolygons(this.m_manifold, this.m_fixtureA.GetShape(), bA.m_xf, this.m_fixtureB.GetShape(), bB.m_xf)
  9354. };
  9355. b2PolygonContact.prototype.Reset = function(fixtureA, fixtureB) {
  9356. this._super.Reset.apply(this, [fixtureA, fixtureB])
  9357. };var ClipVertex = function() {
  9358. this.__varz();
  9359. this.__constructor.apply(this, arguments)
  9360. };
  9361. ClipVertex.prototype.__constructor = function() {
  9362. };
  9363. ClipVertex.prototype.__varz = function() {
  9364. this.v = new b2Vec2;
  9365. this.id = new b2ContactID
  9366. };
  9367. ClipVertex.prototype.Set = function(other) {
  9368. this.v.SetV(other.v);
  9369. this.id.Set(other.id)
  9370. };
  9371. ClipVertex.prototype.v = new b2Vec2;
  9372. ClipVertex.prototype.id = new b2ContactID;var b2ContactFilter = function() {
  9373. this.__varz();
  9374. this.__constructor.apply(this, arguments)
  9375. };
  9376. b2ContactFilter.prototype.__constructor = function() {
  9377. };
  9378. b2ContactFilter.prototype.__varz = function() {
  9379. };
  9380. b2ContactFilter.b2_defaultFilter = new b2ContactFilter;
  9381. b2ContactFilter.prototype.ShouldCollide = function(fixtureA, fixtureB) {
  9382. var filter1 = fixtureA.GetFilterData();
  9383. var filter2 = fixtureB.GetFilterData();
  9384. if(filter1.groupIndex == filter2.groupIndex && filter1.groupIndex != 0) {
  9385. return filter1.groupIndex > 0
  9386. }
  9387. var collide = (filter1.maskBits & filter2.categoryBits) != 0 && (filter1.categoryBits & filter2.maskBits) != 0;
  9388. return collide
  9389. };
  9390. b2ContactFilter.prototype.RayCollide = function(userData, fixture) {
  9391. if(!userData) {
  9392. return true
  9393. }
  9394. return this.ShouldCollide(userData, fixture)
  9395. };var b2NullContact = function() {
  9396. b2Contact.prototype.__varz.call(this);
  9397. this.__varz();
  9398. this.__constructor.apply(this, arguments)
  9399. };
  9400. extend(b2NullContact.prototype, b2Contact.prototype);
  9401. b2NullContact.prototype._super = b2Contact.prototype;
  9402. b2NullContact.prototype.__constructor = function() {
  9403. this._super.__constructor.apply(this, arguments)
  9404. };
  9405. b2NullContact.prototype.__varz = function() {
  9406. };
  9407. b2NullContact.prototype.Evaluate = function() {
  9408. };var b2ContactListener = function() {
  9409. this.__varz();
  9410. this.__constructor.apply(this, arguments)
  9411. };
  9412. b2ContactListener.prototype.__constructor = function() {
  9413. };
  9414. b2ContactListener.prototype.__varz = function() {
  9415. };
  9416. b2ContactListener.b2_defaultListener = new b2ContactListener;
  9417. b2ContactListener.prototype.BeginContact = function(contact) {
  9418. };
  9419. b2ContactListener.prototype.EndContact = function(contact) {
  9420. };
  9421. b2ContactListener.prototype.PreSolve = function(contact, oldManifold) {
  9422. };
  9423. b2ContactListener.prototype.PostSolve = function(contact, impulse) {
  9424. };var b2Island = function() {
  9425. this.__varz();
  9426. this.__constructor.apply(this, arguments)
  9427. };
  9428. b2Island.prototype.__constructor = function() {
  9429. this.m_bodies = new Array;
  9430. this.m_contacts = new Array;
  9431. this.m_joints = new Array
  9432. };
  9433. b2Island.prototype.__varz = function() {
  9434. };
  9435. b2Island.s_impulse = new b2ContactImpulse;
  9436. b2Island.prototype.Initialize = function(bodyCapacity, contactCapacity, jointCapacity, allocator, listener, contactSolver) {
  9437. var i = 0;
  9438. this.m_bodyCapacity = bodyCapacity;
  9439. this.m_contactCapacity = contactCapacity;
  9440. this.m_jointCapacity = jointCapacity;
  9441. this.m_bodyCount = 0;
  9442. this.m_contactCount = 0;
  9443. this.m_jointCount = 0;
  9444. this.m_allocator = allocator;
  9445. this.m_listener = listener;
  9446. this.m_contactSolver = contactSolver;
  9447. for(i = this.m_bodies.length;i < bodyCapacity;i++) {
  9448. this.m_bodies[i] = null
  9449. }
  9450. for(i = this.m_contacts.length;i < contactCapacity;i++) {
  9451. this.m_contacts[i] = null
  9452. }
  9453. for(i = this.m_joints.length;i < jointCapacity;i++) {
  9454. this.m_joints[i] = null
  9455. }
  9456. };
  9457. b2Island.prototype.Clear = function() {
  9458. this.m_bodyCount = 0;
  9459. this.m_contactCount = 0;
  9460. this.m_jointCount = 0
  9461. };
  9462. b2Island.prototype.Solve = function(step, gravity, allowSleep) {
  9463. var i = 0;
  9464. var j = 0;
  9465. var b;
  9466. var joint;
  9467. for(i = 0;i < this.m_bodyCount;++i) {
  9468. b = this.m_bodies[i];
  9469. if(b.GetType() != b2Body.b2_dynamicBody) {
  9470. continue
  9471. }
  9472. b.m_linearVelocity.x += step.dt * (gravity.x + b.m_invMass * b.m_force.x);
  9473. b.m_linearVelocity.y += step.dt * (gravity.y + b.m_invMass * b.m_force.y);
  9474. b.m_angularVelocity += step.dt * b.m_invI * b.m_torque;
  9475. b.m_linearVelocity.Multiply(b2Math.Clamp(1 - step.dt * b.m_linearDamping, 0, 1));
  9476. b.m_angularVelocity *= b2Math.Clamp(1 - step.dt * b.m_angularDamping, 0, 1)
  9477. }
  9478. this.m_contactSolver.Initialize(step, this.m_contacts, this.m_contactCount, this.m_allocator);
  9479. var contactSolver = this.m_contactSolver;
  9480. contactSolver.InitVelocityConstraints(step);
  9481. for(i = 0;i < this.m_jointCount;++i) {
  9482. joint = this.m_joints[i];
  9483. joint.InitVelocityConstraints(step)
  9484. }
  9485. for(i = 0;i < step.velocityIterations;++i) {
  9486. for(j = 0;j < this.m_jointCount;++j) {
  9487. joint = this.m_joints[j];
  9488. joint.SolveVelocityConstraints(step)
  9489. }
  9490. contactSolver.SolveVelocityConstraints()
  9491. }
  9492. for(i = 0;i < this.m_jointCount;++i) {
  9493. joint = this.m_joints[i];
  9494. joint.FinalizeVelocityConstraints()
  9495. }
  9496. contactSolver.FinalizeVelocityConstraints();
  9497. for(i = 0;i < this.m_bodyCount;++i) {
  9498. b = this.m_bodies[i];
  9499. if(b.GetType() == b2Body.b2_staticBody) {
  9500. continue
  9501. }
  9502. var translationX = step.dt * b.m_linearVelocity.x;
  9503. var translationY = step.dt * b.m_linearVelocity.y;
  9504. if(translationX * translationX + translationY * translationY > b2Settings.b2_maxTranslationSquared) {
  9505. b.m_linearVelocity.Normalize();
  9506. b.m_linearVelocity.x *= b2Settings.b2_maxTranslation * step.inv_dt;
  9507. b.m_linearVelocity.y *= b2Settings.b2_maxTranslation * step.inv_dt
  9508. }
  9509. var rotation = step.dt * b.m_angularVelocity;
  9510. if(rotation * rotation > b2Settings.b2_maxRotationSquared) {
  9511. if(b.m_angularVelocity < 0) {
  9512. b.m_angularVelocity = -b2Settings.b2_maxRotation * step.inv_dt
  9513. }else {
  9514. b.m_angularVelocity = b2Settings.b2_maxRotation * step.inv_dt
  9515. }
  9516. }
  9517. b.m_sweep.c0.SetV(b.m_sweep.c);
  9518. b.m_sweep.a0 = b.m_sweep.a;
  9519. b.m_sweep.c.x += step.dt * b.m_linearVelocity.x;
  9520. b.m_sweep.c.y += step.dt * b.m_linearVelocity.y;
  9521. b.m_sweep.a += step.dt * b.m_angularVelocity;
  9522. b.SynchronizeTransform()
  9523. }
  9524. for(i = 0;i < step.positionIterations;++i) {
  9525. var contactsOkay = contactSolver.SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
  9526. var jointsOkay = true;
  9527. for(j = 0;j < this.m_jointCount;++j) {
  9528. joint = this.m_joints[j];
  9529. var jointOkay = joint.SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
  9530. jointsOkay = jointsOkay && jointOkay
  9531. }
  9532. if(contactsOkay && jointsOkay) {
  9533. break
  9534. }
  9535. }
  9536. this.Report(contactSolver.m_constraints);
  9537. if(allowSleep) {
  9538. var minSleepTime = Number.MAX_VALUE;
  9539. var linTolSqr = b2Settings.b2_linearSleepTolerance * b2Settings.b2_linearSleepTolerance;
  9540. var angTolSqr = b2Settings.b2_angularSleepTolerance * b2Settings.b2_angularSleepTolerance;
  9541. for(i = 0;i < this.m_bodyCount;++i) {
  9542. b = this.m_bodies[i];
  9543. if(b.GetType() == b2Body.b2_staticBody) {
  9544. continue
  9545. }
  9546. if((b.m_flags & b2Body.e_allowSleepFlag) == 0) {
  9547. b.m_sleepTime = 0;
  9548. minSleepTime = 0
  9549. }
  9550. if((b.m_flags & b2Body.e_allowSleepFlag) == 0 || b.m_angularVelocity * b.m_angularVelocity > angTolSqr || b2Math.Dot(b.m_linearVelocity, b.m_linearVelocity) > linTolSqr) {
  9551. b.m_sleepTime = 0;
  9552. minSleepTime = 0
  9553. }else {
  9554. b.m_sleepTime += step.dt;
  9555. minSleepTime = b2Math.Min(minSleepTime, b.m_sleepTime)
  9556. }
  9557. }
  9558. if(minSleepTime >= b2Settings.b2_timeToSleep) {
  9559. for(i = 0;i < this.m_bodyCount;++i) {
  9560. b = this.m_bodies[i];
  9561. b.SetAwake(false)
  9562. }
  9563. }
  9564. }
  9565. };
  9566. b2Island.prototype.SolveTOI = function(subStep) {
  9567. var i = 0;
  9568. var j = 0;
  9569. this.m_contactSolver.Initialize(subStep, this.m_contacts, this.m_contactCount, this.m_allocator);
  9570. var contactSolver = this.m_contactSolver;
  9571. for(i = 0;i < this.m_jointCount;++i) {
  9572. this.m_joints[i].InitVelocityConstraints(subStep)
  9573. }
  9574. for(i = 0;i < subStep.velocityIterations;++i) {
  9575. contactSolver.SolveVelocityConstraints();
  9576. for(j = 0;j < this.m_jointCount;++j) {
  9577. this.m_joints[j].SolveVelocityConstraints(subStep)
  9578. }
  9579. }
  9580. for(i = 0;i < this.m_bodyCount;++i) {
  9581. var b = this.m_bodies[i];
  9582. if(b.GetType() == b2Body.b2_staticBody) {
  9583. continue
  9584. }
  9585. var translationX = subStep.dt * b.m_linearVelocity.x;
  9586. var translationY = subStep.dt * b.m_linearVelocity.y;
  9587. if(translationX * translationX + translationY * translationY > b2Settings.b2_maxTranslationSquared) {
  9588. b.m_linearVelocity.Normalize();
  9589. b.m_linearVelocity.x *= b2Settings.b2_maxTranslation * subStep.inv_dt;
  9590. b.m_linearVelocity.y *= b2Settings.b2_maxTranslation * subStep.inv_dt
  9591. }
  9592. var rotation = subStep.dt * b.m_angularVelocity;
  9593. if(rotation * rotation > b2Settings.b2_maxRotationSquared) {
  9594. if(b.m_angularVelocity < 0) {
  9595. b.m_angularVelocity = -b2Settings.b2_maxRotation * subStep.inv_dt
  9596. }else {
  9597. b.m_angularVelocity = b2Settings.b2_maxRotation * subStep.inv_dt
  9598. }
  9599. }
  9600. b.m_sweep.c0.SetV(b.m_sweep.c);
  9601. b.m_sweep.a0 = b.m_sweep.a;
  9602. b.m_sweep.c.x += subStep.dt * b.m_linearVelocity.x;
  9603. b.m_sweep.c.y += subStep.dt * b.m_linearVelocity.y;
  9604. b.m_sweep.a += subStep.dt * b.m_angularVelocity;
  9605. b.SynchronizeTransform()
  9606. }
  9607. var k_toiBaumgarte = 0.75;
  9608. for(i = 0;i < subStep.positionIterations;++i) {
  9609. var contactsOkay = contactSolver.SolvePositionConstraints(k_toiBaumgarte);
  9610. var jointsOkay = true;
  9611. for(j = 0;j < this.m_jointCount;++j) {
  9612. var jointOkay = this.m_joints[j].SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
  9613. jointsOkay = jointsOkay && jointOkay
  9614. }
  9615. if(contactsOkay && jointsOkay) {
  9616. break
  9617. }
  9618. }
  9619. this.Report(contactSolver.m_constraints)
  9620. };
  9621. b2Island.prototype.Report = function(constraints) {
  9622. if(this.m_listener == null) {
  9623. return
  9624. }
  9625. for(var i = 0;i < this.m_contactCount;++i) {
  9626. var c = this.m_contacts[i];
  9627. var cc = constraints[i];
  9628. for(var j = 0;j < cc.pointCount;++j) {
  9629. b2Island.s_impulse.normalImpulses[j] = cc.points[j].normalImpulse;
  9630. b2Island.s_impulse.tangentImpulses[j] = cc.points[j].tangentImpulse
  9631. }
  9632. this.m_listener.PostSolve(c, b2Island.s_impulse)
  9633. }
  9634. };
  9635. b2Island.prototype.AddBody = function(body) {
  9636. body.m_islandIndex = this.m_bodyCount;
  9637. this.m_bodies[this.m_bodyCount++] = body
  9638. };
  9639. b2Island.prototype.AddContact = function(contact) {
  9640. this.m_contacts[this.m_contactCount++] = contact
  9641. };
  9642. b2Island.prototype.AddJoint = function(joint) {
  9643. this.m_joints[this.m_jointCount++] = joint
  9644. };
  9645. b2Island.prototype.m_allocator = null;
  9646. b2Island.prototype.m_listener = null;
  9647. b2Island.prototype.m_contactSolver = null;
  9648. b2Island.prototype.m_bodies = null;
  9649. b2Island.prototype.m_contacts = null;
  9650. b2Island.prototype.m_joints = null;
  9651. b2Island.prototype.m_bodyCount = 0;
  9652. b2Island.prototype.m_jointCount = 0;
  9653. b2Island.prototype.m_contactCount = 0;
  9654. b2Island.prototype.m_bodyCapacity = 0;
  9655. b2Island.prototype.m_contactCapacity = 0;
  9656. b2Island.prototype.m_jointCapacity = 0;var b2PolyAndEdgeContact = function() {
  9657. b2Contact.prototype.__varz.call(this);
  9658. this.__varz();
  9659. this.__constructor.apply(this, arguments)
  9660. };
  9661. extend(b2PolyAndEdgeContact.prototype, b2Contact.prototype);
  9662. b2PolyAndEdgeContact.prototype._super = b2Contact.prototype;
  9663. b2PolyAndEdgeContact.prototype.__constructor = function() {
  9664. this._super.__constructor.apply(this, arguments)
  9665. };
  9666. b2PolyAndEdgeContact.prototype.__varz = function() {
  9667. };
  9668. b2PolyAndEdgeContact.Create = function(allocator) {
  9669. return new b2PolyAndEdgeContact
  9670. };
  9671. b2PolyAndEdgeContact.Destroy = function(contact, allocator) {
  9672. };
  9673. b2PolyAndEdgeContact.prototype.Evaluate = function() {
  9674. var bA = this.m_fixtureA.GetBody();
  9675. var bB = this.m_fixtureB.GetBody();
  9676. this.b2CollidePolyAndEdge(this.m_manifold, this.m_fixtureA.GetShape(), bA.m_xf, this.m_fixtureB.GetShape(), bB.m_xf)
  9677. };
  9678. b2PolyAndEdgeContact.prototype.b2CollidePolyAndEdge = function(manifold, polygon, xf1, edge, xf2) {
  9679. };
  9680. b2PolyAndEdgeContact.prototype.Reset = function(fixtureA, fixtureB) {
  9681. this._super.Reset.apply(this, [fixtureA, fixtureB]);
  9682. b2Settings.b2Assert(fixtureA.GetType() == b2Shape.e_polygonShape);
  9683. b2Settings.b2Assert(fixtureB.GetType() == b2Shape.e_edgeShape)
  9684. };var b2Collision = function() {
  9685. this.__varz();
  9686. this.__constructor.apply(this, arguments)
  9687. };
  9688. b2Collision.prototype.__constructor = function() {
  9689. };
  9690. b2Collision.prototype.__varz = function() {
  9691. };
  9692. b2Collision.MakeClipPointVector = function() {
  9693. var r = new Array(2);
  9694. r[0] = new ClipVertex;
  9695. r[1] = new ClipVertex;
  9696. return r
  9697. };
  9698. b2Collision.ClipSegmentToLine = function(vOut, vIn, normal, offset) {
  9699. var cv;
  9700. var numOut = 0;
  9701. cv = vIn[0];
  9702. var vIn0 = cv.v;
  9703. cv = vIn[1];
  9704. var vIn1 = cv.v;
  9705. var distance0 = normal.x * vIn0.x + normal.y * vIn0.y - offset;
  9706. var distance1 = normal.x * vIn1.x + normal.y * vIn1.y - offset;
  9707. if(distance0 <= 0) {
  9708. vOut[numOut++].Set(vIn[0])
  9709. }
  9710. if(distance1 <= 0) {
  9711. vOut[numOut++].Set(vIn[1])
  9712. }
  9713. if(distance0 * distance1 < 0) {
  9714. var interp = distance0 / (distance0 - distance1);
  9715. cv = vOut[numOut];
  9716. var tVec = cv.v;
  9717. tVec.x = vIn0.x + interp * (vIn1.x - vIn0.x);
  9718. tVec.y = vIn0.y + interp * (vIn1.y - vIn0.y);
  9719. cv = vOut[numOut];
  9720. var cv2;
  9721. if(distance0 > 0) {
  9722. cv2 = vIn[0];
  9723. cv.id = cv2.id
  9724. }else {
  9725. cv2 = vIn[1];
  9726. cv.id = cv2.id
  9727. }
  9728. ++numOut
  9729. }
  9730. return numOut
  9731. };
  9732. b2Collision.EdgeSeparation = function(poly1, xf1, edge1, poly2, xf2) {
  9733. var count1 = poly1.m_vertexCount;
  9734. var vertices1 = poly1.m_vertices;
  9735. var normals1 = poly1.m_normals;
  9736. var count2 = poly2.m_vertexCount;
  9737. var vertices2 = poly2.m_vertices;
  9738. var tMat;
  9739. var tVec;
  9740. tMat = xf1.R;
  9741. tVec = normals1[edge1];
  9742. var normal1WorldX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  9743. var normal1WorldY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  9744. tMat = xf2.R;
  9745. var normal1X = tMat.col1.x * normal1WorldX + tMat.col1.y * normal1WorldY;
  9746. var normal1Y = tMat.col2.x * normal1WorldX + tMat.col2.y * normal1WorldY;
  9747. var index = 0;
  9748. var minDot = Number.MAX_VALUE;
  9749. for(var i = 0;i < count2;++i) {
  9750. tVec = vertices2[i];
  9751. var dot = tVec.x * normal1X + tVec.y * normal1Y;
  9752. if(dot < minDot) {
  9753. minDot = dot;
  9754. index = i
  9755. }
  9756. }
  9757. tVec = vertices1[edge1];
  9758. tMat = xf1.R;
  9759. var v1X = xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  9760. var v1Y = xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  9761. tVec = vertices2[index];
  9762. tMat = xf2.R;
  9763. var v2X = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  9764. var v2Y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  9765. v2X -= v1X;
  9766. v2Y -= v1Y;
  9767. var separation = v2X * normal1WorldX + v2Y * normal1WorldY;
  9768. return separation
  9769. };
  9770. b2Collision.FindMaxSeparation = function(edgeIndex, poly1, xf1, poly2, xf2) {
  9771. var count1 = poly1.m_vertexCount;
  9772. var normals1 = poly1.m_normals;
  9773. var tVec;
  9774. var tMat;
  9775. tMat = xf2.R;
  9776. tVec = poly2.m_centroid;
  9777. var dX = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  9778. var dY = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  9779. tMat = xf1.R;
  9780. tVec = poly1.m_centroid;
  9781. dX -= xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  9782. dY -= xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  9783. var dLocal1X = dX * xf1.R.col1.x + dY * xf1.R.col1.y;
  9784. var dLocal1Y = dX * xf1.R.col2.x + dY * xf1.R.col2.y;
  9785. var edge = 0;
  9786. var maxDot = -Number.MAX_VALUE;
  9787. for(var i = 0;i < count1;++i) {
  9788. tVec = normals1[i];
  9789. var dot = tVec.x * dLocal1X + tVec.y * dLocal1Y;
  9790. if(dot > maxDot) {
  9791. maxDot = dot;
  9792. edge = i
  9793. }
  9794. }
  9795. var s = b2Collision.EdgeSeparation(poly1, xf1, edge, poly2, xf2);
  9796. var prevEdge = edge - 1 >= 0 ? edge - 1 : count1 - 1;
  9797. var sPrev = b2Collision.EdgeSeparation(poly1, xf1, prevEdge, poly2, xf2);
  9798. var nextEdge = edge + 1 < count1 ? edge + 1 : 0;
  9799. var sNext = b2Collision.EdgeSeparation(poly1, xf1, nextEdge, poly2, xf2);
  9800. var bestEdge = 0;
  9801. var bestSeparation;
  9802. var increment = 0;
  9803. if(sPrev > s && sPrev > sNext) {
  9804. increment = -1;
  9805. bestEdge = prevEdge;
  9806. bestSeparation = sPrev
  9807. }else {
  9808. if(sNext > s) {
  9809. increment = 1;
  9810. bestEdge = nextEdge;
  9811. bestSeparation = sNext
  9812. }else {
  9813. edgeIndex[0] = edge;
  9814. return s
  9815. }
  9816. }
  9817. while(true) {
  9818. if(increment == -1) {
  9819. edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1
  9820. }else {
  9821. edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0
  9822. }
  9823. s = b2Collision.EdgeSeparation(poly1, xf1, edge, poly2, xf2);
  9824. if(s > bestSeparation) {
  9825. bestEdge = edge;
  9826. bestSeparation = s
  9827. }else {
  9828. break
  9829. }
  9830. }
  9831. edgeIndex[0] = bestEdge;
  9832. return bestSeparation
  9833. };
  9834. b2Collision.FindIncidentEdge = function(c, poly1, xf1, edge1, poly2, xf2) {
  9835. var count1 = poly1.m_vertexCount;
  9836. var normals1 = poly1.m_normals;
  9837. var count2 = poly2.m_vertexCount;
  9838. var vertices2 = poly2.m_vertices;
  9839. var normals2 = poly2.m_normals;
  9840. var tMat;
  9841. var tVec;
  9842. tMat = xf1.R;
  9843. tVec = normals1[edge1];
  9844. var normal1X = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  9845. var normal1Y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  9846. tMat = xf2.R;
  9847. var tX = tMat.col1.x * normal1X + tMat.col1.y * normal1Y;
  9848. normal1Y = tMat.col2.x * normal1X + tMat.col2.y * normal1Y;
  9849. normal1X = tX;
  9850. var index = 0;
  9851. var minDot = Number.MAX_VALUE;
  9852. for(var i = 0;i < count2;++i) {
  9853. tVec = normals2[i];
  9854. var dot = normal1X * tVec.x + normal1Y * tVec.y;
  9855. if(dot < minDot) {
  9856. minDot = dot;
  9857. index = i
  9858. }
  9859. }
  9860. var tClip;
  9861. var i1 = index;
  9862. var i2 = i1 + 1 < count2 ? i1 + 1 : 0;
  9863. tClip = c[0];
  9864. tVec = vertices2[i1];
  9865. tMat = xf2.R;
  9866. tClip.v.x = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  9867. tClip.v.y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  9868. tClip.id.features.referenceEdge = edge1;
  9869. tClip.id.features.incidentEdge = i1;
  9870. tClip.id.features.incidentVertex = 0;
  9871. tClip = c[1];
  9872. tVec = vertices2[i2];
  9873. tMat = xf2.R;
  9874. tClip.v.x = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  9875. tClip.v.y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  9876. tClip.id.features.referenceEdge = edge1;
  9877. tClip.id.features.incidentEdge = i2;
  9878. tClip.id.features.incidentVertex = 1
  9879. };
  9880. b2Collision.CollidePolygons = function(manifold, polyA, xfA, polyB, xfB) {
  9881. var cv;
  9882. manifold.m_pointCount = 0;
  9883. var totalRadius = polyA.m_radius + polyB.m_radius;
  9884. var edgeA = 0;
  9885. b2Collision.s_edgeAO[0] = edgeA;
  9886. var separationA = b2Collision.FindMaxSeparation(b2Collision.s_edgeAO, polyA, xfA, polyB, xfB);
  9887. edgeA = b2Collision.s_edgeAO[0];
  9888. if(separationA > totalRadius) {
  9889. return
  9890. }
  9891. var edgeB = 0;
  9892. b2Collision.s_edgeBO[0] = edgeB;
  9893. var separationB = b2Collision.FindMaxSeparation(b2Collision.s_edgeBO, polyB, xfB, polyA, xfA);
  9894. edgeB = b2Collision.s_edgeBO[0];
  9895. if(separationB > totalRadius) {
  9896. return
  9897. }
  9898. var poly1;
  9899. var poly2;
  9900. var xf1;
  9901. var xf2;
  9902. var edge1 = 0;
  9903. var flip = 0;
  9904. var k_relativeTol = 0.98;
  9905. var k_absoluteTol = 0.0010;
  9906. var tMat;
  9907. if(separationB > k_relativeTol * separationA + k_absoluteTol) {
  9908. poly1 = polyB;
  9909. poly2 = polyA;
  9910. xf1 = xfB;
  9911. xf2 = xfA;
  9912. edge1 = edgeB;
  9913. manifold.m_type = b2Manifold.e_faceB;
  9914. flip = 1
  9915. }else {
  9916. poly1 = polyA;
  9917. poly2 = polyB;
  9918. xf1 = xfA;
  9919. xf2 = xfB;
  9920. edge1 = edgeA;
  9921. manifold.m_type = b2Manifold.e_faceA;
  9922. flip = 0
  9923. }
  9924. var incidentEdge = b2Collision.s_incidentEdge;
  9925. b2Collision.FindIncidentEdge(incidentEdge, poly1, xf1, edge1, poly2, xf2);
  9926. var count1 = poly1.m_vertexCount;
  9927. var vertices1 = poly1.m_vertices;
  9928. var local_v11 = vertices1[edge1];
  9929. var local_v12;
  9930. if(edge1 + 1 < count1) {
  9931. local_v12 = vertices1[parseInt(edge1 + 1)]
  9932. }else {
  9933. local_v12 = vertices1[0]
  9934. }
  9935. var localTangent = b2Collision.s_localTangent;
  9936. localTangent.Set(local_v12.x - local_v11.x, local_v12.y - local_v11.y);
  9937. localTangent.Normalize();
  9938. var localNormal = b2Collision.s_localNormal;
  9939. localNormal.x = localTangent.y;
  9940. localNormal.y = -localTangent.x;
  9941. var planePoint = b2Collision.s_planePoint;
  9942. planePoint.Set(0.5 * (local_v11.x + local_v12.x), 0.5 * (local_v11.y + local_v12.y));
  9943. var tangent = b2Collision.s_tangent;
  9944. tMat = xf1.R;
  9945. tangent.x = tMat.col1.x * localTangent.x + tMat.col2.x * localTangent.y;
  9946. tangent.y = tMat.col1.y * localTangent.x + tMat.col2.y * localTangent.y;
  9947. var tangent2 = b2Collision.s_tangent2;
  9948. tangent2.x = -tangent.x;
  9949. tangent2.y = -tangent.y;
  9950. var normal = b2Collision.s_normal;
  9951. normal.x = tangent.y;
  9952. normal.y = -tangent.x;
  9953. var v11 = b2Collision.s_v11;
  9954. var v12 = b2Collision.s_v12;
  9955. v11.x = xf1.position.x + (tMat.col1.x * local_v11.x + tMat.col2.x * local_v11.y);
  9956. v11.y = xf1.position.y + (tMat.col1.y * local_v11.x + tMat.col2.y * local_v11.y);
  9957. v12.x = xf1.position.x + (tMat.col1.x * local_v12.x + tMat.col2.x * local_v12.y);
  9958. v12.y = xf1.position.y + (tMat.col1.y * local_v12.x + tMat.col2.y * local_v12.y);
  9959. var frontOffset = normal.x * v11.x + normal.y * v11.y;
  9960. var sideOffset1 = -tangent.x * v11.x - tangent.y * v11.y + totalRadius;
  9961. var sideOffset2 = tangent.x * v12.x + tangent.y * v12.y + totalRadius;
  9962. var clipPoints1 = b2Collision.s_clipPoints1;
  9963. var clipPoints2 = b2Collision.s_clipPoints2;
  9964. var np = 0;
  9965. np = b2Collision.ClipSegmentToLine(clipPoints1, incidentEdge, tangent2, sideOffset1);
  9966. if(np < 2) {
  9967. return
  9968. }
  9969. np = b2Collision.ClipSegmentToLine(clipPoints2, clipPoints1, tangent, sideOffset2);
  9970. if(np < 2) {
  9971. return
  9972. }
  9973. manifold.m_localPlaneNormal.SetV(localNormal);
  9974. manifold.m_localPoint.SetV(planePoint);
  9975. var pointCount = 0;
  9976. for(var i = 0;i < b2Settings.b2_maxManifoldPoints;++i) {
  9977. cv = clipPoints2[i];
  9978. var separation = normal.x * cv.v.x + normal.y * cv.v.y - frontOffset;
  9979. if(separation <= totalRadius) {
  9980. var cp = manifold.m_points[pointCount];
  9981. tMat = xf2.R;
  9982. var tX = cv.v.x - xf2.position.x;
  9983. var tY = cv.v.y - xf2.position.y;
  9984. cp.m_localPoint.x = tX * tMat.col1.x + tY * tMat.col1.y;
  9985. cp.m_localPoint.y = tX * tMat.col2.x + tY * tMat.col2.y;
  9986. cp.m_id.Set(cv.id);
  9987. cp.m_id.features.flip = flip;
  9988. ++pointCount
  9989. }
  9990. }
  9991. manifold.m_pointCount = pointCount
  9992. };
  9993. b2Collision.CollideCircles = function(manifold, circle1, xf1, circle2, xf2) {
  9994. manifold.m_pointCount = 0;
  9995. var tMat;
  9996. var tVec;
  9997. tMat = xf1.R;
  9998. tVec = circle1.m_p;
  9999. var p1X = xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  10000. var p1Y = xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  10001. tMat = xf2.R;
  10002. tVec = circle2.m_p;
  10003. var p2X = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  10004. var p2Y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  10005. var dX = p2X - p1X;
  10006. var dY = p2Y - p1Y;
  10007. var distSqr = dX * dX + dY * dY;
  10008. var radius = circle1.m_radius + circle2.m_radius;
  10009. if(distSqr > radius * radius) {
  10010. return
  10011. }
  10012. manifold.m_type = b2Manifold.e_circles;
  10013. manifold.m_localPoint.SetV(circle1.m_p);
  10014. manifold.m_localPlaneNormal.SetZero();
  10015. manifold.m_pointCount = 1;
  10016. manifold.m_points[0].m_localPoint.SetV(circle2.m_p);
  10017. manifold.m_points[0].m_id.key = 0
  10018. };
  10019. b2Collision.CollidePolygonAndCircle = function(manifold, polygon, xf1, circle, xf2) {
  10020. manifold.m_pointCount = 0;
  10021. var tPoint;
  10022. var dX;
  10023. var dY;
  10024. var positionX;
  10025. var positionY;
  10026. var tVec;
  10027. var tMat;
  10028. tMat = xf2.R;
  10029. tVec = circle.m_p;
  10030. var cX = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  10031. var cY = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  10032. dX = cX - xf1.position.x;
  10033. dY = cY - xf1.position.y;
  10034. tMat = xf1.R;
  10035. var cLocalX = dX * tMat.col1.x + dY * tMat.col1.y;
  10036. var cLocalY = dX * tMat.col2.x + dY * tMat.col2.y;
  10037. var dist;
  10038. var normalIndex = 0;
  10039. var separation = -Number.MAX_VALUE;
  10040. var radius = polygon.m_radius + circle.m_radius;
  10041. var vertexCount = polygon.m_vertexCount;
  10042. var vertices = polygon.m_vertices;
  10043. var normals = polygon.m_normals;
  10044. for(var i = 0;i < vertexCount;++i) {
  10045. tVec = vertices[i];
  10046. dX = cLocalX - tVec.x;
  10047. dY = cLocalY - tVec.y;
  10048. tVec = normals[i];
  10049. var s = tVec.x * dX + tVec.y * dY;
  10050. if(s > radius) {
  10051. return
  10052. }
  10053. if(s > separation) {
  10054. separation = s;
  10055. normalIndex = i
  10056. }
  10057. }
  10058. var vertIndex1 = normalIndex;
  10059. var vertIndex2 = vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0;
  10060. var v1 = vertices[vertIndex1];
  10061. var v2 = vertices[vertIndex2];
  10062. if(separation < Number.MIN_VALUE) {
  10063. manifold.m_pointCount = 1;
  10064. manifold.m_type = b2Manifold.e_faceA;
  10065. manifold.m_localPlaneNormal.SetV(normals[normalIndex]);
  10066. manifold.m_localPoint.x = 0.5 * (v1.x + v2.x);
  10067. manifold.m_localPoint.y = 0.5 * (v1.y + v2.y);
  10068. manifold.m_points[0].m_localPoint.SetV(circle.m_p);
  10069. manifold.m_points[0].m_id.key = 0;
  10070. return
  10071. }
  10072. var u1 = (cLocalX - v1.x) * (v2.x - v1.x) + (cLocalY - v1.y) * (v2.y - v1.y);
  10073. var u2 = (cLocalX - v2.x) * (v1.x - v2.x) + (cLocalY - v2.y) * (v1.y - v2.y);
  10074. if(u1 <= 0) {
  10075. if((cLocalX - v1.x) * (cLocalX - v1.x) + (cLocalY - v1.y) * (cLocalY - v1.y) > radius * radius) {
  10076. return
  10077. }
  10078. manifold.m_pointCount = 1;
  10079. manifold.m_type = b2Manifold.e_faceA;
  10080. manifold.m_localPlaneNormal.x = cLocalX - v1.x;
  10081. manifold.m_localPlaneNormal.y = cLocalY - v1.y;
  10082. manifold.m_localPlaneNormal.Normalize();
  10083. manifold.m_localPoint.SetV(v1);
  10084. manifold.m_points[0].m_localPoint.SetV(circle.m_p);
  10085. manifold.m_points[0].m_id.key = 0
  10086. }else {
  10087. if(u2 <= 0) {
  10088. if((cLocalX - v2.x) * (cLocalX - v2.x) + (cLocalY - v2.y) * (cLocalY - v2.y) > radius * radius) {
  10089. return
  10090. }
  10091. manifold.m_pointCount = 1;
  10092. manifold.m_type = b2Manifold.e_faceA;
  10093. manifold.m_localPlaneNormal.x = cLocalX - v2.x;
  10094. manifold.m_localPlaneNormal.y = cLocalY - v2.y;
  10095. manifold.m_localPlaneNormal.Normalize();
  10096. manifold.m_localPoint.SetV(v2);
  10097. manifold.m_points[0].m_localPoint.SetV(circle.m_p);
  10098. manifold.m_points[0].m_id.key = 0
  10099. }else {
  10100. var faceCenterX = 0.5 * (v1.x + v2.x);
  10101. var faceCenterY = 0.5 * (v1.y + v2.y);
  10102. separation = (cLocalX - faceCenterX) * normals[vertIndex1].x + (cLocalY - faceCenterY) * normals[vertIndex1].y;
  10103. if(separation > radius) {
  10104. return
  10105. }
  10106. manifold.m_pointCount = 1;
  10107. manifold.m_type = b2Manifold.e_faceA;
  10108. manifold.m_localPlaneNormal.x = normals[vertIndex1].x;
  10109. manifold.m_localPlaneNormal.y = normals[vertIndex1].y;
  10110. manifold.m_localPlaneNormal.Normalize();
  10111. manifold.m_localPoint.Set(faceCenterX, faceCenterY);
  10112. manifold.m_points[0].m_localPoint.SetV(circle.m_p);
  10113. manifold.m_points[0].m_id.key = 0
  10114. }
  10115. }
  10116. };
  10117. b2Collision.TestOverlap = function(a, b) {
  10118. var t1 = b.lowerBound;
  10119. var t2 = a.upperBound;
  10120. var d1X = t1.x - t2.x;
  10121. var d1Y = t1.y - t2.y;
  10122. t1 = a.lowerBound;
  10123. t2 = b.upperBound;
  10124. var d2X = t1.x - t2.x;
  10125. var d2Y = t1.y - t2.y;
  10126. if(d1X > 0 || d1Y > 0) {
  10127. return false
  10128. }
  10129. if(d2X > 0 || d2Y > 0) {
  10130. return false
  10131. }
  10132. return true
  10133. };
  10134. b2Collision.b2_nullFeature = 255;
  10135. b2Collision.s_incidentEdge = b2Collision.MakeClipPointVector();
  10136. b2Collision.s_clipPoints1 = b2Collision.MakeClipPointVector();
  10137. b2Collision.s_clipPoints2 = b2Collision.MakeClipPointVector();
  10138. b2Collision.s_edgeAO = new Array(1);
  10139. b2Collision.s_edgeBO = new Array(1);
  10140. b2Collision.s_localTangent = new b2Vec2;
  10141. b2Collision.s_localNormal = new b2Vec2;
  10142. b2Collision.s_planePoint = new b2Vec2;
  10143. b2Collision.s_normal = new b2Vec2;
  10144. b2Collision.s_tangent = new b2Vec2;
  10145. b2Collision.s_tangent2 = new b2Vec2;
  10146. b2Collision.s_v11 = new b2Vec2;
  10147. b2Collision.s_v12 = new b2Vec2;
  10148. b2Collision.b2CollidePolyTempVec = new b2Vec2;var b2PolyAndCircleContact = function() {
  10149. b2Contact.prototype.__varz.call(this);
  10150. this.__varz();
  10151. this.__constructor.apply(this, arguments)
  10152. };
  10153. extend(b2PolyAndCircleContact.prototype, b2Contact.prototype);
  10154. b2PolyAndCircleContact.prototype._super = b2Contact.prototype;
  10155. b2PolyAndCircleContact.prototype.__constructor = function() {
  10156. this._super.__constructor.apply(this, arguments)
  10157. };
  10158. b2PolyAndCircleContact.prototype.__varz = function() {
  10159. };
  10160. b2PolyAndCircleContact.Create = function(allocator) {
  10161. return new b2PolyAndCircleContact
  10162. };
  10163. b2PolyAndCircleContact.Destroy = function(contact, allocator) {
  10164. };
  10165. b2PolyAndCircleContact.prototype.Evaluate = function() {
  10166. var bA = this.m_fixtureA.m_body;
  10167. var bB = this.m_fixtureB.m_body;
  10168. b2Collision.CollidePolygonAndCircle(this.m_manifold, this.m_fixtureA.GetShape(), bA.m_xf, this.m_fixtureB.GetShape(), bB.m_xf)
  10169. };
  10170. b2PolyAndCircleContact.prototype.Reset = function(fixtureA, fixtureB) {
  10171. this._super.Reset.apply(this, [fixtureA, fixtureB]);
  10172. b2Settings.b2Assert(fixtureA.GetType() == b2Shape.e_polygonShape);
  10173. b2Settings.b2Assert(fixtureB.GetType() == b2Shape.e_circleShape)
  10174. };var b2ContactPoint = function() {
  10175. this.__varz();
  10176. this.__constructor.apply(this, arguments)
  10177. };
  10178. b2ContactPoint.prototype.__constructor = function() {
  10179. };
  10180. b2ContactPoint.prototype.__varz = function() {
  10181. this.position = new b2Vec2;
  10182. this.velocity = new b2Vec2;
  10183. this.normal = new b2Vec2;
  10184. this.id = new b2ContactID
  10185. };
  10186. b2ContactPoint.prototype.shape1 = null;
  10187. b2ContactPoint.prototype.shape2 = null;
  10188. b2ContactPoint.prototype.position = new b2Vec2;
  10189. b2ContactPoint.prototype.velocity = new b2Vec2;
  10190. b2ContactPoint.prototype.normal = new b2Vec2;
  10191. b2ContactPoint.prototype.separation = null;
  10192. b2ContactPoint.prototype.friction = null;
  10193. b2ContactPoint.prototype.restitution = null;
  10194. b2ContactPoint.prototype.id = new b2ContactID;var b2CircleContact = function() {
  10195. b2Contact.prototype.__varz.call(this);
  10196. this.__varz();
  10197. this.__constructor.apply(this, arguments)
  10198. };
  10199. extend(b2CircleContact.prototype, b2Contact.prototype);
  10200. b2CircleContact.prototype._super = b2Contact.prototype;
  10201. b2CircleContact.prototype.__constructor = function() {
  10202. this._super.__constructor.apply(this, arguments)
  10203. };
  10204. b2CircleContact.prototype.__varz = function() {
  10205. };
  10206. b2CircleContact.Create = function(allocator) {
  10207. return new b2CircleContact
  10208. };
  10209. b2CircleContact.Destroy = function(contact, allocator) {
  10210. };
  10211. b2CircleContact.prototype.Evaluate = function() {
  10212. var bA = this.m_fixtureA.GetBody();
  10213. var bB = this.m_fixtureB.GetBody();
  10214. b2Collision.CollideCircles(this.m_manifold, this.m_fixtureA.GetShape(), bA.m_xf, this.m_fixtureB.GetShape(), bB.m_xf)
  10215. };
  10216. b2CircleContact.prototype.Reset = function(fixtureA, fixtureB) {
  10217. this._super.Reset.apply(this, [fixtureA, fixtureB])
  10218. };var b2EdgeAndCircleContact = function() {
  10219. b2Contact.prototype.__varz.call(this);
  10220. this.__varz();
  10221. this.__constructor.apply(this, arguments)
  10222. };
  10223. extend(b2EdgeAndCircleContact.prototype, b2Contact.prototype);
  10224. b2EdgeAndCircleContact.prototype._super = b2Contact.prototype;
  10225. b2EdgeAndCircleContact.prototype.__constructor = function() {
  10226. this._super.__constructor.apply(this, arguments)
  10227. };
  10228. b2EdgeAndCircleContact.prototype.__varz = function() {
  10229. };
  10230. b2EdgeAndCircleContact.Create = function(allocator) {
  10231. return new b2EdgeAndCircleContact
  10232. };
  10233. b2EdgeAndCircleContact.Destroy = function(contact, allocator) {
  10234. };
  10235. b2EdgeAndCircleContact.prototype.Evaluate = function() {
  10236. var bA = this.m_fixtureA.GetBody();
  10237. var bB = this.m_fixtureB.GetBody();
  10238. this.b2CollideEdgeAndCircle(this.m_manifold, this.m_fixtureA.GetShape(), bA.m_xf, this.m_fixtureB.GetShape(), bB.m_xf)
  10239. };
  10240. b2EdgeAndCircleContact.prototype.b2CollideEdgeAndCircle = function(manifold, edge, xf1, circle, xf2) {
  10241. };
  10242. b2EdgeAndCircleContact.prototype.Reset = function(fixtureA, fixtureB) {
  10243. this._super.Reset.apply(this, [fixtureA, fixtureB])
  10244. };var b2ContactManager = function() {
  10245. this.__varz();
  10246. this.__constructor.apply(this, arguments)
  10247. };
  10248. b2ContactManager.prototype.__constructor = function() {
  10249. this.m_world = null;
  10250. this.m_contactCount = 0;
  10251. this.m_contactFilter = b2ContactFilter.b2_defaultFilter;
  10252. this.m_contactListener = b2ContactListener.b2_defaultListener;
  10253. this.m_contactFactory = new b2ContactFactory(this.m_allocator);
  10254. this.m_broadPhase = new b2DynamicTreeBroadPhase
  10255. };
  10256. b2ContactManager.prototype.__varz = function() {
  10257. };
  10258. b2ContactManager.s_evalCP = new b2ContactPoint;
  10259. b2ContactManager.prototype.AddPair = function(proxyUserDataA, proxyUserDataB) {
  10260. var fixtureA = proxyUserDataA;
  10261. var fixtureB = proxyUserDataB;
  10262. var bodyA = fixtureA.GetBody();
  10263. var bodyB = fixtureB.GetBody();
  10264. if(bodyA == bodyB) {
  10265. return
  10266. }
  10267. var edge = bodyB.GetContactList();
  10268. while(edge) {
  10269. if(edge.other == bodyA) {
  10270. var fA = edge.contact.GetFixtureA();
  10271. var fB = edge.contact.GetFixtureB();
  10272. if(fA == fixtureA && fB == fixtureB) {
  10273. return
  10274. }
  10275. if(fA == fixtureB && fB == fixtureA) {
  10276. return
  10277. }
  10278. }
  10279. edge = edge.next
  10280. }
  10281. if(bodyB.ShouldCollide(bodyA) == false) {
  10282. return
  10283. }
  10284. if(this.m_contactFilter.ShouldCollide(fixtureA, fixtureB) == false) {
  10285. return
  10286. }
  10287. var c = this.m_contactFactory.Create(fixtureA, fixtureB);
  10288. fixtureA = c.GetFixtureA();
  10289. fixtureB = c.GetFixtureB();
  10290. bodyA = fixtureA.m_body;
  10291. bodyB = fixtureB.m_body;
  10292. c.m_prev = null;
  10293. c.m_next = this.m_world.m_contactList;
  10294. if(this.m_world.m_contactList != null) {
  10295. this.m_world.m_contactList.m_prev = c
  10296. }
  10297. this.m_world.m_contactList = c;
  10298. c.m_nodeA.contact = c;
  10299. c.m_nodeA.other = bodyB;
  10300. c.m_nodeA.prev = null;
  10301. c.m_nodeA.next = bodyA.m_contactList;
  10302. if(bodyA.m_contactList != null) {
  10303. bodyA.m_contactList.prev = c.m_nodeA
  10304. }
  10305. bodyA.m_contactList = c.m_nodeA;
  10306. c.m_nodeB.contact = c;
  10307. c.m_nodeB.other = bodyA;
  10308. c.m_nodeB.prev = null;
  10309. c.m_nodeB.next = bodyB.m_contactList;
  10310. if(bodyB.m_contactList != null) {
  10311. bodyB.m_contactList.prev = c.m_nodeB
  10312. }
  10313. bodyB.m_contactList = c.m_nodeB;
  10314. ++this.m_world.m_contactCount;
  10315. return
  10316. };
  10317. b2ContactManager.prototype.FindNewContacts = function() {
  10318. var that = this;
  10319. this.m_broadPhase.UpdatePairs(function(a, b) {
  10320. return that.AddPair(a, b)
  10321. })
  10322. };
  10323. b2ContactManager.prototype.Destroy = function(c) {
  10324. var fixtureA = c.GetFixtureA();
  10325. var fixtureB = c.GetFixtureB();
  10326. var bodyA = fixtureA.GetBody();
  10327. var bodyB = fixtureB.GetBody();
  10328. if(c.IsTouching()) {
  10329. this.m_contactListener.EndContact(c)
  10330. }
  10331. if(c.m_prev) {
  10332. c.m_prev.m_next = c.m_next
  10333. }
  10334. if(c.m_next) {
  10335. c.m_next.m_prev = c.m_prev
  10336. }
  10337. if(c == this.m_world.m_contactList) {
  10338. this.m_world.m_contactList = c.m_next
  10339. }
  10340. if(c.m_nodeA.prev) {
  10341. c.m_nodeA.prev.next = c.m_nodeA.next
  10342. }
  10343. if(c.m_nodeA.next) {
  10344. c.m_nodeA.next.prev = c.m_nodeA.prev
  10345. }
  10346. if(c.m_nodeA == bodyA.m_contactList) {
  10347. bodyA.m_contactList = c.m_nodeA.next
  10348. }
  10349. if(c.m_nodeB.prev) {
  10350. c.m_nodeB.prev.next = c.m_nodeB.next
  10351. }
  10352. if(c.m_nodeB.next) {
  10353. c.m_nodeB.next.prev = c.m_nodeB.prev
  10354. }
  10355. if(c.m_nodeB == bodyB.m_contactList) {
  10356. bodyB.m_contactList = c.m_nodeB.next
  10357. }
  10358. this.m_contactFactory.Destroy(c);
  10359. --this.m_contactCount
  10360. };
  10361. b2ContactManager.prototype.Collide = function() {
  10362. var c = this.m_world.m_contactList;
  10363. while(c) {
  10364. var fixtureA = c.GetFixtureA();
  10365. var fixtureB = c.GetFixtureB();
  10366. var bodyA = fixtureA.GetBody();
  10367. var bodyB = fixtureB.GetBody();
  10368. if(bodyA.IsAwake() == false && bodyB.IsAwake() == false) {
  10369. c = c.GetNext();
  10370. continue
  10371. }
  10372. if(c.m_flags & b2Contact.e_filterFlag) {
  10373. if(bodyB.ShouldCollide(bodyA) == false) {
  10374. var cNuke = c;
  10375. c = cNuke.GetNext();
  10376. this.Destroy(cNuke);
  10377. continue
  10378. }
  10379. if(this.m_contactFilter.ShouldCollide(fixtureA, fixtureB) == false) {
  10380. cNuke = c;
  10381. c = cNuke.GetNext();
  10382. this.Destroy(cNuke);
  10383. continue
  10384. }
  10385. c.m_flags &= ~b2Contact.e_filterFlag
  10386. }
  10387. var proxyA = fixtureA.m_proxy;
  10388. var proxyB = fixtureB.m_proxy;
  10389. var overlap = this.m_broadPhase.TestOverlap(proxyA, proxyB);
  10390. if(overlap == false) {
  10391. cNuke = c;
  10392. c = cNuke.GetNext();
  10393. this.Destroy(cNuke);
  10394. continue
  10395. }
  10396. c.Update(this.m_contactListener);
  10397. c = c.GetNext()
  10398. }
  10399. };
  10400. b2ContactManager.prototype.m_world = null;
  10401. b2ContactManager.prototype.m_broadPhase = null;
  10402. b2ContactManager.prototype.m_contactList = null;
  10403. b2ContactManager.prototype.m_contactCount = 0;
  10404. b2ContactManager.prototype.m_contactFilter = null;
  10405. b2ContactManager.prototype.m_contactListener = null;
  10406. b2ContactManager.prototype.m_contactFactory = null;
  10407. b2ContactManager.prototype.m_allocator = null;var b2World = function() {
  10408. this.__varz();
  10409. this.__constructor.apply(this, arguments)
  10410. };
  10411. b2World.prototype.__constructor = function(gravity, doSleep) {
  10412. this.m_destructionListener = null;
  10413. this.m_debugDraw = null;
  10414. this.m_bodyList = null;
  10415. this.m_contactList = null;
  10416. this.m_jointList = null;
  10417. this.m_controllerList = null;
  10418. this.m_bodyCount = 0;
  10419. this.m_contactCount = 0;
  10420. this.m_jointCount = 0;
  10421. this.m_controllerCount = 0;
  10422. b2World.m_warmStarting = true;
  10423. b2World.m_continuousPhysics = true;
  10424. this.m_allowSleep = doSleep;
  10425. this.m_gravity = gravity;
  10426. this.m_inv_dt0 = 0;
  10427. this.m_contactManager.m_world = this;
  10428. var bd = new b2BodyDef;
  10429. this.m_groundBody = this.CreateBody(bd)
  10430. };
  10431. b2World.prototype.__varz = function() {
  10432. this.s_stack = new Array;
  10433. this.m_contactManager = new b2ContactManager;
  10434. this.m_contactSolver = new b2ContactSolver;
  10435. this.m_island = new b2Island
  10436. };
  10437. b2World.s_timestep2 = new b2TimeStep;
  10438. b2World.s_backupA = new b2Sweep;
  10439. b2World.s_backupB = new b2Sweep;
  10440. b2World.s_timestep = new b2TimeStep;
  10441. b2World.s_queue = new Array;
  10442. b2World.e_newFixture = 1;
  10443. b2World.e_locked = 2;
  10444. b2World.s_xf = new b2Transform;
  10445. b2World.s_jointColor = new b2Color(0.5, 0.8, 0.8);
  10446. b2World.m_warmStarting = null;
  10447. b2World.m_continuousPhysics = null;
  10448. b2World.prototype.Solve = function(step) {
  10449. var b;
  10450. for(var controller = this.m_controllerList;controller;controller = controller.m_next) {
  10451. controller.Step(step)
  10452. }
  10453. var island = this.m_island;
  10454. island.Initialize(this.m_bodyCount, this.m_contactCount, this.m_jointCount, null, this.m_contactManager.m_contactListener, this.m_contactSolver);
  10455. for(b = this.m_bodyList;b;b = b.m_next) {
  10456. b.m_flags &= ~b2Body.e_islandFlag
  10457. }
  10458. for(var c = this.m_contactList;c;c = c.m_next) {
  10459. c.m_flags &= ~b2Contact.e_islandFlag
  10460. }
  10461. for(var j = this.m_jointList;j;j = j.m_next) {
  10462. j.m_islandFlag = false
  10463. }
  10464. var stackSize = this.m_bodyCount;
  10465. var stack = this.s_stack;
  10466. for(var seed = this.m_bodyList;seed;seed = seed.m_next) {
  10467. if(seed.m_flags & b2Body.e_islandFlag) {
  10468. continue
  10469. }
  10470. if(seed.IsAwake() == false || seed.IsActive() == false) {
  10471. continue
  10472. }
  10473. if(seed.GetType() == b2Body.b2_staticBody) {
  10474. continue
  10475. }
  10476. island.Clear();
  10477. var stackCount = 0;
  10478. stack[stackCount++] = seed;
  10479. seed.m_flags |= b2Body.e_islandFlag;
  10480. while(stackCount > 0) {
  10481. b = stack[--stackCount];
  10482. island.AddBody(b);
  10483. if(b.IsAwake() == false) {
  10484. b.SetAwake(true)
  10485. }
  10486. if(b.GetType() == b2Body.b2_staticBody) {
  10487. continue
  10488. }
  10489. var other;
  10490. for(var ce = b.m_contactList;ce;ce = ce.next) {
  10491. if(ce.contact.m_flags & b2Contact.e_islandFlag) {
  10492. continue
  10493. }
  10494. if(ce.contact.IsSensor() == true || ce.contact.IsEnabled() == false || ce.contact.IsTouching() == false) {
  10495. continue
  10496. }
  10497. island.AddContact(ce.contact);
  10498. ce.contact.m_flags |= b2Contact.e_islandFlag;
  10499. other = ce.other;
  10500. if(other.m_flags & b2Body.e_islandFlag) {
  10501. continue
  10502. }
  10503. stack[stackCount++] = other;
  10504. other.m_flags |= b2Body.e_islandFlag
  10505. }
  10506. for(var jn = b.m_jointList;jn;jn = jn.next) {
  10507. if(jn.joint.m_islandFlag == true) {
  10508. continue
  10509. }
  10510. other = jn.other;
  10511. if(other.IsActive() == false) {
  10512. continue
  10513. }
  10514. island.AddJoint(jn.joint);
  10515. jn.joint.m_islandFlag = true;
  10516. if(other.m_flags & b2Body.e_islandFlag) {
  10517. continue
  10518. }
  10519. stack[stackCount++] = other;
  10520. other.m_flags |= b2Body.e_islandFlag
  10521. }
  10522. }
  10523. island.Solve(step, this.m_gravity, this.m_allowSleep);
  10524. for(var i = 0;i < island.m_bodyCount;++i) {
  10525. b = island.m_bodies[i];
  10526. if(b.GetType() == b2Body.b2_staticBody) {
  10527. b.m_flags &= ~b2Body.e_islandFlag
  10528. }
  10529. }
  10530. }
  10531. for(i = 0;i < stack.length;++i) {
  10532. if(!stack[i]) {
  10533. break
  10534. }
  10535. stack[i] = null
  10536. }
  10537. for(b = this.m_bodyList;b;b = b.m_next) {
  10538. if(b.IsAwake() == false || b.IsActive() == false) {
  10539. continue
  10540. }
  10541. if(b.GetType() == b2Body.b2_staticBody) {
  10542. continue
  10543. }
  10544. b.SynchronizeFixtures()
  10545. }
  10546. this.m_contactManager.FindNewContacts()
  10547. };
  10548. b2World.prototype.SolveTOI = function(step) {
  10549. var b;
  10550. var fA;
  10551. var fB;
  10552. var bA;
  10553. var bB;
  10554. var cEdge;
  10555. var j;
  10556. var island = this.m_island;
  10557. island.Initialize(this.m_bodyCount, b2Settings.b2_maxTOIContactsPerIsland, b2Settings.b2_maxTOIJointsPerIsland, null, this.m_contactManager.m_contactListener, this.m_contactSolver);
  10558. var queue = b2World.s_queue;
  10559. for(b = this.m_bodyList;b;b = b.m_next) {
  10560. b.m_flags &= ~b2Body.e_islandFlag;
  10561. b.m_sweep.t0 = 0
  10562. }
  10563. var c;
  10564. for(c = this.m_contactList;c;c = c.m_next) {
  10565. c.m_flags &= ~(b2Contact.e_toiFlag | b2Contact.e_islandFlag)
  10566. }
  10567. for(j = this.m_jointList;j;j = j.m_next) {
  10568. j.m_islandFlag = false
  10569. }
  10570. for(;;) {
  10571. var minContact = null;
  10572. var minTOI = 1;
  10573. for(c = this.m_contactList;c;c = c.m_next) {
  10574. if(c.IsSensor() == true || c.IsEnabled() == false || c.IsContinuous() == false) {
  10575. continue
  10576. }
  10577. var toi = 1;
  10578. if(c.m_flags & b2Contact.e_toiFlag) {
  10579. toi = c.m_toi
  10580. }else {
  10581. fA = c.m_fixtureA;
  10582. fB = c.m_fixtureB;
  10583. bA = fA.m_body;
  10584. bB = fB.m_body;
  10585. if((bA.GetType() != b2Body.b2_dynamicBody || bA.IsAwake() == false) && (bB.GetType() != b2Body.b2_dynamicBody || bB.IsAwake() == false)) {
  10586. continue
  10587. }
  10588. var t0 = bA.m_sweep.t0;
  10589. if(bA.m_sweep.t0 < bB.m_sweep.t0) {
  10590. t0 = bB.m_sweep.t0;
  10591. bA.m_sweep.Advance(t0)
  10592. }else {
  10593. if(bB.m_sweep.t0 < bA.m_sweep.t0) {
  10594. t0 = bA.m_sweep.t0;
  10595. bB.m_sweep.Advance(t0)
  10596. }
  10597. }
  10598. toi = c.ComputeTOI(bA.m_sweep, bB.m_sweep);
  10599. b2Settings.b2Assert(0 <= toi && toi <= 1);
  10600. if(toi > 0 && toi < 1) {
  10601. toi = (1 - toi) * t0 + toi;
  10602. if(toi > 1) {
  10603. toi = 1
  10604. }
  10605. }
  10606. c.m_toi = toi;
  10607. c.m_flags |= b2Contact.e_toiFlag
  10608. }
  10609. if(Number.MIN_VALUE < toi && toi < minTOI) {
  10610. minContact = c;
  10611. minTOI = toi
  10612. }
  10613. }
  10614. if(minContact == null || 1 - 100 * Number.MIN_VALUE < minTOI) {
  10615. break
  10616. }
  10617. fA = minContact.m_fixtureA;
  10618. fB = minContact.m_fixtureB;
  10619. bA = fA.m_body;
  10620. bB = fB.m_body;
  10621. b2World.s_backupA.Set(bA.m_sweep);
  10622. b2World.s_backupB.Set(bB.m_sweep);
  10623. bA.Advance(minTOI);
  10624. bB.Advance(minTOI);
  10625. minContact.Update(this.m_contactManager.m_contactListener);
  10626. minContact.m_flags &= ~b2Contact.e_toiFlag;
  10627. if(minContact.IsSensor() == true || minContact.IsEnabled() == false) {
  10628. bA.m_sweep.Set(b2World.s_backupA);
  10629. bB.m_sweep.Set(b2World.s_backupB);
  10630. bA.SynchronizeTransform();
  10631. bB.SynchronizeTransform();
  10632. continue
  10633. }
  10634. if(minContact.IsTouching() == false) {
  10635. continue
  10636. }
  10637. var seed = bA;
  10638. if(seed.GetType() != b2Body.b2_dynamicBody) {
  10639. seed = bB
  10640. }
  10641. island.Clear();
  10642. var queueStart = 0;
  10643. var queueSize = 0;
  10644. queue[queueStart + queueSize++] = seed;
  10645. seed.m_flags |= b2Body.e_islandFlag;
  10646. while(queueSize > 0) {
  10647. b = queue[queueStart++];
  10648. --queueSize;
  10649. island.AddBody(b);
  10650. if(b.IsAwake() == false) {
  10651. b.SetAwake(true)
  10652. }
  10653. if(b.GetType() != b2Body.b2_dynamicBody) {
  10654. continue
  10655. }
  10656. for(cEdge = b.m_contactList;cEdge;cEdge = cEdge.next) {
  10657. if(island.m_contactCount == island.m_contactCapacity) {
  10658. break
  10659. }
  10660. if(cEdge.contact.m_flags & b2Contact.e_islandFlag) {
  10661. continue
  10662. }
  10663. if(cEdge.contact.IsSensor() == true || cEdge.contact.IsEnabled() == false || cEdge.contact.IsTouching() == false) {
  10664. continue
  10665. }
  10666. island.AddContact(cEdge.contact);
  10667. cEdge.contact.m_flags |= b2Contact.e_islandFlag;
  10668. var other = cEdge.other;
  10669. if(other.m_flags & b2Body.e_islandFlag) {
  10670. continue
  10671. }
  10672. if(other.GetType() != b2Body.b2_staticBody) {
  10673. other.Advance(minTOI);
  10674. other.SetAwake(true)
  10675. }
  10676. queue[queueStart + queueSize] = other;
  10677. ++queueSize;
  10678. other.m_flags |= b2Body.e_islandFlag
  10679. }
  10680. for(var jEdge = b.m_jointList;jEdge;jEdge = jEdge.next) {
  10681. if(island.m_jointCount == island.m_jointCapacity) {
  10682. continue
  10683. }
  10684. if(jEdge.joint.m_islandFlag == true) {
  10685. continue
  10686. }
  10687. other = jEdge.other;
  10688. if(other.IsActive() == false) {
  10689. continue
  10690. }
  10691. island.AddJoint(jEdge.joint);
  10692. jEdge.joint.m_islandFlag = true;
  10693. if(other.m_flags & b2Body.e_islandFlag) {
  10694. continue
  10695. }
  10696. if(other.GetType() != b2Body.b2_staticBody) {
  10697. other.Advance(minTOI);
  10698. other.SetAwake(true)
  10699. }
  10700. queue[queueStart + queueSize] = other;
  10701. ++queueSize;
  10702. other.m_flags |= b2Body.e_islandFlag
  10703. }
  10704. }
  10705. var subStep = b2World.s_timestep;
  10706. subStep.warmStarting = false;
  10707. subStep.dt = (1 - minTOI) * step.dt;
  10708. subStep.inv_dt = 1 / subStep.dt;
  10709. subStep.dtRatio = 0;
  10710. subStep.velocityIterations = step.velocityIterations;
  10711. subStep.positionIterations = step.positionIterations;
  10712. island.SolveTOI(subStep);
  10713. var i = 0;
  10714. for(i = 0;i < island.m_bodyCount;++i) {
  10715. b = island.m_bodies[i];
  10716. b.m_flags &= ~b2Body.e_islandFlag;
  10717. if(b.IsAwake() == false) {
  10718. continue
  10719. }
  10720. if(b.GetType() != b2Body.b2_dynamicBody) {
  10721. continue
  10722. }
  10723. b.SynchronizeFixtures();
  10724. for(cEdge = b.m_contactList;cEdge;cEdge = cEdge.next) {
  10725. cEdge.contact.m_flags &= ~b2Contact.e_toiFlag
  10726. }
  10727. }
  10728. for(i = 0;i < island.m_contactCount;++i) {
  10729. c = island.m_contacts[i];
  10730. c.m_flags &= ~(b2Contact.e_toiFlag | b2Contact.e_islandFlag)
  10731. }
  10732. for(i = 0;i < island.m_jointCount;++i) {
  10733. j = island.m_joints[i];
  10734. j.m_islandFlag = false
  10735. }
  10736. this.m_contactManager.FindNewContacts()
  10737. }
  10738. };
  10739. b2World.prototype.DrawJoint = function(joint) {
  10740. var b1 = joint.GetBodyA();
  10741. var b2 = joint.GetBodyB();
  10742. var xf1 = b1.m_xf;
  10743. var xf2 = b2.m_xf;
  10744. var x1 = xf1.position;
  10745. var x2 = xf2.position;
  10746. var p1 = joint.GetAnchorA();
  10747. var p2 = joint.GetAnchorB();
  10748. var color = b2World.s_jointColor;
  10749. switch(joint.m_type) {
  10750. case b2Joint.e_distanceJoint:
  10751. this.m_debugDraw.DrawSegment(p1, p2, color);
  10752. break;
  10753. case b2Joint.e_pulleyJoint:
  10754. var pulley = joint;
  10755. var s1 = pulley.GetGroundAnchorA();
  10756. var s2 = pulley.GetGroundAnchorB();
  10757. this.m_debugDraw.DrawSegment(s1, p1, color);
  10758. this.m_debugDraw.DrawSegment(s2, p2, color);
  10759. this.m_debugDraw.DrawSegment(s1, s2, color);
  10760. break;
  10761. case b2Joint.e_mouseJoint:
  10762. this.m_debugDraw.DrawSegment(p1, p2, color);
  10763. break;
  10764. default:
  10765. if(b1 != this.m_groundBody) {
  10766. this.m_debugDraw.DrawSegment(x1, p1, color)
  10767. }
  10768. this.m_debugDraw.DrawSegment(p1, p2, color);
  10769. if(b2 != this.m_groundBody) {
  10770. this.m_debugDraw.DrawSegment(x2, p2, color)
  10771. }
  10772. }
  10773. };
  10774. b2World.prototype.DrawShape = function(shape, xf, color) {
  10775. switch(shape.m_type) {
  10776. case b2Shape.e_circleShape:
  10777. var circle = shape;
  10778. var center = b2Math.MulX(xf, circle.m_p);
  10779. var radius = circle.m_radius;
  10780. var axis = xf.R.col1;
  10781. this.m_debugDraw.DrawSolidCircle(center, radius, axis, color);
  10782. break;
  10783. case b2Shape.e_polygonShape:
  10784. var i = 0;
  10785. var poly = shape;
  10786. var vertexCount = poly.GetVertexCount();
  10787. var localVertices = poly.GetVertices();
  10788. var vertices = new Array(vertexCount);
  10789. for(i = 0;i < vertexCount;++i) {
  10790. vertices[i] = b2Math.MulX(xf, localVertices[i])
  10791. }
  10792. this.m_debugDraw.DrawSolidPolygon(vertices, vertexCount, color);
  10793. break;
  10794. case b2Shape.e_edgeShape:
  10795. var edge = shape;
  10796. this.m_debugDraw.DrawSegment(b2Math.MulX(xf, edge.GetVertex1()), b2Math.MulX(xf, edge.GetVertex2()), color);
  10797. break
  10798. }
  10799. };
  10800. b2World.prototype.SetDestructionListener = function(listener) {
  10801. this.m_destructionListener = listener
  10802. };
  10803. b2World.prototype.SetContactFilter = function(filter) {
  10804. this.m_contactManager.m_contactFilter = filter
  10805. };
  10806. b2World.prototype.SetContactListener = function(listener) {
  10807. this.m_contactManager.m_contactListener = listener
  10808. };
  10809. b2World.prototype.SetDebugDraw = function(debugDraw) {
  10810. this.m_debugDraw = debugDraw
  10811. };
  10812. b2World.prototype.SetBroadPhase = function(broadPhase) {
  10813. var oldBroadPhase = this.m_contactManager.m_broadPhase;
  10814. this.m_contactManager.m_broadPhase = broadPhase;
  10815. for(var b = this.m_bodyList;b;b = b.m_next) {
  10816. for(var f = b.m_fixtureList;f;f = f.m_next) {
  10817. f.m_proxy = broadPhase.CreateProxy(oldBroadPhase.GetFatAABB(f.m_proxy), f)
  10818. }
  10819. }
  10820. };
  10821. b2World.prototype.Validate = function() {
  10822. this.m_contactManager.m_broadPhase.Validate()
  10823. };
  10824. b2World.prototype.GetProxyCount = function() {
  10825. return this.m_contactManager.m_broadPhase.GetProxyCount()
  10826. };
  10827. b2World.prototype.CreateBody = function(def) {
  10828. if(this.IsLocked() == true) {
  10829. return null
  10830. }
  10831. var b = new b2Body(def, this);
  10832. b.m_prev = null;
  10833. b.m_next = this.m_bodyList;
  10834. if(this.m_bodyList) {
  10835. this.m_bodyList.m_prev = b
  10836. }
  10837. this.m_bodyList = b;
  10838. ++this.m_bodyCount;
  10839. return b
  10840. };
  10841. b2World.prototype.DestroyBody = function(b) {
  10842. if(this.IsLocked() == true) {
  10843. return
  10844. }
  10845. var jn = b.m_jointList;
  10846. while(jn) {
  10847. var jn0 = jn;
  10848. jn = jn.next;
  10849. if(this.m_destructionListener) {
  10850. this.m_destructionListener.SayGoodbyeJoint(jn0.joint)
  10851. }
  10852. this.DestroyJoint(jn0.joint)
  10853. }
  10854. var coe = b.m_controllerList;
  10855. while(coe) {
  10856. var coe0 = coe;
  10857. coe = coe.nextController;
  10858. coe0.controller.RemoveBody(b)
  10859. }
  10860. var ce = b.m_contactList;
  10861. while(ce) {
  10862. var ce0 = ce;
  10863. ce = ce.next;
  10864. this.m_contactManager.Destroy(ce0.contact)
  10865. }
  10866. b.m_contactList = null;
  10867. var f = b.m_fixtureList;
  10868. while(f) {
  10869. var f0 = f;
  10870. f = f.m_next;
  10871. if(this.m_destructionListener) {
  10872. this.m_destructionListener.SayGoodbyeFixture(f0)
  10873. }
  10874. f0.DestroyProxy(this.m_contactManager.m_broadPhase);
  10875. f0.Destroy()
  10876. }
  10877. b.m_fixtureList = null;
  10878. b.m_fixtureCount = 0;
  10879. if(b.m_prev) {
  10880. b.m_prev.m_next = b.m_next
  10881. }
  10882. if(b.m_next) {
  10883. b.m_next.m_prev = b.m_prev
  10884. }
  10885. if(b == this.m_bodyList) {
  10886. this.m_bodyList = b.m_next
  10887. }
  10888. --this.m_bodyCount
  10889. };
  10890. b2World.prototype.CreateJoint = function(def) {
  10891. var j = b2Joint.Create(def, null);
  10892. j.m_prev = null;
  10893. j.m_next = this.m_jointList;
  10894. if(this.m_jointList) {
  10895. this.m_jointList.m_prev = j
  10896. }
  10897. this.m_jointList = j;
  10898. ++this.m_jointCount;
  10899. j.m_edgeA.joint = j;
  10900. j.m_edgeA.other = j.m_bodyB;
  10901. j.m_edgeA.prev = null;
  10902. j.m_edgeA.next = j.m_bodyA.m_jointList;
  10903. if(j.m_bodyA.m_jointList) {
  10904. j.m_bodyA.m_jointList.prev = j.m_edgeA
  10905. }
  10906. j.m_bodyA.m_jointList = j.m_edgeA;
  10907. j.m_edgeB.joint = j;
  10908. j.m_edgeB.other = j.m_bodyA;
  10909. j.m_edgeB.prev = null;
  10910. j.m_edgeB.next = j.m_bodyB.m_jointList;
  10911. if(j.m_bodyB.m_jointList) {
  10912. j.m_bodyB.m_jointList.prev = j.m_edgeB
  10913. }
  10914. j.m_bodyB.m_jointList = j.m_edgeB;
  10915. var bodyA = def.bodyA;
  10916. var bodyB = def.bodyB;
  10917. if(def.collideConnected == false) {
  10918. var edge = bodyB.GetContactList();
  10919. while(edge) {
  10920. if(edge.other == bodyA) {
  10921. edge.contact.FlagForFiltering()
  10922. }
  10923. edge = edge.next
  10924. }
  10925. }
  10926. return j
  10927. };
  10928. b2World.prototype.DestroyJoint = function(j) {
  10929. var collideConnected = j.m_collideConnected;
  10930. if(j.m_prev) {
  10931. j.m_prev.m_next = j.m_next
  10932. }
  10933. if(j.m_next) {
  10934. j.m_next.m_prev = j.m_prev
  10935. }
  10936. if(j == this.m_jointList) {
  10937. this.m_jointList = j.m_next
  10938. }
  10939. var bodyA = j.m_bodyA;
  10940. var bodyB = j.m_bodyB;
  10941. bodyA.SetAwake(true);
  10942. bodyB.SetAwake(true);
  10943. if(j.m_edgeA.prev) {
  10944. j.m_edgeA.prev.next = j.m_edgeA.next
  10945. }
  10946. if(j.m_edgeA.next) {
  10947. j.m_edgeA.next.prev = j.m_edgeA.prev
  10948. }
  10949. if(j.m_edgeA == bodyA.m_jointList) {
  10950. bodyA.m_jointList = j.m_edgeA.next
  10951. }
  10952. j.m_edgeA.prev = null;
  10953. j.m_edgeA.next = null;
  10954. if(j.m_edgeB.prev) {
  10955. j.m_edgeB.prev.next = j.m_edgeB.next
  10956. }
  10957. if(j.m_edgeB.next) {
  10958. j.m_edgeB.next.prev = j.m_edgeB.prev
  10959. }
  10960. if(j.m_edgeB == bodyB.m_jointList) {
  10961. bodyB.m_jointList = j.m_edgeB.next
  10962. }
  10963. j.m_edgeB.prev = null;
  10964. j.m_edgeB.next = null;
  10965. b2Joint.Destroy(j, null);
  10966. --this.m_jointCount;
  10967. if(collideConnected == false) {
  10968. var edge = bodyB.GetContactList();
  10969. while(edge) {
  10970. if(edge.other == bodyA) {
  10971. edge.contact.FlagForFiltering()
  10972. }
  10973. edge = edge.next
  10974. }
  10975. }
  10976. };
  10977. b2World.prototype.AddController = function(c) {
  10978. c.m_next = this.m_controllerList;
  10979. c.m_prev = null;
  10980. this.m_controllerList = c;
  10981. c.m_world = this;
  10982. this.m_controllerCount++;
  10983. return c
  10984. };
  10985. b2World.prototype.RemoveController = function(c) {
  10986. if(c.m_prev) {
  10987. c.m_prev.m_next = c.m_next
  10988. }
  10989. if(c.m_next) {
  10990. c.m_next.m_prev = c.m_prev
  10991. }
  10992. if(this.m_controllerList == c) {
  10993. this.m_controllerList = c.m_next
  10994. }
  10995. this.m_controllerCount--
  10996. };
  10997. b2World.prototype.CreateController = function(controller) {
  10998. if(controller.m_world != this) {
  10999. throw new Error("Controller can only be a member of one world");
  11000. }
  11001. controller.m_next = this.m_controllerList;
  11002. controller.m_prev = null;
  11003. if(this.m_controllerList) {
  11004. this.m_controllerList.m_prev = controller
  11005. }
  11006. this.m_controllerList = controller;
  11007. ++this.m_controllerCount;
  11008. controller.m_world = this;
  11009. return controller
  11010. };
  11011. b2World.prototype.DestroyController = function(controller) {
  11012. controller.Clear();
  11013. if(controller.m_next) {
  11014. controller.m_next.m_prev = controller.m_prev
  11015. }
  11016. if(controller.m_prev) {
  11017. controller.m_prev.m_next = controller.m_next
  11018. }
  11019. if(controller == this.m_controllerList) {
  11020. this.m_controllerList = controller.m_next
  11021. }
  11022. --this.m_controllerCount
  11023. };
  11024. b2World.prototype.SetWarmStarting = function(flag) {
  11025. b2World.m_warmStarting = flag
  11026. };
  11027. b2World.prototype.SetContinuousPhysics = function(flag) {
  11028. b2World.m_continuousPhysics = flag
  11029. };
  11030. b2World.prototype.GetBodyCount = function() {
  11031. return this.m_bodyCount
  11032. };
  11033. b2World.prototype.GetJointCount = function() {
  11034. return this.m_jointCount
  11035. };
  11036. b2World.prototype.GetContactCount = function() {
  11037. return this.m_contactCount
  11038. };
  11039. b2World.prototype.SetGravity = function(gravity) {
  11040. this.m_gravity = gravity
  11041. };
  11042. b2World.prototype.GetGravity = function() {
  11043. return this.m_gravity
  11044. };
  11045. b2World.prototype.GetGroundBody = function() {
  11046. return this.m_groundBody
  11047. };
  11048. b2World.prototype.Step = function(dt, velocityIterations, positionIterations) {
  11049. if(this.m_flags & b2World.e_newFixture) {
  11050. this.m_contactManager.FindNewContacts();
  11051. this.m_flags &= ~b2World.e_newFixture
  11052. }
  11053. this.m_flags |= b2World.e_locked;
  11054. var step = b2World.s_timestep2;
  11055. step.dt = dt;
  11056. step.velocityIterations = velocityIterations;
  11057. step.positionIterations = positionIterations;
  11058. if(dt > 0) {
  11059. step.inv_dt = 1 / dt
  11060. }else {
  11061. step.inv_dt = 0
  11062. }
  11063. step.dtRatio = this.m_inv_dt0 * dt;
  11064. step.warmStarting = b2World.m_warmStarting;
  11065. this.m_contactManager.Collide();
  11066. if(step.dt > 0) {
  11067. this.Solve(step)
  11068. }
  11069. if(b2World.m_continuousPhysics && step.dt > 0) {
  11070. this.SolveTOI(step)
  11071. }
  11072. if(step.dt > 0) {
  11073. this.m_inv_dt0 = step.inv_dt
  11074. }
  11075. this.m_flags &= ~b2World.e_locked
  11076. };
  11077. b2World.prototype.ClearForces = function() {
  11078. for(var body = this.m_bodyList;body;body = body.m_next) {
  11079. body.m_force.SetZero();
  11080. body.m_torque = 0
  11081. }
  11082. };
  11083. b2World.prototype.DrawDebugData = function() {
  11084. if(this.m_debugDraw == null) {
  11085. return
  11086. }
  11087. this.m_debugDraw.Clear();
  11088. var flags = this.m_debugDraw.GetFlags();
  11089. var i = 0;
  11090. var b;
  11091. var f;
  11092. var s;
  11093. var j;
  11094. var bp;
  11095. var invQ = new b2Vec2;
  11096. var x1 = new b2Vec2;
  11097. var x2 = new b2Vec2;
  11098. var xf;
  11099. var b1 = new b2AABB;
  11100. var b2 = new b2AABB;
  11101. var vs = [new b2Vec2, new b2Vec2, new b2Vec2, new b2Vec2];
  11102. var color = new b2Color(0, 0, 0);
  11103. if(flags & b2DebugDraw.e_shapeBit) {
  11104. for(b = this.m_bodyList;b;b = b.m_next) {
  11105. xf = b.m_xf;
  11106. for(f = b.GetFixtureList();f;f = f.m_next) {
  11107. s = f.GetShape();
  11108. if(b.IsActive() == false) {
  11109. color.Set(0.5, 0.5, 0.3);
  11110. this.DrawShape(s, xf, color)
  11111. }else {
  11112. if(b.GetType() == b2Body.b2_staticBody) {
  11113. color.Set(0.5, 0.9, 0.5);
  11114. this.DrawShape(s, xf, color)
  11115. }else {
  11116. if(b.GetType() == b2Body.b2_kinematicBody) {
  11117. color.Set(0.5, 0.5, 0.9);
  11118. this.DrawShape(s, xf, color)
  11119. }else {
  11120. if(b.IsAwake() == false) {
  11121. color.Set(0.6, 0.6, 0.6);
  11122. this.DrawShape(s, xf, color)
  11123. }else {
  11124. color.Set(0.9, 0.7, 0.7);
  11125. this.DrawShape(s, xf, color)
  11126. }
  11127. }
  11128. }
  11129. }
  11130. }
  11131. }
  11132. }
  11133. if(flags & b2DebugDraw.e_jointBit) {
  11134. for(j = this.m_jointList;j;j = j.m_next) {
  11135. this.DrawJoint(j)
  11136. }
  11137. }
  11138. if(flags & b2DebugDraw.e_controllerBit) {
  11139. for(var c = this.m_controllerList;c;c = c.m_next) {
  11140. c.Draw(this.m_debugDraw)
  11141. }
  11142. }
  11143. if(flags & b2DebugDraw.e_pairBit) {
  11144. color.Set(0.3, 0.9, 0.9);
  11145. for(var contact = this.m_contactManager.m_contactList;contact;contact = contact.GetNext()) {
  11146. var fixtureA = contact.GetFixtureA();
  11147. var fixtureB = contact.GetFixtureB();
  11148. var cA = fixtureA.GetAABB().GetCenter();
  11149. var cB = fixtureB.GetAABB().GetCenter();
  11150. this.m_debugDraw.DrawSegment(cA, cB, color)
  11151. }
  11152. }
  11153. if(flags & b2DebugDraw.e_aabbBit) {
  11154. bp = this.m_contactManager.m_broadPhase;
  11155. vs = [new b2Vec2, new b2Vec2, new b2Vec2, new b2Vec2];
  11156. for(b = this.m_bodyList;b;b = b.GetNext()) {
  11157. if(b.IsActive() == false) {
  11158. continue
  11159. }
  11160. for(f = b.GetFixtureList();f;f = f.GetNext()) {
  11161. var aabb = bp.GetFatAABB(f.m_proxy);
  11162. vs[0].Set(aabb.lowerBound.x, aabb.lowerBound.y);
  11163. vs[1].Set(aabb.upperBound.x, aabb.lowerBound.y);
  11164. vs[2].Set(aabb.upperBound.x, aabb.upperBound.y);
  11165. vs[3].Set(aabb.lowerBound.x, aabb.upperBound.y);
  11166. this.m_debugDraw.DrawPolygon(vs, 4, color)
  11167. }
  11168. }
  11169. }
  11170. if(flags & b2DebugDraw.e_centerOfMassBit) {
  11171. for(b = this.m_bodyList;b;b = b.m_next) {
  11172. xf = b2World.s_xf;
  11173. xf.R = b.m_xf.R;
  11174. xf.position = b.GetWorldCenter();
  11175. this.m_debugDraw.DrawTransform(xf)
  11176. }
  11177. }
  11178. };
  11179. b2World.prototype.QueryAABB = function(callback, aabb) {
  11180. var broadPhase = this.m_contactManager.m_broadPhase;
  11181. function WorldQueryWrapper(proxy) {
  11182. return callback(broadPhase.GetUserData(proxy))
  11183. }
  11184. broadPhase.Query(WorldQueryWrapper, aabb)
  11185. };
  11186. b2World.prototype.QueryShape = function(callback, shape, transform) {
  11187. if(transform == null) {
  11188. transform = new b2Transform;
  11189. transform.SetIdentity()
  11190. }
  11191. var broadPhase = this.m_contactManager.m_broadPhase;
  11192. function WorldQueryWrapper(proxy) {
  11193. var fixture = broadPhase.GetUserData(proxy);
  11194. if(b2Shape.TestOverlap(shape, transform, fixture.GetShape(), fixture.GetBody().GetTransform())) {
  11195. return callback(fixture)
  11196. }
  11197. return true
  11198. }
  11199. var aabb = new b2AABB;
  11200. shape.ComputeAABB(aabb, transform);
  11201. broadPhase.Query(WorldQueryWrapper, aabb)
  11202. };
  11203. b2World.prototype.QueryPoint = function(callback, p) {
  11204. var broadPhase = this.m_contactManager.m_broadPhase;
  11205. function WorldQueryWrapper(proxy) {
  11206. var fixture = broadPhase.GetUserData(proxy);
  11207. if(fixture.TestPoint(p)) {
  11208. return callback(fixture)
  11209. }
  11210. return true
  11211. }
  11212. var aabb = new b2AABB;
  11213. aabb.lowerBound.Set(p.x - b2Settings.b2_linearSlop, p.y - b2Settings.b2_linearSlop);
  11214. aabb.upperBound.Set(p.x + b2Settings.b2_linearSlop, p.y + b2Settings.b2_linearSlop);
  11215. broadPhase.Query(WorldQueryWrapper, aabb)
  11216. };
  11217. b2World.prototype.RayCast = function(callback, point1, point2) {
  11218. var broadPhase = this.m_contactManager.m_broadPhase;
  11219. var output = new b2RayCastOutput;
  11220. function RayCastWrapper(input, proxy) {
  11221. var userData = broadPhase.GetUserData(proxy);
  11222. var fixture = userData;
  11223. var hit = fixture.RayCast(output, input);
  11224. if(hit) {
  11225. var fraction = output.fraction;
  11226. var point = new b2Vec2((1 - fraction) * point1.x + fraction * point2.x, (1 - fraction) * point1.y + fraction * point2.y);
  11227. return callback(fixture, point, output.normal, fraction)
  11228. }
  11229. return input.maxFraction
  11230. }
  11231. var input = new b2RayCastInput(point1, point2);
  11232. broadPhase.RayCast(RayCastWrapper, input)
  11233. };
  11234. b2World.prototype.RayCastOne = function(point1, point2) {
  11235. var result;
  11236. function RayCastOneWrapper(fixture, point, normal, fraction) {
  11237. result = fixture;
  11238. return fraction
  11239. }
  11240. this.RayCast(RayCastOneWrapper, point1, point2);
  11241. return result
  11242. };
  11243. b2World.prototype.RayCastAll = function(point1, point2) {
  11244. var result = new Array;
  11245. function RayCastAllWrapper(fixture, point, normal, fraction) {
  11246. result[result.length] = fixture;
  11247. return 1
  11248. }
  11249. this.RayCast(RayCastAllWrapper, point1, point2);
  11250. return result
  11251. };
  11252. b2World.prototype.GetBodyList = function() {
  11253. return this.m_bodyList
  11254. };
  11255. b2World.prototype.GetJointList = function() {
  11256. return this.m_jointList
  11257. };
  11258. b2World.prototype.GetContactList = function() {
  11259. return this.m_contactList
  11260. };
  11261. b2World.prototype.IsLocked = function() {
  11262. return(this.m_flags & b2World.e_locked) > 0
  11263. };
  11264. b2World.prototype.s_stack = new Array;
  11265. b2World.prototype.m_flags = 0;
  11266. b2World.prototype.m_contactManager = new b2ContactManager;
  11267. b2World.prototype.m_contactSolver = new b2ContactSolver;
  11268. b2World.prototype.m_island = new b2Island;
  11269. b2World.prototype.m_bodyList = null;
  11270. b2World.prototype.m_jointList = null;
  11271. b2World.prototype.m_contactList = null;
  11272. b2World.prototype.m_bodyCount = 0;
  11273. b2World.prototype.m_contactCount = 0;
  11274. b2World.prototype.m_jointCount = 0;
  11275. b2World.prototype.m_controllerList = null;
  11276. b2World.prototype.m_controllerCount = 0;
  11277. b2World.prototype.m_gravity = null;
  11278. b2World.prototype.m_allowSleep = null;
  11279. b2World.prototype.m_groundBody = null;
  11280. b2World.prototype.m_destructionListener = null;
  11281. b2World.prototype.m_debugDraw = null;
  11282. b2World.prototype.m_inv_dt0 = null;if(typeof exports !== "undefined") {
  11283. exports.b2BoundValues = b2BoundValues;
  11284. exports.b2Math = b2Math;
  11285. exports.b2DistanceOutput = b2DistanceOutput;
  11286. exports.b2Mat33 = b2Mat33;
  11287. exports.b2ContactPoint = b2ContactPoint;
  11288. exports.b2PairManager = b2PairManager;
  11289. exports.b2PositionSolverManifold = b2PositionSolverManifold;
  11290. exports.b2OBB = b2OBB;
  11291. exports.b2CircleContact = b2CircleContact;
  11292. exports.b2PulleyJoint = b2PulleyJoint;
  11293. exports.b2Pair = b2Pair;
  11294. exports.b2TimeStep = b2TimeStep;
  11295. exports.b2FixtureDef = b2FixtureDef;
  11296. exports.b2World = b2World;
  11297. exports.b2PrismaticJoint = b2PrismaticJoint;
  11298. exports.b2Controller = b2Controller;
  11299. exports.b2ContactID = b2ContactID;
  11300. exports.b2RevoluteJoint = b2RevoluteJoint;
  11301. exports.b2JointDef = b2JointDef;
  11302. exports.b2Transform = b2Transform;
  11303. exports.b2GravityController = b2GravityController;
  11304. exports.b2EdgeAndCircleContact = b2EdgeAndCircleContact;
  11305. exports.b2EdgeShape = b2EdgeShape;
  11306. exports.b2BuoyancyController = b2BuoyancyController;
  11307. exports.b2LineJointDef = b2LineJointDef;
  11308. exports.b2Contact = b2Contact;
  11309. exports.b2DistanceJoint = b2DistanceJoint;
  11310. exports.b2Body = b2Body;
  11311. exports.b2DestructionListener = b2DestructionListener;
  11312. exports.b2PulleyJointDef = b2PulleyJointDef;
  11313. exports.b2ContactEdge = b2ContactEdge;
  11314. exports.b2ContactConstraint = b2ContactConstraint;
  11315. exports.b2ContactImpulse = b2ContactImpulse;
  11316. exports.b2DistanceJointDef = b2DistanceJointDef;
  11317. exports.b2ContactResult = b2ContactResult;
  11318. exports.b2EdgeChainDef = b2EdgeChainDef;
  11319. exports.b2Vec2 = b2Vec2;
  11320. exports.b2Vec3 = b2Vec3;
  11321. exports.b2DistanceProxy = b2DistanceProxy;
  11322. exports.b2FrictionJointDef = b2FrictionJointDef;
  11323. exports.b2PolygonContact = b2PolygonContact;
  11324. exports.b2TensorDampingController = b2TensorDampingController;
  11325. exports.b2ContactFactory = b2ContactFactory;
  11326. exports.b2WeldJointDef = b2WeldJointDef;
  11327. exports.b2ConstantAccelController = b2ConstantAccelController;
  11328. exports.b2GearJointDef = b2GearJointDef;
  11329. exports.ClipVertex = ClipVertex;
  11330. exports.b2SeparationFunction = b2SeparationFunction;
  11331. exports.b2ManifoldPoint = b2ManifoldPoint;
  11332. exports.b2Color = b2Color;
  11333. exports.b2PolygonShape = b2PolygonShape;
  11334. exports.b2DynamicTreePair = b2DynamicTreePair;
  11335. exports.b2ContactConstraintPoint = b2ContactConstraintPoint;
  11336. exports.b2FrictionJoint = b2FrictionJoint;
  11337. exports.b2ContactFilter = b2ContactFilter;
  11338. exports.b2ControllerEdge = b2ControllerEdge;
  11339. exports.b2Distance = b2Distance;
  11340. exports.b2Fixture = b2Fixture;
  11341. exports.b2DynamicTreeNode = b2DynamicTreeNode;
  11342. exports.b2MouseJoint = b2MouseJoint;
  11343. exports.b2DistanceInput = b2DistanceInput;
  11344. exports.b2BodyDef = b2BodyDef;
  11345. exports.b2DynamicTreeBroadPhase = b2DynamicTreeBroadPhase;
  11346. exports.b2Settings = b2Settings;
  11347. exports.b2Proxy = b2Proxy;
  11348. exports.b2Point = b2Point;
  11349. exports.b2BroadPhase = b2BroadPhase;
  11350. exports.b2Manifold = b2Manifold;
  11351. exports.b2WorldManifold = b2WorldManifold;
  11352. exports.b2PrismaticJointDef = b2PrismaticJointDef;
  11353. exports.b2RayCastOutput = b2RayCastOutput;
  11354. exports.b2ConstantForceController = b2ConstantForceController;
  11355. exports.b2TimeOfImpact = b2TimeOfImpact;
  11356. exports.b2CircleShape = b2CircleShape;
  11357. exports.b2MassData = b2MassData;
  11358. exports.b2Joint = b2Joint;
  11359. exports.b2GearJoint = b2GearJoint;
  11360. exports.b2DynamicTree = b2DynamicTree;
  11361. exports.b2JointEdge = b2JointEdge;
  11362. exports.b2LineJoint = b2LineJoint;
  11363. exports.b2NullContact = b2NullContact;
  11364. exports.b2ContactListener = b2ContactListener;
  11365. exports.b2RayCastInput = b2RayCastInput;
  11366. exports.b2TOIInput = b2TOIInput;
  11367. exports.Features = Features;
  11368. exports.b2FilterData = b2FilterData;
  11369. exports.b2Island = b2Island;
  11370. exports.b2ContactManager = b2ContactManager;
  11371. exports.b2ContactSolver = b2ContactSolver;
  11372. exports.b2Simplex = b2Simplex;
  11373. exports.b2AABB = b2AABB;
  11374. exports.b2Jacobian = b2Jacobian;
  11375. exports.b2Bound = b2Bound;
  11376. exports.b2RevoluteJointDef = b2RevoluteJointDef;
  11377. exports.b2PolyAndEdgeContact = b2PolyAndEdgeContact;
  11378. exports.b2SimplexVertex = b2SimplexVertex;
  11379. exports.b2WeldJoint = b2WeldJoint;
  11380. exports.b2Collision = b2Collision;
  11381. exports.b2Mat22 = b2Mat22;
  11382. exports.b2SimplexCache = b2SimplexCache;
  11383. exports.b2PolyAndCircleContact = b2PolyAndCircleContact;
  11384. exports.b2MouseJointDef = b2MouseJointDef;
  11385. exports.b2Shape = b2Shape;
  11386. exports.b2Segment = b2Segment;
  11387. exports.b2ContactRegister = b2ContactRegister;
  11388. exports.b2DebugDraw = b2DebugDraw;
  11389. exports.b2Sweep = b2Sweep
  11390. }
  11391. ;