CheeseEngine
A 2D Game Engine
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1#pragma once
2#include <EventBus.hpp>
3#include <entt/entt.hpp>
4#include <glm/glm.hpp>
5#include <variant>
6
7namespace phys2d {
8
9/* Maths */
10constexpr float pi = 3.14159265;
11constexpr float pi2 = pi * 2;
12constexpr float inf = 2e5;
13
14/* Precision*/
15constexpr float epsilon = 1e-6;
16
17/* RigidBody*/
18constexpr float rb_defaultDamping = 0.01f;
19constexpr float rb_defaultRestitution = 0.5f;
20constexpr float rb_defaultFriction = 0.2f;
21constexpr float rb_defaultInvMass = 1.0f;
22constexpr float rb_defaultInvInertia = 1.0f;
23constexpr float rb_defaultGravityScale = 1.0f;
24
25/* Collider */
26constexpr glm::vec2 cd_defaultLocalOffset = {0.0f, 0.0f};
27constexpr bool cd_defaultTriggerStatus = false;
28
29enum class BodyType {
30 Static,
31 Kinematic,
32 Dynamic
34};
35
36enum class BodyShape {
37 Polygon,
38 Circle,
39 Capsule,
40 Segment,
42};
43
44struct AABB {
45 glm::vec2 lowerBound;
46 glm::vec2 upperBound;
47
48 AABB() = default;
55 AABB(const glm::vec2& lb, const glm::vec2& ub);
56
62 bool collidesWith(const AABB& other) const;
63};
64
71bool checkCollision(const AABB& aabb1, const AABB& aabb2);
72
74 glm::vec2 position{0.0f, 0.0f};
75 float rotation{0.0f};
76 float scaling{1.0f};
77
78 mutable bool dirty{true};
79 mutable glm::mat3 modelMatrix{1.0f};
80
86 void updateMatrix() const;
87
91 TransformComponent() = default;
92
96 TransformComponent(const glm::vec2& position, float rotation, float scaling);
97
103 const glm::vec2 getForward() const;
104
107
112 void setPosition(const glm::vec2& new_position);
113
119 void setRotation(const float new_angle);
120
126 void setScale(float new_scale);
128
137 void move(const glm::vec2& direction);
138
144 void rotate(float angle_delta);
145
151 void scale(float scale_coef);
152};
153
156
157 glm::vec2 linearVelocity{0.0f, 0.0f};
158 glm::vec2 force{0.0f, 0.0f};
160
161 float angularVelocity = 0.0f;
162 float torque = 0.0f;
163
164 float invMass =
168
171
174
179
186
202 RigidBodyComponent(BodyType bt, const glm::vec2& linearVelocity, const glm::vec2& force,
203 float gravityScale, float angularVelocity, float torque, float mass,
204 float inertia, float linearDamping, float angularDamping, float restitution,
205 float friction);
206};
207
209 std::vector<glm::vec2> vertices;
210};
211
213 float radius;
214};
215
217 float radius;
219};
220
222 glm::vec2 borders[2];
223};
224
226 std::vector<glm::vec2> points;
227};
228
229using ShapeData = std::variant<PolygonGeometry, CircleGeometry>;
230
231namespace ColliderLayers {
232constexpr uint32_t None = 0x0000;
233constexpr uint32_t Player = 0x0001;
234constexpr uint32_t PlayerProjectile = 0x0002;
235constexpr uint32_t Enemy = 0x0004;
236constexpr uint32_t EnemyProjectile = 0x0008;
237constexpr uint32_t Environment = 0x0010;
238constexpr uint32_t All = 0xFFFF;
239} // namespace ColliderLayers
240
248 uint32_t maskBits =
249 ColliderLayers::All & ~ColliderLayers::PlayerProjectile;
251
254 template <typename Geometry>
255 ColliderComponent(Geometry&& geom, const glm::vec2& offset = cd_defaultLocalOffset,
256 bool trigger = cd_defaultTriggerStatus,
260 if constexpr (std::is_same_v<std::decay_t<Geometry>, phys2d::PolygonGeometry>) {
263 *getPolygon() = std::forward<Geometry>(geom);
264 } else if constexpr (std::is_same_v<std::decay_t<Geometry>, phys2d::CircleGeometry>) {
267 *getCircle() = std::forward<Geometry>(geom);
268 }
269 }
270
277
283 const CircleGeometry* getCircle() const;
284
291
298
305};
306
308public:
313 PhysicsSystem(entt::registry& registry);
314
320 void update(float dt);
321
327 void setGravity(const glm::vec2& g);
328
334 glm::vec2 getGravity() const;
335
341 void setRegistry(entt::registry* registry);
342
349
350private:
352
354
355 void processImpulseAndPushback(entt::entity eA, entt::entity eB, const glm::vec2& normal,
356 float penetration, bool isTriggerA, bool isTriggerB);
357
358 bool collideCircleVsCircle(entt::entity e1, const TransformComponent& tc1,
359 const CircleGeometry& geometry1, entt::entity e2,
360 const TransformComponent& tc2, const CircleGeometry& geometry2);
361
362 bool collideCircleVsPolygon(entt::entity e1, const TransformComponent& tc1,
363 const CircleGeometry& geometry1, entt::entity e2,
364 const TransformComponent& tc2, const PolygonGeometry& geometry2,
365 bool flipNormal);
366
367 bool collidePolygonVsPolygon(entt::entity e1, const TransformComponent& tc1,
368 const PolygonGeometry& geometry1, entt::entity e2,
369 const TransformComponent& tc2, const PolygonGeometry& geometry2);
370
371private:
372 glm::vec2 gravity{0.0f, 0.0f};
373 entt::registry* registry;
375};
376
377} // namespace phys2d
Central event dispatcher connecting isolated engine systems.
Definition EventBus.hpp:90
Definition types.hpp:307
EventBus * eventBus
pointer to the system event bus
Definition types.hpp:374
entt::registry * registry
pointer to the registry for fetching entities
Definition types.hpp:373
bool collideCircleVsCircle(entt::entity e1, const TransformComponent &tc1, const CircleGeometry &geometry1, entt::entity e2, const TransformComponent &tc2, const CircleGeometry &geometry2)
bool collidePolygonVsPolygon(entt::entity e1, const TransformComponent &tc1, const PolygonGeometry &geometry1, entt::entity e2, const TransformComponent &tc2, const PolygonGeometry &geometry2)
void setGravity(const glm::vec2 &g)
sets the global gravity vector for the physics world
void update(float dt)
updates the physics simulation for the current frame
void setRegistry(entt::registry *registry)
updates the pointer to the entt registry
glm::vec2 gravity
global gravity vector applied to dynamic bodies
Definition types.hpp:372
void integrateForcesAndVelocities(float dt)
bool collideCircleVsPolygon(entt::entity e1, const TransformComponent &tc1, const CircleGeometry &geometry1, entt::entity e2, const TransformComponent &tc2, const PolygonGeometry &geometry2, bool flipNormal)
glm::vec2 getGravity() const
gets the current global gravity vector
PhysicsSystem(entt::registry &registry)
constructor for PhysicsSystem
void setEventBus(EventBus *eventBus)
sets the event bus for dispatching physics-related events (e.g., collisions)
void processImpulseAndPushback(entt::entity eA, entt::entity eB, const glm::vec2 &normal, float penetration, bool isTriggerA, bool isTriggerB)
constexpr uint32_t Enemy
Definition types.hpp:235
constexpr uint32_t Environment
Definition types.hpp:237
constexpr uint32_t Player
Definition types.hpp:233
constexpr uint32_t All
Definition types.hpp:238
constexpr uint32_t EnemyProjectile
Definition types.hpp:236
constexpr uint32_t PlayerProjectile
Definition types.hpp:234
constexpr uint32_t None
Definition types.hpp:232
Definition types.hpp:7
constexpr bool cd_defaultTriggerStatus
Definition types.hpp:27
std::variant< PolygonGeometry, CircleGeometry > ShapeData
Definition types.hpp:229
constexpr float pi
Definition types.hpp:10
constexpr float rb_defaultInvMass
Definition types.hpp:21
constexpr glm::vec2 cd_defaultLocalOffset
Definition types.hpp:26
constexpr float inf
Definition types.hpp:12
constexpr float rb_defaultGravityScale
Definition types.hpp:23
BodyShape
Definition types.hpp:36
@ Circle
perfect geometric circle, has radius
@ Capsule
capsule form, is constructed of two circles with same radius and rectangle
@ Segment
simple segment, has length
@ Polygon
convex polygon, has an array of points
BodyType
Definition types.hpp:29
@ Kinematic
zero mass, velocity is set by user, is moved by System
@ Static
zero mass, zero velocity, can be manually moved
constexpr float rb_defaultRestitution
Definition types.hpp:19
bool checkCollision(const AABB &aabb1, const AABB &aabb2)
checks the collision between two AABB-objects
constexpr float epsilon
Definition types.hpp:15
constexpr float pi2
Definition types.hpp:11
constexpr float rb_defaultFriction
Definition types.hpp:20
constexpr float rb_defaultDamping
Definition types.hpp:18
constexpr float rb_defaultInvInertia
Definition types.hpp:22
Definition types.hpp:44
AABB(const glm::vec2 &lb, const glm::vec2 &ub)
constructor for AABB-object
AABB()=default
bool collidesWith(const AABB &other) const
checks the collision between two AABB-objects
glm::vec2 upperBound
point with the biggest coords
Definition types.hpp:46
glm::vec2 lowerBound
point with the smallest coords
Definition types.hpp:45
Definition types.hpp:216
float radius
Definition types.hpp:217
float rectangleLen
Definition types.hpp:218
Definition types.hpp:212
float radius
Definition types.hpp:213
Definition types.hpp:241
bool isTrigger
flag that shows whether the object is trigger zone
Definition types.hpp:245
PolygonGeometry * getPolygon()
getter for accessing the PolygonGeometry data
ColliderComponent(const BodyShape &st)
ShapeData shapeData
shape data: vertices, radius, borders etc
Definition types.hpp:243
AABB getAABB(const TransformComponent &tc) const
getter for accessing the AABB object
const PolygonGeometry * getPolygon() const
getter for accessing the PolygonGeometry data
glm::vec2 localOffset
local offset of the center of the object
Definition types.hpp:244
uint32_t categoryBits
Definition types.hpp:246
const CircleGeometry * getCircle() const
getter for accessing the CircleGeometry data
uint32_t maskBits
Definition types.hpp:248
BodyShape shapeType
shape of the object: polygon, circle, segment etc
Definition types.hpp:242
ColliderComponent(Geometry &&geom, const glm::vec2 &offset=cd_defaultLocalOffset, bool trigger=cd_defaultTriggerStatus, uint32_t categoryBits=ColliderLayers::Player, uint32_t maskBits=ColliderLayers::All &~ColliderLayers::PlayerProjectile)
Definition types.hpp:255
CircleGeometry * getCircle()
getter for accessing the CircleGeometry data
Definition types.hpp:208
std::vector< glm::vec2 > vertices
Definition types.hpp:209
Definition types.hpp:154
BodyType bodyType
bodyType: Static, Kinematic or Dynamic
Definition types.hpp:155
float angularVelocity
angular velocity, in radians clockwise
Definition types.hpp:161
glm::vec2 force
force: two-dimensional vector
Definition types.hpp:158
float friction
friction of the object, 0.2 by default
Definition types.hpp:173
float restitution
restitution of the object, 0.5 by default
Definition types.hpp:172
float invMass
inverted mass coefficient, used for calculating linear acceleration
Definition types.hpp:164
float gravityScale
gravity scale, 1 by default
Definition types.hpp:159
float torque
torque, in radians clockwise
Definition types.hpp:162
glm::vec2 linearVelocity
velocity along some axis
Definition types.hpp:157
RigidBodyComponent()=default
default RigidBodyComponent constructor
RigidBodyComponent(phys2d::BodyType bt)
RigidBodyComponent c-tor that uses only BodyType parameter, other fields are set by default.
float angularDamping
angular damping of the object, from 0 to 1
Definition types.hpp:170
float linearDamping
linear damping of the object, from 0 to 1
Definition types.hpp:169
float invInertia
Definition types.hpp:166
RigidBodyComponent(BodyType bt, const glm::vec2 &linearVelocity, const glm::vec2 &force, float gravityScale, float angularVelocity, float torque, float mass, float inertia, float linearDamping, float angularDamping, float restitution, float friction)
RigidBody c-tor, requires all fields to be customly set.
Definition types.hpp:225
std::vector< glm::vec2 > points
Definition types.hpp:226
Definition types.hpp:221
glm::vec2 borders[2]
Definition types.hpp:222
Definition types.hpp:73
void setRotation(const float new_angle)
sets the new object's rotation in radians, clockwise
glm::mat3 modelMatrix
model matrix for MVP-rendering
Definition types.hpp:79
TransformComponent()=default
default concstructor for TransformComponent
void scale(float scale_coef)
scales the object
void move(const glm::vec2 &direction)
changes the object's position: (x, y) -> (x + direction.x, y + direction.y)
bool dirty
should we update model_matrix or not
Definition types.hpp:78
const glm::vec2 getForward() const
gives the forward vector of an object. Can be used for cannons and bullets, for example
void rotate(float angle_delta)
rotates the object
float rotation
rotation angle of the object, clockwise
Definition types.hpp:75
void updateMatrix() const
recalculates the model matrix
void setScale(float new_scale)
sets the new object's scaling coefficients
float scaling
scaling coefficient
Definition types.hpp:76
void setPosition(const glm::vec2 &new_position)
sets the new object's position on the grid
TransformComponent(const glm::vec2 &position, float rotation, float scaling)
constructor for TransformComponent with all fields
glm::vec2 position
global position of the object on the grid
Definition types.hpp:74