diff --git a/Assets/Scenes/M2Scene.shade b/Assets/Scenes/M2Scene.shade index 443d4a87..6086eed9 100644 --- a/Assets/Scenes/M2Scene.shade +++ b/Assets/Scenes/M2Scene.shade @@ -50,7 +50,7 @@ Colliders: - Is Trigger: false Type: Box - Half Extents: {x: 24.7399445, y: 0.25, z: 8.75} + Half Extents: {x: 1, y: 1, z: 1} Friction: 0.400000006 Bounciness: 0 Density: 1 @@ -90,23 +90,7 @@ Bounciness: 0 Density: 1 Position Offset: {x: 0, y: 0.5, z: 0} - Scripts: - - Type: PlayerController - drag: 2 - currentState: 0 - maxMoveVel: 2 - moveForce: 50 - sprintMultiplier: 2 - rotationFactorPerFrame: 1 - maxJumpHeight: 4 - maxJumpTime: 0.75 - fallMultipler: 2 - lightMultiper: 0.75 - mediumMultiper: 0.5 - heavyMultiper: 0.25 - - Type: PickAndThrow - throwForce: [200, 300, 200] - item: 5 + Scripts: ~ - EID: 3 Name: Default IsActive: true @@ -126,12 +110,7 @@ Translate: {x: 0, y: 0, z: 0} Rotate: {x: 0, y: 0, z: 0} Scale: {x: 1, y: 1, z: 1} - Scripts: - - Type: SHADE_Scripting.ThirdPersonCamera - armLength: 2 - turnSpeedPitch: 0.300000012 - turnSpeedYaw: 0.5 - pitchClamp: 45 + Scripts: ~ - EID: 9 Name: Default IsActive: true @@ -186,9 +165,7 @@ Bounciness: 0 Density: 1 Position Offset: {x: 0, y: 0.5, z: 0} - Scripts: - - Type: Item - currCategory: 0 + Scripts: ~ - EID: 6 Name: AI IsActive: true @@ -223,14 +200,7 @@ Bounciness: 0 Density: 1 Position Offset: {x: 0, y: 0.5, z: 0} - Scripts: - - Type: AIPrototype - movementForceMultiplier: 100 - patrolSpeed: 0.400000006 - chaseSpeed: 0.800000012 - distanceToCapture: 1.20000005 - distanceToStartChase: 2 - distanceToEndChase: 2.5 + Scripts: ~ - EID: 7 Name: Default IsActive: true @@ -256,4 +226,23 @@ Color: {x: 1, y: 1, z: 1, w: 1} Layer: 4294967295 Strength: 0.25 + Scripts: ~ +- EID: 10 + Name: Default + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: 2.45315814, z: -5} + Rotate: {x: -0, y: 0, z: -0} + Scale: {x: 2, y: 1, z: 1} + Collider Component: + Colliders: + - Is Trigger: false + Type: Box + Half Extents: {x: 2, y: 1, z: 1} + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 0, y: 0, z: 0} Scripts: ~ \ No newline at end of file diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index ecacdc79..8281f114 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -92,7 +92,7 @@ namespace Sandbox floorRigidBody.SetType(SHRigidBodyComponent::Type::STATIC); - auto* floorBox = floorCollider.AddBoundingBox(); + floorCollider.AddBoundingBox(); // Create blank entity with a script //testObj = SHADE::SHEntityManager::CreateEntity(); diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index d7ae56c1..85d10c1a 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -224,9 +224,6 @@ namespace SHADE if (!component) return; - // Get transform component for extrapolating relative sizes - auto* transformComponent = SHComponentManager::GetComponent_s(component->GetEID()); - const auto componentType = rttr::type::get(*component); SHEditorWidgets::CheckBox("##IsActive", [component]() {return component->isActive; }, [component](bool const& active) {component->isActive = active; }, "Is Component Active"); ImGui::SameLine(); @@ -249,28 +246,21 @@ namespace SHADE if (collider->GetType() == SHCollisionShape::Type::BOX) { SHEditorWidgets::BeginPanel(std::format("{} Box #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); - SHEditorWidgets::CheckBox("Is Trigger", [collider]() {return collider->IsTrigger(); }, [collider](bool const& value) {collider->SetIsTrigger(value); }, "Is Trigger"); auto box = reinterpret_cast(collider->GetShape()); SHEditorWidgets::DragVec3 ( "Half Extents", { "X", "Y", "Z" }, - [box, transformComponent] { return (box->GetHalfExtents() * 2.0f) / transformComponent->GetWorldScale(); }, + [box] { return box->GetRelativeExtents(); }, [collider](SHVec3 const& vec) { collider->SetBoundingBox(vec); }); } else if (collider->GetType() == SHCollisionShape::Type::SPHERE) { SHEditorWidgets::BeginPanel(std::format("{} Sphere #{}", ICON_MD_CIRCLE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); - SHEditorWidgets::CheckBox("Is Trigger", [collider]() {return collider->IsTrigger(); }, [collider](bool const& value) {collider->SetIsTrigger(value); }, "Is Trigger"); auto sphere = reinterpret_cast(collider->GetShape()); SHEditorWidgets::DragFloat ( "Radius", - [sphere, transformComponent] - { - const SHVec3& TF_WORLD_SCALE = transformComponent->GetWorldScale(); - const float MAX_SCALE = SHMath::Max({ TF_WORLD_SCALE.x, TF_WORLD_SCALE.y, TF_WORLD_SCALE.z }); - return (sphere->GetRadius() / MAX_SCALE) * 2.0f; - }, + [sphere] { return sphere->GetRelativeRadius(); }, [collider](float const& value) { collider->SetBoundingSphere(value); }); } else if (collider->GetType() == SHCollisionShape::Type::CAPSULE) diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp index d0ba2f14..5bbf5e15 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp @@ -25,11 +25,13 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ SHBoundingBox::SHBoundingBox() noexcept + : RelativeExtents { SHVec3::One } { type = Type::BOX; } SHBoundingBox::SHBoundingBox(const SHVec3& c, const SHVec3& hE) noexcept + : RelativeExtents { SHVec3::One } { type = Type::BOX; @@ -45,16 +47,18 @@ namespace SHADE type = Type::BOX; - Center = rhs.Center; - Extents = rhs.Extents; + Center = rhs.Center; + Extents = rhs.Extents; + RelativeExtents = rhs.RelativeExtents; } SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept { type = Type::BOX; - Center = rhs.Center; - Extents = rhs.Extents; + Center = rhs.Center; + Extents = rhs.Extents; + RelativeExtents = rhs.RelativeExtents; } /*-----------------------------------------------------------------------------------*/ @@ -69,8 +73,9 @@ namespace SHADE } else if (this != &rhs) { - Center = rhs.Center; - Extents = rhs.Extents; + Center = rhs.Center; + Extents = rhs.Extents; + RelativeExtents = rhs.RelativeExtents; } return *this; @@ -84,8 +89,9 @@ namespace SHADE } else { - Center = rhs.Center; - Extents = rhs.Extents; + Center = rhs.Center; + Extents = rhs.Extents; + RelativeExtents = rhs.RelativeExtents; } return *this; @@ -100,11 +106,16 @@ namespace SHADE return Center; } - SHVec3 SHBoundingBox::GetHalfExtents() const noexcept + SHVec3 SHBoundingBox::GetWorldExtents() const noexcept { return Extents; } + const SHVec3& SHBoundingBox::GetRelativeExtents() const noexcept + { + return RelativeExtents; + } + SHVec3 SHBoundingBox::GetMin() const noexcept { return SHVec3{ Center.x - Extents.x, Center.y - Extents.y, Center.z - Extents.z }; @@ -124,9 +135,14 @@ namespace SHADE Center = newCenter; } - void SHBoundingBox::SetHalfExtents(const SHVec3& newHalfExtents) noexcept + void SHBoundingBox::SetWorldExtents(const SHVec3& newWorldExtents) noexcept { - Extents = newHalfExtents; + Extents = newWorldExtents; + } + + void SHBoundingBox::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept + { + RelativeExtents = newRelativeExtents; } void SHBoundingBox::SetMin(const SHVec3& min) noexcept diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h index 5b3d26d5..e2757c17 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h @@ -54,21 +54,23 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHVec3 GetCenter () const noexcept; - [[nodiscard]] SHVec3 GetHalfExtents() const noexcept; - [[nodiscard]] SHVec3 GetMin () const noexcept; - [[nodiscard]] SHVec3 GetMax () const noexcept; - [[nodiscard]] std::vector GetVertices () const noexcept; + [[nodiscard]] SHVec3 GetCenter () const noexcept; + [[nodiscard]] SHVec3 GetWorldExtents () const noexcept; + [[nodiscard]] const SHVec3& GetRelativeExtents () const noexcept; + [[nodiscard]] SHVec3 GetMin () const noexcept; + [[nodiscard]] SHVec3 GetMax () const noexcept; + [[nodiscard]] std::vector GetVertices () const noexcept; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - void SetCenter (const SHVec3& newCenter) noexcept; - void SetHalfExtents (const SHVec3& newHalfExtents) noexcept; - void SetMin (const SHVec3& min) noexcept; - void SetMax (const SHVec3& max) noexcept; - void SetMinMax (const SHVec3& min, const SHVec3& max) noexcept; + void SetCenter (const SHVec3& newCenter) noexcept; + void SetWorldExtents (const SHVec3& newWorldExtents) noexcept; + void SetRelativeExtents (const SHVec3& newRelativeExtents) noexcept; + void SetMin (const SHVec3& min) noexcept; + void SetMax (const SHVec3& max) noexcept; + void SetMinMax (const SHVec3& min, const SHVec3& max) noexcept; /*---------------------------------------------------------------------------------*/ /* Function Members */ @@ -89,6 +91,13 @@ namespace SHADE [[nodiscard]] static bool Intersect (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; [[nodiscard]] static SHBoundingBox BuildFromBoxes (const SHBoundingBox* boxes, size_t numBoxes) noexcept; [[nodiscard]] static SHBoundingBox BuildFromVertices (const SHVec3* vertices, size_t numVertices, size_t stride = 0) noexcept; + + private: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + SHVec3 RelativeExtents; }; diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp index 62bf12b2..f843a6bb 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp @@ -25,11 +25,13 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ SHBoundingSphere::SHBoundingSphere() noexcept + : RelativeRadius { 1.0f } { type = Type::SPHERE; } SHBoundingSphere::SHBoundingSphere(const SHVec3& center, float radius) noexcept + : RelativeRadius { 1.0f } { type = Type::SPHERE; @@ -44,16 +46,18 @@ namespace SHADE type = Type::SPHERE; - Center = rhs.Center; - Radius = rhs.Radius; + Center = rhs.Center; + Radius = rhs.Radius; + RelativeRadius = rhs.RelativeRadius; } SHBoundingSphere::SHBoundingSphere(SHBoundingSphere&& rhs) noexcept { type = Type::SPHERE; - Center = rhs.Center; - Radius = rhs.Radius; + Center = rhs.Center; + Radius = rhs.Radius; + RelativeRadius = rhs.RelativeRadius; } /*-----------------------------------------------------------------------------------*/ @@ -68,8 +72,9 @@ namespace SHADE } else if (this != &rhs) { - Center = rhs.Center; - Radius = rhs.Radius; + Center = rhs.Center; + Radius = rhs.Radius; + RelativeRadius = rhs.RelativeRadius; } return *this; @@ -83,8 +88,9 @@ namespace SHADE } else { - Center = rhs.Center; - Radius = rhs.Radius; + Center = rhs.Center; + Radius = rhs.Radius; + RelativeRadius = rhs.RelativeRadius; } return *this; @@ -99,11 +105,16 @@ namespace SHADE return Center; } - float SHBoundingSphere::GetRadius() const noexcept + float SHBoundingSphere::GetWorldRadius() const noexcept { return Radius; } - + + float SHBoundingSphere::GetRelativeRadius() const noexcept + { + return RelativeRadius; + } + /*-----------------------------------------------------------------------------------*/ /* Setter Function Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -113,9 +124,14 @@ namespace SHADE Center = center; } - void SHBoundingSphere::SetRadius(float radius) noexcept + void SHBoundingSphere::SetWorldRadius(float newWorldRadius) noexcept { - Radius = radius; + Radius = newWorldRadius; + } + + void SHBoundingSphere::SetRelativeRadius(float newRelativeRadius) noexcept + { + RelativeRadius = newRelativeRadius; } /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h index 001e889b..d94722d6 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h @@ -48,15 +48,17 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHVec3 GetCenter () const noexcept; - [[nodiscard]] float GetRadius () const noexcept; + [[nodiscard]] SHVec3 GetCenter () const noexcept; + [[nodiscard]] float GetWorldRadius () const noexcept; + [[nodiscard]] float GetRelativeRadius () const noexcept; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - void SetCenter (const SHVec3& center) noexcept; - void SetRadius (float radius) noexcept; + void SetCenter (const SHVec3& center) noexcept; + void SetWorldRadius (float newWorldRadius) noexcept; + void SetRelativeRadius (float newRelativeRadius) noexcept; /*---------------------------------------------------------------------------------*/ /* Function Members */ @@ -79,5 +81,12 @@ namespace SHADE [[nodiscard]] static SHBoundingSphere BuildFromSpheres (const SHBoundingSphere* spheres, size_t numSpheres) noexcept; [[nodiscard]] static SHBoundingSphere BuildFromVertices (const SHVec3* vertices, size_t numVertices, size_t stride = 0) noexcept; + private: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + float RelativeRadius; + }; } // namespace SHADE diff --git a/SHADE_Engine/src/Physics/Components/SHColliderComponent.cpp b/SHADE_Engine/src/Physics/Components/SHColliderComponent.cpp index af3965fb..93126fc5 100644 --- a/SHADE_Engine/src/Physics/Components/SHColliderComponent.cpp +++ b/SHADE_Engine/src/Physics/Components/SHColliderComponent.cpp @@ -48,6 +48,11 @@ namespace SHADE return orientation.ToEuler(); } + const SHVec3& SHColliderComponent::GetScale() const noexcept + { + return scale; + } + const SHColliderComponent::CollisionShapes& SHColliderComponent::GetCollisionShapes() const noexcept { return collisionShapes; @@ -75,12 +80,46 @@ namespace SHADE } - SHBoundingBox* SHColliderComponent::AddBoundingBox(const SHVec3& halfExtents, const SHVec3& posOffset) noexcept + void SHColliderComponent::RecomputeCollisionShapes() noexcept + { + for (auto& collisionShape : collisionShapes) + { + switch (collisionShape.GetType()) + { + case SHCollisionShape::Type::BOX: + { + auto* box = reinterpret_cast(collisionShape.GetShape()); + const SHVec3& RELATIVE_EXTENTS = box->GetRelativeExtents(); + + // Recompute world extents based on new scale and fixed relative extents + const SHVec3 WORLD_EXTENTS = RELATIVE_EXTENTS * (scale * 0.5f); + box->SetWorldExtents(WORLD_EXTENTS); + + continue; + } + case SHCollisionShape::Type::SPHERE: + { + auto* sphere = reinterpret_cast(collisionShape.GetShape()); + const float RELATIVE_RADIUS = sphere->GetRelativeRadius(); + + // Recompute world radius based on new scale and fixed radius + const float MAX_SCALE = SHMath::Max({ scale.x, scale.y, scale.z }); + const float WORLD_RADIUS = RELATIVE_RADIUS * MAX_SCALE * 0.5f; + sphere->SetWorldRadius(WORLD_RADIUS); + + continue; + } + default: continue; + } + } + } + + int SHColliderComponent::AddBoundingBox(const SHVec3& halfExtents, const SHVec3& posOffset, const SHVec3& rotOffset) noexcept { if (!system) { SHLOG_ERROR("Physics system does not exist, unable to add Box Collider!") - return nullptr; + return -1; } static constexpr auto TYPE = SHCollisionShape::Type::BOX; @@ -89,20 +128,21 @@ namespace SHADE collider.entityID = GetEID(); collider.SetPositionOffset(posOffset); + collider.SetRotationOffset(rotOffset); collider.SetBoundingBox(halfExtents); // Notify Physics System system->AddCollisionShape(GetEID(), &collider); - return reinterpret_cast(collider.GetShape()); + return static_cast(collisionShapes.size()) - 1; } - SHBoundingSphere* SHColliderComponent::AddBoundingSphere(float radius, const SHVec3& posOffset) noexcept + int SHColliderComponent::AddBoundingSphere(float radius, const SHVec3& posOffset) noexcept { if (!system) { SHLOG_ERROR("Physics system does not exist, unable to add Sphere Collider!") - return nullptr; + return -1; } static constexpr auto TYPE = SHCollisionShape::Type::SPHERE; @@ -116,7 +156,7 @@ namespace SHADE // Notify Physics System system->AddCollisionShape(GetEID(), &collider); - return reinterpret_cast(collider.GetShape()); + return static_cast(collisionShapes.size()) - 1; } void SHColliderComponent::RemoveCollider(int index) diff --git a/SHADE_Engine/src/Physics/Components/SHColliderComponent.h b/SHADE_Engine/src/Physics/Components/SHColliderComponent.h index ac5e7528..5f9b7a1b 100644 --- a/SHADE_Engine/src/Physics/Components/SHColliderComponent.h +++ b/SHADE_Engine/src/Physics/Components/SHColliderComponent.h @@ -72,6 +72,7 @@ namespace SHADE [[nodiscard]] const SHVec3& GetPosition () const noexcept; [[nodiscard]] const SHQuaternion& GetOrientation () const noexcept; [[nodiscard]] SHVec3 GetRotation () const noexcept; + [[nodiscard]] const SHVec3& GetScale () const noexcept; [[nodiscard]] const CollisionShapes& GetCollisionShapes() const noexcept; [[nodiscard]] SHCollisionShape& GetCollisionShape (int index); @@ -80,13 +81,15 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - void OnCreate () override; - void OnDestroy () override; + void OnCreate () override; + void OnDestroy () override; - void RemoveCollider (int index); + void RecomputeCollisionShapes () noexcept; - SHBoundingBox* AddBoundingBox (const SHVec3& halfExtents = SHVec3::One, const SHVec3& posOffset = SHVec3::Zero) noexcept; - SHBoundingSphere* AddBoundingSphere (float radius = 1.0f, const SHVec3& posOffset = SHVec3::Zero) noexcept; + void RemoveCollider (int index); + + int AddBoundingBox (const SHVec3& halfExtents = SHVec3::One, const SHVec3& posOffset = SHVec3::Zero, const SHVec3& rotOffset = SHVec3::Zero) noexcept; + int AddBoundingSphere (float radius = 1.0f, const SHVec3& posOffset = SHVec3::Zero) noexcept; private: @@ -98,6 +101,7 @@ namespace SHADE SHVec3 position; SHQuaternion orientation; + SHVec3 scale; CollisionShapes collisionShapes; RTTR_ENABLE() diff --git a/SHADE_Engine/src/Physics/SHCollisionShape.cpp b/SHADE_Engine/src/Physics/SHCollisionShape.cpp index 78ec36f9..c8f8020c 100644 --- a/SHADE_Engine/src/Physics/SHCollisionShape.cpp +++ b/SHADE_Engine/src/Physics/SHCollisionShape.cpp @@ -15,8 +15,8 @@ // Project Headers #include "Math/Geometry/SHBoundingBox.h" #include "Math/Geometry/SHBoundingSphere.h" -#include "Math/Transform/SHTransformComponent.h" #include "Math/SHMathHelpers.h" +#include "Physics/Components/SHColliderComponent.h" #include "Reflection/SHReflectionMetadata.h" namespace SHADE @@ -178,56 +178,43 @@ namespace SHADE { dirty = true; - // Set the half extents relative to transform - SHVec3 worldHalfExtents = halfExtents; + const auto* colliderComponent = SHComponentManager::GetComponent(entityID); + // Set the half extents relative to world scale + const SHVec3 WORLD_EXTENTS = halfExtents * colliderComponent->GetScale() * 0.5f; - const auto* transformComponent = SHComponentManager::GetComponent_s(entityID); - if (transformComponent != nullptr) - worldHalfExtents *= (transformComponent->GetWorldScale() * 0.5f); - - if (type == Type::BOX) - { - auto* box = reinterpret_cast(shape); - box->SetHalfExtents(worldHalfExtents); - } - else + if (type != Type::BOX) { type = Type::BOX; delete shape; - shape = new SHBoundingBox{ positionOffset, worldHalfExtents }; + shape = new SHBoundingBox{ positionOffset, WORLD_EXTENTS }; } + + auto* box = reinterpret_cast(shape); + box->SetWorldExtents(WORLD_EXTENTS); + box->SetRelativeExtents(halfExtents); } void SHCollisionShape::SetBoundingSphere(float radius) { dirty = true; - // Set the radius relative to transform - float worldRadius = radius; + const auto* colliderComponent = SHComponentManager::GetComponent(entityID); + // Set the radius relative to world scale + const SHVec3 WORLD_SCALE = colliderComponent->GetScale(); + const float MAX_SCALE = SHMath::Max({ WORLD_SCALE.x, WORLD_SCALE.y, WORLD_SCALE.z }); + const float WORLD_RADIUS = radius * MAX_SCALE * 0.5f; - const auto* transformComponent = SHComponentManager::GetComponent_s(entityID); - if (transformComponent != nullptr) - { - const SHVec3 TF_WORLD_SCALE = transformComponent->GetWorldScale(); - const float MAX_SCALE = SHMath::Max({ TF_WORLD_SCALE.x, TF_WORLD_SCALE.y, TF_WORLD_SCALE.z }); - - worldRadius *= MAX_SCALE * 0.5f; - } - - if (type == Type::SPHERE) - { - auto* sphere = reinterpret_cast(shape); - sphere->SetRadius(worldRadius); - } - else + if (type != Type::SPHERE) { type = Type::SPHERE; delete shape; - shape = new SHBoundingSphere{ positionOffset, worldRadius }; + shape = new SHBoundingSphere{ positionOffset, WORLD_RADIUS }; } - + + auto* sphere = reinterpret_cast(shape); + sphere->SetWorldRadius(WORLD_RADIUS); } void SHCollisionShape::SetIsTrigger(bool trigger) noexcept @@ -299,14 +286,14 @@ namespace SHADE { const auto* RHS_BOX = reinterpret_cast(rhs); - shape = new SHBoundingBox{ positionOffset, RHS_BOX->GetHalfExtents() }; + shape = new SHBoundingBox{ positionOffset, RHS_BOX->GetWorldExtents() }; break; } case Type::SPHERE: { const auto* RHS_SPHERE = reinterpret_cast(rhs); - shape = new SHBoundingSphere{ positionOffset, RHS_SPHERE->GetRadius() }; + shape = new SHBoundingSphere{ positionOffset, RHS_SPHERE->GetWorldRadius() }; break; } default: break; diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp index 5658f304..00c6943b 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp @@ -137,7 +137,7 @@ namespace SHADE case SHCollisionShape::Type::BOX: { const auto* box = reinterpret_cast(collider->GetShape()); - rp3d::BoxShape* newBox = factory->createBoxShape(box->GetHalfExtents()); + rp3d::BoxShape* newBox = factory->createBoxShape(box->GetWorldExtents()); rp3dBody->addCollider(newBox, OFFSETS); break; @@ -145,7 +145,7 @@ namespace SHADE case SHCollisionShape::Type::SPHERE: { const auto* sphere = reinterpret_cast(collider->GetShape()); - rp3d::SphereShape* newSphere = factory->createSphereShape(sphere->GetRadius()); + rp3d::SphereShape* newSphere = factory->createSphereShape(sphere->GetWorldRadius()); rp3dBody->addCollider(newSphere, OFFSETS); break; @@ -193,7 +193,7 @@ namespace SHADE const auto* box = reinterpret_cast(collider.GetShape()); auto* rp3dBoxShape = reinterpret_cast(rp3dCollider->getCollisionShape()); - rp3dBoxShape->setHalfExtents(box->GetHalfExtents()); + rp3dBoxShape->setHalfExtents(box->GetWorldExtents()); break; } @@ -202,7 +202,7 @@ namespace SHADE const auto* sphere = reinterpret_cast(collider.GetShape()); auto* rp3dSphereShape = reinterpret_cast(rp3dCollider->getCollisionShape()); - rp3dSphereShape->setRadius(sphere->GetRadius()); + rp3dSphereShape->setRadius(sphere->GetWorldRadius()); break; } diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index 078a5d6a..437b5ff8 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -395,6 +395,7 @@ namespace SHADE { const auto WORLD_POS = transformComponent->GetWorldPosition(); const auto WORLD_ROT = transformComponent->GetWorldOrientation(); + const auto WORLD_SCL = transformComponent->GetWorldScale(); physicsObject.SetPosition(WORLD_POS); physicsObject.SetOrientation(WORLD_ROT); @@ -409,8 +410,11 @@ namespace SHADE if (colliderComponent) { - colliderComponent->position = WORLD_POS; - colliderComponent->orientation = WORLD_ROT; + colliderComponent->position = WORLD_POS; + colliderComponent->orientation = WORLD_ROT; + colliderComponent->scale = WORLD_SCL; + + colliderComponent->RecomputeCollisionShapes(); } } @@ -744,8 +748,9 @@ namespace SHADE { SHASSERT(colliderComponent != nullptr, "Collider Component was not added to Entity " + std::to_string(ENTITY_ID) + "!"); - colliderComponent->position = transformComponent->GetWorldPosition(); - colliderComponent->orientation = transformComponent->GetWorldOrientation(); + colliderComponent->position = transformComponent->GetWorldPosition(); + colliderComponent->orientation = transformComponent->GetWorldOrientation(); + colliderComponent->scale = transformComponent->GetWorldScale(); if (physicsObject->rp3dBody == nullptr) { diff --git a/SHADE_Engine/src/Serialization/SHYAMLConverters.h b/SHADE_Engine/src/Serialization/SHYAMLConverters.h index 76be74ce..1b93c63a 100644 --- a/SHADE_Engine/src/Serialization/SHYAMLConverters.h +++ b/SHADE_Engine/src/Serialization/SHYAMLConverters.h @@ -131,13 +131,13 @@ namespace YAML case SHCollisionShape::Type::BOX: { auto const bb = reinterpret_cast(rhs.GetShape()); - node[HalfExtents] = bb->GetHalfExtents(); + node[HalfExtents] = bb->GetRelativeExtents(); } break; case SHCollisionShape::Type::SPHERE: { auto const bs = reinterpret_cast(rhs.GetShape()); - node[Radius] = bs->GetRadius(); + node[Radius] = bs->GetRelativeRadius(); } break; case SHCollisionShape::Type::CAPSULE: break; @@ -168,7 +168,7 @@ namespace YAML case SHCollisionShape::Type::BOX: { if (node[HalfExtents].IsDefined()) - rhs.SetBoundingBox(node[HalfExtents].as() * 2.0f); + rhs.SetBoundingBox(node[HalfExtents].as()); } break; case SHCollisionShape::Type::SPHERE: diff --git a/SHADE_Managed/src/Components/Collider.cxx b/SHADE_Managed/src/Components/Collider.cxx index dc5d27af..0e916b7b 100644 --- a/SHADE_Managed/src/Components/Collider.cxx +++ b/SHADE_Managed/src/Components/Collider.cxx @@ -55,11 +55,11 @@ namespace SHADE } Vector3 BoxCollider::HalfExtents::get() { - return Convert::ToCLI(getNativeBoundObject().GetHalfExtents()); + return Convert::ToCLI(getNativeBoundObject().GetWorldExtents()); } void BoxCollider::HalfExtents::set(Vector3 value) { - getNativeBoundObject().SetHalfExtents(Convert::ToNative(value)); + getNativeBoundObject().SetWorldExtents(Convert::ToNative(value)); } Vector3 BoxCollider::Min::get() { @@ -103,11 +103,11 @@ namespace SHADE } float SphereCollider::Radius::get() { - return getNativeBoundObject().GetRadius(); + return getNativeBoundObject().GetWorldRadius(); } void SphereCollider::Radius::set(float value) { - getNativeBoundObject().SetRadius(value); + getNativeBoundObject().SetWorldRadius(value); } /*---------------------------------------------------------------------------------*/