Skip to content

Instantly share code, notes, and snippets.

@vecheslav
Created August 15, 2015 04:34
Show Gist options
  • Save vecheslav/fe19e3ed65ee9491b9e6 to your computer and use it in GitHub Desktop.
Save vecheslav/fe19e3ed65ee9491b9e6 to your computer and use it in GitHub Desktop.
Hack phaser physics matter
/**
* HACK: phaser physics matter
*/
module.exports = (function () {
Phaser.Physics.Matter = function (game, config) {
this.game = game;
this.engine = Matter.Engine.create({
enableSleeping: false
});
/*Matter.World.add(this.engine.world, []);*/
this.world = this.engine.world;
this.world.gravity = {
x: 0,
y: 0
}
this.paused = false;
this.frameRate = this.timeTick = 1 / 60;
if (this.useElapsedTime) {
this.timeTick = this.game.time.physicsElapsed;
}
};
Phaser.Physics.Matter.prototype = {
preUpdate: function () {
// ...
},
postUpdate: function () {
// ...
},
enable: function (object, debug, children) {
if (debug === undefined) {
debug = false;
}
if (children === undefined) {
children = true;
}
var i = 1;
if (Array.isArray(object)) {
i = object.length;
while (i--) {
if (object[i] instanceof Phaser.Group) {
// If it's a Group then we do it on the children regardless
this.enable(object[i].children, debug, children);
} else {
this.enableBody(object[i], debug);
if (children && object[i].hasOwnProperty('children') && object[i].children.length > 0) {
this.enable(object[i], debug, true);
}
}
}
} else {
if (object instanceof Phaser.Group) {
// If it's a Group then we do it on the children regardless
this.enable(object.children, debug, children);
} else {
this.enableBody(object, debug);
if (children && object.hasOwnProperty('children') && object.children.length > 0) {
this.enable(object.children, debug, true);
}
}
}
},
enableBody: function (object, debug) {
if (object.hasOwnProperty('body') && object.body === null) {
object.body = new Phaser.Physics.Matter.Body(this.game, object, object.x, object.y, 1);
object.body.debug = debug;
if (typeof object.anchor !== 'undefined') {
object.anchor.set(0.5);
}
}
},
update: function () {
if (this.paused) {
return;
}
if (this.useElapsedTime) {
this.timeTick = this.game.time.physicsElapsed;
} else {
this.timeTick = this.frameRate;
}
Matter.Engine.update(this.engine, this.timeTick);
},
pause: function () {
this.paused = true;
},
reset: function () {
// ...
},
addBody: function (body) {
if (body.data.world) {
return false;
} else {
Matter.World.addBody(this.world, body.data);
body.data.world = this.world;
return true;
}
},
removeBody: function (body) {
if (body.data.world == this.world) {
Matter.World.remove(this.world, body.data);
}
return body;
}
};
Phaser.Physics.Matter.Body = function (game, sprite, x, y, mass) {
sprite = sprite || null;
x = x || 0;
y = y || 0;
if (mass === undefined) {
mass = 1;
}
this.game = game;
this.world = this.game.physics.matter;
this.sprite = sprite;
this.type = Phaser.Physics.MATTERJS;
this.offset = new Phaser.Point();
// matter body
this.data = Matter.Bodies.rectangle(x, y, this.sprite.width, this.sprite.height);
this.data.parent = this;
this.mass = this.data.mass = mass;
this.slop = this.data.slop = 0;
this.friction = this.data.friction = 1;
this.frictionAir = this.data.frictionAir = 0;
this.restitution = this.data.restitution = 0;
this.velocity = new Phaser.Point();
this.x = x;
this.y = y;
this.force = this.data.force;
this.gravity = new Phaser.Point();
this.debug = false;
this.debugBody = null;
this.dirty = false;
if (sprite) {
if (sprite.exists) {
this.game.physics.matter.addBody(this);
}
}
};
Phaser.Physics.Matter.Body.prototype = {
setZeroVelocity: function () {
this.velocity = {
x: 0,
y: 0
};
},
preUpdate: function () {
this.dirty = true;
// ...
},
postUpdate: function () {
this.sprite.x = this.data.position.x;
this.sprite.y = this.data.position.y;
if (!this.fixedRotation) {
this.sprite.rotation = this.data.angle;
}
this.dirty = false;
},
clearParts: function () {
Matter.Body.setParts(this.data, []);
},
setParts: function (parts) {
Matter.Body.setParts(this.data, parts);
return parts;
},
addCircle: function (radius, offsetX, offsetY) {
var x,
y,
shape;
x = this.data.position.x + offsetX;
y = this.data.position.y + offsetY;
shape = Matter.Bodies.circle(x, y, radius);
this.data.position.x = x;
this.data.position.y = y;
this.data.vertices = shape.vertices;
return this.data;
},
destroy: function () {
// ...
}
};
Phaser.Physics.Matter.Body.prototype.constructor = Phaser.Physics.Matter.Body;
Object.defineProperty(Phaser.Physics.Matter.Body.prototype, "rotation", {
get: function() {
return this.data.angle;
},
set: function(value) {
this.data.angle = value;
}
});
Object.defineProperty(Phaser.Physics.Matter.Body.prototype, "x", {
get: function () {
return this.data.position.x;
},
set: function (value) {
this.data.position.x = value;
}
});
Object.defineProperty(Phaser.Physics.Matter.Body.prototype, "y", {
get: function () {
return this.data.position.y;
},
set: function (value) {
this.data.position.y = value;
}
});
Object.defineProperty(Phaser.Physics.Matter.Body.prototype, "velocity", {
get: function () {
return this.data.velocity;
},
set: function (value) {
value.x *= this.world.timeTick;
value.y *= this.world.timeTick;
Matter.Body.setVelocity(this.data, value);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment