#include "Math.h"
#include <iostream>
CMath Math;
Vector3 CMath::NormalizeAngle(const Vector3 Angle1) {
Vector3 Angle = Angle1;
if (!std::isfinite(Angle.x)) {
Angle.x = 0;
}
if (!std::isfinite(Angle.y)) {
Angle.y = 0;
}
while (Angle.y < -180.0f)
Angle.y += 360.0f;
while (Angle.y > 180.0f)
Angle.y -= 360.0f;
if (Angle.x > 89.0f)
Angle.x = 89.0f;
if (Angle.x < -89.0f)
Angle.x = -89.0f;
Angle.z = 0;
return Angle;
}
Vector3 CMath::SmoothAngle(const Vector3 Angle1, const Vector3 Angle2, float Smooth) {
Vector3 Delta = { Angle2.x - Angle1.x, Angle2.y - Angle1.y, 0 };
Delta = NormalizeAngle(Delta);
Delta.x = (Delta.x / Smooth) + Angle1.x;
Delta.y = (Delta.y / Smooth) + Angle1.y;
return Delta;
}
float CMath::FOV(const Vector3 Angle1, const Vector3 Angle2) {
Vector3 Delta = { Angle1.x - Angle2.x, Angle1.y - Angle2.y, 0 };
Delta = NormalizeAngle(Delta);
return sqrt((Delta.x * Delta.x) + (Delta.y * Delta.y));
}
public static Vector3 NormalizeAngle(this Vector3 angle)
{
while (angle.X > 89.0f)
{
angle.X -= 180f;
}
while (angle.X < -89.0f)
{
angle.X += 180f;
}
while (angle.Y > 180f)
{
angle.Y -= 360f;
}
while (angle.Y < -180f)
{
angle.Y += 360f;
}
return angle;
}
virtual bool NormalizeVector( ) override
{
if ( std::isfinite( m_X ) &&
std::isfinite( m_Y ) &&
std::isfinite( m_Z ) )
{
m_X = std::remainderf( m_X, 360.f );
m_Y = std::remainderf( m_Y, 360.f );
return true;
}
return false;
}
bool ClampAngles( )
{
if ( std::isfinite( m_X ) &&
std::isfinite( m_Y ) &&
std::isfinite( m_Z ) )
{
m_X = std::fmax( std::fmin( m_X, 90.f ), -90.f );
m_Z = std::fmax( std::fmin( m_X, 50.f ), -50.f );;
return true;
}
return false;
}