tengri/Content/UE/MathLibrary.ts

217 lines
6.3 KiB
TypeScript

// Content/UE/SystemLibrary.ts
import { BlueprintFunctionLibrary } from '/Content/UE/BlueprintFunctionLibrary.ts';
import type { Color } from '/Content/UE/Color.ts';
import type { Float } from '/Content/UE/Float.ts';
import { LinearColor } from '/Content/UE/LinearColor.ts';
import { Vector } from '/Content/UE/Vector.ts';
/**
* System Library: Core Mathematical Functions
* Wrapper for JavaScript Math functions with UE5 compatibility
* Provides deterministic mathematical operations for movement calculations
*/
class MathLibraryClass extends BlueprintFunctionLibrary {
constructor(
outer: null | BlueprintFunctionLibrary = null,
name: string = 'MathLibrary'
) {
super(outer, name);
}
// ════════════════════════════════════════════════════════════════════════════════════════
// FUNCTIONS
// ════════════════════════════════════════════════════════════════════════════════════════
/**
* Calculate sine of angle in radians
* @param Value - Angle in radians
* @returns Sine value (-1 to 1)
*/
public Sin(Value: Float): Float {
return Math.sin(Value);
}
/**
* Calculate cosine of angle in radians
* @param Value - Angle in radians
* @returns Cosine value (-1 to 1)
*/
public Cos(Value: Float): Float {
return Math.cos(Value);
}
/**
* Color to LinearColor conversion
* @param color - Color with 0-255 RGBA components
* @returns LinearColor with 0-1 RGBA components
* @example
* // Convert Color(255,0,0,255) to LinearColor
* ColorToLinearColor(new Color(255,0,0,255)) // returns LinearColor(1,0,0,1)
*/
public ColorToLinearColor(color: Color): LinearColor {
return new LinearColor(
color.R / 255,
color.G / 255,
color.B / 255,
color.A / 255
);
}
/**
* Clamp a float value between a minimum and maximum
* @param Value - Value to clamp
* @param Min - Minimum limit
* @param Max - Maximum limit
* @returns Clamped value
* @example
* // Clamp 10 between 0 and 5
* Clamp(10, 0, 5) // returns 5
*/
public ClampFloat(Value: Float, Min: Float, Max: Float): Float {
return Math.min(Math.max(Value, Min), Max);
}
/**
* Calculate vector length (magnitude)
* @param Vector - Input vector
* @returns Length (magnitude) of vector
* @example
* // Length of vector (3,4,0)
* VectorLength(new Vector(3,4,0)) // returns 5
*/
public VectorLength(Vector: Vector): Float {
return Math.sqrt(
Vector.X * Vector.X + Vector.Y * Vector.Y + Vector.Z * Vector.Z
);
}
/**
* Interpolate a float value towards a target
* @param Current - Current value
* @param Target - Target value
* @param DeltaTime - Time since last update
* @param InterpSpeed - Speed of interpolation
* @returns New interpolated value
* @example
* // Interpolate 0 towards 10 over 1 second at speed 5
* FInterpTo(0, 10, 1, 5) // returns 5
*/
public FInterpTo(
Current: Float,
Target: Float,
DeltaTime: Float,
InterpSpeed: Float
): Float {
if (InterpSpeed <= 0) {
return Target;
}
const Dist = Target - Current;
if (Dist * Dist < 0.00001) {
return Target;
}
const DeltaMove = Dist * Math.min(DeltaTime * InterpSpeed, 1);
return Current + DeltaMove;
}
/**
* Interpolate a vector value towards a target
* @param Current - Current value
* @param Target - Target value
* @param DeltaTime - Time since last update
* @param InterpSpeed - Speed of interpolation
* @returns New interpolated value
* @example
* // Interpolate Vector(0,0,0) towards Vector(10,10,0) over 1 second at speed 5
* VInterpTo(new Vector(0,0,0), new Vector(10,10,0), 1, 5) // returns Vector(5,5,0)
*/
public VInterpTo(
Current: Vector,
Target: Vector,
DeltaTime: Float,
InterpSpeed: Float
): Vector {
if (InterpSpeed <= 0) {
return Target;
}
const Dist = new Vector(
Target.X - Current.X,
Target.Y - Current.Y,
Target.Z - Current.Z
);
if (
this.VectorLength(
new Vector(Dist.X * Dist.X, Dist.Y * Dist.Y, Dist.Z * Dist.Z)
) < 0.00001
) {
return Target;
}
const DeltaMove = new Vector(
Dist.X * Math.min(DeltaTime * InterpSpeed, 1),
Dist.Y * Math.min(DeltaTime * InterpSpeed, 1),
Dist.Z * Math.min(DeltaTime * InterpSpeed, 1)
);
return new Vector(
Current.X + DeltaMove.X,
Current.Y + DeltaMove.Y,
Current.Z + DeltaMove.Z
);
}
/**
* Get right vector from roll, pitch, yaw angles
* @param Roll - Rotation around forward axis in radians
* @param Pitch - Rotation around right axis in radians
* @param Yaw - Rotation around up axis in radians
* @returns Right direction vector
* @example
* // Right vector for no rotation
* GetRightVector(0, 0, 0) // returns Vector(1,0,0)
*/
public GetRightVector(
Roll: Float = 0,
Pitch: Float = 0,
Yaw: Float = 0
): Vector {
const CP = this.Cos(Pitch);
const SP = this.Sin(Pitch);
const CY = this.Cos(Yaw);
const SY = this.Sin(Yaw);
console.log(Roll);
return new Vector(CP * CY, CP * SY, SP);
}
/**
* Get forward vector from roll, pitch, yaw angles
* @param Roll - Rotation around forward axis in radians
* @param Pitch - Rotation around right axis in radians
* @param Yaw - Rotation around up axis in radians
* @returns Forward direction vector
* @example
* // Forward vector for no rotation
* GetForwardVector(0, 0, 0) // returns Vector(1,0,0)
*/
public GetForwardVector(
Roll: Float = 0,
Pitch: Float = 0,
Yaw: Float = 0
): Vector {
const CP = this.Cos(Pitch);
const SP = this.Sin(Pitch);
const CY = this.Cos(Yaw);
const SY = this.Sin(Yaw);
console.log(Roll);
return new Vector(CP * CY, CP * SY, SP);
}
}
/**
* Global instance of math library
* Used throughout the movement system for mathematical calculations
*/
export const MathLibrary = new MathLibraryClass();