1 #include "hx3d/physics/2d/colliders/polygon.hpp" 3 #include "hx3d/math/vector_utils.hpp" 15 unsigned int rightMost = 0;
16 float highestXCoord = points[0].x;
17 for (
unsigned int i = 1; i < points.size(); ++i) {
18 float x = points[i].x;
20 if (x > highestXCoord) {
25 else if (x == highestXCoord) {
26 if (points[i].y < points[rightMost].y) {
32 std::vector<unsigned int> hull;
33 unsigned int outCount = 0;
34 unsigned int indexHull = rightMost;
37 hull[outCount] = indexHull;
39 unsigned int nextHullIndex = 0;
40 for (
unsigned int i = 1; i < points.size(); ++i) {
41 if (nextHullIndex == indexHull) {
46 glm::vec2 e1 = points[nextHullIndex] - points[hull[outCount]];
47 glm::vec2 e2 = points[i] - points[hull[outCount]];
59 indexHull = nextHullIndex;
61 if (nextHullIndex == rightMost) {
74 normals.push_back(glm::normalize(glm::vec2({face.y, -face.x})));
81 float bestProjection = -FLT_MAX;
86 float projection = glm::dot(v, dir);
88 if (projection > bestProjection) {
90 bestProjection = projection;
100 float hw = width / 2;
101 float hh = height / 2;
103 vertices.push_back(glm::vec2(-hw, -hh));
104 vertices.push_back(glm::vec2(hw, -hh));
105 vertices.push_back(glm::vec2(hw, hh));
106 vertices.push_back(glm::vec2(-hw, hh));
107 normals.push_back(glm::vec2(0.f, -1.f));
108 normals.push_back(glm::vec2(1.f, 0.f));
109 normals.push_back(glm::vec2(0.f, 1.f));
110 normals.push_back(glm::vec2(-1.f, 0.f));
116 float c = std::cos(angle);
117 float s = std::sin(angle);
133 glm::vec2 centroid = {0.f, 0.f};
136 float inv3 = 1.f / 3.f;
139 unsigned int j = i + 1 < vertexCount ? i + 1 : 0;
144 float triangleArea = 0.5f * D;
146 area += triangleArea;
148 centroid += triangleArea * inv3 * (p1 + p2);
150 float intx2 = p1.x * p1.x + p2.x * p1.x + p2.x * p2.x;
151 float inty2 = p1.y * p1.y + p2.y * p1.y + p2.y * p2.y;
153 I += (0.25f * inv3 * D) * (intx2 + inty2);
156 centroid *= 1.f / area;
void setAsBox(const float width, const float height)
Set the polygon points as a box.
void setDensity(float density)
Set collider density.
std::vector< glm::vec2 > normals
Normals.
glm::vec2 getSupport(glm::vec2 dir)
Get the support vector following a direction.
void setPoints(const std::vector< glm::vec2 > &points)
Set the polygon points.
float squareLength(glm::vec2 vec)
Compute the square length of a 2D vector.
virtual void computeMass(float density) override
Compute the collider mass.
Polygon(const Type colliderType=Type::Dynamic)
Create a polygon.
std::vector< glm::vec2 > vertices
Vertices.
unsigned int vertexCount
Vertex count.
virtual void setOrientation(float angle) override
Set the collider orientation.
void setInertia(float amount)
Set the current inertia.
glm::mat2 u
Rotation matrix.
glm::vec2 cross(glm::vec2 vec, float v)
Calculate the cross product vector between a 2D vector and a scalar.
void setMass(float amount)
Set the current mass.
Polygon or box shaped collider.