Rise
The Vieneo Province
MathUtilities.h
Go to the documentation of this file.
1 #pragma once
2 
3 inline float Clamp(float val, float min, float max)
4 {
5  if (val < min)
6  return min;
7  if (val > max)
8  return max;
9  return val;
10 }
11 
12 inline float Lerp(float val1, float val2, float lerp)
13 {
14  return val2 * lerp + val1 * (1.0f - lerp);
15 }
16 
17 inline D3DXVECTOR2 CalculateLatLng(D3DXVECTOR3 position)
18 {
19  position = -position; // needs the inverse
20 
21  D3DXVECTOR2 latlng;
22 
23  // Latitude and longitude
24  latlng.x = -D3DXToDegree(atanf(position.z / sqrtf(position.x*position.x + position.y*position.y)));
25  if (position.x <= 0 && position.y > 0)
26  latlng.y = -D3DXToDegree(atanf(position.x / position.y)); // PM to EP
27  else if (position.x < 0 && position.y <= 0)
28  latlng.y = D3DXToDegree(atanf(position.y / position.x)) + 90.0f; // EP to IDL
29  else if (position.x >= 0 && position.y < 0)
30  latlng.y = -D3DXToDegree(atanf(position.x / position.y)) - 180.0f; // IDL to WP
31  else if (position.x > 0 && position.y >= 0) // west?
32  latlng.y = D3DXToDegree(atanf(position.y / position.x)) - 90.0f; // WP to PM
33 
34  return latlng;
35 }
36 
37 inline float GetArcDist(D3DXVECTOR3 from, D3DXVECTOR3 to, bool includeAltDiff)
38 {
39  D3DXVECTOR3 result = to - from;
40  const float olddist = D3DXVec3Length(&result);
41  const float lerp = max(0.0f, min(1.0f, (olddist - 320.0f) / 160.0f)); // 320 then 0... 480 then 1...
42 
43  D3DXVECTOR2 latlng34 = CalculateLatLng(from);
44  D3DXVECTOR2 latlng56 = CalculateLatLng(to);
45 
46  const float dLat = D3DXToRadian(latlng34.x - latlng56.x);
47  const float dLon = D3DXToRadian(latlng34.y - latlng56.y);
48 
50  const float a = sinf(dLat * 0.5f) * sinf(dLat * 0.5f) + cosf(D3DXToRadian(latlng56.x)) * cosf(D3DXToRadian(latlng34.x)) *
51  sinf(dLon * 0.5f) * sinf(dLon * 0.5f);
52  const float c = 2.0f * atan2f(sqrtf(a), sqrtf(1.0f - a));
53  float newdist = c * radiusC;
54 
55  if (includeAltDiff)
56  {
57  const float alt = D3DXVec3Length(&from) - D3DXVec3Length(&to);
58  newdist = sqrtf(newdist*newdist + alt * alt);
59  }
60 
61  return newdist * lerp + olddist * (1.0f - lerp);
62 }
63 
64 inline D3DXVECTOR3 GetArcBear(D3DXVECTOR3 vec2, float dist, float altd, D3DXVECTOR3 posnorml)
65 {
66  // effect is based on 160 to 320 km 0 to 1
67 
68  D3DXVECTOR3 cross, location = -posnorml, tempvelocity, relativel;
69  D3DXVec3Normalize(&tempvelocity, &vec2);
70  D3DXVec3Cross(&cross, &location, &tempvelocity); // Find perpendicular vector
71  float f_temp;
72  const float angle = D3DXVec3Dot(&location, &tempvelocity); // Find our trajectory to the surface
73  // curvalinear mark, if it is far away it is 0, if it is div0 it is 1
74  if (angle >= 1.0f)
75  f_temp = -D3DX_HALFPI;
76  else if (angle <= -1.0f)
77  f_temp = -D3DX_PI;
78  else
79  {
80  f_temp = -acosf(angle) - D3DX_HALFPI;
81  f_temp += atanf(altd / dist); // alt of change over distance, at 1km each it is 45 deg
82  }
83 
84  D3DXMATRIX dor;
85  D3DXMatrixRotationAxis(&dor, &cross, f_temp); // Set up a matrix
86  D3DXVec3TransformCoord(&relativel, &tempvelocity, &dor); // Use DOR to fix trajectory
87 
88  const float lerp = max(0.0f, min(1.0f, (dist - 320.0f) / 160.0f)); // 320 then 0... 480 then 1...
89  return -relativel * lerp + vec2 * (1.0f - lerp);
90 }
91 
92 inline bool RectEquality(RECT rect1, RECT rect2)
93 {
94  return rect1.left == rect2.left && rect1.right == rect2.right && rect1.top == rect2.top && rect1.bottom == rect2.bottom;
95 }
96 
97 // Returns a random floating point number between 0 and 1
98 inline float RandomFloat()
99 {
100  return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
101 }
#define radiusC
Definition: globals.h:88
float GetArcDist(D3DXVECTOR3 from, D3DXVECTOR3 to, bool includeAltDiff)
Definition: MathUtilities.h:37
bool RectEquality(RECT rect1, RECT rect2)
Definition: MathUtilities.h:92
D3DXVECTOR2 CalculateLatLng(D3DXVECTOR3 position)
Definition: MathUtilities.h:17
D3DXVECTOR3 GetArcBear(D3DXVECTOR3 vec2, float dist, float altd, D3DXVECTOR3 posnorml)
Definition: MathUtilities.h:64
float Clamp(float val, float min, float max)
Definition: MathUtilities.h:3
float Lerp(float val1, float val2, float lerp)
Definition: MathUtilities.h:12
float RandomFloat()
Definition: MathUtilities.h:98