From 411c4a4e564da3f14740efa58fb953ea6fe2481a Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 19 Nov 2022 04:21:46 +0800 Subject: [PATCH] Added C# Interface for Raycasting Untested with Scripts, tested engine side. --- .../Collision/SHPhysicsRaycastResult.h | 34 +++++ .../Physics/Collision/SHPhysicsRaycaster.h | 15 +- .../src/Physics/System/SHPhysicsSystem.cpp | 6 +- .../src/Physics/System/SHPhysicsSystem.h | 6 +- .../System/SHPhysicsSystemInterface.cpp | 78 ++++++++++- .../Physics/System/SHPhysicsSystemInterface.h | 25 +++- SHADE_Managed/src/Math/Ray.cxx | 53 +++++--- SHADE_Managed/src/Math/Ray.hxx | 95 +++++++------ SHADE_Managed/src/Physics/Physics.cxx | 105 ++++++++++++++ SHADE_Managed/src/Physics/Physics.hxx | 128 ++++++++++++++++++ SHADE_Managed/src/Physics/RaycastHit.cxx | 19 +++ SHADE_Managed/src/Physics/RaycastHit.hxx | 64 +++++++++ SHADE_Managed/src/Utility/Convert.cxx | 46 ++++++- SHADE_Managed/src/Utility/Convert.hxx | 38 ++++-- 14 files changed, 609 insertions(+), 103 deletions(-) create mode 100644 SHADE_Engine/src/Physics/Collision/SHPhysicsRaycastResult.h create mode 100644 SHADE_Managed/src/Physics/Physics.cxx create mode 100644 SHADE_Managed/src/Physics/Physics.hxx create mode 100644 SHADE_Managed/src/Physics/RaycastHit.cxx create mode 100644 SHADE_Managed/src/Physics/RaycastHit.hxx diff --git a/SHADE_Engine/src/Physics/Collision/SHPhysicsRaycastResult.h b/SHADE_Engine/src/Physics/Collision/SHPhysicsRaycastResult.h new file mode 100644 index 00000000..cce01845 --- /dev/null +++ b/SHADE_Engine/src/Physics/Collision/SHPhysicsRaycastResult.h @@ -0,0 +1,34 @@ +/**************************************************************************************** + * \file SHPhysicsRaycastResult.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a Physics Raycast Result + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +// Project Includes +#include "ECS_Base/SHECSMacros.h" +#include "Math/SHRay.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + struct SH_API SHPhysicsRaycastResult : public SHRaycastResult + { + public: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + EntityID entityHit = MAX_EID; + int shapeIndex = -1; + + }; +} diff --git a/SHADE_Engine/src/Physics/Collision/SHPhysicsRaycaster.h b/SHADE_Engine/src/Physics/Collision/SHPhysicsRaycaster.h index 6d1e6f2d..81165b56 100644 --- a/SHADE_Engine/src/Physics/Collision/SHPhysicsRaycaster.h +++ b/SHADE_Engine/src/Physics/Collision/SHPhysicsRaycaster.h @@ -15,10 +15,11 @@ #include // Project Headers +#include "Math/SHRay.h" #include "Physics/PhysicsObject/SHPhysicsObjectManager.h" #include "Physics/SHPhysicsWorld.h" -#include "Math/SHRay.h" #include "SH_API.h" +#include "SHPhysicsRaycastResult.h" namespace SHADE { @@ -26,18 +27,6 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - struct SH_API SHPhysicsRaycastResult : public SHRaycastResult - { - public: - /*---------------------------------------------------------------------------------*/ - /* Data Members */ - /*---------------------------------------------------------------------------------*/ - - EntityID entityHit = MAX_EID; - int shapeIndex = -1; - - }; - class SH_API SHPhysicsRaycaster : public reactphysics3d::RaycastCallback { private: diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp index 1a99b808..f3513ffb 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp @@ -185,7 +185,7 @@ namespace SHADE return raycaster.Raycast(ray, distance); } - SHPhysicsRaycastResult SHPhysicsSystem::Linecast(const SHVec3& start, const SHVec3& end) + SHPhysicsRaycastResult SHPhysicsSystem::Linecast(const SHVec3& start, const SHVec3& end) noexcept { return raycaster.Linecast(start, end); } @@ -195,7 +195,7 @@ namespace SHADE return raycaster.ColliderRaycast(eid, ray, distance); } - SHPhysicsRaycastResult SHPhysicsSystem::ColliderRaycast(EntityID eid, int shapeIndex, const SHRay& ray, float distance) + SHPhysicsRaycastResult SHPhysicsSystem::ColliderRaycast(EntityID eid, int shapeIndex, const SHRay& ray, float distance) noexcept { return raycaster.ColliderRaycast(eid, shapeIndex, ray, distance); } @@ -205,7 +205,7 @@ namespace SHADE return raycaster.ColliderLinecast(eid, start, end); } - SHPhysicsRaycastResult SHPhysicsSystem::ColliderLinecast(EntityID eid, int shapeIndex, const SHVec3& start, const SHVec3& end) + SHPhysicsRaycastResult SHPhysicsSystem::ColliderLinecast(EntityID eid, int shapeIndex, const SHVec3& start, const SHVec3& end) noexcept { return raycaster.ColliderLinecast(eid, shapeIndex, start, end); } diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h index 4892ee90..f92be4cd 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h @@ -101,7 +101,7 @@ namespace SHADE ( const SHVec3& start , const SHVec3& end - ); + ) noexcept; /** * @brief Casts a ray at a body with colliders. @@ -131,7 +131,7 @@ namespace SHADE , int shapeIndex , const SHRay& ray , float distance = std::numeric_limits::infinity() - ); + ) noexcept; /** * @brief Casts a bounded ray at a body with colliders. @@ -161,7 +161,7 @@ namespace SHADE , int shapeIndex , const SHVec3& start , const SHVec3& end - ); + ) noexcept; // Specific Handling for Collision Shapes as they are not under the Component System. // This is done as events need to be sent out. diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystemInterface.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsSystemInterface.cpp index b142d54c..a028ffb5 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystemInterface.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystemInterface.cpp @@ -33,7 +33,7 @@ namespace SHADE return phySystem->GetAllCollisionInfo(); } - SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get collision events. Empty vector returned instead."); + SHLOGV_WARNING("Failed to get collision events. Empty vector returned instead."); return emptyVec; } const std::vector& SHPhysicsSystemInterface::GetTriggerInfo() noexcept @@ -46,7 +46,7 @@ namespace SHADE return phySystem->GetAllTriggerInfo(); } - SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get trigger events. Empty vector returned instead."); + SHLOGV_WARNING("Failed to get trigger events. Empty vector returned instead."); return emptyVec; } @@ -58,7 +58,79 @@ namespace SHADE return phySystem->GetFixedUpdateRate(); } - SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get fixed delta time. 0.0 returned instead."); + SHLOGV_WARNING("Failed to get fixed delta time. 0.0 returned instead."); return 0.0; } + + SHPhysicsRaycastResult SHPhysicsSystemInterface::Raycast(const SHRay& ray, float distance) noexcept + { + auto* physicsSystem = SHSystemManager::GetSystem(); + if (physicsSystem) + { + return physicsSystem->Raycast(ray, distance); + } + + SHLOGV_WARNING("Failed to get the physics system. No ray was casted."); + return SHPhysicsRaycastResult{}; + } + + SHPhysicsRaycastResult SHPhysicsSystemInterface::Linecast(const SHVec3& start, const SHVec3& end) noexcept + { + auto* physicsSystem = SHSystemManager::GetSystem(); + if (physicsSystem) + { + return physicsSystem->Linecast(start, end); + } + + SHLOGV_WARNING("Failed to get the physics system. No ray was casted."); + return SHPhysicsRaycastResult{}; + } + + SHPhysicsRaycastResult SHPhysicsSystemInterface::ColliderRaycast(EntityID eid, const SHRay& ray, float distance) noexcept + { + auto* physicsSystem = SHSystemManager::GetSystem(); + if (physicsSystem) + { + return physicsSystem->ColliderRaycast(eid, ray, distance); + } + + SHLOGV_WARNING("Failed to get the physics system. No ray was casted."); + return SHPhysicsRaycastResult{}; + } + + SHPhysicsRaycastResult SHPhysicsSystemInterface::ColliderRaycast(EntityID eid, int shapeIndex, const SHRay& ray, float distance) noexcept + { + auto* physicsSystem = SHSystemManager::GetSystem(); + if (physicsSystem) + { + return physicsSystem->ColliderRaycast(eid, shapeIndex, ray, distance); + } + + SHLOGV_WARNING("Failed to get the physics system. No ray was casted."); + return SHPhysicsRaycastResult{}; + } + + SHPhysicsRaycastResult SHPhysicsSystemInterface::ColliderLinecast(EntityID eid, const SHVec3& start, const SHVec3& end) noexcept + { + auto* physicsSystem = SHSystemManager::GetSystem(); + if (physicsSystem) + { + return physicsSystem->ColliderLinecast(eid, start, end); + } + + SHLOGV_WARNING("Failed to get the physics system. No ray was casted."); + return SHPhysicsRaycastResult{}; + } + + SHPhysicsRaycastResult SHPhysicsSystemInterface::ColliderLinecast(EntityID eid, int shapeIndex, const SHVec3& start, const SHVec3& end) noexcept + { + auto* physicsSystem = SHSystemManager::GetSystem(); + if (physicsSystem) + { + return physicsSystem->ColliderLinecast(eid, shapeIndex, start, end); + } + + SHLOGV_WARNING("Failed to get the physics system. No ray was casted."); + return SHPhysicsRaycastResult{}; + } } diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystemInterface.h b/SHADE_Engine/src/Physics/System/SHPhysicsSystemInterface.h index bdd04686..0065aee3 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystemInterface.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystemInterface.h @@ -14,12 +14,21 @@ of DigiPen Institute of Technology is prohibited. // STL Includes #include +// Project Headers +#include "ECS_Base/Entity/SHEntity.h" + + namespace SHADE { /*-----------------------------------------------------------------------------------*/ /* Forward Declarations */ /*-----------------------------------------------------------------------------------*/ - class SHCollisionInfo; + + class SHCollisionInfo; + class SHVec3; + struct SHRay; + struct SHPhysicsRaycastResult; + /*-----------------------------------------------------------------------------------*/ /* Type Definitions */ @@ -39,8 +48,16 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Static Usage Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] static const std::vector& GetCollisionInfo() noexcept; - [[nodiscard]] static const std::vector& GetTriggerInfo() noexcept; - [[nodiscard]] static double GetFixedDT() noexcept; + + [[nodiscard]] static const std::vector& GetCollisionInfo() noexcept; + [[nodiscard]] static const std::vector& GetTriggerInfo () noexcept; + [[nodiscard]] static double GetFixedDT () noexcept; + + [[nodiscard]] static SHPhysicsRaycastResult Raycast (const SHRay& ray, float distance = std::numeric_limits::infinity()) noexcept; + [[nodiscard]] static SHPhysicsRaycastResult Linecast (const SHVec3& start, const SHVec3& end) noexcept; + [[nodiscard]] static SHPhysicsRaycastResult ColliderRaycast (EntityID eid, const SHRay& ray, float distance = std::numeric_limits::infinity()) noexcept; + [[nodiscard]] static SHPhysicsRaycastResult ColliderRaycast (EntityID eid, int shapeIndex, const SHRay& ray, float distance = std::numeric_limits::infinity()) noexcept; + [[nodiscard]] static SHPhysicsRaycastResult ColliderLinecast (EntityID eid, const SHVec3& start, const SHVec3& end) noexcept; + [[nodiscard]] static SHPhysicsRaycastResult ColliderLinecast (EntityID eid, int shapeIndex, const SHVec3& start, const SHVec3& end) noexcept; }; } diff --git a/SHADE_Managed/src/Math/Ray.cxx b/SHADE_Managed/src/Math/Ray.cxx index ee614cbe..bfb99578 100644 --- a/SHADE_Managed/src/Math/Ray.cxx +++ b/SHADE_Managed/src/Math/Ray.cxx @@ -1,28 +1,37 @@ -/************************************************************************************//*! -\file Ray.cxx -\author Tng Kah Wei, kahwei.tng, 390009620 -\par email: kahwei.tng\@digipen.edu -\date Oct 20, 2022 -\brief Contains the definitions of functions of the Vector2 struct. - - Note: This file is written in C++17/CLI. +/**************************************************************************************** + * \file Ray.cxx + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the managed Ray struct. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ -Copyright (C) 2022 DigiPen Institute of Technology. -Reproduction or disclosure of this file or its contents without the prior written consent -of DigiPen Institute of Technology is prohibited. -*//*************************************************************************************/ -// Precompiled Headers #include "SHpch.h" + // Primary Header -#include "Math/Ray.hxx" +#include "Ray.hxx" namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* Constructors */ - /*---------------------------------------------------------------------------------*/ - Ray::Ray(Vector3 origin, Vector3 direction) - : Origin { origin } - , Direction{ direction } - {} -} \ No newline at end of file + /*-----------------------------------------------------------------------------------*/ + /* Constructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + Ray::Ray(Vector3 position, Vector3 direction) + { + Position = position; + Direction = direction; + } + + /*-----------------------------------------------------------------------------------*/ + /* Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void Ray::LookAt(Vector3 target) + { + Direction = (target - Position).GetNormalised(); + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Managed/src/Math/Ray.hxx b/SHADE_Managed/src/Math/Ray.hxx index c50191f8..b684aa91 100644 --- a/SHADE_Managed/src/Math/Ray.hxx +++ b/SHADE_Managed/src/Math/Ray.hxx @@ -1,50 +1,61 @@ -/************************************************************************************//*! -\file Ray.hxx -\author Tng Kah Wei, kahwei.tng, 390009620 -\par email: kahwei.tng\@digipen.edu -\date Oct 20, 2021 -\brief Contains the definitions of Vector2 struct. - - Note: This file is written in C++17/CLI. +/**************************************************************************************** + * \file Ray.hxx + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for the managed Ray struct. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ -Copyright (C) 2022 DigiPen Institute of Technology. -Reproduction or disclosure of this file or its contents without the prior written consent -of DigiPen Institute of Technology is prohibited. -*//*************************************************************************************/ #pragma once // Project Includes -#include "Vector3.hxx" +#include "Math/Vector3.hxx" namespace SHADE { - /// - /// CLR version of the the SHADE Engine's Ray class that represents a ray in - /// 3-Dimensional space. - /// - public value struct Ray - { - public: - /*-----------------------------------------------------------------------------*/ - /* Public Members */ - /*-----------------------------------------------------------------------------*/ - /// - /// The start point of the ray. - /// - Vector3 Origin; - /// - /// The direction that a ray travels in. - /// - Vector3 Direction; + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ - /*-----------------------------------------------------------------------------*/ - /* Constructors */ - /*-----------------------------------------------------------------------------*/ - /// - /// Creates a ray starting at origin along direction. - /// - /// Source of the ray. - /// Direction the ray travels in. - Ray(Vector3 origin, Vector3 direction); - }; -} + public value struct Ray + { + public: + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + + /// + /// Constructor for a ray. + /// + /// The starting position of the ray. + /// The direction of the ray. + Ray(Vector3 position, Vector3 direction); + + + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + + /// + /// The starting point of the Ray. + /// + property Vector3 Position; + /// + /// The direction of the ray. This should be a normalised vector. + /// + property Vector3 Direction; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + /// + /// Rotates the ray's direction towards a target. + /// + /// The target to direct the ray towards. + void LookAt(Vector3 target); + }; + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Managed/src/Physics/Physics.cxx b/SHADE_Managed/src/Physics/Physics.cxx new file mode 100644 index 00000000..9e2c1413 --- /dev/null +++ b/SHADE_Managed/src/Physics/Physics.cxx @@ -0,0 +1,105 @@ +/**************************************************************************************** + * \file Physics.cxx + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the managed Physics class. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include "SHpch.h" + +// Primary Header +#include "Physics.hxx" +// External Dependencies +#include "Physics/System/SHPhysicsSystemInterface.h" +// Project Header +#include "Engine/GameObject.hxx" +#include "Utility/Convert.hxx" +#include "Utility/Debug.hxx" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Property Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + Vector3 Physics::Gravity::get() + { + // TODO(Diren) + + return Vector3::Zero; + } + + void Physics::Gravity::set(Vector3 value) + { + (void)value; + } + + /*-----------------------------------------------------------------------------------*/ + /* Raycast Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + RaycastHit Physics::Raycast(Ray ray) + { + return Convert::ToCLI(SHPhysicsSystemInterface::Raycast(Convert::ToNative(ray))); + } + + RaycastHit Physics::Raycast(Ray ray, float distance) + { + return Convert::ToCLI(SHPhysicsSystemInterface::Raycast(Convert::ToNative(ray), distance)); + } + + RaycastHit Physics::Linecast(Vector3 start, Vector3 end) + { + return Convert::ToCLI(SHPhysicsSystemInterface::Linecast(Convert::ToNative(start), Convert::ToNative(end))); + } + + RaycastHit Physics::ColliderRaycast(GameObject object, Ray ray) + { + return Convert::ToCLI(SHPhysicsSystemInterface::ColliderRaycast(object.EntityId, Convert::ToNative(ray))); + } + + RaycastHit Physics::ColliderRaycast(GameObject object, Ray ray, float distance) + { + return Convert::ToCLI(SHPhysicsSystemInterface::ColliderRaycast(object.EntityId, Convert::ToNative(ray), distance)); + } + + RaycastHit Physics::ColliderRaycast(GameObject object, int shapeIndex, Ray ray) + { + return Convert::ToCLI(SHPhysicsSystemInterface::ColliderRaycast(object.EntityId, shapeIndex, Convert::ToNative(ray))); + } + + RaycastHit Physics::ColliderRaycast(GameObject object, int shapeIndex, Ray ray, float distance) + { + return Convert::ToCLI(SHPhysicsSystemInterface::ColliderRaycast(object.EntityId, shapeIndex, Convert::ToNative(ray), distance)); + } + + RaycastHit Physics::ColliderLineCast(GameObject object, Vector3 start, Vector3 end) + { + return Convert::ToCLI(SHPhysicsSystemInterface::ColliderLinecast(object.EntityId, Convert::ToNative(start), Convert::ToNative(end))); + } + + RaycastHit Physics::ColliderLineCast(GameObject object, int shapeIndex, Vector3 start, Vector3 end) + { + return Convert::ToCLI(SHPhysicsSystemInterface::ColliderLinecast(object.EntityId, shapeIndex, Convert::ToNative(start), Convert::ToNative(end))); + } + + /*-----------------------------------------------------------------------------------*/ + /* Private Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + RaycastHit Physics::generateDefaultResult() + { + RaycastHit default; + default.Hit = false; + default.Other = System::Nullable(); + default.Position = Vector3::Zero; + default.Normal = Vector3::Zero; + default.Distance = System::Single::PositiveInfinity; + default.CollisionShapeIndex = -1; + + return default; + } +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Managed/src/Physics/Physics.hxx b/SHADE_Managed/src/Physics/Physics.hxx new file mode 100644 index 00000000..f13e5952 --- /dev/null +++ b/SHADE_Managed/src/Physics/Physics.hxx @@ -0,0 +1,128 @@ +/**************************************************************************************** + * \file Physics.hxx + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for the managed Physics class. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +// Project Includes +#include "Math/Ray.hxx" +#include "RaycastHit.hxx" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + public ref class Physics abstract sealed + { + public: + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + + static property Vector3 Gravity + { + Vector3 get(); + void set(Vector3 value); + } + + // TODO(Diren): Add more properties for physics system settings. + + /*---------------------------------------------------------------------------------*/ + /* Raycast Function Members */ + /*---------------------------------------------------------------------------------*/ + + /// + /// Casts an infinite ray into the world. + /// + /// The ray to cast. + /// The result of the raycast. + static RaycastHit Raycast (Ray ray); + + /// + /// Casts a ray for a given distance into the world. + /// + /// The ray to cast. + /// The distance to cast the ray. + /// The result of the raycast. + static RaycastHit Raycast (Ray ray, float distance); + + /// + /// Casts a bounded ray into the world. + /// + /// The start of the bounded ray. + /// The end of the bounded ray. + /// The result of the raycast. + static RaycastHit Linecast (Vector3 start, Vector3 end); + + /// + /// Casts an infinite ray w.r.t a GameObject. + /// + /// The GameObject to cast the ray to. + /// The ray to cast. + /// The result of the raycast. + static RaycastHit ColliderRaycast (GameObject object, Ray ray); + + /// + /// Casts a ray for a given distance w.r.t a GameObject. + /// + /// The GameObject to cast the ray to. + /// The ray to cast. + /// The distance to cast the ray. + /// The result of the raycast. + static RaycastHit ColliderRaycast (GameObject object, Ray ray, float distance); + + /// + /// Casts an infinite ray w.r.t a specific collider on a GameObject. + /// + /// The GameObject to cast the ray to. + /// The collision shape index on the collider to cast to. + /// The ray to cast. + /// The result of the raycast. + static RaycastHit ColliderRaycast (GameObject object, int shapeIndex, Ray ray); + + /// + /// Casts a ray for a given distance w.r.t a specific collider on a GameObject. + /// + /// The GameObject to cast the ray to. + /// The collision shape index on the collider to cast to. + /// The ray to cast. + /// The distance to cast the ray. + /// The result of the raycast. + static RaycastHit ColliderRaycast (GameObject object, int shapeIndex, Ray ray, float distance); + + /// + /// Casts a bounded ray w.r.t a GameObject. + /// + /// The GameObject to cast the ray to. + /// The start of the bounded ray. + /// + /// The result of the raycast. + static RaycastHit ColliderLineCast (GameObject object, Vector3 start, Vector3 end); + + /// + /// Casts a bounded ray w.r.t a specific collider on a GameObject. + /// + /// The GameObject to cast the ray to. + /// The collision shape index on the collider to cast to. + /// The start of the bounded ray. + /// The end of the bounded ray. + /// The result of the raycast. + static RaycastHit ColliderLineCast (GameObject object, int shapeIndex, Vector3 start, Vector3 end); + + private: + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + static RaycastHit generateDefaultResult (); + }; + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Managed/src/Physics/RaycastHit.cxx b/SHADE_Managed/src/Physics/RaycastHit.cxx new file mode 100644 index 00000000..e7ee9b89 --- /dev/null +++ b/SHADE_Managed/src/Physics/RaycastHit.cxx @@ -0,0 +1,19 @@ +/**************************************************************************************** + * \file RaycastHit.cxx + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the managed RaycastHit struct. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include "SHpch.h" + +// Primary Header +#include "RaycastHit.hxx" + +namespace SHADE +{ + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Managed/src/Physics/RaycastHit.hxx b/SHADE_Managed/src/Physics/RaycastHit.hxx new file mode 100644 index 00000000..260da2a1 --- /dev/null +++ b/SHADE_Managed/src/Physics/RaycastHit.hxx @@ -0,0 +1,64 @@ +/**************************************************************************************** + * \file RaycastHit.hxx + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for the managed RaycastHit struct. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +// Project Includes +#include "Engine/GameObject.hxx" +#include "Math/Vector3.hxx" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + /// + /// Defines a struct that contains the information of a raycast. + /// + public value struct RaycastHit + { + public: + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + + /// + /// Whether or not the raycast hit a collider. + /// + property bool Hit; + + /// + /// The other game object hit. + /// + property System::Nullable Other; + + /// + /// The position where the ray cast hit. Zero if not hit. + /// + property Vector3 Position; + + /// + /// The normal where the ray cast hit. Zero if not hit. + /// + property Vector3 Normal; + + /// + /// The distance the ray was cast. Infinity if not hit. + /// + property float Distance; + + /// + /// The index of the collision shape hit on the collider. -1 if not hit. + /// + property int CollisionShapeIndex; + }; + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Managed/src/Utility/Convert.cxx b/SHADE_Managed/src/Utility/Convert.cxx index 3b1f0f38..38ea2f50 100644 --- a/SHADE_Managed/src/Utility/Convert.cxx +++ b/SHADE_Managed/src/Utility/Convert.cxx @@ -19,6 +19,8 @@ of DigiPen Institute of Technology is prohibited. // External Dependencies #include #include "ECS_Base/Managers/SHEntityManager.h" +// Project Headers +#include "Engine/GameObject.hxx" namespace SHADE { @@ -62,14 +64,14 @@ namespace SHADE return Quaternion{ quat.x, quat.y, quat.z, quat.w }; } - SHRay Convert::ToNative(Ray vec) + SHRay Convert::ToNative(Ray ray) { - return SHRay(ToNative(vec.Origin), ToNative(vec.Direction)); + return SHRay(ToNative(ray.Position), ToNative(ray.Direction)); } - Ray Convert::ToCLI(const SHRay& vec) + Ray Convert::ToCLI(const SHRay& ray) { - return Ray(ToCLI(vec.position), ToCLI(vec.direction)); + return Ray(ToCLI(ray.position), ToCLI(ray.direction)); } SHColour Convert::ToNative(Color col) @@ -95,6 +97,42 @@ namespace SHADE return msclr::interop::marshal_as(str); } + /*---------------------------------------------------------------------------------*/ + /* Physics Conversions */ + /*---------------------------------------------------------------------------------*/ + + SHPhysicsRaycastResult Convert::ToNative(RaycastHit cli) + { + // This function shouldn't be used anyway, so we leave the entityHit empty. + + SHPhysicsRaycastResult native; + + native.hit = cli.Hit; + native.position = ToNative(cli.Position); + native.normal = ToNative(cli.Normal); + native.distance = cli.Distance; + native.shapeIndex = cli.CollisionShapeIndex; + + return native; + } + + RaycastHit Convert::ToCLI(const SHPhysicsRaycastResult& native) + { + RaycastHit cli; + + cli.Hit = native.hit; + cli.Position = ToCLI(native.position); + cli.Normal = ToCLI(native.normal); + cli.Distance = native.distance; + cli.CollisionShapeIndex = native.shapeIndex; + + cli.Other = SHEntityManager::IsValidEID(native.entityHit) + ? GameObject(native.entityHit) + : System::Nullable(); + + return cli; + } + /*---------------------------------------------------------------------------------*/ /* Handle Conversions */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Utility/Convert.hxx b/SHADE_Managed/src/Utility/Convert.hxx index 4d0c5b59..04407f77 100644 --- a/SHADE_Managed/src/Utility/Convert.hxx +++ b/SHADE_Managed/src/Utility/Convert.hxx @@ -28,9 +28,11 @@ of DigiPen Institute of Technology is prohibited. #include "Math/Vector3.hxx" #include "Math/Quaternion.hxx" #include "Math/Ray.hxx" +#include "Physics/RaycastHit.hxx" #include "Engine/GenericHandle.hxx" #include "Math/SHColour.h" #include "Graphics/Color.hxx" +#include "Physics/Collision/SHPhysicsRaycastResult.h" namespace SHADE { @@ -95,22 +97,22 @@ namespace SHADE /// The native Quaternion to convert from. /// Managed copy of a native Quaternion. static Quaternion ToCLI(const SHQuaternion& quat); - /// Converts from a managed Vector2 to a native Vector2. + /// Converts from a managed Ray to a native Ray. /// - /// The managed Vector2 to convert from. - /// Native copy of a managed Vector2. + /// The managed Ray to convert from. + /// Native copy of a managed Ray. static SHRay ToNative(Ray vec); /// - /// Converts from a native Vector2 to a managed Vector2. + /// Converts from a native Ray to a managed Ray. /// - /// The native Vector2 to convert from. - /// Managed copy of a native Vector2. - static Ray ToCLI(const SHRay& vec); + /// The native Ray to convert from. + /// Managed copy of a native Ray. + static Ray ToCLI(const SHRay& ray); /// Converts from a managed Color to a native Colour. /// /// The managed Color to convert from. /// Native copy of a managed Color. - static SHColour ToNative(Color col); + static SHColour ToNative(Color ray); /// /// Converts from a native Colour to a managed Color. /// @@ -134,9 +136,27 @@ namespace SHADE /// Managed copy of a native std::string. static System::String^ ToCLI(const std::string& str); + /*-----------------------------------------------------------------------------*/ + /* Physics Conversions */ + /*-----------------------------------------------------------------------------*/ + + /// + /// Converts from a managed RaycastHit to a native SHPhysicsRaycastResult + /// + /// The managed RaycastHit to convert from. + /// Native copy of a managed RaycastHit. + static SHPhysicsRaycastResult ToNative(RaycastHit cli); + + /// + /// Converts from native SHPhysicsRaycastResult to a managed RaycastHit. + /// + /// The native SHPhysicsRaycastResult to convert from. + /// Managed copy of a native SHPhysicsRaycastResult. + static RaycastHit ToCLI(const SHPhysicsRaycastResult& native); + /*-----------------------------------------------------------------------------*/ /* Handle Conversions */ - /*-----------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------*/ /// /// Converts from a managed GenericHandle to a Handle. ///