' @module BGE.Math
namespace BGE.Math
function Min(a as dynamic, b as dynamic) as dynamic
if a <= b
return a
end if
return b
end function
function Max(a as dynamic, b as dynamic) as dynamic
if a >= b
return a
end if
return b
end function
function Clamp(number as dynamic, minVal as dynamic, maxVal as dynamic) as dynamic
if number < minVal
return minVal
else if number > maxVal
return maxVal
else
return number
end if
end function
function PI() as float
return 3.1415926535897932384626433832795
end function
function Atan2(y as float, x as float) as float
piValue = PI()
if x > 0
angle = Atn(y / x)
else if y >= 0 and x < 0
angle = Atn(y / x) + pi
else if y < 0 and x < 0
angle = Atn(y / x) - pi
else if y > 0 and x = 0
angle = piValue / 2
else if y < 0 and x = 0
angle = (piValue / 2) * -1
else
angle = 0
end if
return angle
end function
function IsIntegerEven(number as integer) as boolean
return (number MOD 2 = 0)
end function
function IsIntegerOdd(number as integer) as boolean
return (number MOD 2 <> 0)
end function
function Power(number as dynamic, pow as integer) as dynamic
n = 1
for i = 0 to pow - 1
n *= number
end for
return n
end function
function Round(number as float, decimals = 0 as integer) as float
if 0 = decimals
return cint(number)
else
magnitude = Power(10, decimals)
return cint(number * magnitude) / magnitude
end if
end function
function DegreesToRadians(degrees as float) as float
return (degrees / 180) * PI()
end function
function RadiansToDegrees(radians as float) as float
return (180 / PI()) * radians
end function
function RandomRange(lowest_int as integer, highest_int as integer) as integer
return rnd(highest_int - (lowest_int - 1)) + (lowest_int - 1)
end function
function RotateVectorAroundVector(vector1 as object, vector2 as object, radians as float) as object
v = new Vector2d(vector1.x, vector1.y)
s = sin(radians)
c = cos(radians)
v.x -= vector2.x
v.y -= vector2.y
new_x = v.x * c + v.y * s
new_y = -v.x * s + v.y * c
v.x = new_x + vector2.x
v.y = new_y + vector2.y
return v
end function
function TotalDistance(vector1 as object, vector2 as object) as float
x_distance = vector1.x - vector2.x
y_distance = vector1.y - vector2.y
total_distance = Sqr(x_distance * x_distance + y_distance * y_distance)
return total_distance
end function
function GetAngle(vector1 as object, vector2 as object) as float
x_distance = vector1.x - vector2.x
y_distance = vector1.y - vector2.y
return Atan2(y_distance, x_distance) + PI()
end function
class Rectangle
x as float = 0
y as float = 0
width as float = 0
height as float = 0
function new(x as float, y as float, width as float, height as float) as void
m.x = x
m.y = y
m.width = width
m.height = height
end function
function right() as float
return m.x + m.width
end function
function left() as float
return m.x
end function
function top() as float
return m.y
end function
function bottom() as float
return m.y + m.height
end function
function center() as object
return {x: m.x + m.width / 2, y: m.y + m.height / 2}
end function
function copy() as object
return new Rectangle(m.x, m.y, m.width, m.height)
end function
end class
class Circle
x as float
y as float
radius as float
function new(x as float, y as float, radius as float) as void
m.x = x
m.y = y
m.radius = radius
end function
end class
class Vector2d
x as float
y as float
function new(x = 0 as float, y = 0 as float) as void
m.x = x
m.y = y
end function
end class
end namespace