Rise
The Vieneo Province
Loading...
Searching...
No Matches
MathUtilities.h
Go to the documentation of this file.
1#pragma once
2
3template <typename T1, typename T2, typename T3>
4inline constexpr auto Clamp(T1 val, T2 min, T3 max)
5{
6 using T = std::common_type_t<T1, T2, T3>;
7 T v = static_cast<T>(val);
8 T mn = static_cast<T>(min);
9 T mx = static_cast<T>(max);
10
11 if (v < mn) return mn;
12 if (v > mx) return mx;
13 return v;
14}
15
16inline float Lerp(float val1, float val2, float lerp)
17{
18 return val2 * lerp + val1 * (1.0f - lerp);
19}
20
22{
23 position = -position;
24
25 RiseMath::Vector2 latlng;
26
27 latlng.x = -RiseMath::ToDegree(atan2f(
28 position.z,
29 sqrtf(position.x * position.x + position.y * position.y)));
30
31 // Longitude: angle around Z from +Y axis, clockwise
32 latlng.y = -RiseMath::ToDegree(atan2f(position.x, position.y));
33 // equivalently: RiseMath::ToDegree(atan2f(-position.x, position.y));
34
35 return latlng;
36}
37
38inline float GetArcDist(RiseMath::Vector3 from, RiseMath::Vector3 to, bool includeAltDiff)
39{
40 const RiseMath::Vector3 result = to - from;
41 const float olddist = RiseMath::Vec3Length(&result);
42 const float lerp = RiseMath::Max(0.0f, RiseMath::Min(1.0f, (olddist - 320.0f) / 160.0f)); // 320 then 0... 480 then 1...
43
44 const RiseMath::Vector2 latlng34 = CalculateLatLng(from);
45 const RiseMath::Vector2 latlng56 = CalculateLatLng(to);
46
47 const float dLat = RiseMath::ToRadian(latlng34.x - latlng56.x);
48 const float dLon = RiseMath::ToRadian(latlng34.y - latlng56.y);
49
51 const float a = sinf(dLat * 0.5f) * sinf(dLat * 0.5f) + cosf(RiseMath::ToRadian(latlng56.x)) * cosf(RiseMath::ToRadian(latlng34.x)) *
52 sinf(dLon * 0.5f) * sinf(dLon * 0.5f);
53 const float c = 2.0f * atan2f(sqrtf(a), sqrtf(1.0f - a));
54 float newdist = c * radiusC;
55
56 if (includeAltDiff)
57 {
58 const float alt = RiseMath::Vec3Length(&from) - RiseMath::Vec3Length(&to);
59 newdist = sqrtf(newdist*newdist + alt * alt);
60 }
61
62 return newdist * lerp + olddist * (1.0f - lerp);
63}
64
65inline RiseMath::Vector3 GetArcBear(RiseMath::Vector3 vec2, float dist, float altd, RiseMath::Vector3 posnorml)
66{
67 // effect is based on 160 to 320 km 0 to 1
68
69 RiseMath::Vector3 cross, tempvelocity, relativel;
70 const RiseMath::Vector3 location = -posnorml;
71 RiseMath::Vec3Normalize(&tempvelocity, &vec2);
72 RiseMath::Vec3Cross(&cross, &location, &tempvelocity); // Find perpendicular vector
73 float f_temp;
74 const float angle = RiseMath::Vec3Dot(&location, &tempvelocity); // Find our trajectory to the surface
75 // curvalinear mark, if it is far away it is 0, if it is div0 it is 1
76 if (angle >= 1.0f)
77 f_temp = -RiseMath::HalfPi;
78 else if (angle <= -1.0f)
79 f_temp = -RiseMath::Pi;
80 else
81 {
82 f_temp = -acosf(angle) - RiseMath::HalfPi;
83 f_temp += atanf(altd / dist); // alt of change over distance, at 1km each it is 45 deg
84 }
85
87 RiseMath::MatrixRotationAxis(&dor, &cross, f_temp); // Set up a matrix
88 RiseMath::Vec3TransformNormal(&relativel, &tempvelocity, &dor); // Use DOR to fix trajectory
89
90 const float lerp = RiseMath::Max(0.0f, RiseMath::Min(1.0f, (dist - 320.0f) / 160.0f)); // 320 then 0... 480 then 1...
91 return -relativel * lerp + vec2 * (1.0f - lerp);
92}
93
94inline bool RectEquality(RECT rect1, RECT rect2)
95{
96 return rect1.left == rect2.left && rect1.right == rect2.right && rect1.top == rect2.top && rect1.bottom == rect2.bottom;
97}
98
99// Returns a random floating point number between 0 and 1
100inline float RandomFloat()
101{
102 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
103}
float Lerp(float val1, float val2, float lerp)
float RandomFloat()
constexpr auto Clamp(T1 val, T2 min, T3 max)
bool RectEquality(RECT rect1, RECT rect2)
RiseMath::Vector3 GetArcBear(RiseMath::Vector3 vec2, float dist, float altd, RiseMath::Vector3 posnorml)
RiseMath::Vector2 CalculateLatLng(RiseMath::Vector3 position)
float GetArcDist(RiseMath::Vector3 from, RiseMath::Vector3 to, bool includeAltDiff)
#define radiusC
Definition globals.h:87
auto ToRadian(T value)
Definition ClientMath.h:13
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
constexpr float Pi
Definition ClientMath.h:12
Vector3 * Vec3Cross(Vector3 *out, const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:105
constexpr auto Min(A first, B second)
Definition ClientMath.h:15
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107
Matrix * MatrixRotationAxis(Matrix *out, const Vector3 *axis, float angle)
Definition ClientMath.h:129
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:93
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Definition ClientMath.h:103
auto ToDegree(T value)
Definition ClientMath.h:14
constexpr auto Max(A first, B second)
Definition ClientMath.h:16
constexpr float HalfPi
Definition ClientMath.h:12