Rise
The Vieneo Province
Orbit.cpp
Go to the documentation of this file.
1 #include "Instrument.h"
2 #include "../Bus.h"
3 
4 Orbit::Orbit(int prmX, int prmY, float prmXScale, float prmYScale, Bus* prmBus, Logger* prmLogger, DeviceObject* prmDevice, std::vector<Font*> prmFonts) :
5  Instrument(prmX, prmY, prmXScale, prmYScale, prmBus, prmLogger, prmDevice, prmFonts)
6 {
7  logger->AddToCallStack("Orbit::ctor");
8  defaultFont = 1;
9 }
10 
12 {
13  if (f_MSL < 100.0f) // K�rm�n line
14  return;
15 
16  WCHAR str[99];
17 
18  if (ec < 1.0f) // apoapsis is infinite if the orbit is parabolic/hyperbolic
19  {
20  // period is infinite if the orbit is parabolic/hyperbolic
21  swprintf_s(str, 99, L"TP: %5.2f s", tp);
22  DrawTextW(str, 0, 0, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 128, 128, 255));
23  }
24 
25  swprintf_s(str, 99, L" e: %0.6f", ec);
26  if (ec >= 1.0f)
27  DrawTextW(str, 0, 22, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 0, 0));
28  else if (ec >= 0.75f)
29  DrawTextW(str, 0, 22, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 128, 0, 0));
30  else
31  DrawTextW(str, 0, 22, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 0, 128, 0));
32 
33  if (ec > 1.0f)
34  {
35  if (flasher)
36  DrawTextW(L"aa: HYPERBOLIC", 0, 12, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 0, 0));
37  }
38  else
39  {
40  swprintf_s(str, 99, L"aa: %4.3f km", aa);
41  if (ec >= 0.75f)
42  DrawTextW(str, 0, 12, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 128, 0, 0));
43  else
44  DrawTextW(str, 0, 12, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 0, 128, 0));
45  }
46 
47  swprintf_s(str, 99, L"ap: %4.3f km", ap);
48  if (ap <= 0.0f)
49  {
50  if (flasher)
51  DrawTextW(L"ap: SURFACE", 0, 32, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 0, 0));
52  }
53  else if (ap < 73.49f)
54  DrawTextW(str, 0, 32, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 0, 0));
55  else if (ap < 146.98f)
56  DrawTextW(str, 0, 32, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 128, 0, 0));
57  else
58  DrawTextW(str, 0, 32, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 0, 128, 0));
59 
60  swprintf_s(str, 99, L"vo: %3.1f", D3DXToDegree(vo));
61  DrawTextW(str, 0, 42, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 128, 128, 128));
62 
63  swprintf_s(str, 99, L" i: %1.6f", D3DXToDegree(ix));
64  DrawTextW(str, 0, 52, 0, DT_NOCLIP, D3DCOLOR_ARGB(255, 128, 128, 128));
65 
66 
67  // Reverse engineer
68  // f_temp=p/(1.0f+D3DXVec3Length( &ex )*cosf(vo));
69  // rx.x=f_temp*cosf(vo); rx.y=f_temp*sinf(vo); rx.z=0.0f;
70  // f_temp=sqrtf(mu/p);
71  // vx.x=f_temp*-sinf(vo); vx.y=f_temp*(D3DXVec3Length( &ex )+cosf(vo)); vx.z=0.0f;
72  //
73  // D3DXMatrixIdentity( &matrixWorld );
74  // D3DXMatrixRotationZ( &matrixTemp, -ox );
75  // D3DXMatrixMultiply( &matrixWorld, &matrixWorld, &matrixTemp );
76  // D3DXMatrixRotationX( &matrixTemp, ix );
77  // D3DXMatrixMultiply( &matrixWorld, &matrixWorld, &matrixTemp );
78  // D3DXMatrixRotationZ( &matrixTemp, wx );
79  // D3DXMatrixMultiply( &matrixWorld, &matrixWorld, &matrixTemp );
80  // D3DXMatrixInverse( &matrixWorld, NULL, &matrixWorld );
81 
82  // D3DXVec3TransformCoord( &rx, &rx, &matrixWorld );
83  // D3DXVec3TransformCoord( &vx, &vx, &matrixWorld );
84 
85  // sprintf_s( msg, sizeof(msg), "i: %1.3f j: %1.3f k: %1.3f", vx.x, vx.y, vx.z );
86  // m_pFont->DrawText( 2, 220, D3DCOLOR_ARGB(255,128,128,128), msg );
87  // sprintf_s( msg, sizeof(msg), "x: %1.3f y: %1.3f z: %1.3f", rx.x, rx.y, rx.z );
88  // m_pFont->DrawText( 2, 240, D3DCOLOR_ARGB(255,128,128,128), msg );
89  // Orbital elements -------------------------------------------------------------------------
90 }
91 
92 void Orbit::FrameMove(float fElapsed)
93 {
94  if (f_MSL < 100.0f) // K�rm�n line
95  return;
96 
97  halfsec += fElapsed;
98  if (halfsec > 0.5f)
99  halfsec -= 0.5f;
100  else
101  return;
102 
103  vx = oldvelocity;
104  rx = oldposition;
105 
106  D3DXVec3Cross(&hx, &rx, &vx);
107  D3DXVec3Cross(&nx, &northpoleC, &hx);
108 
109  const float param = powf(D3DXVec3Length(&hx), 2.0f) / mu;
110 
111  D3DXVec3Scale(&ex, &rx, (powf(D3DXVec3Length(&vx), 2.0f) - mu / D3DXVec3Length(&rx)) / mu);
112  D3DXVec3Scale(&tv, &vx, D3DXVec3Dot(&rx, &vx) / mu);
113  D3DXVec3Subtract(&ex, &ex, &tv); // eccentricity vector
114  ec = D3DXVec3Length(&ex); // eccentricity magnitude
115 
116  const float rp = param / (1.0f + ec); // radius sub periapsis
117  ap = rp - radiusC;
118 
119  const float a = param / (1.0f - powf(ec, 2.0f)); // semi-major axis
120 
121  if (ec <= 1.0f)
122  {
123  //float ra = prm / (1.0f - ec); // radius sub apoapsis old way is slower because of division
124  const float ra = 2.0f * a - rp; // radius sub apoapsis
125  aa = ra - radiusC; // altitude at apoapsis
126  tp = D3DX_TAU * sqrtf(powf(a, 3.0f) / mu);
127  }
128  else
129  {
130  aa = -1.0f;
131  tp = 0.0f;
132  }
133 
134  const float f_temp = D3DXVec3Dot(&ex, &rx) / (ec * D3DXVec3Length(&rx));
135  if (f_temp >= 1.0f)
136  vo = 0.0f;
137  else if (f_temp <= -1.0f)
138  vo = D3DX_HALFPI;
139  else
140  vo = acosf(f_temp);
141  if (D3DXVec3Dot(&rx, &vx) < 0.0f)
142  vo = D3DX_TAU - vo;
143 
144  ix = acosf(hx.z / D3DXVec3Length(&hx));
145 
146 
147  // float uo=acosf( D3DXVec3Dot( &nx, &rx )/(D3DXVec3Length( &nx )*D3DXVec3Length( &rx )) ); // argument of latitude
148  // if (rx.z<0.0f)
149  // uo=D3DX_TAU-uo;
150 
151  //f_temp = nx.x / D3DXVec3Length(&nx);
152  //float ox; // longitude of ascending node
153  //if (f_temp >= 1.0f)
154  // ox = 0.0f;
155  //else if (f_temp <= -1.0f)
156  // ox = D3DX_HALFPI;
157  //else
158  // ox = acosf(f_temp);
159  //if (nx.y < 0.0f)
160  // ox = D3DX_TAU - ox;
161 
162  //f_temp = D3DXVec3Dot(&nx, &ex) / (D3DXVec3Length(&nx) * ec);
163  //float wx; // Argument of periapsis?
164  //if (f_temp >= 1.0f)
165  // wx = 0.0f;
166  //else if (f_temp <= -1.0f)
167  // wx = D3DX_HALFPI;
168  //else
169  // wx = acosf(f_temp);
170  //if (ex.z < 0.0f)
171  // wx = D3DX_TAU - wx;
172 
173  // float M=sqrtf(mu/powf(a, 3.0f));
174  // sprintf_s( msg, sizeof(msg), "dv: %0.6f", dv );
175  // m_pFont->DrawText( 907-130.0f, rtHeight-160.0f, D3DCOLOR_ARGB(255,128,128,255), msg );
176 
177  // float dv=M+2.0f*D3DXVec3Length(&ex)*sinf(M)+5.0f/4.0f*powf(D3DXVec3Length(&ex), 2.0f)*sinf(2.0f*M);
178  // sprintf_s( msg, sizeof(msg), "M: %0.6f", M );
179  // m_pFont->DrawText( 907-120.0f, rtHeight-140.0f, D3DCOLOR_ARGB(255,128,128,255), msg );
180 
183 }
#define radiusC
Definition: globals.h:88
float vo
Definition: Instrument.h:338
D3DXVECTOR3 position
Definition: globals.h:549
Definition: Logger.h:9
D3DXVECTOR3 hx
Definition: Instrument.h:331
void FrameMove(float fElapsed) override
Definition: Orbit.cpp:92
D3DXVECTOR3 ex
Definition: Instrument.h:331
D3DXVECTOR3 nx
Definition: Instrument.h:331
D3DXVECTOR3 rx
Definition: Instrument.h:331
s_network_objects playerships[MAX_SCAN]
Definition: globals.cpp:174
float aa
Definition: Instrument.h:336
float halfsec
Definition: Instrument.h:340
D3DXVECTOR3 vx
Definition: Instrument.h:331
#define mu
Definition: globals.h:76
Definition: Bus.h:16
const D3DXVECTOR3 northpoleC
float ec
Definition: Instrument.h:334
D3DXVECTOR3 tv
Definition: Instrument.h:331
float tp
Definition: Instrument.h:335
UINT defaultFont
Definition: Instrument.h:20
float f_MSL
Definition: globals.cpp:36
float flasher
Definition: globals.cpp:65
D3DXVECTOR3 velocity
Definition: globals.h:540
Orbit(int prmX, int prmY, float prmXScale, float prmYScale, Bus *prmBus, Logger *prmLogger, DeviceObject *prmDevice, std::vector< Font *> prmFonts)
Definition: Orbit.cpp:4
void Render() override
Definition: Orbit.cpp:11
Logger * logger
Definition: Instrument.h:19
D3DXVECTOR3 oldvelocity
Definition: Instrument.h:333
float ap
Definition: Instrument.h:337
D3DXVECTOR3 oldposition
Definition: Instrument.h:332
float ix
Definition: Instrument.h:339
void AddToCallStack(const char *msg)
Definition: Logger.cpp:86