PageRenderTime 596ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 3ms

/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

Large files files are truncated, but you can click here to view the full file

  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. b2A

Large files files are truncated, but you can click here to view the full file