TombEngine/TR5Main/Game/trmath.cpp

103 lines
2.2 KiB
C++
Raw Normal View History

#include "framework.h"
#include "trmath.h"
2020-04-24 19:15:05 +02:00
#include <cmath>
2020-10-24 01:05:59 -03:00
#include "prng.h"
2020-12-21 13:16:29 -03:00
using namespace T5M::Math::Random;
2020-04-24 19:15:05 +02:00
2020-04-28 12:24:10 -03:00
short ANGLE(float angle)
{
2020-05-27 20:08:19 +02:00
return angle * 65536.0f / 360.0f;
2019-12-15 16:19:01 +01:00
}
short FROM_RAD(float angle)
{
return angle / RADIAN * 65536.0f / 360.0f;
}
float TO_DEGREES(short angle)
2019-12-15 16:19:01 +01:00
{
2020-05-27 20:08:19 +02:00
return (unsigned short) angle * 360.0f / 65536.0f;
}
float TO_RAD(short angle)
2020-04-28 12:24:10 -03:00
{
2020-05-27 20:08:19 +02:00
return angle * 360.0f / 65536.0f * RADIAN;
2020-04-28 12:24:10 -03:00
}
2020-05-27 19:37:01 +02:00
const float lerp(float v0, float v1, float t)
{
return (1 - t) * v0 + t * v1;
}
2020-04-24 19:15:05 +02:00
2020-08-16 16:01:24 +02:00
const Vector3 getRandomVector()
{
2020-10-24 01:05:59 -03:00
Vector3 v = {generateFloat(-1,1),generateFloat(-1,1),generateFloat(-1,1)};
2020-08-16 16:01:24 +02:00
v.Normalize();
return v;
}
const Vector3 getRandomVectorInCone(const Vector3& direction, const float angleDegrees)
{
2020-10-24 01:05:59 -03:00
float x = generateFloat(-angleDegrees, angleDegrees) * RADIAN;
float y = generateFloat(-angleDegrees, angleDegrees) * RADIAN;
float z = generateFloat(-angleDegrees, angleDegrees) * RADIAN;
2020-08-16 16:01:24 +02:00
Matrix m = Matrix::CreateRotationX(x)* Matrix::CreateRotationY(y) * Matrix::CreateRotationZ(z);
Vector3 result = direction.TransformNormal(direction, m);
result.Normalize();
return result;
}
2020-10-05 22:24:57 -03:00
float phd_sin(short a)
2020-04-24 19:15:05 +02:00
{
2020-10-05 22:24:57 -03:00
return sin(TO_RAD(a));
2020-04-24 19:15:05 +02:00
}
2020-10-05 22:24:57 -03:00
float phd_cos(short a)
2020-04-24 19:15:05 +02:00
{
2020-10-05 22:24:57 -03:00
return cos(TO_RAD(a));
2020-04-24 19:15:05 +02:00
}
int mGetAngle(int x1, int y1, int x2, int y2)
{
return (65536 - phd_atan(x2 - x1, y2 - y1)) % 65536;
2020-04-24 19:15:05 +02:00
}
2020-04-25 16:23:53 +02:00
int phd_atan(int x, int y)
2020-04-24 19:15:05 +02:00
{
return FROM_RAD(atan2(y, x));
2020-04-25 16:23:53 +02:00
}
void phd_GetVectorAngles(int x, int y, int z, short* angles)
{
const auto angle = atan2(x, z);
2020-04-25 16:23:53 +02:00
auto vector = Vector3(x, y, z);
const auto matrix = Matrix::CreateRotationY(-angle);
Vector3::Transform(vector, matrix, vector);
2020-04-25 16:23:53 +02:00
angles[0] = FROM_RAD(angle);
angles[1] = FROM_RAD(-atan2(y, vector.z));
2020-04-25 16:23:53 +02:00
}
2020-07-25 18:02:35 +02:00
void phd_RotBoundingBoxNoPersp(PHD_3DPOS* pos, BOUNDING_BOX* bounds, BOUNDING_BOX* tbounds)
2020-04-25 16:23:53 +02:00
{
Matrix world = Matrix::CreateFromYawPitchRoll(
TO_RAD(pos->yRot),
TO_RAD(pos->xRot),
TO_RAD(pos->zRot)
);
2020-07-25 18:02:35 +02:00
Vector3 bMin = Vector3(bounds->X1, bounds->Y1, bounds->Z1);
Vector3 bMax = Vector3(bounds->X2, bounds->Y2, bounds->Z2);
2020-04-25 16:23:53 +02:00
bMin = Vector3::Transform(bMin, world);
bMax = Vector3::Transform(bMax, world);
2020-07-25 18:02:35 +02:00
tbounds->X1 = bMin.x;
tbounds->X2 = bMax.x;
tbounds->Y1 = bMin.y;
tbounds->Y2 = bMax.y;
tbounds->Z1 = bMin.z;
tbounds->Z2 = bMax.z;
2020-04-24 19:15:05 +02:00
}