UNPKG

planck-js

Version:

2D physics engine for JavaScript/HTML5 game development

94 lines (84 loc) 2.69 kB
/* * Copyright (c) 2016 Ali Shakiba http://shakiba.me/planck.js * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be * appreciated but is not required. * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. */ var create = require('../util/create'); var native = Math; var math = module.exports = create(native); math.EPSILON = 1e-9; // TODO /** * This function is used to ensure that a floating point number is not a NaN or * infinity. */ math.isFinite = function(x) { return (typeof x === 'number') && isFinite(x) && !isNaN(x); } math.Assert = function(x) { if (!math.isFinite(x)) { console.log(x); throw new Error('Invalid Number!'); } } /** * TODO: This is a approximate yet fast inverse square-root. */ math.invSqrt = function(x) { // TODO return 1 / native.sqrt(x); } /** * Next Largest Power of 2 Given a binary integer value x, the next largest * power of 2 can be computed by a SWAR algorithm that recursively "folds" the * upper bits into the lower bits. This process yields a bit vector with the * same most significant 1 as x, but all 1's below it. Adding 1 to that value * yields the next largest power of 2. For a 32-bit value: */ math.nextPowerOfTwo = function(x) { // TODO x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); x |= (x >> 8); x |= (x >> 16); return x + 1; } math.isPowerOfTwo = function(x) { return x > 0 && (x & (x - 1)) == 0; } math.mod = function(num, min, max) { if (typeof min === 'undefined') { max = 1, min = 0; } else if (typeof max === 'undefined') { max = min, min = 0; } if (max > min) { num = (num - min) % (max - min); return num + (num < 0 ? max : min); } else { num = (num - max) % (min - max); return num + (num <= 0 ? min : max); } }; math.clamp = function(num, min, max) { if (num < min) { return min; } else if (num > max) { return max; } else { return num; } };