Added C# Interface for Raycasting

Untested with Scripts, tested engine side.
This commit is contained in:
Diren D Bharwani 2022-11-19 04:21:46 +08:00
parent a4e5a1c269
commit 411c4a4e56
14 changed files with 609 additions and 103 deletions

View File

@ -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;
};
}

View File

@ -15,10 +15,11 @@
#include <reactphysics3d/reactphysics3d.h> #include <reactphysics3d/reactphysics3d.h>
// Project Headers // Project Headers
#include "Math/SHRay.h"
#include "Physics/PhysicsObject/SHPhysicsObjectManager.h" #include "Physics/PhysicsObject/SHPhysicsObjectManager.h"
#include "Physics/SHPhysicsWorld.h" #include "Physics/SHPhysicsWorld.h"
#include "Math/SHRay.h"
#include "SH_API.h" #include "SH_API.h"
#include "SHPhysicsRaycastResult.h"
namespace SHADE namespace SHADE
{ {
@ -26,18 +27,6 @@ namespace SHADE
/* Type Definitions */ /* 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 class SH_API SHPhysicsRaycaster : public reactphysics3d::RaycastCallback
{ {
private: private:

View File

@ -185,7 +185,7 @@ namespace SHADE
return raycaster.Raycast(ray, distance); 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); return raycaster.Linecast(start, end);
} }
@ -195,7 +195,7 @@ namespace SHADE
return raycaster.ColliderRaycast(eid, ray, distance); 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); return raycaster.ColliderRaycast(eid, shapeIndex, ray, distance);
} }
@ -205,7 +205,7 @@ namespace SHADE
return raycaster.ColliderLinecast(eid, start, end); 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); return raycaster.ColliderLinecast(eid, shapeIndex, start, end);
} }

View File

@ -101,7 +101,7 @@ namespace SHADE
( (
const SHVec3& start const SHVec3& start
, const SHVec3& end , const SHVec3& end
); ) noexcept;
/** /**
* @brief Casts a ray at a body with colliders. * @brief Casts a ray at a body with colliders.
@ -131,7 +131,7 @@ namespace SHADE
, int shapeIndex , int shapeIndex
, const SHRay& ray , const SHRay& ray
, float distance = std::numeric_limits<float>::infinity() , float distance = std::numeric_limits<float>::infinity()
); ) noexcept;
/** /**
* @brief Casts a bounded ray at a body with colliders. * @brief Casts a bounded ray at a body with colliders.
@ -161,7 +161,7 @@ namespace SHADE
, int shapeIndex , int shapeIndex
, const SHVec3& start , const SHVec3& start
, const SHVec3& end , const SHVec3& end
); ) noexcept;
// Specific Handling for Collision Shapes as they are not under the Component System. // Specific Handling for Collision Shapes as they are not under the Component System.
// This is done as events need to be sent out. // This is done as events need to be sent out.

View File

@ -33,7 +33,7 @@ namespace SHADE
return phySystem->GetAllCollisionInfo(); 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; return emptyVec;
} }
const std::vector<SHCollisionInfo>& SHPhysicsSystemInterface::GetTriggerInfo() noexcept const std::vector<SHCollisionInfo>& SHPhysicsSystemInterface::GetTriggerInfo() noexcept
@ -46,7 +46,7 @@ namespace SHADE
return phySystem->GetAllTriggerInfo(); 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; return emptyVec;
} }
@ -58,7 +58,79 @@ namespace SHADE
return phySystem->GetFixedUpdateRate(); 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; return 0.0;
} }
SHPhysicsRaycastResult SHPhysicsSystemInterface::Raycast(const SHRay& ray, float distance) noexcept
{
auto* physicsSystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
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<SHPhysicsSystem>();
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<SHPhysicsSystem>();
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<SHPhysicsSystem>();
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<SHPhysicsSystem>();
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<SHPhysicsSystem>();
if (physicsSystem)
{
return physicsSystem->ColliderLinecast(eid, shapeIndex, start, end);
}
SHLOGV_WARNING("Failed to get the physics system. No ray was casted.");
return SHPhysicsRaycastResult{};
}
} }

View File

@ -14,12 +14,21 @@ of DigiPen Institute of Technology is prohibited.
// STL Includes // STL Includes
#include <vector> #include <vector>
// Project Headers
#include "ECS_Base/Entity/SHEntity.h"
namespace SHADE namespace SHADE
{ {
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Forward Declarations */ /* Forward Declarations */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
class SHCollisionInfo; class SHCollisionInfo;
class SHVec3;
struct SHRay;
struct SHPhysicsRaycastResult;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Type Definitions */ /* Type Definitions */
@ -39,8 +48,16 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Static Usage Functions */ /* Static Usage Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] static const std::vector<SHCollisionInfo>& GetCollisionInfo() noexcept; [[nodiscard]] static const std::vector<SHCollisionInfo>& GetCollisionInfo() noexcept;
[[nodiscard]] static const std::vector<SHCollisionInfo>& GetTriggerInfo () noexcept; [[nodiscard]] static const std::vector<SHCollisionInfo>& GetTriggerInfo () noexcept;
[[nodiscard]] static double GetFixedDT () noexcept; [[nodiscard]] static double GetFixedDT () noexcept;
[[nodiscard]] static SHPhysicsRaycastResult Raycast (const SHRay& ray, float distance = std::numeric_limits<float>::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<float>::infinity()) noexcept;
[[nodiscard]] static SHPhysicsRaycastResult ColliderRaycast (EntityID eid, int shapeIndex, const SHRay& ray, float distance = std::numeric_limits<float>::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;
}; };
} }

View File

@ -1,28 +1,37 @@
/************************************************************************************//*! /****************************************************************************************
\file Ray.cxx * \file Ray.cxx
\author Tng Kah Wei, kahwei.tng, 390009620 * \author Diren D Bharwani, diren.dbharwani, 390002520
\par email: kahwei.tng\@digipen.edu * \brief Implementation for the managed Ray struct.
\date Oct 20, 2022 *
\brief Contains the definitions of functions of the Vector2 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.
****************************************************************************************/
Note: This file is written in C++17/CLI.
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" #include "SHpch.h"
// Primary Header // Primary Header
#include "Math/Ray.hxx" #include "Ray.hxx"
namespace SHADE namespace SHADE
{ {
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Constructors */ /* Constructor Definitions */
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
Ray::Ray(Vector3 origin, Vector3 direction)
: Origin { origin } Ray::Ray(Vector3 position, Vector3 direction)
, Direction{ direction } {
{} Position = position;
Direction = direction;
} }
/*-----------------------------------------------------------------------------------*/
/* Function Member Definitions */
/*-----------------------------------------------------------------------------------*/
void Ray::LookAt(Vector3 target)
{
Direction = (target - Position).GetNormalised();
}
} // namespace SHADE

View File

@ -1,50 +1,61 @@
/************************************************************************************//*! /****************************************************************************************
\file Ray.hxx * \file Ray.hxx
\author Tng Kah Wei, kahwei.tng, 390009620 * \author Diren D Bharwani, diren.dbharwani, 390002520
\par email: kahwei.tng\@digipen.edu * \brief Interface for the managed Ray struct.
\date Oct 20, 2021 *
\brief Contains the definitions of Vector2 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.
****************************************************************************************/
Note: This file is written in C++17/CLI.
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 #pragma once
// Project Includes // Project Includes
#include "Vector3.hxx" #include "Math/Vector3.hxx"
namespace SHADE namespace SHADE
{ {
///<summary> /*-----------------------------------------------------------------------------------*/
/// CLR version of the the SHADE Engine's Ray class that represents a ray in /* Type Definitions */
/// 3-Dimensional space. /*-----------------------------------------------------------------------------------*/
/// </summary>
public value struct Ray public value struct Ray
{ {
public: public:
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Public Members */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// The start point of the ray.
/// </summary>
Vector3 Origin;
/// <summary>
/// The direction that a ray travels in.
/// </summary>
Vector3 Direction;
/*-----------------------------------------------------------------------------*/
/* Constructors */ /* Constructors */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/// <summary> /// <summary>
/// Creates a ray starting at origin along direction. /// Constructor for a ray.
/// </summary> /// </summary>
/// <param name="origin">Source of the ray.</param> /// <param name="position">The starting position of the ray.</param>
/// <param name="direction">Direction the ray travels in.</param> /// <param name="direction">The direction of the ray.</param>
Ray(Vector3 origin, Vector3 direction); Ray(Vector3 position, Vector3 direction);
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// The starting point of the Ray.
/// </summary>
property Vector3 Position;
/// <summary>
/// The direction of the ray. This should be a normalised vector.
/// </summary>
property Vector3 Direction;
/*---------------------------------------------------------------------------------*/
/* Function Members */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Rotates the ray's direction towards a target.
/// </summary>
/// <param name="target">The target to direct the ray towards.</param>
void LookAt(Vector3 target);
}; };
}
} // namespace SHADE

View File

@ -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<GameObject>();
default.Position = Vector3::Zero;
default.Normal = Vector3::Zero;
default.Distance = System::Single::PositiveInfinity;
default.CollisionShapeIndex = -1;
return default;
}
} // namespace SHADE

View File

@ -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 */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Casts an infinite ray into the world.
/// </summary>
/// <param name="ray">The ray to cast.</param>
/// <returns>The result of the raycast.</returns>
static RaycastHit Raycast (Ray ray);
/// <summary>
/// Casts a ray for a given distance into the world.
/// </summary>
/// <param name="ray">The ray to cast.</param>
/// <param name="distance">The distance to cast the ray.</param>
/// <returns>The result of the raycast.</returns>
static RaycastHit Raycast (Ray ray, float distance);
/// <summary>
/// Casts a bounded ray into the world.
/// </summary>
/// <param name="start">The start of the bounded ray.</param>
/// <param name="end">The end of the bounded ray.</param>
/// <returns>The result of the raycast.</returns>
static RaycastHit Linecast (Vector3 start, Vector3 end);
/// <summary>
/// Casts an infinite ray w.r.t a GameObject.
/// </summary>
/// <param name="object">The GameObject to cast the ray to.</param>
/// <param name="ray">The ray to cast.</param>
/// <returns>The result of the raycast.</returns>
static RaycastHit ColliderRaycast (GameObject object, Ray ray);
/// <summary>
/// Casts a ray for a given distance w.r.t a GameObject.
/// </summary>
/// <param name="object">The GameObject to cast the ray to.</param>
/// <param name="ray">The ray to cast.</param>
/// <param name="distance">The distance to cast the ray.</param>
/// <returns>The result of the raycast.</returns>
static RaycastHit ColliderRaycast (GameObject object, Ray ray, float distance);
/// <summary>
/// Casts an infinite ray w.r.t a specific collider on a GameObject.
/// </summary>
/// <param name="object">The GameObject to cast the ray to.</param>
/// <param name="shapeIndex">The collision shape index on the collider to cast to.</param>
/// <param name="ray">The ray to cast.</param>
/// <returns>The result of the raycast.</returns>
static RaycastHit ColliderRaycast (GameObject object, int shapeIndex, Ray ray);
/// <summary>
/// Casts a ray for a given distance w.r.t a specific collider on a GameObject.
/// </summary>
/// <param name="object">The GameObject to cast the ray to.</param>
/// <param name="shapeIndex">The collision shape index on the collider to cast to.</param>
/// <param name="ray">The ray to cast.</param>
/// <param name="distance">The distance to cast the ray.</param>
/// <returns>The result of the raycast.</returns>
static RaycastHit ColliderRaycast (GameObject object, int shapeIndex, Ray ray, float distance);
/// <summary>
/// Casts a bounded ray w.r.t a GameObject.
/// </summary>
/// <param name="object">The GameObject to cast the ray to.</param>
/// <param name="start">The start of the bounded ray.</param>
/// <param name="end"></param>
/// <returns>The result of the raycast.</returns>
static RaycastHit ColliderLineCast (GameObject object, Vector3 start, Vector3 end);
/// <summary>
/// Casts a bounded ray w.r.t a specific collider on a GameObject.
/// </summary>
/// <param name="object">The GameObject to cast the ray to.</param>
/// <param name="shapeIndex">The collision shape index on the collider to cast to.</param>
/// <param name="start">The start of the bounded ray.</param>
/// <param name="end">The end of the bounded ray.</param>
/// <returns>The result of the raycast.</returns>
static RaycastHit ColliderLineCast (GameObject object, int shapeIndex, Vector3 start, Vector3 end);
private:
/*---------------------------------------------------------------------------------*/
/* Function Members */
/*---------------------------------------------------------------------------------*/
static RaycastHit generateDefaultResult ();
};
} // namespace SHADE

View File

@ -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

View File

@ -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 */
/*-----------------------------------------------------------------------------------*/
/// <summary>
/// Defines a struct that contains the information of a raycast.
/// </summary>
public value struct RaycastHit
{
public:
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Whether or not the raycast hit a collider.
/// </summary>
property bool Hit;
/// <summary>
/// The other game object hit.
/// </summary>
property System::Nullable<GameObject> Other;
/// <summary>
/// The position where the ray cast hit. Zero if not hit.
/// </summary>
property Vector3 Position;
/// <summary>
/// The normal where the ray cast hit. Zero if not hit.
/// </summary>
property Vector3 Normal;
/// <summary>
/// The distance the ray was cast. Infinity if not hit.
/// </summary>
property float Distance;
/// <summary>
/// The index of the collision shape hit on the collider. -1 if not hit.
/// </summary>
property int CollisionShapeIndex;
};
} // namespace SHADE

View File

@ -19,6 +19,8 @@ of DigiPen Institute of Technology is prohibited.
// External Dependencies // External Dependencies
#include <msclr/marshal_cppstd.h> #include <msclr/marshal_cppstd.h>
#include "ECS_Base/Managers/SHEntityManager.h" #include "ECS_Base/Managers/SHEntityManager.h"
// Project Headers
#include "Engine/GameObject.hxx"
namespace SHADE namespace SHADE
{ {
@ -62,14 +64,14 @@ namespace SHADE
return Quaternion{ quat.x, quat.y, quat.z, quat.w }; 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) SHColour Convert::ToNative(Color col)
@ -95,6 +97,42 @@ namespace SHADE
return msclr::interop::marshal_as<System::String^>(str); return msclr::interop::marshal_as<System::String^>(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<GameObject>();
return cli;
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Handle Conversions */ /* Handle Conversions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -28,9 +28,11 @@ of DigiPen Institute of Technology is prohibited.
#include "Math/Vector3.hxx" #include "Math/Vector3.hxx"
#include "Math/Quaternion.hxx" #include "Math/Quaternion.hxx"
#include "Math/Ray.hxx" #include "Math/Ray.hxx"
#include "Physics/RaycastHit.hxx"
#include "Engine/GenericHandle.hxx" #include "Engine/GenericHandle.hxx"
#include "Math/SHColour.h" #include "Math/SHColour.h"
#include "Graphics/Color.hxx" #include "Graphics/Color.hxx"
#include "Physics/Collision/SHPhysicsRaycastResult.h"
namespace SHADE namespace SHADE
{ {
@ -95,22 +97,22 @@ namespace SHADE
/// <param name="quat">The native Quaternion to convert from.</param> /// <param name="quat">The native Quaternion to convert from.</param>
/// <returns>Managed copy of a native Quaternion.</returns> /// <returns>Managed copy of a native Quaternion.</returns>
static Quaternion ToCLI(const SHQuaternion& quat); static Quaternion ToCLI(const SHQuaternion& quat);
/// Converts from a managed Vector2 to a native Vector2. /// Converts from a managed Ray to a native Ray.
/// </summary> /// </summary>
/// <param name="vec">The managed Vector2 to convert from.</param> /// <param name="ray">The managed Ray to convert from.</param>
/// <returns>Native copy of a managed Vector2.</returns> /// <returns>Native copy of a managed Ray.</returns>
static SHRay ToNative(Ray vec); static SHRay ToNative(Ray vec);
/// <summary> /// <summary>
/// Converts from a native Vector2 to a managed Vector2. /// Converts from a native Ray to a managed Ray.
/// </summary> /// </summary>
/// <param name="vec">The native Vector2 to convert from.</param> /// <param name="ray">The native Ray to convert from.</param>
/// <returns>Managed copy of a native Vector2.</returns> /// <returns>Managed copy of a native Ray.</returns>
static Ray ToCLI(const SHRay& vec); static Ray ToCLI(const SHRay& ray);
/// Converts from a managed Color to a native Colour. /// Converts from a managed Color to a native Colour.
/// </summary> /// </summary>
/// <param name="vec">The managed Color to convert from.</param> /// <param name="vec">The managed Color to convert from.</param>
/// <returns>Native copy of a managed Color.</returns> /// <returns>Native copy of a managed Color.</returns>
static SHColour ToNative(Color col); static SHColour ToNative(Color ray);
/// <summary> /// <summary>
/// Converts from a native Colour to a managed Color. /// Converts from a native Colour to a managed Color.
/// </summary> /// </summary>
@ -134,6 +136,24 @@ namespace SHADE
/// <returns>Managed copy of a native std::string.</returns> /// <returns>Managed copy of a native std::string.</returns>
static System::String^ ToCLI(const std::string& str); static System::String^ ToCLI(const std::string& str);
/*-----------------------------------------------------------------------------*/
/* Physics Conversions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Converts from a managed RaycastHit to a native SHPhysicsRaycastResult
/// </summary>
/// <param name="raycastHit">The managed RaycastHit to convert from.</param>
/// <returns>Native copy of a managed RaycastHit.</returns>
static SHPhysicsRaycastResult ToNative(RaycastHit cli);
/// <summary>
/// Converts from native SHPhysicsRaycastResult to a managed RaycastHit.
/// </summary>
/// <param name="raycastResult">The native SHPhysicsRaycastResult to convert from.</param>
/// <returns>Managed copy of a native SHPhysicsRaycastResult.</returns>
static RaycastHit ToCLI(const SHPhysicsRaycastResult& native);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Handle Conversions */ /* Handle Conversions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/