swb_shared/util/MathUtil.cs

Utility math helpers for the game. Provides framerate-independent Lerp wrappers for floats, Vector2, Vector3, Angles and Rotation using RealTime.SmoothDelta, a function to add a vector relative to a rotation, a small Bezier easing helper, and simple conversions between Angles, Vector3 and Rotation.

using System;

/* 
 * Utility class to handle framerate independent + useful calculations
*/

namespace SWB.Shared;

class MathUtil
{
	public static float FILerp( float fromF, float toF, float amount )
	{
		return fromF.LerpTo( toF, amount * RealTime.SmoothDelta );
	}

	public static Vector3 FILerp( Vector3 fromVec, Vector3 toVec, float amount )
	{
		return fromVec.LerpTo( toVec, amount * RealTime.SmoothDelta );
	}

	public static Vector2 FILerp( Vector2 fromVec, Vector2 toVec, float amount )
	{
		return fromVec.LerpTo( toVec, amount * RealTime.SmoothDelta );
	}

	public static Angles FILerp( Angles fromAng, Angles toAng, float amount )
	{
		return Angles.Lerp( fromAng, toAng, amount * RealTime.SmoothDelta );
	}

	public static Rotation FILerp( Rotation fromRot, Rotation toRot, float amount )
	{
		return Rotation.Lerp( fromRot, toRot, amount * RealTime.SmoothDelta );
	}

	public static Vector3 RelativeAdd( Vector3 vec1, Vector3 vec2, Rotation rot )
	{
		vec1 += vec2.x * rot.Right;
		vec1 += vec2.y * rot.Up;
		vec1 += vec2.z * rot.Forward;

		return vec1;
	}


	// Helpful bezier function. Use this if you gotta: https://www.desmos.com/calculator/cahqdxeshd
	public static float BezierY( float f, float a, float b, float c )
	{
		f *= 3.2258f;
		return MathF.Pow( (1.0f - f), 2.0f ) * a + 2.0f * (1.0f - f) * f * b + MathF.Pow( f, 2.0f ) * c;
	}

	public static Vector3 ToVector3( Angles angles )
	{
		return new Vector3( angles.pitch, angles.yaw, angles.roll );
	}

	public static Angles ToAngles( Vector3 vector )
	{
		return new Angles( vector.x, vector.y, vector.z );
	}

	public static Rotation ToRotation( Vector3 vector )
	{
		return ToAngles( vector ).ToRotation();
	}
}