TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Position.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef Trinity_game_Position_h__
19 #define Trinity_game_Position_h__
20 
21 #include "Common.h"
22 
23 #include <G3D/Vector3.h>
24 
25 class ByteBuffer;
26 
28 {
29  Position(float x = 0, float y = 0, float z = 0, float o = 0)
30  : m_positionX(x), m_positionY(y), m_positionZ(z), m_orientation(NormalizeOrientation(o)) { }
31 
32  Position(Position const& loc) { Relocate(loc); }
33 
35  {
36  explicit PositionXYStreamer(Position& pos) : Pos(&pos) { }
38  };
39 
41  {
42  explicit PositionXYZStreamer(Position& pos) : Pos(&pos) { }
44  };
45 
47  {
48  explicit PositionXYZOStreamer(Position& pos) : Pos(&pos) { }
50  };
51 
52  float m_positionX;
53  float m_positionY;
54  float m_positionZ;
55  // Better to limit access to _orientation field, to guarantee the value is normalized
56 private:
58 
59 public:
60  bool operator==(Position const &a);
61 
62  inline bool operator!=(Position const &a)
63  {
64  return !(operator==(a));
65  }
66 
67  void Relocate(float x, float y)
68  {
69  m_positionX = x; m_positionY = y;
70  }
71 
72  void Relocate(float x, float y, float z)
73  {
74  m_positionX = x; m_positionY = y; m_positionZ = z;
75  }
76 
77  void Relocate(float x, float y, float z, float orientation)
78  {
79  m_positionX = x; m_positionY = y; m_positionZ = z; SetOrientation(orientation);
80  }
81 
82  void Relocate(Position const &pos)
83  {
84  m_positionX = pos.m_positionX; m_positionY = pos.m_positionY; m_positionZ = pos.m_positionZ; SetOrientation(pos.m_orientation);
85  }
86 
87  void Relocate(Position const* pos)
88  {
89  m_positionX = pos->m_positionX; m_positionY = pos->m_positionY; m_positionZ = pos->m_positionZ; SetOrientation(pos->m_orientation);
90  }
91 
92  void Relocate(G3D::Vector3 const& pos)
93  {
94  m_positionX = pos.x; m_positionY = pos.y; m_positionZ = pos.z;
95  }
96 
97  void RelocateOffset(Position const &offset);
98 
99  void SetOrientation(float orientation)
100  {
101  m_orientation = NormalizeOrientation(orientation);
102  }
103 
104  float GetPositionX() const { return m_positionX; }
105  float GetPositionY() const { return m_positionY; }
106  float GetPositionZ() const { return m_positionZ; }
107  float GetOrientation() const { return m_orientation; }
108 
109  void GetPosition(float &x, float &y) const
110  {
111  x = m_positionX; y = m_positionY;
112  }
113 
114  void GetPosition(float &x, float &y, float &z) const
115  {
116  x = m_positionX; y = m_positionY; z = m_positionZ;
117  }
118 
119  void GetPosition(float &x, float &y, float &z, float &o) const
120  {
121  x = m_positionX; y = m_positionY; z = m_positionZ; o = m_orientation;
122  }
123 
124  Position GetPosition() const { return *this; }
125 
129 
130  bool IsPositionValid() const;
131 
132  float GetExactDist2dSq(float x, float y) const
133  {
134  float dx = m_positionX - x; float dy = m_positionY - y; return dx*dx + dy*dy;
135  }
136 
137  float GetExactDist2d(const float x, const float y) const
138  {
139  return std::sqrt(GetExactDist2dSq(x, y));
140  }
141 
142  float GetExactDist2dSq(Position const* pos) const
143  {
144  float dx = m_positionX - pos->m_positionX; float dy = m_positionY - pos->m_positionY; return dx*dx + dy*dy;
145  }
146 
147  float GetExactDist2d(Position const* pos) const
148  {
149  return std::sqrt(GetExactDist2dSq(pos));
150  }
151 
152  float GetExactDistSq(float x, float y, float z) const
153  {
154  float dz = m_positionZ - z; return GetExactDist2dSq(x, y) + dz*dz;
155  }
156 
157  float GetExactDist(float x, float y, float z) const
158  {
159  return std::sqrt(GetExactDistSq(x, y, z));
160  }
161 
162  float GetExactDistSq(Position const* pos) const
163  {
164  float dx = m_positionX - pos->m_positionX; float dy = m_positionY - pos->m_positionY; float dz = m_positionZ - pos->m_positionZ; return dx*dx + dy*dy + dz*dz;
165  }
166 
167  float GetExactDist(Position const* pos) const
168  {
169  return std::sqrt(GetExactDistSq(pos));
170  }
171 
172  void GetPositionOffsetTo(Position const & endPos, Position & retOffset) const;
173  Position GetPositionWithOffset(Position const& offset) const;
174 
175  float GetAngle(Position const* pos) const;
176  float GetAngle(float x, float y) const;
177  float GetRelativeAngle(Position const* pos) const
178  {
179  return GetAngle(pos) - m_orientation;
180  }
181 
182  float GetRelativeAngle(float x, float y) const { return GetAngle(x, y) - m_orientation; }
183  void GetSinCos(float x, float y, float &vsin, float &vcos) const;
184 
185  bool IsInDist2d(float x, float y, float dist) const
186  {
187  return GetExactDist2dSq(x, y) < dist * dist;
188  }
189 
190  bool IsInDist2d(Position const* pos, float dist) const
191  {
192  return GetExactDist2dSq(pos) < dist * dist;
193  }
194 
195  bool IsInDist(float x, float y, float z, float dist) const
196  {
197  return GetExactDistSq(x, y, z) < dist * dist;
198  }
199 
200  bool IsInDist(Position const* pos, float dist) const
201  {
202  return GetExactDistSq(pos) < dist * dist;
203  }
204 
205  bool IsWithinBox(const Position& center, float xradius, float yradius, float zradius) const;
206  bool HasInArc(float arcangle, Position const* pos, float border = 2.0f) const;
207  bool HasInLine(Position const* pos, float width) const;
208  std::string ToString() const;
209 
210  // modulos a radian orientation to the range of 0..2PI
211  static float NormalizeOrientation(float o)
212  {
213  // fmod only supports positive numbers. Thus we have
214  // to emulate negative numbers
215  if (o < 0)
216  {
217  float mod = o *-1;
218  mod = std::fmod(mod, 2.0f * static_cast<float>(M_PI));
219  mod = -mod + 2.0f * static_cast<float>(M_PI);
220  return mod;
221  }
222  return std::fmod(o, 2.0f * static_cast<float>(M_PI));
223  }
224 };
225 
226 #define MAPID_INVALID 0xFFFFFFFF
227 
229 {
230 public:
231  explicit WorldLocation(uint32 mapId = MAPID_INVALID, float x = 0.f, float y = 0.f, float z = 0.f, float o = 0.f)
232  : Position(x, y, z, o), m_mapId(mapId) { }
233 
235  : Position(loc), m_mapId(loc.GetMapId()) { }
236 
237  void WorldRelocate(WorldLocation const& loc)
238  {
239  m_mapId = loc.GetMapId();
240  Relocate(loc);
241  }
242 
243  void WorldRelocate(uint32 mapId = MAPID_INVALID, float x = 0.f, float y = 0.f, float z = 0.f, float o = 0.f)
244  {
245  m_mapId = mapId;
246  Relocate(x, y, z, o);
247  }
248 
250  {
251  return *this;
252  }
253 
254  uint32 GetMapId() const { return m_mapId; }
255 
257 };
258 
265 
266 #endif // Trinity_game_Position_h__
float x
Definition: Vector3.h:62
TC_GAME_API ByteBuffer & operator<<(ByteBuffer &buf, Position::PositionXYStreamer const &streamer)
Definition: Position.cpp:167
Definition: Position.h:228
PositionXYZOStreamer(Position &pos)
Definition: Position.h:48
Definition: ByteBuffer.h:70
Position::PositionXYZOStreamer PositionXYZOStream()
Definition: Position.h:128
float GetExactDist(Position const *pos) const
Definition: Position.h:167
Position::PositionXYStreamer PositionXYStream()
Definition: Position.h:126
float GetExactDist2dSq(float x, float y) const
Definition: Position.h:132
#define M_PI
Definition: Common.h:163
float m_positionY
Definition: Position.h:53
void GetPosition(float &x, float &y, float &z) const
Definition: Position.h:114
void SetOrientation(float orientation)
Definition: Position.h:99
Position * Pos
Definition: Position.h:43
float m_positionX
Definition: Position.h:52
static float NormalizeOrientation(float o)
Definition: Position.h:211
Position * Pos
Definition: Position.h:49
Position::PositionXYZStreamer PositionXYZStream()
Definition: Position.h:127
float y
Definition: Vector3.h:62
Definition: Vector3.h:58
void Relocate(float x, float y, float z)
Definition: Position.h:72
bool IsInDist2d(Position const *pos, float dist) const
Definition: Position.h:190
uint32 m_mapId
Definition: Position.h:256
float GetExactDist2d(const float x, const float y) const
Definition: Position.h:137
float GetExactDist2d(Position const *pos) const
Definition: Position.h:147
float GetOrientation() const
Definition: Position.h:107
void Relocate(float x, float y)
Definition: Position.h:67
WorldLocation GetWorldLocation() const
Definition: Position.h:249
WorldLocation(uint32 mapId=MAPID_INVALID, float x=0.f, float y=0.f, float z=0.f, float o=0.f)
Definition: Position.h:231
string ToString(int i)
Definition: strutil.h:491
float GetRelativeAngle(float x, float y) const
Definition: Position.h:182
Definition: Position.h:46
Definition: Position.h:40
float GetPositionY() const
Definition: Position.h:105
G3D::int16 z
Definition: Vector3int16.h:46
void GetPosition(float &x, float &y) const
Definition: Position.h:109
Position(float x=0, float y=0, float z=0, float o=0)
Definition: Position.h:29
float GetExactDistSq(float x, float y, float z) const
Definition: Position.h:152
PositionXYStreamer(Position &pos)
Definition: Position.h:36
float GetPositionZ() const
Definition: Position.h:106
bool operator==(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Definition: GridDefines.h:160
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
float GetRelativeAngle(Position const *pos) const
Definition: Position.h:177
uint32 GetMapId() const
Definition: Position.h:254
float m_positionZ
Definition: Position.h:54
Position GetPosition() const
Definition: Position.h:124
void Relocate(Position const *pos)
Definition: Position.h:87
float GetExactDist2dSq(Position const *pos) const
Definition: Position.h:142
bool IsInDist2d(float x, float y, float dist) const
Definition: Position.h:185
Position * Pos
Definition: Position.h:37
void WorldRelocate(uint32 mapId=MAPID_INVALID, float x=0.f, float y=0.f, float z=0.f, float o=0.f)
Definition: Position.h:243
float z
Definition: Vector3.h:62
bool IsInDist(Position const *pos, float dist) const
Definition: Position.h:200
Position(Position const &loc)
Definition: Position.h:32
#define TC_GAME_API
Definition: Define.h:134
float GetExactDist(float x, float y, float z) const
Definition: Position.h:157
Definition: Position.h:27
#define MAPID_INVALID
Definition: Position.h:226
TC_GAME_API ByteBuffer & operator>>(ByteBuffer &buf, Position::PositionXYStreamer const &streamer)
Definition: Position.cpp:174
void Relocate(G3D::Vector3 const &pos)
Definition: Position.h:92
void Relocate(Position const &pos)
Definition: Position.h:82
G3D::int16 x
Definition: Vector2int16.h:37
void GetPosition(float &x, float &y, float &z, float &o) const
Definition: Position.h:119
float GetPositionX() const
Definition: Position.h:104
void Relocate(float x, float y, float z, float orientation)
Definition: Position.h:77
Pos
Definition: boss_gothik.cpp:115
PositionXYZStreamer(Position &pos)
Definition: Position.h:42
Definition: Position.h:34
void WorldRelocate(WorldLocation const &loc)
Definition: Position.h:237
WorldLocation(WorldLocation const &loc)
Definition: Position.h:234
float m_orientation
Definition: Position.h:57
bool IsInDist(float x, float y, float z, float dist) const
Definition: Position.h:195
float GetExactDistSq(Position const *pos) const
Definition: Position.h:162
bool operator!=(Position const &a)
Definition: Position.h:62