Fix math package and improve docstrings

This commit is contained in:
Moritz Brückner 2020-02-19 14:24:31 +01:00
parent ee0b948961
commit 486b462153
2 changed files with 23 additions and 13 deletions

View file

@ -1,5 +1,7 @@
package armory.math;
import iron.math.Vec4;
class Helper {
/**
@ -31,7 +33,7 @@ class Helper {
return Math.PI / 180 * degrees;
}
/**
* rounds the precision of a float (default 2).
* Rounds the precision of a float (default 2).
* @return float with rounded precision
*/
public static function roundfp(f: Float, precision = 2): Float {
@ -39,16 +41,23 @@ class Helper {
return std.Math.round(f) / std.Math.pow(10, precision);
}
/**
* clamps a float within some limits.
* Clamps a float within some limits.
* @return same float, min or max if exceeded limits.
*/
public static function clamp(f: Float, min: Float, max: Float): Float {
return f < min ? min : f > max ? max : f;
}
/*
* Convenience function to map a variable from one coordinate space
* to another. Equivalent to unlerp() followed by lerp().
*/
/**
* Convenience function to map a variable from one coordinate space to
* another. Equivalent to unlerp() followed by lerp().
* @param value
* @param leftMin The lower bound of the input coordinate space
* @param leftMax The higher bound of the input coordinate space
* @param rightMin The lower bound of the output coordinate space
* @param rightMax The higher bound of the output coordinate space
* @return Float
*/
public static inline function map(value: Float, leftMin: Float, leftMax: Float, rightMin: Float, rightMax: Float): Float {
return rightMin + (value - leftMin) / (leftMax - leftMin) * (rightMax- rightMin);
}

View file

@ -1,6 +1,7 @@
package armory.math;
import kha.FastFloat;
import iron.math.Mat4;
class Rotator {
public var pitch: FastFloat; // X - look up or down around the X axis
@ -14,16 +15,16 @@ class Rotator {
}
public function toDegrees(): Rotator {
pitch = Math.toDegrees(pitch);
roll = Math.toDegrees(roll);
yaw = Math.toDegrees(yaw);
pitch = Helper.radToDeg(pitch);
roll = Helper.radToDeg(roll);
yaw = Helper.radToDeg(yaw);
return this;
}
public function toRadians(): Rotator {
pitch = Math.toRadians(pitch);
roll = Math.toRadians(roll);
yaw = Math.toRadians(yaw);
pitch = Helper.degToRad(pitch);
roll = Helper.degToRad(roll);
yaw = Helper.degToRad(yaw);
return this;
}
@ -182,7 +183,7 @@ class Rotator {
}
public static inline function clampAxis(angle: FastFloat): FastFloat {
angle = Math.mod(angle, 360); // Makes the angle between -360 and +360
angle = angle % 360; // Makes the angle between -360 and +360
if (angle < 0.0) angle += 360.0;
return angle;
}