0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-03-14 00:38:41 -04:00

adds simple stress tracking to bodies

This commit is contained in:
Ryan Slack 2015-05-01 16:50:03 -06:00
parent f4f3bf7c48
commit b179d796c9
4 changed files with 45 additions and 3 deletions

View file

@ -43,6 +43,7 @@ var Body = {};
angularSpeed: 0,
velocity: { x: 0, y: 0 },
angularVelocity: 0,
stress: { x: 0, y: 0 },
isStatic: false,
isSleeping: false,
motion: 0,
@ -429,6 +430,10 @@ var Body = {};
body.position.x += body.velocity.x;
body.position.y += body.velocity.y;
// set stress due to forces
body.stress.x = Math.abs(body.force.x * (deltaTime * timeScale * body.timeScale));
body.stress.y = Math.abs(body.force.y * (deltaTime * timeScale * body.timeScale));
// update angular velocity with verlet integration
body.angularVelocity = ((body.angle - body.anglePrev) * frictionAir * correction) + (body.torque / body.inertia) * deltaTimeSquared;
body.anglePrev = body.angle;

View file

@ -156,6 +156,9 @@ var Resolver = {};
bodyA.positionPrev.x += impulse.x * bodyA.inverseMass;
bodyA.positionPrev.y += impulse.y * bodyA.inverseMass;
bodyA.anglePrev += Vector.cross(offset, impulse) * bodyA.inverseInertia;
bodyA.stress.x += Math.abs(impulse.x);
bodyA.stress.y += Math.abs(impulse.y);
}
if (!(bodyB.isStatic || bodyB.isSleeping)) {
@ -163,6 +166,9 @@ var Resolver = {};
bodyB.positionPrev.x -= impulse.x * bodyB.inverseMass;
bodyB.positionPrev.y -= impulse.y * bodyB.inverseMass;
bodyB.anglePrev -= Vector.cross(offset, impulse) * bodyB.inverseInertia;
bodyB.stress.x += Math.abs(impulse.x);
bodyB.stress.y += Math.abs(impulse.y);
}
}
}
@ -269,4 +275,4 @@ var Resolver = {};
}
};
})();
})();

View file

@ -213,6 +213,9 @@ var Constraint = {};
bodyA.position.x -= force.x;
bodyA.position.y -= force.y;
bodyA.angle += torque;
bodyA.stress.x += Math.abs(force.x);
bodyA.stress.y += Math.abs(force.y);
}
if (bodyB && !bodyB.isStatic) {
@ -233,6 +236,9 @@ var Constraint = {};
bodyB.position.x += force.x;
bodyB.position.y += force.y;
bodyB.angle -= torque;
bodyB.stress.x += Math.abs(force.x);
bodyB.stress.y += Math.abs(force.y);
}
};
@ -376,4 +382,4 @@ var Constraint = {};
* @type number
*/
})();
})();

View file

@ -46,7 +46,8 @@ var Render = {};
showPositions: false,
showAngleIndicator: false,
showIds: false,
showShadows: false
showShadows: false,
showStress: false
}
};
@ -193,6 +194,9 @@ var Render = {};
if (options.showVelocity)
Render.bodyVelocity(engine, bodies, context);
if (options.showStress)
Render.bodyStress(engine, bodies, context);
if (options.showIds)
Render.bodyIds(engine, bodies, context);
@ -649,6 +653,27 @@ var Render = {};
}
};
Render.bodyStress = function(engine, bodies, context) {
var c = context;
for (var i = 0; i < bodies.length; i++) {
var body = bodies[i];
if (!body.render.visible)
continue;
c.beginPath();
c.moveTo(body.position.x + body.stress.x*4, body.position.y);
c.lineTo(body.position.x, body.position.y);
c.lineTo(body.position.x, body.position.y - body.stress.y*4);
c.lineWidth = 1;
c.strokeStyle = 'teal';
c.stroke();
}
};
/**
* Description
* @private