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

changed Math.random to Common.random and use a seeded RNG

This commit is contained in:
liabru 2014-05-10 15:01:35 +01:00
parent 3043baa53c
commit 0a46cb248b
3 changed files with 32 additions and 18 deletions

View file

@ -96,7 +96,7 @@
// round the edges of some bodies
var chamfer = null;
if (sides > 2 && Math.random() > 0.7) {
if (sides > 2 && Common.random() > 0.7) {
chamfer = {
radius: 10
};
@ -104,7 +104,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(25, 50), Common.random(25, 50), { chamfer: chamfer });
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(25, 30), { chamfer: chamfer });
@ -243,7 +243,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50));
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30));
@ -365,7 +365,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50));
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30));
@ -523,7 +523,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50));
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30));
@ -551,7 +551,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50));
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30));
@ -580,7 +580,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50));
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30));
@ -647,8 +647,8 @@
var forceMagnitude = 0.04 * body.mass;
Body.applyForce(body, { x: 0, y: 0 }, {
x: (forceMagnitude + Math.random() * forceMagnitude) * Common.choose([1, -1]),
y: -forceMagnitude + Math.random() * -forceMagnitude
x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]),
y: -forceMagnitude + Common.random() * -forceMagnitude
});
}
}
@ -699,7 +699,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50), bodyOptions);
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30), bodyOptions);
@ -955,8 +955,8 @@
var forceMagnitude = 0.01 * body.mass;
Body.applyForce(body, { x: 0, y: 0 }, {
x: (forceMagnitude + Math.random() * forceMagnitude) * Common.choose([1, -1]),
y: -forceMagnitude + Math.random() * -forceMagnitude
x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]),
y: -forceMagnitude + Common.random() * -forceMagnitude
});
}
}
@ -1069,7 +1069,7 @@
]);
var stack = Composites.stack(20, 20, 15, 4, 0, 0, function(x, y, column, row) {
if (Math.random() > 0.35) {
if (Common.random() > 0.35) {
return Bodies.rectangle(x, y, 64, 64, {
render: {
strokeStyle: '#ffffff',
@ -1110,7 +1110,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50));
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30));
@ -1275,6 +1275,9 @@
// reset id pool
Common._nextId = 0;
// reset random seed
Common._seed = 0;
// reset mouse offset and scale (only required for Demo.views)
Mouse.setScale(_engine.input.mouse, { x: 1, y: 1 });
Mouse.setOffset(_engine.input.mouse, { x: 0, y: 0 });

View file

@ -66,7 +66,7 @@
switch (Math.round(Common.random(0, 1))) {
case 0:
if (Math.random() < 0.8) {
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 40), Common.random(20, 40), { friction: 0.01, restitution: 0.4 });
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30), { friction: 0.01, restitution: 0.4 });
@ -139,6 +139,8 @@
Demo.reset = function() {
var _world = _engine.world;
Common._seed = 2;
World.clear(_world);
Engine.clear(_engine);

View file

@ -9,6 +9,7 @@ var Common = {};
(function() {
Common._nextId = 0;
Common._seed = 0;
/**
* Description
@ -132,7 +133,7 @@ var Common = {};
*/
Common.shuffle = function(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var j = Math.floor(Common.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
@ -147,7 +148,7 @@ var Common = {};
* @return {object} A random choice object from the array
*/
Common.choose = function(choices) {
return choices[Math.floor(Math.random() * choices.length)];
return choices[Math.floor(Common.random() * choices.length)];
};
/**
@ -222,7 +223,9 @@ var Common = {};
* @return {number} A random number between min and max inclusive
*/
Common.random = function(min, max) {
return min + Math.random() * (max - min);
min = (typeof min !== "undefined") ? min : 0;
max = (typeof max !== "undefined") ? max : 1;
return min + _seededRandom() * (max - min);
};
/**
@ -278,4 +281,10 @@ var Common = {};
return Common._nextId++;
};
var _seededRandom = function() {
// https://gist.github.com/ngryman/3830489
Common._seed = (Common._seed * 9301 + 49297) % 233280;
return Common._seed / 233280;
};
})();