Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. var Collider = function(type, params) {
  2.  
  3. PIXI.Container.call(this);
  4.  
  5. this.group = params.group || "main";
  6. this.type = type;
  7. this.radius;
  8. this.active = true;
  9. this.id = Collider.count++;
  10. this.scalable = params.scalable || true;
  11.  
  12. this.x = params.x || 0;
  13. this.y = params.y || 0;
  14. this.w = params.w || 0;
  15. this.h = params.h || 0;
  16.  
  17. switch(type) {
  18. case "circle":
  19. this.radius = params.r || 1;
  20. break;
  21. case "rect":
  22.  
  23. break;
  24. default: return;
  25. }
  26.  
  27. this.makeDebugGraphics = function() {
  28. this.debug = new PIXI.Graphics();
  29. this.addChild(this.debug);
  30. switch(type) {
  31. case "circle":
  32. var color = 0x00ff00;
  33. this.debug.lineStyle(1, color, 1);
  34. this.debug.beginFill(color, 0.4);
  35. this.debug.drawCircle(0, 0,this.radius);
  36. this.debug.endFill();
  37. break;
  38. case "rect":
  39. var color = 0x00ff00;
  40. this.debug.lineStyle(1, color, 1);
  41. this.debug.beginFill(color, 0.4);
  42. this.debug.drawRect(-this.w / 2, -this.h / 2, this.w, this.h);
  43. this.debug.endFill();
  44. break;
  45. }
  46. }
  47.  
  48. Collisions.add(this);
  49.  
  50. }
  51.  
  52. Collider.count = 0;
  53. Collider.prototype = Object.create(PIXI.Container.prototype);
  54.  
  55. var Collisions = (function() {
  56. var debug = true;
  57. var groups = [];
  58. var handlers = [];
  59. var contacts = [];
  60.  
  61. function update() {
  62. for (var i = 0; i < handlers.length; i++) {
  63. var h = handlers[i];
  64. var g1 = groups[h.group1];
  65. var g2 = groups[h.group2];
  66.  
  67. if (!g1 || !g2) continue;
  68.  
  69. for (var m = 0; m < g1.length; m++) {
  70. for (var n = 0; n < g2.length; n++) {
  71. if (g1[m] !== g2[n]) {
  72. var event = _collide(g1[m], g2[n]);
  73. if (event) {
  74. h.f(event);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81.  
  82. function add(c) {
  83. if (!(c.group in groups)) groups[c.group] = [];
  84. groups[c.group].push(c);
  85.  
  86. if (debug) {
  87. c.makeDebugGraphics();
  88. }
  89. }
  90.  
  91. function on(group1, group2, f) {
  92. handlers.push({
  93. group1: group1,
  94. group2: group2,
  95. f: f
  96. });
  97. }
  98.  
  99. function _collide(c1, c2) {
  100.  
  101. var collided = false;
  102. var hash = c1.id + "#" + c2.id;
  103.  
  104. if (c1.type === "rect" && c2.type === "rect") {
  105. //console.log(c1, c2)
  106. var wt1 = c1.worldTransform;
  107. var wt2 = c2.worldTransform;
  108.  
  109. var r1 = {
  110. left: wt1.tx - (c1.w * 0.5) * wt1.a,
  111. right: wt1.tx + (c1.w * 0.5) * wt1.a,
  112. top: wt1.ty - (c1.h * 0.5) * wt1.d,
  113. bottom: wt1.ty + (c1.h * 0.5) * wt1.d,
  114. }
  115.  
  116. var r2 = {
  117. left: wt2.tx - (c2.w * 0.5) * wt2.a,
  118. right: wt2.tx + (c2.w * 0.5) * wt2.a,
  119. top: wt2.ty - (c2.h * 0.5) * wt2.d,
  120. bottom: wt2.ty + (c2.h * 0.5) * wt2.d,
  121. }
  122.  
  123.  
  124.  
  125. collided = _intersectRect(r1, r2);
  126. }
  127.  
  128. if (hash in contacts) {
  129. if (collided && !contacts[hash]) {
  130. contacts[hash] = true;
  131. return {
  132. event: "start", c1: c1, c2: c2
  133. }
  134. }
  135. if (!collided && contacts[hash]) {
  136. contacts[hash] = false;
  137. return {
  138. event: "end", c1: c1, c2: c2
  139. }
  140. }
  141. return null;
  142. } else {
  143. contacts[hash] = true;
  144. return {
  145. event: "start", c1: c1, c2: c2
  146. }
  147. }
  148. }
  149.  
  150. function _intersectRect(r1, r2) {
  151. return !(r2.left > r1.right ||
  152. r2.right < r1.left ||
  153. r2.top > r1.bottom ||
  154. r2.bottom < r1.top);
  155. }
  156.  
  157. return {
  158. update: update,
  159. add: add,
  160. on: on
  161. }
  162. })();
  163.  
  164.  
  165.  
  166.  
  167. // Test app //////////////////////////////////////////////////
  168. //////////////////////////////////////////////////////////////
  169.  
  170. var app = new PIXI.Application(innerWidth, innerHeight, {backgroundColor : 0x000000});
  171. document.body.appendChild(app.view);
  172.  
  173. // create a new Sprite from an image path
  174. var bunny = PIXI.Sprite.fromImage('https://pixijs.github.io/examples/required/assets/basics/bunny.png')
  175. var bunny2 = PIXI.Sprite.fromImage('https://pixijs.github.io/examples/required/assets/basics/bunny.png')
  176. var text = new PIXI.Text("Collision", {fill: 0xffffff});
  177. text.x = 50;
  178. text.y = 20;
  179. text.visible = false;
  180.  
  181. // center the sprite's anchor point
  182. bunny.anchor.set(0.5);
  183. bunny2.anchor.set(0.5);
  184.  
  185. // move the sprite to the center of the screen
  186. bunny.x = app.renderer.width / 2;
  187. bunny.y = app.renderer.height / 2;
  188. bunny2.x = app.renderer.width / 2;
  189. bunny2.y = app.renderer.height / 2;
  190.  
  191. //bunny.addChild(new Collider("circle", {x: 0, y: -10, r: 15}));
  192. bunny.addChild(new Collider("rect", {x: 0, y: 0, w: 15, h: 30, group: "b1"}));
  193. //bunny2.addChild(new Collider("circle", {x: 0, y: -10, r: 15}));
  194. bunny2.addChild(new Collider("rect", {x: 0, y: 0, w: 15, h: 30, group: "b2"}));
  195.  
  196. bunny.scale.set(2,2);
  197. bunny2.scale.set(2,2);
  198.  
  199. app.stage.addChild(bunny);
  200. app.stage.addChild(bunny2);
  201. app.stage.addChild(text);
  202.  
  203. Collisions.on("b1", "b2", function(e) {
  204. if (e.event === "start") {
  205. text.visible = true;
  206. }
  207. if (e.event === "end") {
  208. text.visible = false;
  209. }
  210. })
  211.  
  212. // Listen for animate update
  213. app.ticker.add(function(delta) {
  214. // just for fun, let's rotate mr rabbit a little
  215. // delta is 1 if running at 100% performance
  216. // creates frame-independent tranformation
  217. // bunny.rotation += 0.01 * delta;
  218. //bunny2.rotation -= 0.01 * delta;
  219.  
  220. bunny2.x = app.renderer.width /2 + Math.sin(Date.now() / 2000) * 50;
  221. Collisions.update();
  222. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement