diff --git a/Assets/Scenes/PhysicsTest.shade b/Assets/Scenes/PhysicsTest.shade index b3fda265..155f6263 100644 --- a/Assets/Scenes/PhysicsTest.shade +++ b/Assets/Scenes/PhysicsTest.shade @@ -4,14 +4,14 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 0, y: 7, z: 0} + Translate: {x: 0, y: 7, z: -0.5} Rotate: {x: 0, y: 0, z: 0.785398185} Scale: {x: 0.999999344, y: 0.999999821, z: 0.999999523} IsActive: true RigidBody Component: Type: Dynamic Drag: 0.00999999978 - Angular Drag: 0 + Angular Drag: 0.100000001 Use Gravity: true Interpolate: true Sleeping Enabled: true @@ -34,11 +34,7 @@ Position Offset: {x: 0, y: 0, z: 0} Rotation Offset: {x: 0, y: 0, z: 0} IsActive: true - Scripts: - - Type: PhysicsTestObj - Enabled: true - forceAmount: 200 - torqueAmount: 5 + Scripts: ~ - EID: 1 Name: Default IsActive: true @@ -70,7 +66,7 @@ Camera Component: Position: {x: 0, y: 2, z: 7} Pitch: 0 - Yaw: 95 + Yaw: 0 Roll: 0 Width: 1920 Height: 1080 @@ -182,9 +178,9 @@ Scale: {x: 1, y: 1, z: 1} IsActive: true RigidBody Component: - Type: Static + Type: Dynamic Drag: 0.00999999978 - Angular Drag: 0.00999999978 + Angular Drag: 0.100000001 Use Gravity: true Interpolate: true Sleeping Enabled: true diff --git a/Assets/Scenes/x.shade b/Assets/Scenes/x.shade index d7aaee0e..b7f3c00f 100644 --- a/Assets/Scenes/x.shade +++ b/Assets/Scenes/x.shade @@ -4,7 +4,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 0, y: 0, z: 0} + Translate: {x: 2, y: 0, z: 0} Rotate: {x: 0, y: 0, z: 0} Scale: {x: 1, y: 1, z: 1} IsActive: true @@ -12,7 +12,7 @@ Type: Dynamic Drag: 0.00999999978 Angular Drag: 0.100000001 - Use Gravity: false + Use Gravity: true Interpolate: true Sleeping Enabled: false Freeze Position X: false @@ -41,7 +41,7 @@ NumberOfChildren: 0 Components: Camera Component: - Position: {x: 0, y: 0, z: -10} + Position: {x: 0, y: 0, z: 5} Pitch: 0 Yaw: 0 Roll: 0 @@ -51,4 +51,50 @@ Far: 10000 Perspective: true IsActive: true + Scripts: ~ +- EID: 2 + Name: Default + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: -3, z: 0} + Rotate: {x: 0, y: 0, z: 0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + RigidBody Component: + Type: Dynamic + Drag: 0.00999999978 + Angular Drag: 0.100000001 + Use Gravity: false + Interpolate: true + Sleeping Enabled: false + Freeze Position X: true + Freeze Position Y: true + Freeze Position Z: true + Freeze Rotation X: false + Freeze Rotation Y: false + Freeze Rotation Z: false + IsActive: true + Collider Component: + Colliders: + - Is Trigger: false + Collision Tag: 1 + Type: Sphere + Radius: 1 + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 0, y: 0, z: 0} + Rotation Offset: {x: 0, y: 0, z: 0} + - Is Trigger: false + Collision Tag: 1 + Type: Box + Half Extents: {x: 1, y: 1, z: 1} + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 2, y: 0, z: 0} + Rotation Offset: {x: 0, y: 0, z: 0} + IsActive: true Scripts: ~ \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.cpp b/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.cpp index f8049451..3a47a5cf 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.cpp +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.cpp @@ -97,6 +97,19 @@ namespace SHADE /* Public Member Function Definitions */ /*-----------------------------------------------------------------------------------*/ + void SHBox::Update() noexcept + { + const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); + SetScale(PARENT_TRANSFORM.scale); + + if (rp3dCollider) + { + const rp3d::Transform OFFSETS{ positionOffset, SHQuaternion::FromEuler(rotationOffset) }; + rp3dCollider->setLocalToBodyTransform(OFFSETS); + } + } + + SHMatrix SHBox::GetTRS() const noexcept { const SHQuaternion ROTATION = collider->GetTransform().orientation * SHQuaternion::FromEuler(rotationOffset); diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.h b/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.h index 5fa394d9..7cb425d5 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.h +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.h @@ -65,6 +65,7 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ + void Update () noexcept override; [[nodiscard]] SHMatrix GetTRS () const noexcept override; private: diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.h b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.h index b9b42daa..3f7bf10c 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.h +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.h @@ -127,7 +127,7 @@ namespace SHADE * @brief * Computes the transform of the shape. */ - void Update() noexcept; + virtual void Update() noexcept; /** * @brief diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.cpp b/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.cpp index 46fbc7db..ec753542 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.cpp +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.cpp @@ -98,10 +98,25 @@ namespace SHADE /* Public Member Function Definitions */ /*-----------------------------------------------------------------------------------*/ + void SHSphere::Update() noexcept + { + const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); + + const float SPHERE_SCALE = std::fabs(SHMath::Max({ PARENT_TRANSFORM.scale.x, PARENT_TRANSFORM.scale.y, PARENT_TRANSFORM.scale.z })); + SetScale(SPHERE_SCALE); + + if (rp3dCollider) + { + const rp3d::Transform OFFSETS{ positionOffset, SHQuaternion::FromEuler(rotationOffset) }; + rp3dCollider->setLocalToBodyTransform(OFFSETS); + } + } + + SHMatrix SHSphere::GetTRS() const noexcept { const SHQuaternion ROTATION = collider->GetTransform().orientation * SHQuaternion::FromEuler(rotationOffset); - const SHVec3 SCALE = GetWorldRadius() * 2.0f; + const SHVec3 SCALE = GetWorldRadius(); const SHMatrix TRS = SHMatrix::Rotate(ROTATION) * SHMatrix::Translate(collider->GetTransform().position); const SHVec3 POSITION = SHVec3::Transform(positionOffset, TRS); diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.h b/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.h index bf9301fe..a1e65f3f 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.h +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.h @@ -65,6 +65,7 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ + void Update () noexcept override; [[nodiscard]] SHMatrix GetTRS () const noexcept override; private: diff --git a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp index 36af4592..32acaabe 100644 --- a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp +++ b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp @@ -33,19 +33,6 @@ namespace SHADE , collisionBody { nullptr } {} - SHColliderComponent::~SHColliderComponent() - { - int32_t numShapes = static_cast(shapes.size()); - while (--numShapes >= 0) - { - delete shapes[numShapes]; - shapes[numShapes] = nullptr; - } - - shapes.clear(); - } - - /*-----------------------------------------------------------------------------------*/ /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -150,6 +137,18 @@ namespace SHADE /* Public Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ + void SHColliderComponent::OnDestroy() + { + int32_t numShapes = static_cast(shapes.size()); + while (--numShapes >= 0) + { + delete shapes[numShapes]; + shapes[numShapes] = nullptr; + } + + shapes.clear(); + } + const SHMatrix& SHColliderComponent::ComputeTRS() noexcept { return transform.ComputeTRS(); @@ -198,8 +197,6 @@ namespace SHADE int SHColliderComponent::AddBoxCollisionShape(const SHVec3& relativeExtents, const SHVec3& posOffset, const SHVec3& rotOffset) { - SHASSERT(factory, "Physics factory missing from Collider Component! Unable to add colliders!") - const uint32_t NEW_INDEX = static_cast(shapes.size()); // Create collision shape diff --git a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.h b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.h index 6b971e23..7dcde150 100644 --- a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.h +++ b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.h @@ -60,7 +60,7 @@ namespace SHADE SHColliderComponent () noexcept; SHColliderComponent (const SHColliderComponent& rhs) noexcept = default; SHColliderComponent (SHColliderComponent&& rhs) noexcept = default; - ~SHColliderComponent () override; + ~SHColliderComponent () override = default; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ @@ -101,6 +101,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ + void OnDestroy() override; + /** * @brief * Computes the TRS for the collider's transform diff --git a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp index a374665d..ee4db90d 100644 --- a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp +++ b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp @@ -155,21 +155,22 @@ namespace SHADE type = newType; - SHASSERT(rigidBody, "Unable to find rp3dBody on RigidBodyComponent!") - - switch (type) + if (rigidBody) { - case Type::STATIC: - rigidBody->setType(rp3d::BodyType::STATIC); - break; - case Type::KINEMATIC: - rigidBody->setType(rp3d::BodyType::KINEMATIC); - break; - case Type::DYNAMIC: - rigidBody->setType(rp3d::BodyType::DYNAMIC); - break; - default: - break; + switch (type) + { + case Type::STATIC: + rigidBody->setType(rp3d::BodyType::STATIC); + break; + case Type::KINEMATIC: + rigidBody->setType(rp3d::BodyType::KINEMATIC); + break; + case Type::DYNAMIC: + rigidBody->setType(rp3d::BodyType::DYNAMIC); + break; + default: + break; + } } } diff --git a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.h b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.h index a75256b8..6332d1e6 100644 --- a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.h +++ b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.h @@ -158,8 +158,8 @@ namespace SHADE , LINEAR_Y = 0x8 , LINEAR_Z = 0x10 , ANGULAR_X = 0x20 - , ANGULAR_Y = 0x30 - , ANGULAR_Z = 0x40 + , ANGULAR_Y = 0x40 + , ANGULAR_Z = 0x80 }; /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp index 22a51c41..3fd441b4 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp @@ -134,6 +134,7 @@ namespace SHADE } objectManager.SetFactory(&factory); + collisionListener.BindToSystem(this); raycaster.BindToSystem(this); }