Modified rmgen GetAngle function to use atan2 and the standard mathematical angle convention (WARNING this changes return values so will break scripts using the old version) and changed getGradient so it returns Infinity for infinite gradient lines. Added errors if people try and use unsafe javascript maths functions.

This was SVN commit r11363.
This commit is contained in:
Jonathan Waller 2012-03-18 18:40:27 +00:00
parent b72d72dec2
commit 7c51e743fa
2 changed files with 30 additions and 23 deletions

View File

@ -38,12 +38,16 @@ Math.atan = function(a)
sign = -1;
a *= -1;
}
if (a > 1){
if (a > 1)
{
// tan(pi/2 - x) = 1/tan(x)
inverted = true;
a = 1/a;
}
if (a > tanPiBy12){
if (a > tanPiBy12)
{
// tan(x-pi/6) = (tan(x) - tan(pi/6)) / (1 + tan(pi/6)tan(x))
tanPiBy6Shift = Math.PI/6;
a = (a - tanPiBy6) / (1 + tanPiBy6*a);
@ -113,3 +117,18 @@ Math.atan2 = function(y,x)
return r;
}
};
Math.acos = function()
{
error("Math.acos() does not have a synchronization safe implementation");
};
Math.asin = function()
{
error("Math.asin() does not have a synchronization safe implementation");
};
Math.tan = function()
{
error("Math.tan() does not have a synchronization safe implementation");
};

View File

@ -51,11 +51,6 @@ function sin(x)
return Math.sin(x);
}
function tan(x)
{
return Math.tan(x);
}
function abs(x) {
return Math.abs(x);
}
@ -613,31 +608,24 @@ function checkIfInClass(x, z, id)
}
// Function to get the distance between 2 points
// Returns the distance between 2 points
function getDistance(x1, z1, x2, z2)
{
return Math.pow(Math.pow(x1 - x2, 2) + Math.pow(z1 - z2, 2), 1/2)
return Math.pow(Math.pow(x1 - x2, 2) + Math.pow(z1 - z2, 2), 1/2);
}
// Returns the abgle between two points in radians. --Warning:This can cause sync problems in cross-platform multiplayer games--
// Returns the angle of the vector between point 1 and point 2. The angle is anticlockwise from the positive x axis.
function getAngle(x1, z1, x2, z2)
{
var vector = [x2 - x1, z2 - z1];
var output = 0;
if (vector[0] !== 0 || vector[1] !== 0)
{
var output = Math.acos(vector[0]/getDistance(x1, z1, x2, z2));
if (vector[1] > 0) {output = PI + (PI - Math.acos(vector[0]/getDistance(x1, z1, x2, z2)))};
};
return (output + PI/2) % (2*PI);
};
return output = Math.atan2(z2 - z1, x2 - x1);
}
// Returns the tangent of angle between the line that is created by two points and the X+ axis.
function getDirection(x1, z1, x2, z2)
// Returns the gradient of the line between point 1 and 2 in the form dz/dx
function getGradient(x1, z1, x2, z2)
{
if (x1 == x2)
if (x1 == x2 && z1 == z2)
{
return 100000;
return 0;
}
else
{