1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-20 11:47:47 +00:00
sapphire/src/common/Util/UtilMath.cpp

136 lines
3.4 KiB
C++
Raw Normal View History

2017-08-28 16:13:23 -03:00
#include <cmath>
2017-08-08 13:53:47 +02:00
#include "UtilMath.h"
#include "Common.h"
#include <cmath>
2017-08-08 13:53:47 +02:00
using namespace Sapphire::Common;
float Util::distanceSq( float x, float y, float z, float x1, float y1, float z1 )
2017-08-08 13:53:47 +02:00
{
float deltaX = x - x1;
float deltaY = y - y1;
float deltaZ = z - z1;
2017-08-08 13:53:47 +02:00
return ( deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ );
2017-08-08 13:53:47 +02:00
}
float Util::distance( float x, float y, float z, float x1, float y1, float z1 )
2017-08-08 13:53:47 +02:00
{
return sqrtf( distanceSq( x, y, z, x1, y1, z1 ) );
2017-08-08 13:53:47 +02:00
}
2019-06-02 21:39:37 +10:00
float Util::distance( const Sapphire::Common::FFXIVARR_POSITION3& pos1,
const Sapphire::Common::FFXIVARR_POSITION3& pos2 )
{
return sqrtf( distanceSq( pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z ) );
}
float Util::distance2DSq( float x, float y, float x1, float y1 )
2017-08-08 13:53:47 +02:00
{
float deltaX = x - x1;
float deltaY = y - y1;
return ( deltaX * deltaX + deltaY * deltaY );
2017-08-08 13:53:47 +02:00
}
float Util::distance2D( float x, float y, float x1, float y1 )
2017-08-08 13:53:47 +02:00
{
return sqrtf( distance2DSq( x, y, x1, y1 ) );
2017-08-08 13:53:47 +02:00
}
float Util::calcAngTo( float x, float y, float x1, float y1 )
2017-08-08 13:53:47 +02:00
{
float dx = x - x1;
float dy = y - y1;
if( dy != 0.0f )
{
return atan2( dy, dx );
}
else
{
return 0.0f;
}
2017-08-08 13:53:47 +02:00
}
float Util::calcAngFrom( float x, float y, float x1, float y1 )
2017-08-08 13:53:47 +02:00
{
float dx = x - x1;
float dy = y - y1;
if( dy != 0.0f )
{
return atan2( dy, dx );
}
else
{
return 0.0f;
}
2017-08-08 13:53:47 +02:00
}
uint16_t Util::floatToUInt16( float val )
2017-08-08 13:53:47 +02:00
{
return static_cast< uint16_t >( ( ( val + 1000.0f ) * 100.0f) * 0.32767501f );
2017-08-08 13:53:47 +02:00
}
uint16_t Util::floatToUInt16Rot( float val )
2017-08-08 13:53:47 +02:00
{
return static_cast< uint16_t >( ( ( ( val + 3.1415927f ) * 100.f) * 103.30219106f ) );
2017-08-08 13:53:47 +02:00
}
uint8_t Util::floatToUInt8Rot( float val )
2017-08-08 13:53:47 +02:00
{
return static_cast< uint8_t >( 0x80 * ( ( val + PI ) ) / PI );
}
FFXIVARR_POSITION3 Util::transform( const FFXIVARR_POSITION3& vector, const Matrix33& matrix )
{
FFXIVARR_POSITION3 dst{};
dst.x = vector.z * matrix.m[ 2 ][ 0 ] + vector.x * matrix.m[ 0 ][ 0 ] + vector.y * matrix.m[ 1 ][ 0 ];
dst.y = vector.z * matrix.m[ 2 ][ 1 ] + vector.x * matrix.m[ 0 ][ 1 ] + vector.y * matrix.m[ 1 ][ 1 ];
dst.z = vector.z * matrix.m[ 2 ][ 2 ] + vector.x * matrix.m[ 0 ][ 2 ] + vector.y * matrix.m[ 1 ][ 2 ];
return dst;
}
float Util::eulerToDirection( const FFXIVARR_POSITION3 &euler )
{
Matrix33 matrix;
auto sinZ = sinf( euler.z );
auto cosZ = cosf( euler.z );
auto sinY = sinf( euler.y );
auto cosY = cosf( euler.y );
auto sinX = sinf( euler.x );
auto cosX = cosf( euler.x );
matrix.m[0][0] = cosZ * cosY;
matrix.m[0][1] = sinZ * cosX;
matrix.m[0][2] = sinZ * sinX + ( -cosZ * sinY ) * cosX;
matrix.m[1][0] = -sinZ * cosY;
matrix.m[1][1] = cosZ * cosX + ( -sinZ * sinY * sinX );
matrix.m[1][2] = cosZ * sinX + sinZ * sinY * cosX;
matrix.m[2][0] = sinY;
matrix.m[2][1] = -cosY * sinX;
matrix.m[2][2] = cosY * cosX;
FFXIVARR_POSITION3 AXIS_Z{ 0.0f, 0.0f, 1.0f };
auto result = transform( AXIS_Z, matrix );
auto squared = result.z * result.z + result.x * result.x;
auto v1{0.0f};
auto v2{0.0f};
if( squared > 0.00000011920929f )
{
auto ret = sqrtf( squared );
ret = -( ( squared * ret ) * ret - 1.0f ) * ( 0.5f * ret ) + ret;
ret = -( ( squared * ret ) * ret - 1.0f ) * ( 0.5f * ret ) + ret;
v1 = result.z * ( -( ( ( squared * ret ) * ret ) - 1.0f ) * ( 0.5f * ret ) + ret );
v2 = result.x * ( -( ( ( squared * ret ) * ret ) - 1.0f ) * ( 0.5f * ret ) + ret );
}
return atan2f( v2, v1 );
}