0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00

fixes for compound bodies

This commit is contained in:
liabru 2015-02-04 23:26:13 +00:00
parent 10e5d0fcd2
commit 87f90655b7
4 changed files with 41 additions and 29 deletions

View file

@ -402,6 +402,9 @@ var Body = {};
Vertices.rotate(part.vertices, delta, body.position); Vertices.rotate(part.vertices, delta, body.position);
Axes.rotate(part.axes, delta); Axes.rotate(part.axes, delta);
Bounds.update(part.bounds, part.vertices, body.velocity); Bounds.update(part.bounds, part.vertices, body.velocity);
if (i > 0) {
Vector.rotateAbout(part.position, delta, body.position, part.position);
}
} }
}; };
@ -528,16 +531,23 @@ var Body = {};
// transform the body geometry // transform the body geometry
for (var i = 0; i < body.parts.length; i++) { for (var i = 0; i < body.parts.length; i++) {
var part = body.parts[i]; var part = body.parts[i];
Vertices.translate(part.vertices, body.velocity); 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) { if (i > 0) {
part.position.x += body.velocity.x; part.position.x += body.velocity.x;
part.position.y += body.velocity.y; 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);
} }
}; };

View file

@ -91,23 +91,19 @@ var Resolver = {};
var body = bodies[i]; var body = bodies[i];
if (body.positionImpulse.x !== 0 || body.positionImpulse.y !== 0) { 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 // update body geometry
for (var j = 0; j < body.parts.length; j++) { for (var j = 0; j < body.parts.length; j++) {
var part = body.parts[j]; var part = body.parts[j];
Vertices.translate(part.vertices, body.positionImpulse); Vertices.translate(part.vertices, body.positionImpulse);
Bounds.update(part.bounds, body.vertices, body.velocity); Bounds.update(part.bounds, part.vertices, body.velocity);
if (j > 0) { part.position.x += body.positionImpulse.x;
part.position.x += body.positionImpulse.x; part.position.y += body.positionImpulse.y;
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 // dampen accumulator to warm the next step
body.positionImpulse.x *= _positionWarming; body.positionImpulse.x *= _positionWarming;
body.positionImpulse.y *= _positionWarming; body.positionImpulse.y *= _positionWarming;

View file

@ -251,20 +251,24 @@ var Constraint = {};
// update geometry and reset // update geometry and reset
for (var j = 0; j < body.parts.length; j++) { for (var j = 0; j < body.parts.length; j++) {
var part = body.parts[j]; var part = body.parts[j];
Vertices.translate(part.vertices, impulse); Vertices.translate(part.vertices, impulse);
if (impulse.angle !== 0) {
Vertices.rotate(part.vertices, impulse.angle, body.position);
Axes.rotate(part.axes, impulse.angle);
impulse.angle = 0;
}
Bounds.update(part.bounds, body.vertices);
if (j > 0) { if (j > 0) {
part.position.x += impulse.x; part.position.x += impulse.x;
part.position.y += impulse.y; 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, part.vertices);
} }
impulse.x = 0; impulse.x = 0;

View file

@ -77,14 +77,16 @@ var Vector = {};
* @param {vector} vector * @param {vector} vector
* @param {number} angle * @param {number} angle
* @param {vector} point * @param {vector} point
* @param {vector} [output]
* @return {vector} A new vector rotated about the point * @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); var cos = Math.cos(angle), sin = Math.sin(angle);
return { if (!output) output = {};
x: point.x + ((vector.x - point.x) * cos - (vector.y - point.y) * sin), var 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) output.y = point.y + ((vector.x - point.x) * sin + (vector.y - point.y) * cos);
}; output.x = x;
return output;
}; };
/** /**