Skip to content

Commit 8add251

Browse files
committed
Added Factory constraint functions
1 parent f62409d commit 8add251

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

v3/src/physics/matter-js/Factory.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Bodies = require('./lib/factory/Bodies');
22
var Class = require('../../utils/Class');
33
var Composites = require('./lib/factory/Composites');
4+
var Constraint = require('./lib/constraint/Constraint');
45
var MatterImage = require('./MatterImage');
56
var MatterSprite = require('./MatterSprite');
67

@@ -79,6 +80,34 @@ var Factory = new Class({
7980
return stack;
8081
},
8182

83+
// To help those used to Box2D
84+
joint: function (bodyA, bodyB, length, stiffness, options)
85+
{
86+
return this.constraint(bodyA, bodyB, length, stiffness, options);
87+
},
88+
89+
spring: function (bodyA, bodyB, length, stiffness, options)
90+
{
91+
return this.constraint(bodyA, bodyB, length, stiffness, options);
92+
},
93+
94+
constraint: function (bodyA, bodyB, length, stiffness, options)
95+
{
96+
if (stiffness === undefined) { stiffness = 1; }
97+
if (options === undefined) { options = {}; }
98+
99+
options.bodyA = (bodyA.type === 'body') ? bodyA : bodyA.body;
100+
options.bodyB = (bodyB.type === 'body') ? bodyB : bodyB.body;
101+
options.length = length;
102+
options.stiffness = stiffness;
103+
104+
var constraint = Constraint.create(options);
105+
106+
this.world.add(constraint);
107+
108+
return constraint;
109+
},
110+
82111
image: function (x, y, key, frame, options)
83112
{
84113
var image = new MatterImage(this.world, x, y, key, frame, options);

0 commit comments

Comments
 (0)