Steeriously  0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Transformations.hpp
1 #ifndef TRANSFORMATIONS_HPP
2 #define TRANSFORMATIONS_HPP
3 
4 #include <vector>
5 
6 #include <steeriously/Matrix.hpp>
7 #include <steeriously/Vector2.hpp>
8 #include <steeriously/VectorMath.hpp>
9 
10 namespace steer //steeriously namepsace
11 {
12 
26  inline std::vector<steer::Vector2> WorldTransform(std::vector<steer::Vector2> &points,
27  const steer::Vector2 &pos,
28  const steer::Vector2 &forward,
29  const steer::Vector2 &side,
30  const steer::Vector2 &scale)
31  {
32  //copy the original vertices into the buffer about to be transformed
33  std::vector<steer::Vector2> TranVector2Ds = points;
34 
35  //create a transformation matrix
36  steer::Matrix2D matTransform;
37 
38  //scale
39  if ( (scale.x != 1.0) || (scale.y != 1.0) )
40  {
41  matTransform.Scale(scale.x, scale.y);
42  }
43 
44  //rotate
45  matTransform.Rotate(forward, side);
46 
47  //and translate
48  matTransform.Translate(pos.x, pos.y);
49 
50  //now transform the object's vertices
51  matTransform.TransformVector2Ds(TranVector2Ds);
52 
53  return TranVector2Ds;
54  }
55 
67  inline std::vector<steer::Vector2> WorldTransform(std::vector<steer::Vector2> &points,
68  const steer::Vector2 &pos,
69  const steer::Vector2 &forward,
70  const steer::Vector2 &side)
71  {
72  //copy the original vertices into the buffer about to be transformed
73  std::vector<steer::Vector2> TranVector2Ds = points;
74 
75  //create a transformation matrix
76  steer::Matrix2D matTransform;
77 
78  //rotate
79  matTransform.Rotate(forward, side);
80 
81  //and translate
82  matTransform.Translate(pos.x, pos.y);
83 
84  //now transform the object's vertices
85  matTransform.TransformVector2Ds(TranVector2Ds);
86 
87  return TranVector2Ds;
88  }
89 
102  const steer::Vector2 &AgentHeading,
103  const steer::Vector2 &AgentSide,
104  const steer::Vector2 &AgentPosition)
105  {
106  //make a copy of the point
107  steer::Vector2 TransPoint = point;
108 
109  //create a transformation matrix
110  steer::Matrix2D matTransform;
111 
112  //rotate
113  matTransform.Rotate(AgentHeading, AgentSide);
114 
115  //and translate
116  matTransform.Translate(AgentPosition.x, AgentPosition.y);
117 
118  //now transform the vertices
119  matTransform.TransformVector2Ds(TransPoint);
120 
121  return TransPoint;
122  }
123 
134  const steer::Vector2 &AgentHeading,
135  const steer::Vector2 &AgentSide)
136  {
137  //make a copy of the point
138  steer::Vector2 TransVec = vec;
139 
140  //create a transformation matrix
141  steer::Matrix2D matTransform;
142 
143  //rotate
144  matTransform.Rotate(AgentHeading, AgentSide);
145 
146  //now transform the vertices
147  matTransform.TransformVector2Ds(TransVec);
148 
149  return TransVec;
150  }
151 
164  const steer::Vector2 &AgentHeading,
165  const steer::Vector2 &AgentSide,
166  const steer::Vector2 &AgentPosition)
167  {
168 
169  //make a copy of the point
170  steer::Vector2 TransPoint = point;
171 
172  //create a transformation matrix
173  steer::Matrix2D matTransform;
174 
175  float Tx = steer::VectorMath::dotProduct(AgentPosition.GetReverse(), AgentHeading);
176  float Ty = steer::VectorMath::dotProduct(AgentPosition.GetReverse(), AgentSide);
177 
178  //create the transformation matrix
179  matTransform._11(AgentHeading.x); matTransform._12(AgentSide.x);
180  matTransform._21(AgentHeading.y); matTransform._22(AgentSide.y);
181  matTransform._31(Tx); matTransform._32(Ty);
182 
183  //now transform the vertices
184  matTransform.TransformVector2Ds(TransPoint);
185 
186  return TransPoint;
187  }
188 
199  const steer::Vector2 &AgentHeading,
200  const steer::Vector2 &AgentSide)
201  {
202 
203  //make a copy of the point
204  steer::Vector2 TransPoint = vec;
205 
206  //create a transformation matrix
207  steer::Matrix2D matTransform;
208 
209  //create the transformation matrix
210  matTransform._11(AgentHeading.x); matTransform._12(AgentSide.x);
211  matTransform._21(AgentHeading.y); matTransform._22(AgentSide.y);
212 
213  //now transform the vertices
214  matTransform.TransformVector2Ds(TransPoint);
215 
216  return TransPoint;
217  }
218 
225  inline void Vec2DRotateAroundOrigin(steer::Vector2& v, float ang)
226  {
227  //create a transformation matrix
228  steer::Matrix2D mat;
229 
230  //rotate
231  mat.Rotate(ang);
232 
233  //now transform the object's vertices
234  mat.TransformVector2Ds(v);
235  }
236 
253  inline std::vector<steer::Vector2> CreateWhiskers(unsigned int NumWhiskers,
254  float WhiskerLength,
255  float fov,
256  steer::Vector2 facing,
257  steer::Vector2 origin)
258  {
259  //this is the magnitude of the angle separating each whisker
260  float SectorSize = fov/(float)(NumWhiskers-1);
261 
262  std::vector<steer::Vector2> whiskers;
263  steer::Vector2 temp;
264  float angle = -fov*0.5;
265 
266  for (unsigned int w=0; w<NumWhiskers; ++w)
267  {
268  //create the whisker extending outwards at this angle
269  temp = facing;
270  Vec2DRotateAroundOrigin(temp, angle);
271  whiskers.push_back(origin + WhiskerLength * temp);
272 
273  angle+=SectorSize;
274  }
275 
276  return whiskers;
277  }
278 
279 } //namespace steeriously
280 
281 #endif //TRANSFORMATIONS_HPP
282