Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
PhysicsGeometry.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_PHYSICS_GEOMETRY_H
22 #define GF_PHYSICS_GEOMETRY_H
23 
24 #include "Circ.h"
25 #include "Polygon.h"
26 #include "Rect.h"
27 #include "Vector.h"
28 #include "Matrix.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  class RenderTarget;
36 
37  /**
38  * @ingroup game
39  * @brief The geometry of a physics body
40  *
41  * The geometry is defined in model coordinates.
42  *
43  * @sa gf::PhysicsBody
44  */
46  public:
47  /**
48  * @brief The type of geometry
49  */
50  enum class Type {
51  Circle, ///< A circle (see gf::CircleGeometry)
52  Polygon, ///< A polygon (see gf::PolygonGeometry)
53  };
54 
55  /**
56  * @brief Constructor
57  *
58  * @param type The type of the geometry
59  */
61  : m_type(type)
62  {
63 
64  }
65 
66  /**
67  * @brief Destructor
68  */
69  virtual ~PhysicsGeometry();
70 
71  /**
72  * @brief Get the type of the geometry
73  */
74  Type getType() const {
75  return m_type;
76  }
77 
78  /**
79  * @brief Compute the area of the geometry
80  *
81  * @returns The area in world units
82  */
83  virtual float getArea() const = 0;
84 
85  /**
86  * @brief Get a bouding circle
87  *
88  * The circle may not be the [minimum bouding circle](https://en.wikipedia.org/wiki/Smallest-circle_problem).
89  *
90  * @returns A bounding circle
91  */
92  virtual CircF getBoundingCircle() const = 0;
93 
94  /**
95  * @brief Render the geometry
96  *
97  * @param target The render target
98  * @param position The position of the geometry
99  * @param angle The angle of the geometry
100  */
101  virtual void renderAt(RenderTarget& target, Vector2f position, float angle) const = 0;
102 
103  private:
104  Type m_type;
105  };
106 
107  /**
108  * @brief A circle physics geometry
109  */
111  public:
112  /**
113  * @brief Constructor
114  *
115  * The geometry is centered in @f$ (0,0) @f$
116  *
117  * @param radius The radius of the circle
118  */
119  CircleGeometry(float radius);
120 
121  /**
122  * @brief Constructor
123  *
124  * @param circle The base circle
125  */
126  CircleGeometry(CircF circle);
127 
128  /**
129  * @brief Get the circle
130  *
131  * @returns The current circle
132  */
133  const CircF& get() const;
134 
135  virtual float getArea() const override;
136  virtual CircF getBoundingCircle() const override;
137  virtual void renderAt(RenderTarget& target, Vector2f position, float angle) const override;
138 
139  private:
140  CircF m_circle;
141  };
142 
143  /**
144  * @brief A polygon physics geometry
145  *
146  * This geometry includes rectangles.
147  */
149  public:
150  /**
151  * @brief Constructor
152  *
153  * @param polygon The base polygon
154  */
155  PolygonGeometry(Polygon polygon);
156 
157  /**
158  * @brief Constructor
159  *
160  * The geometry is centered in @f$ (0,0) @f$
161  *
162  * @param size The size of the rectangle
163  */
164  PolygonGeometry(Vector2f size);
165 
166  /**
167  * @brief Constructor
168  *
169  * @param rectangle The base rectangle
170  */
171  PolygonGeometry(RectF rectangle);
172 
173  /**
174  * @brief Get the polygon
175  *
176  * @returns The current polygon
177  */
178  const Polygon& get() const;
179 
180  virtual float getArea() const override;
181  virtual CircF getBoundingCircle() const override;
182  virtual void renderAt(RenderTarget& target, Vector2f position, float angle) const override;
183 
184  private:
185  void computeBoundingCircle();
186 
187  private:
188  Polygon m_polygon;
189  CircF m_boundingCircle;
190  };
191 
192 #ifndef DOXYGEN_SHOULD_SKIP_THIS
193 }
194 #endif
195 }
196 
197 #endif // GF_PHYSICS_GEOMETRY_H
Type
The type of geometry.
Definition: PhysicsGeometry.h:50
virtual void renderAt(RenderTarget &target, Vector2f position, float angle) const override
Render the geometry.
virtual CircF getBoundingCircle() const =0
Get a bouding circle.
CircleGeometry(CircF circle)
Constructor.
virtual void renderAt(RenderTarget &target, Vector2f position, float angle) const override
Render the geometry.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
A polygon (see gf::PolygonGeometry)
A circle physics geometry.
Definition: PhysicsGeometry.h:110
A polygon physics geometry.
Definition: PhysicsGeometry.h:148
PhysicsGeometry(Type type)
Constructor.
Definition: PhysicsGeometry.h:60
virtual float getArea() const =0
Compute the area of the geometry.
PolygonGeometry(Polygon polygon)
Constructor.
virtual CircF getBoundingCircle() const override
Get a bouding circle.
The namespace for gf classes.
Definition: Action.h:34
virtual CircF getBoundingCircle() const override
Get a bouding circle.
virtual float getArea() const override
Compute the area of the geometry.
virtual ~PhysicsGeometry()
Destructor.
PolygonGeometry(Vector2f size)
Constructor.
virtual void renderAt(RenderTarget &target, Vector2f position, float angle) const =0
Render the geometry.
A convex polygon.
Definition: Polygon.h:52
PolygonGeometry(RectF rectangle)
Constructor.
const CircF & get() const
Get the circle.
A circle (see gf::CircleGeometry)
CircleGeometry(float radius)
Constructor.
The geometry of a physics body.
Definition: PhysicsGeometry.h:45
#define GF_API
Definition: Portability.h:35
virtual float getArea() const override
Compute the area of the geometry.
const Polygon & get() const
Get the polygon.
Type getType() const
Get the type of the geometry.
Definition: PhysicsGeometry.h:74