Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,121 @@
package leenkx.math;
import kha.FastFloat;
import iron.math.Vec4;
class Helper {
/**
Returns angle in radians between 2 vectors perpendicular to the z axis.
**/
public static function getAngle(va: Vec4, vb: Vec4): Float {
var vn = Vec4.zAxis();
var dot = va.dot(vb);
var det = va.x * vb.y * vn.z +
vb.x * vn.y * va.z +
vn.x * va.y * vb.z -
va.z * vb.y * vn.x -
vb.z * vn.y * va.x -
vn.z * va.y * vb.x;
return Math.atan2(det, dot);
}
/**
Returns a copy of the current vector summed by delta towards the target vector without passing it.
**/
public static function moveTowards(current: Vec4, target: Vec4, delta: FastFloat): Vec4 {
var v1 = current.clone();
var v2 = target.clone();
var diff = v2.clone().sub(v1);
var length = diff.length();
if (length <= delta || length == 0.0) v1.setFrom(v2);
else v1.add(diff.mult(1.0 / length * delta));
return v1;
}
/**
Converts an angle in radians to degrees.
@return angle in degrees
**/
inline public static function radToDeg(radians: Float): Float {
return 180 / Math.PI * radians;
}
/**
Converts an angle in degrees to radians.
@return angle in radians
**/
inline public static function degToRad(degrees: Float): Float {
return Math.PI / 180 * degrees;
}
/**
Rounds the precision of a float (default 2).
@return float with rounded precision
**/
public static function roundfp(f: Float, precision = 2): Float {
f *= std.Math.pow(10, precision);
return std.Math.round(f) / std.Math.pow(10, precision);
}
/**
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;
}
/**
Clamps an integer within some limits.
@return same integer, min or max if exceeded limits.
**/
public static function clampInt(f: Int, min: Int, max: Int): Int {
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().
@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);
}
public static inline function mapInt(value: Int, leftMin: Int, leftMax: Int, rightMin: Int, rightMax: Int): Int {
var result = Std.int(map(value, leftMin, leftMax, rightMin, rightMax));
return result;
}
public static inline function mapClamped(value: Float, leftMin: Float, leftMax: Float, rightMin: Float, rightMax: Float): Float {
if (value >= leftMax) return rightMax;
if (value <= leftMin) return rightMin;
return map(value, leftMin, leftMax, rightMin, rightMax);
}
/**
Return the sign of the given value represented as `1.0` (positive value)
or `-1.0` (negative value). The sign of `0` is `0`.
**/
public static inline function sign(value: Float): Float {
if (value == 0) return 0;
return (value < 0) ? -1.0 : 1.0;
}
/**
Return the base-2 logarithm of a number.
**/
public static inline function log2(v: Float): Float {
// 1.44269504089 = 1.0 / ln(2.0)
return Math.log(v) * 1.44269504089;
}
}

View File

@ -0,0 +1,208 @@
package leenkx.math;
import kha.FastFloat;
import iron.math.Mat4;
class Rotator {
public var pitch: FastFloat; // X - look up or down around the X axis
public var roll: FastFloat; // Y - axis tilt left or right around the Y axis
public var yaw: FastFloat; // Z - heading or facing angle around the Z (up) axis
public function new(pitch: FastFloat = 0.0, roll: FastFloat = 0.0, yaw: FastFloat = 0.0) {
this.pitch = pitch;
this.roll = roll;
this.yaw = yaw;
}
public function toDegrees(): Rotator {
pitch = Helper.radToDeg(pitch);
roll = Helper.radToDeg(roll);
yaw = Helper.radToDeg(yaw);
return this;
}
public function toRadians(): Rotator {
pitch = Helper.degToRad(pitch);
roll = Helper.degToRad(roll);
yaw = Helper.degToRad(yaw);
return this;
}
public function cross(r: Rotator): Rotator {
var x2 = roll * r.yaw - yaw * r.roll;
var y2 = yaw * r.pitch - pitch * r.yaw;
var z2 = pitch * r.roll - roll * r.pitch;
pitch = x2;
roll = y2;
yaw = z2;
return this;
}
public function crossvecs(a: Rotator, b: Rotator): Rotator {
var x2 = a.roll * b.yaw - a.yaw * b.roll;
var y2 = a.yaw * b.pitch - a.pitch * b.yaw;
var z2 = a.pitch * b.roll - a.roll * b.pitch;
pitch = x2;
roll = y2;
yaw = z2;
return this;
}
public function set(pitch: FastFloat, roll: FastFloat, yaw: FastFloat): Rotator{
this.pitch = pitch;
this.roll = roll;
this.yaw = yaw;
return this;
}
public function add(r: Rotator): Rotator {
pitch += r.pitch;
roll += r.roll;
yaw += r.yaw;
return this;
}
public function addf(pitch: FastFloat, roll: FastFloat, yaw: FastFloat): Rotator {
this.pitch += pitch;
this.roll += roll;
this.yaw += yaw;
return this;
}
public function addvecs(a: Rotator, b: Rotator): Rotator {
pitch = a.pitch + b.pitch;
roll = a.roll + b.roll;
yaw = a.yaw + b.yaw;
return this;
}
public function subvecs(a: Rotator, b: Rotator): Rotator {
pitch = a.pitch - b.pitch;
roll = a.roll - b.roll;
yaw = a.yaw - b.yaw;
return this;
}
public function normalize(): Rotator {
var n = length();
if (n > 0.0) {
var invN = 1.0 / n;
this.pitch *= invN; this.roll *= invN; this.yaw *= invN;
}
return this;
}
public function mult(f: FastFloat): Rotator {
pitch *= f; roll *= f; yaw *= f;
return this;
}
public function dot(r: Rotator): FastFloat {
return pitch * r.pitch + roll * r.roll + yaw * r.yaw;
}
public function setFrom(r: Rotator): Rotator {
pitch = r.pitch; roll = r.roll; yaw = r.yaw;
return this;
}
public function clone(): Rotator {
return new Rotator(pitch, roll, yaw);
}
public static function lerp(from: Rotator, to: Rotator, s: FastFloat): Rotator {
var target = new Rotator();
target.pitch = from.pitch + (to.pitch - from.pitch) * s;
target.roll = from.roll + (to.roll - from.roll) * s;
target.yaw = from.yaw + (to.yaw - from.yaw) * s;
return target;
}
public function applyproj(m: Mat4): Rotator {
var pitch = this.pitch; var roll = this.roll; var yaw = this.yaw;
// Perspective divide
var d = 1.0 / (m._03 * pitch + m._13 * roll + m._23 * yaw + m._33);
this.pitch = (m._00 * pitch + m._10 * roll + m._20 * yaw + m._30) * d;
this.roll = (m._01 * pitch + m._11 * roll + m._21 * yaw + m._31) * d;
this.yaw = (m._02 * pitch + m._12 * roll + m._22 * yaw + m._32) * d;
return this;
}
public function applymat(m: Mat4): Rotator {
var pitch = this.pitch; var roll = this.roll; var yaw = this.yaw;
this.pitch = m._00 * pitch + m._10 * roll + m._20 * yaw + m._30;
this.roll = m._01 * pitch + m._11 * roll + m._21 * yaw + m._31;
this.yaw = m._02 * pitch + m._12 * roll + m._22 * yaw + m._32;
return this;
}
public inline function equals(r: Rotator): Bool {
return pitch == r.pitch && roll == r.roll && yaw == r.yaw;
}
public inline function length(): FastFloat {
return Math.sqrt(pitch * pitch + roll * roll + yaw * yaw);
}
public inline function normalizeTo(newLength: FastFloat): Rotator {
var v = normalize();
v = mult(newLength);
return v;
}
public function sub(r: Rotator): Rotator {
pitch -= r.pitch; roll -= r.roll; yaw -= r.yaw;
return this;
}
public static inline function distance(r1: Rotator, r2: Rotator): FastFloat {
return distancef(r1.pitch, r1.roll, r1.yaw, r2.pitch, r2.roll, r2.yaw);
}
public static inline function distancef(r1pitch: FastFloat, r1roll: FastFloat, r1yaw: FastFloat, r2pitch: FastFloat, r2roll: FastFloat, r2yaw: FastFloat): FastFloat {
var pitch = r1pitch - r2pitch;
var roll = r1roll - r2roll;
var yaw = r1yaw - r2yaw;
return Math.sqrt(pitch * pitch + roll * roll + yaw * yaw);
}
public function distanceTo(r: Rotator): FastFloat {
return Math.sqrt((r.pitch - pitch) * (r.pitch - pitch) + (r.roll - roll) * (r.roll - roll) + (r.yaw - yaw) * (r.yaw - yaw));
}
public function clamp(): Rotator {
this.pitch = clampAxis(this.pitch);
this.roll = clampAxis(this.roll);
this.yaw = clampAxis(this.yaw);
return this;
}
public static inline function clampAxis(angle: FastFloat): FastFloat {
angle = angle % 360; // Makes the angle between -360 and +360
if (angle < 0.0) angle += 360.0;
return angle;
}
public static function xAxis(): Rotator { return new Rotator(1.0, 0.0, 0.0); }
public static function yAxis(): Rotator { return new Rotator(0.0, 1.0, 0.0); }
public static function zAxis(): Rotator { return new Rotator(0.0, 0.0, 1.0); }
public static function one(): Rotator { return new Rotator(1.0, 1.0, 1.0); }
public static function zero(): Rotator { return new Rotator(0.0, 0.0, 0.0); }
public static function back(): Rotator { return new Rotator(0.0, -1.0, 0.0); }
public static function forward(): Rotator { return new Rotator(0.0, 1.0, 0.0); }
public static function down(): Rotator { return new Rotator(0.0, 0.0, -1.0); }
public static function up(): Rotator { return new Rotator(0.0, 0.0, 1.0); }
public static function left(): Rotator { return new Rotator(-1.0, 0.0, 0.0); }
public static function right(): Rotator { return new Rotator(1.0, 0.0, 0.0); }
public static function negativeInfinity(): Rotator { return new Rotator(Math.NEGATIVE_INFINITY, Math.NEGATIVE_INFINITY, Math.NEGATIVE_INFINITY); }
public static function positiveInfinity(): Rotator { return new Rotator(Math.POSITIVE_INFINITY, Math.POSITIVE_INFINITY, Math.POSITIVE_INFINITY); }
public function toString(): String {
return "(" + this.pitch + ", " + this.roll + ", " + this.yaw + ")";
}
}