Implemented a custom physics engine #316

Merged
direnbharwani merged 95 commits from SHPhysics into main 2023-01-23 15:55:45 +08:00
2 changed files with 52 additions and 7 deletions
Showing only changes of commit fba338eaef - Show all commits

View File

@ -70,6 +70,9 @@ namespace SHADE
box->Orientation = createInfo.Orientation; box->Orientation = createInfo.Orientation;
box->scale = createInfo.Scale; box->scale = createInfo.Scale;
// Set convex polyhedron for the box
box->polyhedron = &boxPolyhedron;
return box; return box;
} }
@ -109,6 +112,9 @@ namespace SHADE
void SHCollisionShapeLibrary::createBoxPolyhedron() noexcept void SHCollisionShapeLibrary::createBoxPolyhedron() noexcept
{ {
static constexpr int NUM_VERTICES_PER_FACE = 4;
static constexpr int NUM_FACES = 6;
/* /*
* Vertices (Front/Back Face): * Vertices (Front/Back Face):
* *
@ -120,16 +126,49 @@ namespace SHADE
* *
* Faces: * Faces:
* *
* Front: 0 (0,1,2,3) * Front: 0 (0,1,2,3) Normal: Z
* Right: 1 (1,5,6,2) * Right: 1 (1,5,6,2) Normal: X
* Back: 2 (5,4,7,6) * Back: 2 (5,4,7,6) Normal: -Z
* Left: 3 (4,0,3,7) * Left: 3 (4,0,3,7) Normal: -X
* Top: 4 (3,2,6,7) * Top: 4 (3,2,6,7) Normal: Y
* Bottom: 5 (4,5,1,0) * Bottom: 5 (4,5,1,0) Normal: -Y
* *
*/ */
// Create face data // Create face data
const SHVec3 FACE_NORMALS[NUM_FACES]
{
-SHVec3::UnitZ
, SHVec3::UnitX
, SHVec3::UnitZ
, -SHVec3::UnitX
, SHVec3::UnitY
, -SHVec3::UnitY
};
const int32_t FACE_VERTICES[NUM_FACES][NUM_VERTICES_PER_FACE]
{
{ 0, 1, 2, 3 }
, { 1, 5, 6, 2 }
, { 5, 4, 7, 6 }
, { 4, 0, 3, 7 }
, { 3, 2, 6, 7 }
, { 4, 5, 1, 0 }
};
for (int i = 0; i < NUM_FACES; ++i)
{
SHConvexPolyhedron::Face newFace;
newFace.normal = FACE_NORMALS[i];
for (int j = 0; j < NUM_VERTICES_PER_FACE; ++j)
newFace.vertexIndices.emplace_back(FACE_VERTICES[i][j]);
boxPolyhedron.AddFace(newFace);
}
boxPolyhedron.BuildPolyhedron();
} }
} // namespace SHADE } // namespace SHADE

View File

@ -167,11 +167,17 @@ namespace SHADE
{ {
const Face& FACE = faces[i]; const Face& FACE = faces[i];
if (FACE.vertexIndices.empty())
{
SHLOGV_CRITICAL("Unable to build convex polyhedron, no vertices have been added to face {}!", i)
return;
}
// Iterate through vertices and build half-edges // Iterate through vertices and build half-edges
for (size_t j = 0; j < FACE.vertexIndices.size(); ++j) for (size_t j = 0; j < FACE.vertexIndices.size(); ++j)
{ {
const int32_t TAIL = FACE.vertexIndices[j]; const int32_t TAIL = FACE.vertexIndices[j];
const int32_t HEAD = (TAIL + 1) > FACE.vertexIndices.back() ? FACE.vertexIndices.front() : TAIL + 1; // Wrap around const int32_t HEAD = j + 1 < FACE.vertexIndices.size() ? FACE.vertexIndices[j + 1] : FACE.vertexIndices[0];
const uint64_t NEW_EDGE_ID = BUILD_UINT64_FROM_UINT32S(TAIL, HEAD); const uint64_t NEW_EDGE_ID = BUILD_UINT64_FROM_UINT32S(TAIL, HEAD);
const uint64_t TWIN_EDGE_ID = BUILD_UINT64_FROM_UINT32S(HEAD, TAIL); const uint64_t TWIN_EDGE_ID = BUILD_UINT64_FROM_UINT32S(HEAD, TAIL);