Gamedev Framework (gf)  0.6.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  struct RenderStates;
36  class RenderTarget;
37 
38  /**
39  * @ingroup game
40  * @brief The geometry of a physics body
41  *
42  * The geometry is defined in model coordinates.
43  *
44  * @sa gf::PhysicsBody
45  */
47  public:
48  /**
49  * @brief The type of geometry
50  */
51  enum class Type {
52  Circle, ///< A circle (see gf::CircleGeometry)
53  Polygon, ///< A polygon (see gf::PolygonGeometry)
54  };
55 
56  /**
57  * @brief Constructor
58  *
59  * @param type The type of the geometry
60  */
62  : m_type(type)
63  {
64 
65  }
66 
67  /**
68  * @brief Destructor
69  */
70  virtual ~PhysicsGeometry();
71 
72  /**
73  * @brief Get the type of the geometry
74  */
75  Type getType() const {
76  return m_type;
77  }
78 
79  /**
80  * @brief Compute the area of the geometry
81  *
82  * @returns The area in world units
83  */
84  virtual float getArea() const = 0;
85 
86  /**
87  * @brief Get a bouding circle
88  *
89  * The circle may not be the [minimum bouding circle](https://en.wikipedia.org/wiki/Smallest-circle_problem).
90  *
91  * @returns A bounding circle
92  */
93  virtual CircF getBoundingCircle() const = 0;
94 
95  /**
96  * @brief Render the geometry
97  *
98  * @param target The render target
99  * @param states The render states to use for drawing
100  * @param position The position of the geometry
101  * @param angle The angle of the geometry
102  */
103  virtual void renderAt(RenderTarget& target, const RenderStates& states, Vector2f position, float angle) const = 0;
104 
105  private:
106  Type m_type;
107  };
108 
109  /**
110  * @brief A circle physics geometry
111  */
113  public:
114  /**
115  * @brief Constructor
116  *
117  * The geometry is centered in @f$ (0,0) @f$
118  *
119  * @param radius The radius of the circle
120  */
121  CircleGeometry(float radius);
122 
123  /**
124  * @brief Constructor
125  *
126  * @param circle The base circle
127  */
128  CircleGeometry(CircF circle);
129 
130  /**
131  * @brief Get the circle
132  *
133  * @returns The current circle
134  */
135  const CircF& get() const;
136 
137  virtual float getArea() const override;
138  virtual CircF getBoundingCircle() const override;
139  virtual void renderAt(RenderTarget& target, const RenderStates& states, Vector2f position, float angle) const override;
140 
141  private:
142  CircF m_circle;
143  };
144 
145  /**
146  * @brief A polygon physics geometry
147  *
148  * This geometry includes rectangles.
149  */
151  public:
152  /**
153  * @brief Constructor
154  *
155  * @param polygon The base polygon
156  */
157  PolygonGeometry(Polygon polygon);
158 
159  /**
160  * @brief Constructor
161  *
162  * The geometry is centered in @f$ (0,0) @f$
163  *
164  * @param size The size of the rectangle
165  */
166  PolygonGeometry(Vector2f size);
167 
168  /**
169  * @brief Constructor
170  *
171  * @param rectangle The base rectangle
172  */
173  PolygonGeometry(RectF rectangle);
174 
175  /**
176  * @brief Get the polygon
177  *
178  * @returns The current polygon
179  */
180  const Polygon& get() const;
181 
182  virtual float getArea() const override;
183  virtual CircF getBoundingCircle() const override;
184  virtual void renderAt(RenderTarget& target, const RenderStates& states, Vector2f position, float angle) const override;
185 
186  private:
187  void computeBoundingCircle();
188 
189  private:
190  Polygon m_polygon;
191  CircF m_boundingCircle;
192  };
193 
194 #ifndef DOXYGEN_SHOULD_SKIP_THIS
195 }
196 #endif
197 }
198 
199 #endif // GF_PHYSICS_GEOMETRY_H
Type
The type of geometry.
Definition: PhysicsGeometry.h:51
virtual CircF getBoundingCircle() const =0
Get a bouding circle.
CircleGeometry(CircF circle)
Constructor.
virtual void renderAt(RenderTarget &target, const RenderStates &states, Vector2f position, float angle) const override
Render the geometry.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
A circle physics geometry.
Definition: PhysicsGeometry.h:112
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
const CircF & get() const
Get the circle.
A polygon physics geometry.
Definition: PhysicsGeometry.h:150
virtual void renderAt(RenderTarget &target, const RenderStates &states, Vector2f position, float angle) const override
Render the geometry.
PhysicsGeometry(Type type)
Constructor.
Definition: PhysicsGeometry.h:61
virtual float getArea() const =0
Compute the area of the geometry.
const Polygon & get() const
Get the polygon.
PolygonGeometry(Polygon polygon)
Constructor.
A polygon (see gf::PolygonGeometry)
virtual CircF getBoundingCircle() const override
Get a bouding circle.
Type getType() const
Get the type of the geometry.
Definition: PhysicsGeometry.h:75
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.
virtual void renderAt(RenderTarget &target, const RenderStates &states, Vector2f position, float angle) const =0
Render the geometry.
PolygonGeometry(Vector2f size)
Constructor.
A circle (see gf::CircleGeometry)
A convex polygon.
Definition: Polygon.h:52
PolygonGeometry(RectF rectangle)
Constructor.
CircleGeometry(float radius)
Constructor.
The geometry of a physics body.
Definition: PhysicsGeometry.h:46
#define GF_API
Definition: Portability.h:35
virtual float getArea() const override
Compute the area of the geometry.