CoriEngine
Loading...
Searching...
No Matches
Physics/Physics.cpp
Go to the documentation of this file.
1#include "Physics.hpp"
2
3namespace Cori {
4 namespace Physics {
5 WindingOrder GetPolygonWindingOrder(const std::vector<Vec2>& polygon) {
6 const int32_t n = polygon.size();
7 if (n < 3) {
9 }
10
11 double signedAreaSum = 0.0f;
12
13 for (int32_t i = 0; i < n; ++i) {
14 const auto& [x1, y1] = polygon.at(i);
15 const auto& [x2, y2] = polygon.at((i + 1) % n);
16 signedAreaSum += x1 * y2 - x2 * y1;
17 }
18
19 constexpr double epsilon = 1e-9;
20
21 if (signedAreaSum > epsilon) {
23 }
24 if (signedAreaSum < -epsilon) {
26 }
28 }
29
30 WindingOrder GetPolygonWindingOrder(const std::vector<tmx::Vector2f>& polygon) {
31 const int32_t n = polygon.size();
32 if (n < 3) {
34 }
35
36 double signedAreaSum = 0.0f;
37
38 for (int32_t i = 0; i < n; ++i) {
39 const tmx::Vector2f& p1 = polygon.at(i);
40 const tmx::Vector2f& p2 = polygon.at((i + 1) % n);
41
42 signedAreaSum += p1.x * p2.y - p2.x * p1.y;
43 }
44
45 constexpr double epsilon = 1e-9;
46
47 if (signedAreaSum > epsilon) {
49 }
50 if (signedAreaSum < -epsilon) {
52 }
54 }
55
56 const char* WindingOrderToString(const WindingOrder order) {
57 switch (order) {
59 return "Collinear";
61 return "Clockwise (CW)";
63 return "Counter-Clockwise (CCW)";
64 default:
65 return "Unknown";
66 }
67 }
68
69 // camera space "pixels" not screen space pixels
70 glm::vec2 ToPixels(const Vec2 vec) {
71 return { vec.x * CORI_PIXELS_PER_METER, vec.y * CORI_PIXELS_PER_METER };
72 }
73
74 Vec2 ToMeters(const glm::vec2 vec) {
75 return { vec.x / static_cast<float>(CORI_PIXELS_PER_METER), vec.y / static_cast<float>(CORI_PIXELS_PER_METER) };
76 }
77
78 std::string Vec2ToString(const Vec2 vec) {
79 return std::string("(") + std::to_string(vec.x) + std::string(", ") + std::to_string(vec.y) + std::string(")");
80 }
81 }
82}
#define CORI_PIXELS_PER_METER
Anything connected to physics is in this namespace. Please refer to Box2D docs 'https://box2d....
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.
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.
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.