2 #define _USE_MATH_DEFINES 42 float closestWaypointDist{ 0.0f };
43 float activeWaypointDist{ 0.0f };
48 catch (std::exception error) {
49 closestWaypointDist = 99.0f;
50 activeWaypointDist = 99.0f;
53 float nodeDist = 9999.0f;
55 bool clearNodes =
false;
60 if (nodeDist < closestWaypointDist) {
108 this->
CreateVB(DXUTGetD3D9Device());
114 this->
UpdateVB(DXUTGetD3D9Device());
131 D3DVIEWPORT9 mvp, oldmvp;
138 mvp.MinZ = 0.0f; mvp.MaxZ = 1.0f;
139 V(pd3dDevice->GetViewport(&oldmvp));
140 V(pd3dDevice->SetViewport(&mvp));
142 V(pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,
false));
143 V(pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2));
146 V(pd3dDevice->DrawPrimitive(D3DPT_LINESTRIP, 0,
lineCount));
148 V(pd3dDevice->SetViewport(&oldmvp));
181 V(
m_avRouteMap->Lock(0, 0, reinterpret_cast<void**>(&vtxs), D3DLOCK_DISCARD));
184 (*vtxs).
x = 68.0f; (*vtxs).y = 248.0f; (*vtxs).z = 0.0f; (*vtxs).w = 1.0f;
185 (*vtxs).diffuse = D3DCOLOR_XRGB(255, 255, 255);
195 (*vtxs).x = 68.0f + mapPosition.x; (*vtxs).y = 248.0f + mapPosition.y; (*vtxs).z = 0.0f; (*vtxs).w = 1.0f;
196 (*vtxs).diffuse = D3DCOLOR_XRGB(255, 255, 0);
238 SClientPacket outpacket;
240 outpacket.f_x =
static_cast<float>(pEdgeId);
241 outpacket.f_y = pDistance;
242 outpacket.f_z =
static_cast<float>(pDirection);
243 outpacket.f_w =
static_cast<float>(pType);
253 float distanceChecked = 0.0f;
255 D3DXVECTOR2 lastPosition;
258 size_t iterCounter = 0;
267 bool gotChange =
false;
269 float headingDiff = 0.0;
270 float distanceIncludingThisLoop = 0.0;
279 lastPosition =
bus->
nodes.at(index + 1).position;
288 sprintf_s(msg, 255,
"Heading diff exceeded threshold! TargetHeading: %f, CurrentHeading: %f, diff: %f, distance: %f, iter: %u, startIndex: %u, endIndex: %u", heading,
bus->
HeadingTrueDegrees, headingDiff, distanceChecked, iterCounter,
headingChangeStartWaypointIndex,
headingChangeEndWaypointIndex);
293 distanceChecked += distanceIncludingThisLoop;
301 size_t direction = Direction::STRAIGHT;
302 if (firstHeadingChangeDegrees < -30.0f && firstHeadingChangeDegrees >= -60.0f) {
303 direction = Direction::BEAR_LEFT;
305 else if (firstHeadingChangeDegrees < -60.0f && firstHeadingChangeDegrees > -130.0f) {
306 direction = Direction::TURN_LEFT;
309 direction = Direction::BEAR_RIGHT;
312 direction = Direction::TURN_RIGHT;
315 direction = Direction::U_TURN;
321 const D3DXVECTOR2 directionVector = targetPos - startingPos;
322 D3DXVECTOR2 normalizedDirVector;
323 D3DXVec2Normalize(&normalizedDirVector, &directionVector);
328 const float twoPi = 2.0f *
static_cast<float>(M_PI);
329 float radians = atan2f(vec.y, vec.x);
330 if (radians < 0.0f) {
331 return fmodf(twoPi + radians + static_cast<float>(M_PI_2), twoPi) * (180.0f /
static_cast<float>(M_PI));
335 return fmodf(radians + static_cast<float>(M_PI_2), twoPi) * (180.0f /
static_cast<float>(M_PI));
340 const D3DXVECTOR2 distanceVec = positionB - positionA;
341 return D3DXVec2Length(&distanceVec);
346 return dist * 2000.0f;
351 float headingChange = pTargetHeading - pCurrentHeading;
352 return headingChange =
customModulo(headingChange + 180.0f, 360.0f) - 180.0f;
359 return a - floor(a / n) * n;
364 const float maxWarnDist = 0.070f;
365 const float minWarnDist = 0.020f;
366 const float maxSpeed = 0.040f;
368 const float warnDist = ((
bus->
GroundSpeedKms / maxSpeed) * (maxWarnDist - minWarnDist)) + minWarnDist;
380 else if (distanceUntilFirstHeadingChange <= 0.05f && approachingNodeLastDistanceSent > 0.06f) {
395 if (headingChange > -90.0f && headingChange < 90.0f) {
397 if (this->
headingState != State::APPROACHING_HEADING_CHANGE) {
399 sprintf_s(msg, 255,
"Changing heading state to approach");
410 if (headingChange < -90.0f || headingChange > 90.0) {
412 if (this->
headingState != State::COMPLETED_HEADING_CHANGE) {
413 sprintf_s(msg, 255,
"Changing heading state to completed");
421 sprintf_s(msg, 255,
"Changing heading state to in heading change");
size_t closestWaypointIndex
float getHeadingChange(float targetHeading, float currentHeading)
float angleBetweenPositionsDeg(const D3DXVECTOR2 &vectorA, const D3DXVECTOR2 &vectorB)
int edgeIdAfterFirstHeadingChange
void SendToServer(void *pData, DWORD dwSize, bool bGuaranteed, PacketOrdering order=ORDERING_NONE) const
size_t firstHeadingChangeDirection
float customModulo(float a, float n)
int headingChangeEndWaypointIndex
void Draw(IDirect3DDevice9 *pd3dDevice)
float distanceBetweenPositions(const D3DXVECTOR2 &vectorA, const D3DXVECTOR2 &vectorB)
float approachingNodeLastDistanceSent
void sendUpdateToServer(int pEdgeId, float pDistance, size_t pDirection, size_t pType)
const float cDirectionChangeThreshold
D3DXVECTOR2 lastVehiclePos
void updateHeadingState()
const float cDistanceToCheckAhead
D3DXVECTOR2 convertWorldPositionToMapPosition(const D3DXVECTOR2 &pos)
const float cReachedWaypointThreshold
size_t activeWaypointIndex
float MapScaleInnerRingKm
float convertDistanceToMeters(float dist)
void UpdateVB(IDirect3DDevice9 *pd3dDevice)
int headingChangeStartWaypointIndex
float vectorToDegrees(const D3DXVECTOR2 &vec)
TurnByTurn(Bus *pBus, Logger *pLoggger, Networking *pNetworking)
float firstHeadingChangeDegrees
void Log(const char *msg, Level level=Info, int errorCode=0)
LPDIRECT3DVERTEXBUFFER9 m_avRouteMap
void CreateVB(IDirect3DDevice9 *pd3dDevice)
float distanceUntilFirstHeadingChange
std::vector< Snode > nodes
void Update(bool pIsGrndVehicle, float pElasedTime)
void AddToCallStack(const char *msg)