Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
Collision.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_COLLISION_H
22 #define GF_COLLISION_H
23 
24 #include "Circ.h"
25 #include "Polygon.h"
26 #include "Portability.h"
27 #include "Rect.h"
28 #include "Vector.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  struct Transform;
36 
37  /**
38  * @ingroup game
39  * @brief Data about the collision between two objects
40  *
41  * @sa [How to Create a Custom Physics Engine](http://gamedevelopment.tutsplus.com/series/how-to-create-a-custom-physics-engine--gamedev-12715)
42  */
44  Vector2f normal; ///< Collision normal
45  float depth; ///< Penetration depth
46  };
47 
48  /**
49  * @relates Penetration
50  * @brief Check if two circles collides
51  *
52  * @param lhs First circle
53  * @param lhsTrans Transformation of the first circle
54  * @param rhs Second circle
55  * @param rhsTrans Transformation of the second circle
56  * @param p Data to fill if there is a collision
57  * @return True if there is a collision
58  */
59  GF_API bool collides(const CircF& lhs, const Transform& lhsTrans, const CircF& rhs, const Transform& rhsTrans, Penetration& p);
60 
61  /**
62  * @relates Penetration
63  * @brief Check if two circles collides
64  *
65  * @param lhs First circle
66  * @param rhs Second circle
67  * @param p Data to fill if there is a collision
68  * @return True if there is a collision
69  */
70  GF_API bool collides(const CircF& lhs, const CircF& rhs, Penetration& p);
71 
72  /**
73  * @relates Penetration
74  * @brief Check if a rectangle collides with a circle
75  *
76  * @param lhs The rectangle
77  * @param rhs The circle
78  * @param p Data to fill if there is a collision
79  * @return True if there is a collision
80  */
81  GF_API bool collides(const RectF& lhs, const CircF& rhs, Penetration& p);
82 
83  /**
84  * @relates Penetration
85  * @brief Check if a circle collides with a rectangle
86  *
87  * @param lhs The circle
88  * @param rhs The rectangle
89  * @param p Data to fill if there is a collision
90  * @return True if there is a collision
91  */
92  GF_API bool collides(const CircF& lhs, const RectF& rhs, Penetration& p);
93 
94  /**
95  * @relates Penetration
96  * @brief Check if two rectangles collides
97  *
98  * @param lhs First rectangle
99  * @param rhs Second rectangle
100  * @param p Data to fill if there is a collision
101  * @return True if there is a collision
102  */
103  GF_API bool collides(const RectF& lhs, const RectF& rhs, Penetration& p);
104 
105  /**
106  * @relates Penetration
107  * @brief Check if a circle collides with a polygon
108  *
109  * @param lhs The circle
110  * @param lhsTrans Transformation of the circle
111  * @param rhs The polygon
112  * @param rhsTrans Transformation of the polygon
113  * @param p Data to fill if there is a collision
114  * @return True if there is a collision
115  */
116  GF_API bool collides(const CircF& lhs, const Transform& lhsTrans, const Polygon& rhs, const Transform& rhsTrans, Penetration& p);
117 
118  /**
119  * @relates Penetration
120  * @brief Check if a circle collides with a polygon
121  *
122  * @param lhs The circle
123  * @param rhs The polygon
124  * @param p Data to fill if there is a collision
125  * @return True if there is a collision
126  */
127  GF_API bool collides(const CircF& lhs, const Polygon& rhs, Penetration& p);
128 
129  /**
130  * @relates Penetration
131  * @brief Check if a polygon collides with a circle
132  *
133  * @param lhs The polygon
134  * @param lhsTrans Transformation of the polygon
135  * @param rhs The circle
136  * @param rhsTrans Transformation of the circle
137  * @param p Data to fill if there is a collision
138  * @return True if there is a collision
139  */
140  GF_API bool collides(const Polygon& lhs, const Transform& lhsTrans, const CircF& rhs, const Transform& rhsTrans, Penetration& p);
141 
142  /**
143  * @relates Penetration
144  * @brief Check if a polygon collides with a circle
145  *
146  * @param lhs The polygon
147  * @param rhs The circle
148  * @param p Data to fill if there is a collision
149  * @return True if there is a collision
150  */
151  GF_API bool collides(const Polygon& lhs, const CircF& rhs, Penetration& p);
152 
153  /**
154  * @relates Penetration
155  * @brief Check if two polygons collides
156  *
157  * @param lhs First polygon
158  * @param lhsTrans Transformation of the first polygon
159  * @param rhs Second polygon
160  * @param rhsTrans Transformation of the second polygon
161  * @param p Data to fill if there is a collision
162  * @return True if there is a collision
163  */
164  GF_API bool collides(const Polygon& lhs, const Transform& lhsTrans, const Polygon& rhs, const Transform& rhsTrans, Penetration& p);
165 
166  /**
167  * @relates Penetration
168  * @brief Check if two polygons collides
169  *
170  * @param lhs First polygon
171  * @param rhs Second polygon
172  * @param p Data to fill if there is a collision
173  * @return True if there is a collision
174  */
175  GF_API bool collides(const Polygon& lhs, const Polygon& rhs, Penetration& p);
176 
177 #ifndef DOXYGEN_SHOULD_SKIP_THIS
178 }
179 #endif
180 }
181 
182 #endif // GF_COLLISION_H
bool collides(const Polygon &lhs, const Polygon &rhs, Penetration &p)
Check if two polygons collides.
bool collides(const Polygon &lhs, const Transform &lhsTrans, const CircF &rhs, const Transform &rhsTrans, Penetration &p)
Check if a polygon collides with a circle.
A simple transformation (rotation then translation)
Definition: Transform.h:204
bool collides(const CircF &lhs, const Transform &lhsTrans, const CircF &rhs, const Transform &rhsTrans, Penetration &p)
Check if two circles collides.
bool collides(const Polygon &lhs, const CircF &rhs, Penetration &p)
Check if a polygon collides with a circle.
bool collides(const CircF &lhs, const RectF &rhs, Penetration &p)
Check if a circle collides with a rectangle.
bool collides(const Polygon &lhs, const Transform &lhsTrans, const Polygon &rhs, const Transform &rhsTrans, Penetration &p)
Check if two polygons collides.
Vector2f normal
Collision normal.
Definition: Collision.h:44
bool collides(const CircF &lhs, const CircF &rhs, Penetration &p)
Check if two circles collides.
The namespace for gf classes.
Definition: Action.h:34
float depth
Penetration depth.
Definition: Collision.h:45
A convex polygon.
Definition: Polygon.h:52
Data about the collision between two objects.
Definition: Collision.h:43
bool collides(const RectF &lhs, const CircF &rhs, Penetration &p)
Check if a rectangle collides with a circle.
bool collides(const CircF &lhs, const Polygon &rhs, Penetration &p)
Check if a circle collides with a polygon.
bool collides(const CircF &lhs, const Transform &lhsTrans, const Polygon &rhs, const Transform &rhsTrans, Penetration &p)
Check if a circle collides with a polygon.
#define GF_API
Definition: Portability.h:35
bool collides(const RectF &lhs, const RectF &rhs, Penetration &p)
Check if two rectangles collides.