Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Collider = function(type, params) {
- PIXI.Container.call(this);
- this.group = params.group || "main";
- this.type = type;
- this.radius;
- this.active = true;
- this.id = Collider.count++;
- this.scalable = params.scalable || true;
- this.x = params.x || 0;
- this.y = params.y || 0;
- this.w = params.w || 0;
- this.h = params.h || 0;
- switch(type) {
- case "circle":
- this.radius = params.r || 1;
- break;
- case "rect":
- break;
- default: return;
- }
- this.makeDebugGraphics = function() {
- this.debug = new PIXI.Graphics();
- this.addChild(this.debug);
- switch(type) {
- case "circle":
- var color = 0x00ff00;
- this.debug.lineStyle(1, color, 1);
- this.debug.beginFill(color, 0.4);
- this.debug.drawCircle(0, 0,this.radius);
- this.debug.endFill();
- break;
- case "rect":
- var color = 0x00ff00;
- this.debug.lineStyle(1, color, 1);
- this.debug.beginFill(color, 0.4);
- this.debug.drawRect(-this.w / 2, -this.h / 2, this.w, this.h);
- this.debug.endFill();
- break;
- }
- }
- Collisions.add(this);
- }
- Collider.count = 0;
- Collider.prototype = Object.create(PIXI.Container.prototype);
- var Collisions = (function() {
- var debug = true;
- var groups = [];
- var handlers = [];
- var contacts = [];
- function update() {
- for (var i = 0; i < handlers.length; i++) {
- var h = handlers[i];
- var g1 = groups[h.group1];
- var g2 = groups[h.group2];
- if (!g1 || !g2) continue;
- for (var m = 0; m < g1.length; m++) {
- for (var n = 0; n < g2.length; n++) {
- if (g1[m] !== g2[n]) {
- var event = _collide(g1[m], g2[n]);
- if (event) {
- h.f(event);
- }
- }
- }
- }
- }
- }
- function add(c) {
- if (!(c.group in groups)) groups[c.group] = [];
- groups[c.group].push(c);
- if (debug) {
- c.makeDebugGraphics();
- }
- }
- function on(group1, group2, f) {
- handlers.push({
- group1: group1,
- group2: group2,
- f: f
- });
- }
- function _collide(c1, c2) {
- var collided = false;
- var hash = c1.id + "#" + c2.id;
- if (c1.type === "rect" && c2.type === "rect") {
- //console.log(c1, c2)
- var wt1 = c1.worldTransform;
- var wt2 = c2.worldTransform;
- var r1 = {
- left: wt1.tx - (c1.w * 0.5) * wt1.a,
- right: wt1.tx + (c1.w * 0.5) * wt1.a,
- top: wt1.ty - (c1.h * 0.5) * wt1.d,
- bottom: wt1.ty + (c1.h * 0.5) * wt1.d,
- }
- var r2 = {
- left: wt2.tx - (c2.w * 0.5) * wt2.a,
- right: wt2.tx + (c2.w * 0.5) * wt2.a,
- top: wt2.ty - (c2.h * 0.5) * wt2.d,
- bottom: wt2.ty + (c2.h * 0.5) * wt2.d,
- }
- collided = _intersectRect(r1, r2);
- }
- if (hash in contacts) {
- if (collided && !contacts[hash]) {
- contacts[hash] = true;
- return {
- event: "start", c1: c1, c2: c2
- }
- }
- if (!collided && contacts[hash]) {
- contacts[hash] = false;
- return {
- event: "end", c1: c1, c2: c2
- }
- }
- return null;
- } else {
- contacts[hash] = true;
- return {
- event: "start", c1: c1, c2: c2
- }
- }
- }
- function _intersectRect(r1, r2) {
- return !(r2.left > r1.right ||
- r2.right < r1.left ||
- r2.top > r1.bottom ||
- r2.bottom < r1.top);
- }
- return {
- update: update,
- add: add,
- on: on
- }
- })();
- // Test app //////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////
- var app = new PIXI.Application(innerWidth, innerHeight, {backgroundColor : 0x000000});
- document.body.appendChild(app.view);
- // create a new Sprite from an image path
- var bunny = PIXI.Sprite.fromImage('https://pixijs.github.io/examples/required/assets/basics/bunny.png')
- var bunny2 = PIXI.Sprite.fromImage('https://pixijs.github.io/examples/required/assets/basics/bunny.png')
- var text = new PIXI.Text("Collision", {fill: 0xffffff});
- text.x = 50;
- text.y = 20;
- text.visible = false;
- // center the sprite's anchor point
- bunny.anchor.set(0.5);
- bunny2.anchor.set(0.5);
- // move the sprite to the center of the screen
- bunny.x = app.renderer.width / 2;
- bunny.y = app.renderer.height / 2;
- bunny2.x = app.renderer.width / 2;
- bunny2.y = app.renderer.height / 2;
- //bunny.addChild(new Collider("circle", {x: 0, y: -10, r: 15}));
- bunny.addChild(new Collider("rect", {x: 0, y: 0, w: 15, h: 30, group: "b1"}));
- //bunny2.addChild(new Collider("circle", {x: 0, y: -10, r: 15}));
- bunny2.addChild(new Collider("rect", {x: 0, y: 0, w: 15, h: 30, group: "b2"}));
- bunny.scale.set(2,2);
- bunny2.scale.set(2,2);
- app.stage.addChild(bunny);
- app.stage.addChild(bunny2);
- app.stage.addChild(text);
- Collisions.on("b1", "b2", function(e) {
- if (e.event === "start") {
- text.visible = true;
- }
- if (e.event === "end") {
- text.visible = false;
- }
- })
- // Listen for animate update
- app.ticker.add(function(delta) {
- // just for fun, let's rotate mr rabbit a little
- // delta is 1 if running at 100% performance
- // creates frame-independent tranformation
- // bunny.rotation += 0.01 * delta;
- //bunny2.rotation -= 0.01 * delta;
- bunny2.x = app.renderer.width /2 + Math.sin(Date.now() / 2000) * 50;
- Collisions.update();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement