Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Common.extend() in Constraint #849

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/constraint/Constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ var Common = require('../core/Common');
* @return {constraint} constraint
*/
Constraint.create = function(options) {
var constraint = options;
var defaults = {
id: Common.nextId(),
type: 'constraint',
label: 'Constraint',
damping: 0,
angularStiffness: 0,
plugin: {},
};

// NOTE: defaults are merged non-recursively, so any deep defaults must be merged separately.
var constraint = Common.extend(defaults, false, options);

// if bodies defined but no points, use body centre
if (constraint.bodyA && !constraint.pointA)
Expand All @@ -49,19 +59,13 @@ var Common = require('../core/Common');
var initialPointA = constraint.bodyA ? Vector.add(constraint.bodyA.position, constraint.pointA) : constraint.pointA,
initialPointB = constraint.bodyB ? Vector.add(constraint.bodyB.position, constraint.pointB) : constraint.pointB,
length = Vector.magnitude(Vector.sub(initialPointA, initialPointB));

constraint.length = typeof constraint.length !== 'undefined' ? constraint.length : length;

// option defaults
constraint.id = constraint.id || Common.nextId();
constraint.label = constraint.label || 'Constraint';
constraint.type = 'constraint';
constraint.stiffness = constraint.stiffness || (constraint.length > 0 ? 1 : 0.7);
constraint.damping = constraint.damping || 0;
constraint.angularStiffness = constraint.angularStiffness || 0;
constraint.angleA = constraint.bodyA ? constraint.bodyA.angle : constraint.angleA;
constraint.angleB = constraint.bodyB ? constraint.bodyB.angle : constraint.angleB;
constraint.plugin = {};

// render
var render = {
Expand Down Expand Up @@ -157,7 +161,7 @@ var Common = require('../core/Common');
Vector.rotate(pointA, bodyA.angle - constraint.angleA, pointA);
constraint.angleA = bodyA.angle;
}

// update reference angle
if (bodyB && !bodyB.isStatic) {
Vector.rotate(pointB, bodyB.angle - constraint.angleB, pointB);
Expand Down Expand Up @@ -235,7 +239,7 @@ var Common = require('../core/Common');
// keep track of applied impulses for post solving
bodyB.constraintImpulse.x += force.x * share;
bodyB.constraintImpulse.y += force.y * share;

// apply forces
bodyB.position.x += force.x * share;
bodyB.position.y += force.y * share;
Expand Down Expand Up @@ -274,7 +278,7 @@ var Common = require('../core/Common');
// update geometry and reset
for (var j = 0; j < body.parts.length; j++) {
var part = body.parts[j];

Vertices.translate(part.vertices, impulse);

if (j > 0) {
Expand Down Expand Up @@ -390,7 +394,7 @@ var Common = require('../core/Common');
*/

/**
* A `String` that defines the constraint rendering type.
* A `String` that defines the constraint rendering type.
* The possible values are 'line', 'pin', 'spring'.
* An appropriate render type will be automatically chosen unless one is given in options.
*
Expand Down Expand Up @@ -450,7 +454,7 @@ var Common = require('../core/Common');
*/

/**
* A `Number` that specifies the damping of the constraint,
* A `Number` that specifies the damping of the constraint,
* i.e. the amount of resistance applied to each body based on their velocities to limit the amount of oscillation.
* Damping will only be apparent when the constraint also has a very low `stiffness`.
* A value of `0.1` means the constraint will apply heavy damping, resulting in little to no oscillation.
Expand All @@ -462,7 +466,7 @@ var Common = require('../core/Common');
*/

/**
* A `Number` that specifies the target resting length of the constraint.
* A `Number` that specifies the target resting length of the constraint.
* It is calculated automatically in `Constraint.create` from initial positions of the `constraint.bodyA` and `constraint.bodyB`.
*
* @property length
Expand Down