Skip to content

Commit

Permalink
Fixed #4
Browse files Browse the repository at this point in the history
Moved math related utility functions to a new Math.Utils object.
  • Loading branch information
ls-yannick committed Jan 3, 2013
1 parent 85045c8 commit 8d60e8b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 70 deletions.
2 changes: 1 addition & 1 deletion lib/SAT.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Cannon.SAT.CollisionSolver.solveCircleCircleCollision = function(circle1, circle
var result = new Cannon.SAT.CollisionResult(circle1, circle2);

var radiis = circle1.radius+circle2.radius;
var distance = Cannon.Utils.distance(circle1, circle2);
var distance = Cannon.Math.Utils.distance(circle1, circle2);
if (distance > radiis){
result.colliding = false;
}
Expand Down
127 changes: 65 additions & 62 deletions lib/cannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,67 +161,6 @@ Cannon.Utils = {
return child instanceof parent;
},
/**
Computes a random value between 2 values (included)
@param (number) min lower treshhold touse
@param (number) max upper treshhold touse
@type float
*/
randomIn: function(min, max){
return min+(max-min)*Math.random();
},
/**
Converts a hexadecimal value into a decimal one
@param (number) hexadecimal value
@return decimal value
@type int
*/
hexToDec: function(value){
return parseInt(value, 16);
},
/**
Converts a decimal value into a hexadecimal one
@param (number) decimal value
@return hexadecimal value
@type string
*/
decToHex: function(value){
return parseInt(value).toString(16).toUpperCase();
},
/**
Converst a radian value into degrees
@param (number) value to be converted
@return value in degrees
^type number
*/
radiansToDegrees: function(value){
return value*180/Math.PI;
},
/**
Converst a value in degrees into radians
@param (number) value to be converted
@return value in radians
^type number
*/
degreesToRadians: function(value){
return value*Math.PI/180;
},
/**
Computes the distance between two points using Pythagora. Alternatively, the 2 first arguments can be points with x and y properties
@param (number | point) x1 or first point
@param (number | point) y1 or seco,d point
@param (point) x2
@param (point) y2
@type number
*/
distance: function(x1, y1, x2, y2){
if (arguments.length == 4){
return Math.sqrt((Math.pow((x2-x1), 2))+(Math.pow((y2-y1), 2)))
}
else if (arguments.length == 2){
return Math.sqrt((Math.pow((arguments[1].x-arguments[0].x), 2))+(Math.pow((arguments[1].y-arguments[0].y), 2)))
}
},
/**
Removes the first occurence of a value in the given array
@param (array) array
@param (mixed) value value to be removed
Expand Down Expand Up @@ -1283,7 +1222,7 @@ Cannon.DisplayObject = Cannon.ChildSystem.extend({
ctx.shadowColor = this.shadowColor;

//enforce rotation
this.radians = Cannon.Utils.degreesToRadians(this.rotation);
this.radians = Cannon.Math.Utils.degreesToRadians(this.rotation);

//@FIXME
var fillStyle;
Expand Down Expand Up @@ -1348,6 +1287,70 @@ Cannon.registerPackage('Math');
/**
A simple mathematical matrix
*/
Cannon.Math.Utils= {
/**
Computes a random value between 2 values (included)
@param (number) min lower treshhold touse
@param (number) max upper treshhold touse
@type float
*/
randomIn: function(min, max){
return min+(max-min)*Math.random();
},
/**
Converts a hexadecimal value into a decimal one
@param (number) hexadecimal value
@return decimal value
@type int
*/
hexToDec: function(value){
return parseInt(value, 16);
},
/**
Converts a decimal value into a hexadecimal one
@param (number) decimal value
@return hexadecimal value
@type string
*/
decToHex: function(value){
return parseInt(value).toString(16).toUpperCase();
},
/**
Converst a radian value into degrees
@param (number) value to be converted
@return value in degrees
^type number
*/
radiansToDegrees: function(value){
return value*180/Math.PI;
},
/**
Converst a value in degrees into radians
@param (number) value to be converted
@return value in radians
^type number
*/
degreesToRadians: function(value){
return value*Math.PI/180;
},
/**
Computes the distance between two points using Pythagora. Alternatively, the 2 first arguments can be points with x and y properties
@param (number | point) x1 or first point
@param (number | point) y1 or seco,d point
@param (point) x2
@param (point) y2
@type number
*/
distance: function(x1, y1, x2, y2){
if (arguments.length == 4){
return Math.sqrt((Math.pow((x2-x1), 2))+(Math.pow((y2-y1), 2)))
}
else if (arguments.length == 2){
return Math.sqrt((Math.pow((arguments[1].x-arguments[0].x), 2))+(Math.pow((arguments[1].y-arguments[0].y), 2)))
}
},
};

Cannon.Math.Matrix = Cannon.ClassFactory.extend({
/**
Creates a new matrix
Expand Down
2 changes: 1 addition & 1 deletion lib/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Cannon.Display.Circle = Cannon.DisplayObject.extend({
},
__isWithin: function(x, y){
var r = this.__getConcernedChilds(x-this.x, y-this.y);
if (Math.abs(Cannon.Utils.distance(x, y, this.x, this.y)) <= this.radius)
if (Math.abs(Cannon.Math.Utils.distance(x, y, this.x, this.y)) <= this.radius)
{
return [this].concat(r);;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ Cannon.Misc.Color = Cannon.ClassFactory.extend({
{
if (s.length == 7)
{
this.r = this.__colorify(Cannon.Utils.hexToDec(s.substring(1,3)));
this.g = this.__colorify(Cannon.Utils.hexToDec(s.substring(3,5)));
this.b = this.__colorify(Cannon.Utils.hexToDec(s.substring(5,7)));
this.r = this.__colorify(Cannon.Math.Utils.hexToDec(s.substring(1,3)));
this.g = this.__colorify(Cannon.Math.Utils.hexToDec(s.substring(3,5)));
this.b = this.__colorify(Cannon.Math.Utils.hexToDec(s.substring(5,7)));
this.a = 1;
}
else if (s.length == 4)
{
this.r = this.__colorify(Cannon.Utils.hexToDec(s.substring(1,2)+s.substring(1,2)));
this.g = this.__colorify(Cannon.Utils.hexToDec(s.substring(2,3)+s.substring(2,3)));
this.b = this.__colorify(Cannon.Utils.hexToDec(s.substring(3,4)+s.substring(3,4)));
this.r = this.__colorify(Cannon.Math.Utils.hexToDec(s.substring(1,2)+s.substring(1,2)));
this.g = this.__colorify(Cannon.Math.Utils.hexToDec(s.substring(2,3)+s.substring(2,3)));
this.b = this.__colorify(Cannon.Math.Utils.hexToDec(s.substring(3,4)+s.substring(3,4)));
this.a = 1;
}
}
Expand Down

0 comments on commit 8d60e8b

Please sign in to comment.