CoriEngine
Loading...
Searching...
No Matches
Physics/Physics.hpp
Go to the documentation of this file.
1#pragma once
2#include <box2cpp/box2cpp.h>
3#include <tmxlite/Map.hpp>
5
6#ifndef CORI_PIXELS_PER_METER
7 #define CORI_PIXELS_PER_METER 16
8#endif
9
10namespace Cori {
11 namespace World {
12 namespace Components {
13 namespace Scene {
17 //class PhysicsWorld : public Physics::World {
18 //public:
19 // explicit PhysicsWorld(const Params& params = {}) : World{ params } {}
20//
21 // PhysicsWorld(const PhysicsWorld&) = delete;
22 // PhysicsWorld& operator=(const PhysicsWorld&) = delete;
23//
24 // PhysicsWorld(PhysicsWorld&&) noexcept = default;
25 // PhysicsWorld& operator=(PhysicsWorld&&) noexcept = default;
26 //};
27 }
28 }
29 }
30
37 namespace Physics {
38
42 using Vec2 = b2Vec2;
43 using Rot = b2Rot;
44 using Transform = b2Transform;
45 using CollisionPlane = b2CollisionPlane;
46 using RayResult = b2RayResult;
47
48
49 enum CollisionBits : uint64_t {
50 StaticBit = 1ull << 0,
51 MoverBit = 1ull << 1,
52 DynamicBit = 1ull << 2,
53 DebrisBit = 1ull << 3,
54 SensorBit = 1ull << 4,
55 CustomBit5 = 1ull << 5,
56 CustomBit6 = 1ull << 6,
57 CustomBit7 = 1ull << 7,
58 CustomBit8 = 1ull << 8,
59 CustomBit9 = 1ull << 9,
60 CustomBit10 = 1ull << 10,
61 CustomBit11 = 1ull << 11,
62 CustomBit12 = 1ull << 12,
63 CustomBit13 = 1ull << 13,
64 CustomBit14 = 1ull << 14,
65 CustomBit15 = 1ull << 15,
66 CustomBit16 = 1ull << 16,
67 CustomBit17 = 1ull << 17,
68 CustomBit18 = 1ull << 18,
69 CustomBit19 = 1ull << 19,
70 CustomBit20 = 1ull << 20,
71 CustomBit21 = 1ull << 21,
72 CustomBit22 = 1ull << 22,
73 CustomBit23 = 1ull << 23,
74 CustomBit24 = 1ull << 24,
75 CustomBit25 = 1ull << 25,
76 CustomBit26 = 1ull << 26,
77 CustomBit27 = 1ull << 27,
78 CustomBit28 = 1ull << 28,
79 CustomBit29 = 1ull << 29,
80 CustomBit30 = 1ull << 30,
81 CustomBit31 = 1ull << 31,
82 CustomBit32 = 1ull << 32,
83 CustomBit33 = 1ull << 33,
84 CustomBit34 = 1ull << 34,
85 CustomBit35 = 1ull << 35,
86 CustomBit36 = 1ull << 36,
87 CustomBit37 = 1ull << 37,
88 CustomBit38 = 1ull << 38,
89 CustomBit39 = 1ull << 39,
90 CustomBit40 = 1ull << 40,
91 CustomBit41 = 1ull << 41,
92 CustomBit42 = 1ull << 42,
93 CustomBit43 = 1ull << 43,
94 CustomBit44 = 1ull << 44,
95 CustomBit45 = 1ull << 45,
96 CustomBit46 = 1ull << 46,
97 CustomBit47 = 1ull << 47,
98 CustomBit48 = 1ull << 48,
99 CustomBit49 = 1ull << 49,
100 CustomBit50 = 1ull << 50,
101 CustomBit51 = 1ull << 51,
102 CustomBit52 = 1ull << 52,
103 CustomBit53 = 1ull << 53,
104 CustomBit54 = 1ull << 54,
105 CustomBit55 = 1ull << 55,
106 CustomBit56 = 1ull << 56,
107 CustomBit57 = 1ull << 57,
108 CustomBit58 = 1ull << 58,
109 CustomBit59 = 1ull << 59,
110 CustomBit60 = 1ull << 60,
111 CustomBit61 = 1ull << 61,
112 CustomBit62 = 1ull << 62,
113 CustomBit63 = 1ull << 63,
114
115 AllBits = ~0u,
116 };
117
123
125 ShapeUserData() = default;
126
127 explicit ShapeUserData(const Cori::World::Entity& entity) : m_Entity(entity) {}
128
130 };
131
132 struct CastResult {
133 CastResult() = default;
134
135 bool hit{ false };
136 float fraction;
139 ShapeRef shape;
140 };
141
147 WindingOrder GetPolygonWindingOrder(const std::vector<Vec2>& polygon);
148
154 WindingOrder GetPolygonWindingOrder(const std::vector<tmx::Vector2f>& polygon);
155
161 const char* WindingOrderToString(WindingOrder order);
162
163
170 glm::vec2 ToPixels(const Vec2 vec);
171
178 Vec2 ToMeters(const glm::vec2 vec);
179
186 std::string Vec2ToString(Vec2 vec);
187
189 public:
190 ConvexHull() = default;
191
192 ConvexHull(const b2Hull& hull) : m_Hull(hull) {} // NOLINT
193
194 [[nodiscard]] static ConvexHull Create(const std::vector<Vec2>& vertices) {
195#ifdef DEBUG_BUILD
196 const b2Hull hull = b2ComputeHull(vertices.data(), vertices.size());
197 if (hull.count == 0) {
198 std::string str_vertices;
199 for (auto [x, y] : vertices) {
200 str_vertices += "(" + std::to_string(x) + ", " + std::to_string(x) + ")";
201 }
202 CORI_CORE_CHECK(hull.count != 0, "Failed to create ConvexHull. Vertices: {}", str_vertices);
203 }
204 return hull;
205#endif
206#ifndef DEBUG_BUILD
207 return b2ComputeHull(vertices.data(), vertices.size());
208#endif
209 }
210
211 operator b2Hull& () { // NOLINT
212 return m_Hull;
213 }
214
215 operator const b2Hull* () const { // NOLINT
216 return &m_Hull;
217 }
218
219 private:
220 b2Hull m_Hull;
221 };
222
223 class Polygon : public b2Polygon {
224 public:
225 Polygon() = default;
226
227 Polygon(const b2Polygon& polygon) : b2Polygon(polygon) {} // NOLINT
228
229 [[nodiscard]] static Polygon CreateBox(const Vec2 halfSize, const Vec2 offset, const Rot rotation = Rot{ 1, 0 }, const float radius = 0.0f) {
230 return b2MakeOffsetRoundedBox(halfSize.x, halfSize.y, offset, rotation, radius);
231 }
232
233 [[nodiscard]] static Polygon CreateBox(const Vec2 halfSize) {
234 return b2MakeBox(halfSize.x, halfSize.y);
235 }
236
237 [[nodiscard]] static Polygon CreateBox(const Vec2 halfSize, const float radius) {
238 return b2MakeRoundedBox(halfSize.x, halfSize.y, radius);
239 }
240
241 [[nodiscard]] static Polygon CreatePolygon(const ConvexHull& hull, const float radius = 0.0f) {
242 return b2MakePolygon(hull, radius);
243 }
244
245 [[nodiscard]] static Polygon CreatePolygon(const ConvexHull& hull, const Vec2 offset, const Rot rotation = Rot{ 1, 0 }, const float radius = 0.0f) {
246 return b2MakeOffsetRoundedPolygon(hull, offset, rotation, radius);
247 }
248 };
249
250 class Circle : public b2Circle {
251 public:
252 Circle() = default;
253
254 Circle(const b2Circle& circle) : b2Circle(circle) {} // NOLINT
255 Circle(const Vec2 center, const float radius) : b2Circle{center, radius} {}
256
257 [[nodiscard]] static Circle Create(Vec2 center, float radius) {
258 return { center, radius };
259 }
260 };
261
262 class Capsule : public b2Capsule {
263 public:
264 Capsule() = default;
265
266 Capsule(const b2Capsule& capsule) : b2Capsule(capsule) {} // NOLINT
267 Capsule(const Vec2 center1, const Vec2 center2, const float radius) : b2Capsule{ center1, center2, radius } {}
268
269 [[nodiscard]] static Capsule Create(Vec2 center1, Vec2 center2, float radius) {
270 return { center1, center2, radius };
271 }
272 };
273
274 class Segment : public b2Segment {
275 public:
276 Segment() = default;
277
278 Segment(const b2Segment& segment) : b2Segment(segment) {} // NOLINT
279 Segment(const Vec2 point1, const Vec2 point2) : b2Segment{ point1, point2 } {}
280
281 [[nodiscard]] static Segment Create(Vec2 point1, Vec2 point2) {
282 return { point1, point2 };
283 }
284 };
285
286 }
287}
288
290{
291 a.x *= b.x;
292 a.y *= b.y;
293}
#define CORI_CORE_CHECK(x,...)
Definition Logger.hpp:1042
void operator*=(Cori::Physics::Vec2 &a, Cori::Physics::Vec2 b)
Capsule(const b2Capsule &capsule)
Capsule(const Vec2 center1, const Vec2 center2, const float radius)
static Capsule Create(Vec2 center1, Vec2 center2, float radius)
Circle(const Vec2 center, const float radius)
static Circle Create(Vec2 center, float radius)
Circle(const b2Circle &circle)
ConvexHull(const b2Hull &hull)
static ConvexHull Create(const std::vector< Vec2 > &vertices)
static Polygon CreatePolygon(const ConvexHull &hull, const Vec2 offset, const Rot rotation=Rot{ 1, 0 }, const float radius=0.0f)
static Polygon CreatePolygon(const ConvexHull &hull, const float radius=0.0f)
Polygon(const b2Polygon &polygon)
static Polygon CreateBox(const Vec2 halfSize, const Vec2 offset, const Rot rotation=Rot{ 1, 0 }, const float radius=0.0f)
static Polygon CreateBox(const Vec2 halfSize)
static Polygon CreateBox(const Vec2 halfSize, const float radius)
static Segment Create(Vec2 point1, Vec2 point2)
Segment(const b2Segment &segment)
Segment(const Vec2 point1, const Vec2 point2)
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
b2RayResult RayResult
b2Vec2 Vec2
An alias for Box2D native vec2 type, if you see it somewhere, be sure data there is in meters contrar...
glm::vec2 ToPixels(const Vec2 vec)
Converts physical meters to camera space pixels.
b2Transform Transform
WindingOrder GetPolygonWindingOrder(const std::vector< Vec2 > &polygon)
Calculates the winding order of the polygon made up from individual points. Box2D version....
const char * WindingOrderToString(const WindingOrder order)
Converts the WindingOrder enumerator to string, for logging.
b2CollisionPlane CollisionPlane
std::string Vec2ToString(const Vec2 vec)
Converts a native Box2D vec2 to string, for logging.
Vec2 ToMeters(const glm::vec2 vec)
Converts camera space pixels to physical meters.
Global engine namespace.
ShapeUserData(const Cori::World::Entity &entity)