Skip to content

Commit

Permalink
fixes for compound bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Feb 4, 2015
1 parent 10e5d0f commit 87f9065
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
20 changes: 15 additions & 5 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ var Body = {};
Vertices.rotate(part.vertices, delta, body.position);
Axes.rotate(part.axes, delta);
Bounds.update(part.bounds, part.vertices, body.velocity);
if (i > 0) {
Vector.rotateAbout(part.position, delta, body.position, part.position);
}
}
};

Expand Down Expand Up @@ -528,16 +531,23 @@ var Body = {};
// transform the body geometry
for (var i = 0; i < body.parts.length; i++) {
var part = body.parts[i];

Vertices.translate(part.vertices, body.velocity);
if (body.angularVelocity !== 0) {
Vertices.rotate(part.vertices, body.angularVelocity, body.position);
Axes.rotate(part.axes, body.angularVelocity);
}
Bounds.update(part.bounds, body.vertices, body.velocity);

if (i > 0) {
part.position.x += body.velocity.x;
part.position.y += body.velocity.y;
}

if (body.angularVelocity !== 0) {
Vertices.rotate(part.vertices, body.angularVelocity, body.position);
Axes.rotate(part.axes, body.angularVelocity);
if (i > 0) {
Vector.rotateAbout(part.position, body.angularVelocity, body.position, part.position);
}
}

Bounds.update(part.bounds, part.vertices, body.velocity);
}
};

Expand Down
18 changes: 7 additions & 11 deletions src/collision/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,18 @@ var Resolver = {};
var body = bodies[i];

if (body.positionImpulse.x !== 0 || body.positionImpulse.y !== 0) {
// move the body without changing velocity
body.position.x += body.positionImpulse.x;
body.position.y += body.positionImpulse.y;
body.positionPrev.x += body.positionImpulse.x;
body.positionPrev.y += body.positionImpulse.y;

// update body geometry
for (var j = 0; j < body.parts.length; j++) {
var part = body.parts[j];
Vertices.translate(part.vertices, body.positionImpulse);
Bounds.update(part.bounds, body.vertices, body.velocity);
if (j > 0) {
part.position.x += body.positionImpulse.x;
part.position.y += body.positionImpulse.y;
}
Bounds.update(part.bounds, part.vertices, body.velocity);
part.position.x += body.positionImpulse.x;
part.position.y += body.positionImpulse.y;
}

// move the body without changing velocity
body.positionPrev.x += body.positionImpulse.x;
body.positionPrev.y += body.positionImpulse.y;

// dampen accumulator to warm the next step
body.positionImpulse.x *= _positionWarming;
Expand Down
16 changes: 10 additions & 6 deletions src/constraint/Constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,24 @@ var Constraint = {};
// 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) {
part.position.x += impulse.x;
part.position.y += impulse.y;
}

if (impulse.angle !== 0) {
Vertices.rotate(part.vertices, impulse.angle, body.position);
Axes.rotate(part.axes, impulse.angle);
if (j > 0) {
Vector.rotateAbout(part.position, impulse.angle, body.position, part.position);
}
impulse.angle = 0;
}

Bounds.update(part.bounds, body.vertices);

if (j > 0) {
part.position.x += impulse.x;
part.position.y += impulse.y;
}
Bounds.update(part.bounds, part.vertices);
}

impulse.x = 0;
Expand Down
12 changes: 7 additions & 5 deletions src/geometry/Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ var Vector = {};
* @param {vector} vector
* @param {number} angle
* @param {vector} point
* @param {vector} [output]
* @return {vector} A new vector rotated about the point
*/
Vector.rotateAbout = function(vector, angle, point) {
Vector.rotateAbout = function(vector, angle, point, output) {
var cos = Math.cos(angle), sin = Math.sin(angle);
return {
x: point.x + ((vector.x - point.x) * cos - (vector.y - point.y) * sin),
y: point.y + ((vector.x - point.x) * sin + (vector.y - point.y) * cos)
};
if (!output) output = {};
var x = point.x + ((vector.x - point.x) * cos - (vector.y - point.y) * sin);
output.y = point.y + ((vector.x - point.x) * sin + (vector.y - point.y) * cos);
output.x = x;
return output;
};

/**
Expand Down

0 comments on commit 87f9065

Please sign in to comment.