This repository was archived by the owner on Feb 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathRectangleColliderComponent.h
More file actions
executable file
·303 lines (285 loc) · 11.4 KB
/
RectangleColliderComponent.h
File metadata and controls
executable file
·303 lines (285 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#pragma once
#include "BaseColliderComponent.h"
#include "../../Helpers/Rect.h"
namespace star
{
struct Context;
class CircleColliderComponent;
class RectangleColliderComponent final : public BaseColliderComponent
{
public:
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// If you use this constructor, make sure to
/// a) Add a <see cref="SpriteComponent"/> or <see cref="SpriteSheetComponent"/>
/// before you add this component,
/// the width and height of the visible part of the sprite will be used to
/// determine the size of the colliders.
/// b) Use SetCollisionRectSize to define the size of the collider.
/// Using collision layers is strongly advised, so use this constructor with care.
/// The collider will be added to the default collision group.
/// </summary>
RectangleColliderComponent();
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// Using collision layers is strongly advised, so use this constructor with care.
/// The collider will be added to the default collision group.
/// </summary>
/// <param name="size">The size of the collider.</param>
RectangleColliderComponent(const vec2 & size);
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// Using collision layers is strongly advised, so use this constructor with care.
/// The collider will be added to the default collision group.
/// </summary>
/// <param name="width">The width of the collider.</param>
/// <param name="height">The width of the collider.</param>
RectangleColliderComponent(
float32 width,
float32 height
);
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// If you use this constructor, make sure to
/// a) Add a <see cref="SpriteComponent"/> or <see cref="SpriteSheetComponent"/>
/// before you add this component,
/// the width and height of the visible part of the sprite will be used to
/// determine the size of the colliders.
/// b) Use SetCollisionRectSize to define the size of the collider.
/// </summary>
/// <param name="layer">The name of the layer to add the component to.</param>
RectangleColliderComponent(
const tstring & layers
);
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// If you use this constructor, make sure to
/// a) Add a <see cref="SpriteComponent"/> or <see cref="SpriteSheetComponent"/>
/// before you add this component,
/// the width and height of the visible part of the sprite will be used to
/// determine the size of the colliders.
/// b) Use SetCollisionRectSize to define the size of the collider.
/// </summary>
/// <param name="layers">An array of layers to add the component to.</param>
/// <param name="n">The number of layers in the array.</param>
RectangleColliderComponent(
const tstring* layers,
uint8 n
);
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// </summary>
/// <param name="size">The size of the collider.</param>
/// <param name="layer">The name of the layer to add the component to.</param>
RectangleColliderComponent(
const vec2 & size,
const tstring & layer
);
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// </summary>
/// <param name="size">The size of the collider.</param>
/// <param name="layers">An array of layers to add the component to.</param>
/// <param name="n">The number of layers in the array.</param>
RectangleColliderComponent(
const vec2 & size,
const tstring* layers,
uint8 n
);
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// </summary>
/// <param name="width">The width of the collider.</param>
/// <param name="height">The width of the collider.</param>
/// <param name="layer">The name of the layer to add the component to.</param>
RectangleColliderComponent(
float32 width,
float32 height,
const tstring & layer
);
/// <summary>
/// Initializes a new instance of the <see cref="RectangleColliderComponent"/> class.
/// </summary>
/// <param name="width">The width of the collider.</param>
/// <param name="height">The width of the collider.</param>
/// <param name="layers">An array of layers to add the component to.</param>
/// <param name="n">The number of layers in the array.</param>
RectangleColliderComponent(
float32 width,
float32 height,
const tstring* layers,
uint8 n
);
/// <summary>
/// Finalizes an instance of the <see cref="RectangleColliderComponent"/> class.
/// </summary>
~RectangleColliderComponent();
/// <summary>
/// Determines if there is a collision between a provided point and this collider.
/// </summary>
/// <param name="point">The point to check the collision with.</param>
/// <returns>True if there is a collision</returns>
bool CollidesWithPoint2D(const vec2 & point2D) const;
/// <summary>
/// Determines if there is a collision between a provided line and this collider.
/// </summary>
/// <param name="point1">First point of the line.</param>
/// <param name="point2">Second point of the line.</param>
/// <returns>True if there is a collision</returns>
bool CollidesWithLine2D(
const vec2& point2D1,
const vec2& point2D2
) const;
/// <summary>
/// Determines if there is a collision between an other collider component and this collider.
/// This function is internally used by the Collision Manager,
/// using this function yourself is not advised.
/// </summary>
/// <param name="other">The other collider component.</param>
/// <returns>True if there is a collision</returns>
bool CollidesWith(const BaseColliderComponent* other) const;
/// <summary>
/// Gets the collision rectangle.
/// </summary>
/// <returns>The collision rectangle.</returns>
Rect GetCollisionRect() const;
/// <summary>
/// Gets the center point of the collider.
/// </summary>
/// <returns>The center point</returns>
vec2 GetCenterPoint() const;
/// <summary>
/// Gets the unit vector in the x direction, correctly orientated according to the rotation
/// of this collider.
/// </summary>
/// <returns>The Unit vector.</returns>
vec2 GetOrientatedUnitVecX() const;
/// <summary>
/// Gets the unit vector in the y direction, correctly orientated according to the rotation
/// of this collider.
/// </summary>
/// <returns>The Unit vector.</returns>
vec2 GetOrientatedUnitVecY() const;
/// <summary>
/// Gets the width of the collision rectangle.
/// </summary>
/// <returns>The width of the collision rectangle</returns>
float32 GetCollisionRectWidth() const;
/// <summary>
/// Gets the height of the collision rectangle.
/// </summary>
/// <returns>The height of the collision rectangle</returns>
float32 GetCollisionRectHeight() const;
/// <summary>
/// Gets the size of the colliison rectangle.
/// </summary>
/// <param name="outVec">The size of the collision rectangle.</param>
void GetColliisonRectSize(vec2& outVec) const;
/// <summary>
/// Sets the size of the collision rectangle.
/// </summary>
/// <param name="width">The new width of the collision rectangle.</param>
/// <param name="height">The new height of the collision rectangle.</param>
void SetCollisionRectSize(
float32 width,
float32 height);
/// <summary>
/// Sets the size of the collision rectangle.
/// </summary>
/// <param name="size">The new size of the collision rectangle.</param>
void SetCollisionRectSize(const vec2& size);
/// <summary>
/// Sets the width of the collision rectangle.
/// </summary>
/// <param name="width">The new width of the collision rectangle.</param>
void SetCollisionRectWidth(float32 width);
/// <summary>
/// Sets the height of the collision rectangle.
/// </summary>
/// <param name="width">The new height of the collision rectangle.</param>
void SetCollisionRectHeight(float32 height);
protected:
/// <summary>
/// Initializes the collider component.
/// </summary>
void InitializeColliderComponent();
/// <summary>
/// Draws this instance.
/// </summary>
void Draw();
/// <summary>
/// Collision check between 2 object orientated rectangles.
/// </summary>
/// <param name="rect1">The first rectangle.</param>
/// <param name="rect2">The second rectangle.</param>
/// <returns>True if a collision exists.</returns>
bool OOBBRectangleRectangleCollision(
const Rect& rect1,
const Rect& rect2
) const;
/// <summary>
/// Collision check between 2 axis aligned rectangles.
/// </summary>
/// <param name="rect1">The first rectangle.</param>
/// <param name="rect2">The second rectangle.</param>
/// <returns>True if a collision exists.</returns>
bool AABBRectangleRectangleCollision(
const Rect& rect1,
const Rect& rect2
) const;
/// <summary>
/// Test for the Seperating axis theorem.
/// Tests if 2 rectangles overlap over a certain axis.
/// </summary>
/// <param name="rect1">The first rectangle.</param>
/// <param name="rect2">The second rectangle.</param>
/// <param name="axis">The axis to check the overlap on.</param>
/// <returns>True if an overlap occurs</returns>
bool CalculateAxisSpecificCollision(
const Rect& rect1,
const Rect& rect2,
const vec2& axis
) const;
/// <summary>
/// Test for the Seperating axis theorem.
/// Tests if a rectangle and a line overlap over a certain axis.
/// </summary>
/// <param name="rect">The rectangle.</param>
/// <param name="point1">The first point of the line.</param>
/// <param name="point2">The second point of the line.</param>
/// <param name="axis">The axis to check the overlap on.</param>
/// <returns>True if an overlap occurs</returns>
bool CalculateAxisSpecificCollision(
const Rect& rect,
const vec2& point1,
const vec2& point2,
const vec2& axis
) const;
/// <summary>
/// Test for the Seperating axis theorem.
/// Tests if a rectangle and a point overlap over a certain axis.
/// </summary>
/// <param name="rect1">The rectangle.</param>
/// <param name="point1">The point.</param>
/// <param name="axis">The axis to check the overlap on.</param>
/// <returns>True if an overlap occurs</returns>
bool CalculateAxisSpecificCollision(
const Rect& rect1,
const vec2& point1,
const vec2& axis
) const;
Rect m_CollisionRect;
private:
vec2 m_CustomColliderSize;
/// <summary>
/// Creates the dimensions of the collider, based on the size of the visible part of the
/// optionally provided <see cref="SpriteComponent"/> or <see cref="SpriteSheetComponent"/>,
/// or on the size passed by the SetCollisionRectSize method.
/// </summary>
void CreateDimensions();
RectangleColliderComponent(const RectangleColliderComponent& t);
RectangleColliderComponent(RectangleColliderComponent&& t);
RectangleColliderComponent& operator=(const RectangleColliderComponent& t);
RectangleColliderComponent& operator=(RectangleColliderComponent&& t);
};
}