Repository navigation
Expand file tree
/
Copy pathMovingTarget.cpp
More file actions
155 lines (145 loc) · 3.83 KB
/
Copy pathMovingTarget.cpp
File metadata and controls
155 lines (145 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2010 Daniel Albano
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#define _USE_MATH_DEFINES
#include "MovingTarget.h"
#include <cmath>
/**
* Initializes a moving target with blank coordinates.
*/
MovingTarget::MovingTarget() : Target(), _dest(0), _speedLon(0.0), _speedLat(0.0), _speed(0)
{
}
MovingTarget::~MovingTarget()
{
}
/**
* Returns the destination the moving target is heading to.
* @return Pointer to destination.
*/
Target *MovingTarget::getDestination()
{
return _dest;
}
/**
* Changes the destination the moving target is heading to.
* @param dest Pointer to destination.
*/
void MovingTarget::setDestination(Target *dest)
{
// Remove moving target from old destination's followers
if (_dest != 0)
{
for (vector<Target*>::iterator i = _dest->getFollowers()->begin(); i != _dest->getFollowers()->end(); i++)
{
if ((*i) == this)
{
_dest->getFollowers()->erase(i);
break;
}
}
}
_dest = dest;
// Add moving target to new destination's followers
if (_dest != 0)
{
_dest->getFollowers()->push_back(this);
}
calculateSpeed();
}
/**
* Returns the speed of the moving target.
* @return Speed in knots.
*/
int MovingTarget::getSpeed()
{
return _speed;
}
/**
* Changes the speed of the moving target.
* @param speed Speed in knots.
*/
void MovingTarget::setSpeed(int speed)
{
_speed = speed;
calculateSpeed();
}
/**
* Converts the speed from the standard knots (nautical miles per hour),
* and converts it into radians per 5 in-game seconds.
* @return Speed in radians.
*/
double MovingTarget::getRadianSpeed()
{
// Each nautical mile is 1/60th of a degree.
// Each hour contains 300 5-seconds.
return _speed * (1 / 60.0) * (M_PI / 180) / 300.0;
}
/**
* Returns the distance between this moving target and any other
* target on the globe, accounting for the wrap-around.
* @param target Pointer to other target.
* @param dLon Returned longitude difference.
* @param dLat Returned latitude difference.
* @returns Distance in radian.
*/
double MovingTarget::getDistance(Target *target, double *dLon, double *dLat)
{
double minLength = 2*M_PI, lat = target->getLatitude();
for (double lon = target->getLongitude() - 2*M_PI; lon <= target->getLongitude() + 2*M_PI; lon += 2*M_PI)
{
double dx = lon - _lon;
double dy = lat - _lat;
double length = sqrt(dx * dx + dy * dy);
if (length < minLength)
{
minLength = length;
*dLon = dx;
*dLat = dy;
}
}
return minLength;
}
/**
* Calculates the speed vector for the moving target
* based on the current raw speed and destination.
*/
void MovingTarget::calculateSpeed()
{
if (_dest != 0)
{
double dLon, dLat;
double length = getDistance(_dest, &dLon, &dLat);
_speedLon = dLon / length * getRadianSpeed();
_speedLat = dLat / length * getRadianSpeed();
}
else
{
_speedLon = 0;
_speedLat = 0;
}
}
/**
* Checks if the moving target has reached its destination by checking
* if it has exceeded the destination position based on the speed vector.
*/
bool MovingTarget::reachedDestination()
{
return (((_speedLon > 0 && _lon >= _dest->getLongitude()) || (_speedLon < 0 && _lon <= _dest->getLongitude())) &&
((_speedLat > 0 && _lat >= _dest->getLatitude()) || (_speedLat < 0 && _lat <= _dest->getLatitude())));
}