Unretarded the collision tags #337

Merged
direnbharwani merged 4 commits from SP3-2-Physics into main 2023-02-03 23:23:20 +08:00
12 changed files with 109 additions and 94 deletions

View File

@ -1,16 +1,16 @@
0 1 3 0 1 0000000000000000
1 2 65535 1 2 0000000000000000
2 3 65534 2 3 0000000000000000
3 4 65534 3 4 0000000000000000
4 5 65534 4 5 0000000000000000
5 6 65534 5 6 0000000000000000
6 7 65534 6 7 0000000000000000
7 8 65534 7 8 0000000000000000
8 9 65534 8 9 0000000000000000
9 10 65534 9 10 0000000000000000
10 11 65534 10 11 0000000000000000
11 12 65534 11 12 0000000000000000
12 13 65534 12 13 0000000000000000
13 14 65534 13 14 0000000000000000
14 15 65534 14 15 0000000000000000
15 16 65534 15 16 0000000000000000

View File

@ -132,23 +132,23 @@ Size=165,120
Collapsed=0 Collapsed=0
[Table][0x9D40AE32,17] [Table][0x9D40AE32,17]
Column 0 Weight=0.9945 Column 0 Weight=1.0696
Column 1 Weight=0.9945 Column 1 Weight=0.9194
Column 2 Weight=0.9945 Column 2 Weight=0.9945
Column 3 Weight=0.9945 Column 3 Weight=1.0320
Column 4 Weight=0.9945 Column 4 Weight=0.9570
Column 5 Weight=0.9945 Column 5 Weight=0.9945
Column 6 Weight=0.9945 Column 6 Weight=0.9945
Column 7 Weight=0.9945 Column 7 Weight=0.9945
Column 8 Weight=0.9945 Column 8 Weight=0.9945
Column 9 Weight=0.9945 Column 9 Weight=1.0320
Column 10 Weight=0.9945 Column 10 Weight=0.9570
Column 11 Weight=0.9945 Column 11 Weight=1.0133
Column 12 Weight=1.0132 Column 12 Weight=0.9945
Column 13 Weight=0.8444 Column 13 Weight=0.8444
Column 14 Weight=0.9945 Column 14 Weight=0.9945
Column 15 Weight=1.2009 Column 15 Weight=0.8632
Column 16 Weight=1.0132 Column 16 Weight=1.3510
[Docking][Data] [Docking][Data]
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=0,71 Size=1920,941 Split=X DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=0,71 Size=1920,941 Split=X
@ -159,7 +159,7 @@ DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=0,71 Size=1920,941 Split
DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1130,1036 Split=Y Selected=0xB41284E7 DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1130,1036 Split=Y Selected=0xB41284E7
DockNode ID=0x00000007 Parent=0x00000002 SizeRef=1501,672 Split=Y Selected=0xB41284E7 DockNode ID=0x00000007 Parent=0x00000002 SizeRef=1501,672 Split=Y Selected=0xB41284E7
DockNode ID=0x00000009 Parent=0x00000007 SizeRef=1501,700 Split=Y Selected=0xB41284E7 DockNode ID=0x00000009 Parent=0x00000007 SizeRef=1501,700 Split=Y Selected=0xB41284E7
DockNode ID=0x0000000B Parent=0x00000009 SizeRef=1501,705 CentralNode=1 Selected=0xB41284E7 DockNode ID=0x0000000B Parent=0x00000009 SizeRef=1501,705 CentralNode=1 Selected=0x69428B24
DockNode ID=0x0000000C Parent=0x00000009 SizeRef=1501,234 Selected=0xB128252A DockNode ID=0x0000000C Parent=0x00000009 SizeRef=1501,234 Selected=0xB128252A
DockNode ID=0x0000000A Parent=0x00000007 SizeRef=1501,310 Selected=0xD446F7B6 DockNode ID=0x0000000A Parent=0x00000007 SizeRef=1501,310 Selected=0xD446F7B6
DockNode ID=0x00000008 Parent=0x00000002 SizeRef=1501,338 Selected=0xD9F31532 DockNode ID=0x00000008 Parent=0x00000002 SizeRef=1501,338 Selected=0xD9F31532

View File

@ -13,7 +13,7 @@
Drag: 0.00999999978 Drag: 0.00999999978
Angular Drag: 0.100000001 Angular Drag: 0.100000001
Use Gravity: false Use Gravity: false
Interpolate: true Interpolate: false
Sleeping Enabled: true Sleeping Enabled: true
Freeze Position X: false Freeze Position X: false
Freeze Position Y: false Freeze Position Y: false
@ -25,7 +25,7 @@
Collider Component: Collider Component:
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Collision Tag: 1 Collision Tag: 2
Type: Box Type: Box
Half Extents: {x: 1, y: 1, z: 1} Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006 Friction: 0.400000006
@ -52,7 +52,7 @@
Collider Component: Collider Component:
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Collision Tag: 1 Collision Tag: 5
Type: Box Type: Box
Half Extents: {x: 1, y: 1, z: 1} Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006 Friction: 0.400000006
@ -161,7 +161,7 @@
Collider Component: Collider Component:
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Collision Tag: 1 Collision Tag: 7
Type: Box Type: Box
Half Extents: {x: 1, y: 1, z: 1} Half Extents: {x: 1, y: 1, z: 1}
Friction: 0.400000006 Friction: 0.400000006

View File

@ -82,7 +82,7 @@ public class PhysicsTestObj : Script
{ {
Ray colliderRay = new Ray(); Ray colliderRay = new Ray();
colliderRay.Direction = Vector3.Right; colliderRay.Direction = Vector3.Right;
Physics.ColliderRaycast(collider.Owner, colliderRay, false, 8); Physics.ColliderRaycast(collider.Owner, colliderRay, false, (ushort)64);
for (int i = 0; i < 6; ++i) for (int i = 0; i < 6; ++i)
{ {

View File

@ -16,48 +16,52 @@ namespace SHADE
ImGui::TableNextRow(); ImGui::TableNextRow();
ImGui::PushID("CollisionTagNames"); ImGui::PushID("CollisionTagNames");
for (int i = SHCollisionTag::NUM_LAYERS; i >= 1; --i) // Draw Layers
// We have 16 layers for each tag
ImGui::TableNextColumn();
for (int i = 1; i <= SHCollisionTag::NUM_LAYERS; ++i)
{ {
ImGui::TableNextColumn(); ImGui::TableNextColumn();
if(i == SHCollisionTag::NUM_LAYERS) continue;
std::string const& tagName = SHCollisionTagMatrix::GetTagName(i); std::string const& tagName = SHCollisionTagMatrix::GetTagName(i);
auto tag = SHCollisionTagMatrix::GetTag(i); auto tag = SHCollisionTagMatrix::GetTag(i);
if (!tag) if (!tag)
continue; continue;
//ImGui::Text(tagName.data());
ImGui::PushID(i); // Show the layer number
SHEditorWidgets::InputText("##", [i]{return SHCollisionTagMatrix::GetTagName(i);}, [i](std::string const& value){SHCollisionTagMatrix::GetTag(i)->SetName(value);}, tagName.data(), ImGuiInputTextFlags_EnterReturnsTrue); ImGui::Text(std::to_string(i).c_str());
ImGui::PopID();
} }
ImGui::PopID(); ImGui::PopID();
// Draw the table
// Convenient how we have 16 layers & 16 tags huh?
for (int i = 0; i < SHCollisionTag::NUM_LAYERS - 1; ++i) for (int i = 0; i < SHCollisionTag::NUM_LAYERS - 1; ++i)
{ {
std::string tagName = SHCollisionTagMatrix::GetTagName(i); std::string tagName = SHCollisionTagMatrix::GetTagName(i);
auto tag = SHCollisionTagMatrix::GetTag(i); auto tag = SHCollisionTagMatrix::GetTag(i);
if(!tag) if(!tag)
continue; continue;
ImGui::TableNextRow(); ImGui::TableNextRow();
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text(tagName.data());
for (int j = (SHCollisionTag::NUM_LAYERS) - 1; j >= i; --j) ImGui::PushID(i);
SHEditorWidgets::InputText("##", [i]{return SHCollisionTagMatrix::GetTagName(i);}, [i](std::string const& value){SHCollisionTagMatrix::GetTag(i)->SetName(value);}, tagName.data(), ImGuiInputTextFlags_EnterReturnsTrue);
ImGui::PopID();
// The checkboxes are just to set the layers of each tag
// Nothing else should be altered.
for (int j = 0; j < SHCollisionTag::NUM_LAYERS; ++j)
{ {
int idx = j;
std::string tagName2 = SHCollisionTagMatrix::GetTagName(idx);
auto tag2 = SHCollisionTagMatrix::GetTag(idx);
if(!tag2)
continue;
if(tagName.empty())
tagName = std::to_string(i);
if(tagName2.empty())
tagName2 = std::to_string(idx);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
if(i == idx)
continue; std::string label = std::format("##{}, Layer {}", tagName, j);
std::string label = std::format("##{} vs {}", tagName, tagName2); if (SHEditorWidgets::CheckBox(label, [tag, &j] {return tag->GetLayerState(j); }, [tag, j](bool const& value) {tag->SetLayerState(j, value); }, label.substr(2)))
if (SHEditorWidgets::CheckBox(label, [tag, &idx] {return tag->GetLayerState(idx); }, [tag, i, idx](bool const& value) {tag->SetLayerState(idx, value); SHCollisionTagMatrix::GetTag(idx)->SetLayerState(i, value); }, label.substr(2)))
{ {
isDirty = true; isDirty = true;
} }
@ -77,7 +81,7 @@ namespace SHADE
{ {
std::filesystem::path defaultCollisionTagNameFilePath{ ASSET_ROOT }; std::filesystem::path defaultCollisionTagNameFilePath{ ASSET_ROOT };
defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig");
SHCollisionTagMatrix::Exit(defaultCollisionTagNameFilePath); SHCollisionTagMatrix::SaveToFile(defaultCollisionTagNameFilePath);
isDirty = false; isDirty = false;
} }
ImGui::EndDisabled(); ImGui::EndDisabled();

View File

@ -166,7 +166,7 @@ namespace SHADE
/* Public Function Member Definitions */ /* Public Function Member Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHCollisionTagMatrix::Init(const std::filesystem::path& tagNameFilePath) noexcept void SHCollisionTagMatrix::LoadFromFile(const std::filesystem::path& tagNameFilePath) noexcept
{ {
/** /**
* I HATE FILE IO * I HATE FILE IO
@ -240,13 +240,18 @@ namespace SHADE
, linesRead + 1 , linesRead + 1
, tagIndex , tagIndex
) )
collisionTags[tagIndex].SetMask(mask);
} }
else else
{ {
mask = static_cast<uint16_t>(std::stoi(maskString)); mask = static_cast<uint16_t>(std::stoi(maskString));
for (int i = 0; i < SHCollisionTag::NUM_LAYERS; ++i)
{
const bool LAYER_STATE = mask & 1U << i;
collisionTags[tagIndex].SetLayerState(i, LAYER_STATE);
}
} }
collisionTags[tagIndex].SetMask(mask);
ss.clear(); ss.clear();
} }
@ -254,7 +259,7 @@ namespace SHADE
collisionTagNamesFile.close(); collisionTagNamesFile.close();
} }
void SHCollisionTagMatrix::Exit(const std::filesystem::path& tagNameFilePath) noexcept void SHCollisionTagMatrix::SaveToFile(const std::filesystem::path& tagNameFilePath) noexcept
{ {
std::ofstream collisionTagNamesFile { tagNameFilePath }; std::ofstream collisionTagNamesFile { tagNameFilePath };
@ -266,7 +271,15 @@ namespace SHADE
// Index Name Mask // Index Name Mask
for (int i = 0; i < SHCollisionTag::NUM_LAYERS; ++i) for (int i = 0; i < SHCollisionTag::NUM_LAYERS; ++i)
collisionTagNamesFile << i << " " << collisionTags[i].GetName() << " " << collisionTags[i].GetMask() << std::endl; {
collisionTagNamesFile << i << " " << collisionTags[i].GetName() << " ";
// Write 1 or 0 for each layer for improved clarity
for (int j = 0; j < SHCollisionTag::NUM_LAYERS; ++j)
collisionTagNamesFile << (collisionTags[i].GetMask() & 1U << j ? 1 : 0);
collisionTagNamesFile << std::endl;
}
collisionTagNamesFile.close(); collisionTagNamesFile.close();
} }

View File

@ -55,8 +55,8 @@ namespace SHADE
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
static void Init (const std::filesystem::path& tagNameFilePath) noexcept; static void LoadFromFile (const std::filesystem::path& tagNameFilePath) noexcept;
static void Exit (const std::filesystem::path& tagNameFilePath) noexcept; static void SaveToFile (const std::filesystem::path& tagNameFilePath) noexcept;
/** /**
* @brief * @brief

View File

@ -120,8 +120,11 @@ namespace SHADE
collisionTag = newCollisionTag; collisionTag = newCollisionTag;
if (rp3dCollider) if (rp3dCollider)
{
rp3dCollider->setCollisionCategoryBits(collisionTag->GetMask());
rp3dCollider->setCollideWithMaskBits(collisionTag->GetMask()); rp3dCollider->setCollideWithMaskBits(collisionTag->GetMask());
} }
}
void SHCollisionShape::SetFriction(float friction) noexcept void SHCollisionShape::SetFriction(float friction) noexcept
{ {
@ -144,7 +147,12 @@ namespace SHADE
material.SetDensity(density); material.SetDensity(density);
if (rp3dCollider) if (rp3dCollider)
{
rp3dCollider->getMaterial().setMassDensity(material.GetDensity()); rp3dCollider->getMaterial().setMassDensity(material.GetDensity());
// Recompute the bodies' mass
dynamic_cast<rp3d::RigidBody*>(collider->collisionBody)->updateMassPropertiesFromColliders();
}
} }
void SHCollisionShape::SetMaterial(const SHPhysicsMaterial& newMaterial) noexcept void SHCollisionShape::SetMaterial(const SHPhysicsMaterial& newMaterial) noexcept
@ -157,6 +165,9 @@ namespace SHADE
rp3dMaterial.setFrictionCoefficient(material.GetFriction()); rp3dMaterial.setFrictionCoefficient(material.GetFriction());
rp3dMaterial.setBounciness(material.GetBounciness()); rp3dMaterial.setBounciness(material.GetBounciness());
rp3dMaterial.setMassDensity(material.GetDensity()); rp3dMaterial.setMassDensity(material.GetDensity());
// Recompute the bodies' mass
dynamic_cast<rp3d::RigidBody*>(collider->collisionBody)->updateMassPropertiesFromColliders();
} }
} }
@ -180,7 +191,13 @@ namespace SHADE
isTrigger ? flags |= FLAG_VALUE : flags &= ~FLAG_VALUE; isTrigger ? flags |= FLAG_VALUE : flags &= ~FLAG_VALUE;
if (rp3dCollider) if (rp3dCollider)
{
rp3dCollider->setIsTrigger(isTrigger); rp3dCollider->setIsTrigger(isTrigger);
// Recompute the bodies' mass
dynamic_cast<rp3d::RigidBody*>(collider->collisionBody)->updateMassPropertiesFromColliders();
}
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -189,9 +206,12 @@ namespace SHADE
void SHCollisionShape::UpdateCollisionTags() noexcept void SHCollisionShape::UpdateCollisionTags() noexcept
{ {
if (collisionTag->IsDirty()) if (rp3dCollider && collisionTag->IsDirty())
{
rp3dCollider->setCollisionCategoryBits(collisionTag->GetMask());
rp3dCollider->setCollideWithMaskBits(collisionTag->GetMask()); rp3dCollider->setCollideWithMaskBits(collisionTag->GetMask());
} }
}
void SHCollisionShape::Update() noexcept void SHCollisionShape::Update() noexcept

View File

@ -210,9 +210,6 @@ namespace SHADE
{ {
.entityID = rigidBody->GetEID() .entityID = rigidBody->GetEID()
, .bodyType = rigidBody->type , .bodyType = rigidBody->type
, .interpolate = rigidBody->interpolate
, .drag = rigidBody->drag
, .angularDrag = rigidBody->angularDrag
}; };
rigidBodyQueue.push(RIGID_BODY_DEF); rigidBodyQueue.push(RIGID_BODY_DEF);
@ -232,22 +229,8 @@ namespace SHADE
SHColliderDef::ShapeDef shapeDef SHColliderDef::ShapeDef shapeDef
{ {
.type = shape->GetType() .type = shape->GetType()
, .posOffset = shape->GetPositionOffset()
, .rotOffset = shape->GetRotationOffset()
}; };
switch (shape->GetType())
{
case SHCollisionShape::Type::SPHERE:
shapeDef.size.x = dynamic_cast<const SHSphere*>(shape)->GetWorldRadius();
break;
case SHCollisionShape::Type::BOX:
shapeDef.size = dynamic_cast<const SHBox*>(shape)->GetWorldExtents();
break;
default:
break;
}
colliderDef.shapes.emplace_back(shapeDef); colliderDef.shapes.emplace_back(shapeDef);
} }
@ -311,9 +294,9 @@ namespace SHADE
rigidBodyComponent->SetFreezeRotationY (rigidBodyComponent->GetFreezeRotationY()); rigidBodyComponent->SetFreezeRotationY (rigidBodyComponent->GetFreezeRotationY());
rigidBodyComponent->SetFreezeRotationZ (rigidBodyComponent->GetFreezeRotationZ()); rigidBodyComponent->SetFreezeRotationZ (rigidBodyComponent->GetFreezeRotationZ());
rigidBodyComponent->SetInterpolate (DEF.interpolate); rigidBodyComponent->SetInterpolate (rigidBodyComponent->IsInterpolating());
rigidBodyComponent->SetDrag (DEF.drag); rigidBodyComponent->SetDrag (rigidBodyComponent->GetDrag());
rigidBodyComponent->SetAngularDrag (DEF.angularDrag); rigidBodyComponent->SetAngularDrag (rigidBodyComponent->GetAngularDrag());
rigidBodyQueue.pop(); rigidBodyQueue.pop();
} }

View File

@ -47,9 +47,6 @@ namespace SHADE
{ {
EntityID entityID = MAX_EID; EntityID entityID = MAX_EID;
SHRigidBodyComponent::Type bodyType = SHRigidBodyComponent::Type::STATIC; SHRigidBodyComponent::Type bodyType = SHRigidBodyComponent::Type::STATIC;
bool interpolate = true;
float drag = 0.0f;
float angularDrag = 0.0f;
}; };
struct SHColliderDef struct SHColliderDef
@ -57,9 +54,6 @@ namespace SHADE
struct ShapeDef struct ShapeDef
{ {
SHCollisionShape::Type type = SHCollisionShape::Type::SPHERE; SHCollisionShape::Type type = SHCollisionShape::Type::SPHERE;
SHVec3 posOffset = SHVec3::Zero;
SHVec3 rotOffset = SHVec3::Zero;
SHVec3 size = SHVec3::Zero; // x for sphere radius, all 3 for box extents
}; };
EntityID entityID = MAX_EID; EntityID entityID = MAX_EID;

View File

@ -42,6 +42,7 @@ namespace SHADE
/* Friends */ /* Friends */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
friend class SHCollisionShape;
friend class SHPhysicsSystem; friend class SHPhysicsSystem;
friend class SHPhysicsObjectManager; friend class SHPhysicsObjectManager;

View File

@ -122,7 +122,7 @@ namespace SHADE
// Initialise collision tags // Initialise collision tags
std::filesystem::path defaultCollisionTagNameFilePath { ASSET_ROOT }; std::filesystem::path defaultCollisionTagNameFilePath { ASSET_ROOT };
defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig");
SHCollisionTagMatrix::Init(defaultCollisionTagNameFilePath); SHCollisionTagMatrix::LoadFromFile(defaultCollisionTagNameFilePath);
// Register Events // Register Events
for (int i = 0; i < NUM_EVENT_FUNCTIONS; ++i) for (int i = 0; i < NUM_EVENT_FUNCTIONS; ++i)
@ -145,7 +145,7 @@ namespace SHADE
// Write collision tag names to file // Write collision tag names to file
std::filesystem::path defaultCollisionTagNameFilePath { ASSET_ROOT }; std::filesystem::path defaultCollisionTagNameFilePath { ASSET_ROOT };
defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig");
SHCollisionTagMatrix::Exit(defaultCollisionTagNameFilePath); SHCollisionTagMatrix::SaveToFile(defaultCollisionTagNameFilePath);
} }
const std::vector<SHPhysicsRaycastResult>& SHPhysicsSystem::Raycast(const SHRaycaster::RaycastInfo& info) noexcept const std::vector<SHPhysicsRaycastResult>& SHPhysicsSystem::Raycast(const SHRaycaster::RaycastInfo& info) noexcept
@ -155,7 +155,7 @@ namespace SHADE
// Load start and end points into the container for debug drawing // Load start and end points into the container for debug drawing
#ifdef SHEDITOR #ifdef SHEDITOR
SHVec3 endPos = info.ray.position + info.ray.direction * std::clamp(info.distance,0.0f, SHRay::MAX_RAYCAST_DIST); SHVec3 endPos = info.ray.position + info.ray.direction * std::clamp(info.distance, 0.0f, SHRay::MAX_RAYCAST_DIST);
if (!results.empty()) if (!results.empty())
endPos = results.back().position; endPos = results.back().position;