0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-30 10:20:52 -05:00

refactor body base delta constant

This commit is contained in:
liabru 2022-09-02 20:29:35 +01:00
parent dc46472720
commit 66e291b57d

View file

@ -25,25 +25,7 @@ var Axes = require('../geometry/Axes');
Body._nextCollidingGroupId = 1; Body._nextCollidingGroupId = 1;
Body._nextNonCollidingGroupId = -1; Body._nextNonCollidingGroupId = -1;
Body._nextCategory = 0x0001; Body._nextCategory = 0x0001;
Body._baseDelta = 1000 / 60;
/**
* The constant unit of time in milliseconds in relation to getting or setting body
* speed, velocity or force related properties or methods (except friction related).
*
* This defaults to `1000 / 60` milliseconds.
* To use 1 second (as in the [SI base time unit](https://en.wikipedia.org/wiki/SI_base_unit)) then set this value to `1000`.
*
* _Note about the default:_ For ease of use in browser games and compatibility with previous versions, the current default time unit
* is chosen so that with the default engine delta `1000 / 60` milliseconds and default rendering at _1:1_, velocity matches _pixels_ per _frame_.
*
* If your project requires real-world metrics or a different engine delta, consider using SI units shown above instead of this default.
*
* @static
* @property timeUnit
* @type number
* @default 1000 / 60
*/
Body.timeUnit = 1000 / 60;
/** /**
* Creates a new rigid body model. The options parameter is an object that specifies any properties you wish to override the defaults. * Creates a new rigid body model. The options parameter is an object that specifies any properties you wish to override the defaults.
@ -551,7 +533,7 @@ var Axes = require('../geometry/Axes');
* @param {vector} velocity * @param {vector} velocity
*/ */
Body.setVelocity = function(body, velocity) { Body.setVelocity = function(body, velocity) {
var timeScale = body.deltaTime / Body.timeUnit; var timeScale = body.deltaTime / Body._baseDelta;
body.positionPrev.x = body.position.x - velocity.x * timeScale; body.positionPrev.x = body.position.x - velocity.x * timeScale;
body.positionPrev.y = body.position.y - velocity.y * timeScale; body.positionPrev.y = body.position.y - velocity.y * timeScale;
body.velocity.x = (body.position.x - body.positionPrev.x) / timeScale; body.velocity.x = (body.position.x - body.positionPrev.x) / timeScale;
@ -566,7 +548,7 @@ var Axes = require('../geometry/Axes');
* @return {vector} velocity * @return {vector} velocity
*/ */
Body.getVelocity = function(body) { Body.getVelocity = function(body) {
var timeScale = Body.timeUnit / body.deltaTime; var timeScale = Body._baseDelta / body.deltaTime;
return { return {
x: (body.position.x - body.positionPrev.x) * timeScale, x: (body.position.x - body.positionPrev.x) * timeScale,
@ -604,7 +586,7 @@ var Axes = require('../geometry/Axes');
* @param {number} velocity * @param {number} velocity
*/ */
Body.setAngularVelocity = function(body, velocity) { Body.setAngularVelocity = function(body, velocity) {
var timeScale = body.deltaTime / Body.timeUnit; var timeScale = body.deltaTime / Body._baseDelta;
body.anglePrev = body.angle - velocity * timeScale; body.anglePrev = body.angle - velocity * timeScale;
body.angularVelocity = (body.angle - body.anglePrev) / timeScale; body.angularVelocity = (body.angle - body.anglePrev) / timeScale;
body.angularSpeed = Math.abs(body.angularVelocity); body.angularSpeed = Math.abs(body.angularVelocity);
@ -617,7 +599,7 @@ var Axes = require('../geometry/Axes');
* @return {number} angular velocity * @return {number} angular velocity
*/ */
Body.getAngularVelocity = function(body) { Body.getAngularVelocity = function(body) {
return (body.angle - body.anglePrev) * Body.timeUnit / body.deltaTime; return (body.angle - body.anglePrev) * Body._baseDelta / body.deltaTime;
}; };
/** /**
@ -802,12 +784,12 @@ var Axes = require('../geometry/Axes');
}; };
/** /**
* Updates properties `body.velocity`, `body.speed`, `body.angularVelocity` and `body.angularSpeed` which are normalised in relation to `Body.timeUnit`. * Updates properties `body.velocity`, `body.speed`, `body.angularVelocity` and `body.angularSpeed` which are normalised in relation to `Body._baseDelta`.
* @method updateVelocities * @method updateVelocities
* @param {body} body * @param {body} body
*/ */
Body.updateVelocities = function(body) { Body.updateVelocities = function(body) {
var timeScale = Body.timeUnit / body.deltaTime, var timeScale = Body._baseDelta / body.deltaTime,
bodyVelocity = body.velocity; bodyVelocity = body.velocity;
bodyVelocity.x = (body.position.x - body.positionPrev.x) * timeScale; bodyVelocity.x = (body.position.x - body.positionPrev.x) * timeScale;
@ -831,7 +813,7 @@ var Axes = require('../geometry/Axes');
* @param {vector} force * @param {vector} force
*/ */
Body.applyForce = function(body, position, force) { Body.applyForce = function(body, position, force) {
var timeScale = body.deltaTime / Body.timeUnit; var timeScale = body.deltaTime / Body._baseDelta;
body.force.x += force.x / timeScale; body.force.x += force.x / timeScale;
body.force.y += force.y / timeScale; body.force.y += force.y / timeScale;
var offset = { x: position.x - body.position.x, y: position.y - body.position.y }; var offset = { x: position.x - body.position.x, y: position.y - body.position.y };