UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
geometry.h
Go to the documentation of this file.
1 #ifndef UGDK_MATH_GEOMETRY_H_
2 #define UGDK_MATH_GEOMETRY_H_
3 
4 #include <ugdk/math/vector2D.h>
5 
6 #include <cmath>
7 #define GLM_FORCE_RADIANS
8 #include <glm/glm.hpp>
9 #include <glm/ext.hpp>
10 
11 
12 namespace ugdk {
13 namespace math {
14 
15 class Geometry {
16  public:
18  Geometry() : rotation_(0.0), matrix_(1.0f) {}
19 
21 
28  Geometry(const math::Vector2D& offset, const math::Vector2D scale = math::Vector2D(1.0, 1.0), double rot = 0.0)
29  : rotation_(-rot),
30  matrix_(float(scale.x*cos(-rot)), -float(scale.x*sin(-rot)), 0.0f, 0.0f, // First column
31  float(scale.y*sin(-rot)), float(scale.y*cos(-rot)), 0.0f, 0.0f,
32  0.0f, 0.0f, 1.0f, 0.0f,
33  float(offset.x), float(offset.y), 0.0f, 1.0f) {}
34 
35  // Destructor
36  ~Geometry() {}
37 
38  double rotation() const { return rotation_; }
39 
42  return math::Vector2D(matrix_[3].x, matrix_[3].y);
43  }
44 
45  void set_offset(const ugdk::math::Vector2D& _offset) {
46  ChangeOffset(_offset);
47  }
48 
49  void ChangeOffset(const math::Vector2D& _offset) {
50  matrix_[3] = glm::vec4(_offset.x, _offset.y, 0.0, 1.0);
51  }
52 
53  void Compose(const Geometry& other) {
54  rotation_ += other.rotation_;
55  matrix_ *= other.matrix_;
56  }
57 
59  Compose(rhs);
60  return *this;
61  }
62 
63  Geometry operator * (const Geometry& rhs) const {
64  Geometry result(*this);
65  result.Compose(rhs);
66  return result;
67  }
68 
69  const glm::mat4& AsMat4() const {
70  return matrix_;
71  }
72 
74  double c = cos(rotation_);
75  if(c > 0.5)
76  return math::Vector2D( matrix_[0].x / cos(rotation_), matrix_[1].y / cos(rotation_));
77  else
78  return math::Vector2D(-matrix_[1].x / sin(rotation_), matrix_[0].y / sin(rotation_));
79  }
80 
81  private:
82  double rotation_;
83  glm::mat4 matrix_;
84 };
85 
86 } // namespace math
87 } // namespace ugdk
88 
89 #endif /* UGDK_MATH_MODIFIER_H_ */
Definition: vector2D.h:18
Geometry()
Creates an identity Geometry;.
Definition: geometry.h:18
Geometry(const math::Vector2D &offset, const math::Vector2D scale=math::Vector2D(1.0, 1.0), double rot=0.0)
Creates a new Geometry object with the specified values.
Definition: geometry.h:28
double x
Definition: vector2D.h:45
void ChangeOffset(const math::Vector2D &_offset)
Definition: geometry.h:49
math::Vector2D CalculateScale() const
Definition: geometry.h:73
ugdk::math::Vector2D offset() const
Calculates by how much this Geometry translates the rendering.
Definition: geometry.h:41
Definition: animation.h:11
double rotation() const
Definition: geometry.h:38
Geometry operator*(const Geometry &rhs) const
Definition: geometry.h:63
Geometry & operator*=(const Geometry &rhs)
Definition: geometry.h:58
~Geometry()
Definition: geometry.h:36
Definition: geometry.h:15
double y
Definition: vector2D.h:45
void set_offset(const ugdk::math::Vector2D &_offset)
Definition: geometry.h:45
void Compose(const Geometry &other)
Definition: geometry.h:53
const glm::mat4 & AsMat4() const
Definition: geometry.h:69