Added Collision Tags #233

Merged
direnbharwani merged 10 commits from SP3-2-Physics into main 2022-11-20 22:40:53 +08:00
4 changed files with 48 additions and 65 deletions
Showing only changes of commit cada3acb8a - Show all commits

View File

@ -46,6 +46,26 @@ namespace SHADE
return -1; return -1;
} }
SHCollisionTag* SHCollisionTagMatrix::GetTag(int tagIndex)
{
if (tagIndex < 0 || tagIndex > SHCollisionTag::NUM_LAYERS)
throw std::invalid_argument("Index out of range!");
return &collisionTags[tagIndex];
}
SHCollisionTag* SHCollisionTagMatrix::GetTag(const std::string& tagName) noexcept
{
for (int i = 0; i < SHCollisionTag::NUM_LAYERS; ++i)
{
if (collisionTags[i].GetName() == tagName)
return &collisionTags[i];
}
SHLOGV_WARNING("Collision Tag {} cannot be found!", tagName)
return nullptr;
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Setter Function Definitions */ /* Setter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -200,56 +220,4 @@ namespace SHADE
collisionTagNamesFile.close(); collisionTagNamesFile.close();
} }
void SHCollisionTagMatrix::UpdateTagNamesFromFile(const std::filesystem::path& tagNameFilePath) noexcept
{
std::ifstream collisionTagNamesFile { tagNameFilePath };
if (!collisionTagNamesFile.is_open())
{
SHLOG_ERROR("Failed to open file for Collision Tag Names! Default tag names used!")
return;
}
std::stringstream ss;
std::string line;
int linesRead = 0;
while (std::getline(collisionTagNamesFile, line))
{
// Do not read anything beyond the first 16 lines
if (linesRead >= 16)
break;
ss << line;
++linesRead;
// First element is index.
int tagIndex;
ss >> tagIndex;
// Next element is name of the tag
std::string tagName;
ss >> tagName;
// If no tag name read, use default.
if (tagName.empty())
{
SHLOG_ERROR
(
"Collision tag file line {} does not match the required format of 'index<space>tag name'. Name left unchanged for index {}"
, linesRead + 1
, tagIndex
)
continue;
}
collisionTags[tagIndex].SetName(tagName);
ss.clear();
}
collisionTagNamesFile.close();
}
} // namespace SHADE } // namespace SHADE

View File

@ -29,8 +29,11 @@ namespace SHADE
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] static const std::string& GetTagName (int tagIndex); [[nodiscard]] static const std::string& GetTagName (int tagIndex);
[[nodiscard]] static int GetTagIndex (const std::string& tagName) noexcept; [[nodiscard]] static int GetTagIndex (const std::string& tagName) noexcept;
[[nodiscard]] static SHCollisionTag* GetTag (int tagIndex);
[[nodiscard]] static SHCollisionTag* GetTag (const std::string& tagName) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Setter Functions */ /* Setter Functions */
@ -53,7 +56,6 @@ namespace SHADE
static void Init (const std::filesystem::path& tagNameFilePath) noexcept; static void Init (const std::filesystem::path& tagNameFilePath) noexcept;
static void Exit (const std::filesystem::path& tagNameFilePath) noexcept; static void Exit (const std::filesystem::path& tagNameFilePath) noexcept;
static void UpdateTagNamesFromFile (const std::filesystem::path& tagNameFilePath) noexcept;
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -16,6 +16,7 @@
#include "Math/Geometry/SHBox.h" #include "Math/Geometry/SHBox.h"
#include "Math/Geometry/SHSphere.h" #include "Math/Geometry/SHSphere.h"
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Physics/Collision/SHCollisionTagMatrix.h"
#include "Reflection/SHReflectionMetadata.h" #include "Reflection/SHReflectionMetadata.h"
#include "SHColliderComponent.h" #include "SHColliderComponent.h"
@ -26,12 +27,13 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHCollisionShape::SHCollisionShape(EntityID eid, Type colliderType, const SHPhysicsMaterial& physicsMaterial) SHCollisionShape::SHCollisionShape(EntityID eid, Type colliderType, const SHPhysicsMaterial& physicsMaterial)
: type { colliderType } : type { colliderType }
, entityID { eid } , entityID { eid }
, isTrigger { false } , isTrigger { false }
, dirty { true } , dirty { true }
, shape { nullptr } , shape { nullptr }
, material { physicsMaterial } , material { physicsMaterial }
, collisionTag { SHCollisionTagMatrix::GetTag(0) }
{ {
switch (type) switch (type)
{ {
@ -57,6 +59,8 @@ namespace SHADE
, shape { nullptr } , shape { nullptr }
, material { rhs.material } , material { rhs.material }
, positionOffset { rhs.positionOffset } , positionOffset { rhs.positionOffset }
, rotationOffset { rhs.rotationOffset }
, collisionTag { rhs.collisionTag }
{ {
CopyShape(rhs.shape); CopyShape(rhs.shape);
} }
@ -69,6 +73,8 @@ namespace SHADE
, shape { nullptr } , shape { nullptr }
, material { rhs.material } , material { rhs.material }
, positionOffset { rhs.positionOffset } , positionOffset { rhs.positionOffset }
, rotationOffset { rhs.rotationOffset }
, collisionTag { rhs.collisionTag }
{ {
CopyShape(rhs.shape); CopyShape(rhs.shape);
} }
@ -93,6 +99,8 @@ namespace SHADE
dirty = true; dirty = true;
material = rhs.material; material = rhs.material;
positionOffset = rhs.positionOffset; positionOffset = rhs.positionOffset;
rotationOffset = rhs.rotationOffset;
collisionTag = rhs.collisionTag;
delete shape; delete shape;
CopyShape(rhs.shape); CopyShape(rhs.shape);
@ -108,6 +116,8 @@ namespace SHADE
dirty = true; dirty = true;
material = rhs.material; material = rhs.material;
positionOffset = rhs.positionOffset; positionOffset = rhs.positionOffset;
rotationOffset = rhs.rotationOffset;
collisionTag = rhs.collisionTag;
delete shape; delete shape;
CopyShape(rhs.shape); CopyShape(rhs.shape);
@ -136,7 +146,7 @@ namespace SHADE
const SHCollisionTag& SHCollisionShape::GetCollisionTag() const noexcept const SHCollisionTag& SHCollisionShape::GetCollisionTag() const noexcept
{ {
return collisionTag; return *collisionTag;
} }
float SHCollisionShape::GetFriction() const noexcept float SHCollisionShape::GetFriction() const noexcept
@ -245,7 +255,7 @@ namespace SHADE
isTrigger = trigger; isTrigger = trigger;
} }
void SHCollisionShape::SetCollisionTag(const SHCollisionTag& newCollisionTag) noexcept void SHCollisionShape::SetCollisionTag(SHCollisionTag* newCollisionTag) noexcept
{ {
dirty = true; dirty = true;
collisionTag = newCollisionTag; collisionTag = newCollisionTag;

View File

@ -95,7 +95,7 @@ namespace SHADE
void SetBoundingSphere (float radius); void SetBoundingSphere (float radius);
void SetIsTrigger (bool isTrigger) noexcept; void SetIsTrigger (bool isTrigger) noexcept;
void SetCollisionTag (const SHCollisionTag& newCollisionTag) noexcept; void SetCollisionTag (SHCollisionTag* newCollisionTag) noexcept;
void SetFriction (float friction) noexcept; void SetFriction (float friction) noexcept;
void SetBounciness (float bounciness) noexcept; void SetBounciness (float bounciness) noexcept;
void SetDensity (float density) noexcept; void SetDensity (float density) noexcept;
@ -113,12 +113,15 @@ namespace SHADE
EntityID entityID; // The entity this collider belongs to EntityID entityID; // The entity this collider belongs to
bool isTrigger; bool isTrigger;
bool dirty; bool dirty;
SHCollisionTag collisionTag;
SHShape* shape; SHShape* shape;
SHPhysicsMaterial material; SHPhysicsMaterial material;
SHVec3 positionOffset; SHVec3 positionOffset;
SHVec3 rotationOffset; SHVec3 rotationOffset;
SHCollisionTag* collisionTag;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/