Add Material to RayCastHit for surface detection logic

This commit is contained in:
Wojtek Figat
2024-02-16 17:39:35 +01:00
parent 85f2910718
commit c140cc4e7c
3 changed files with 28 additions and 3 deletions

View File

@@ -17,6 +17,7 @@
#include <ThirdParty/PhysX/foundation/PxBounds3.h>
#include <ThirdParty/PhysX/characterkinematic/PxExtended.h>
#include <ThirdParty/PhysX/PxShape.h>
#include <ThirdParty/PhysX/PxMaterial.h>
#include <ThirdParty/PhysX/PxQueryReport.h>
namespace physx
@@ -233,12 +234,28 @@ inline float RadPerSToRpm(float v)
return v * (30.0f / PI);
}
inline PhysicalMaterial* GetMaterial(const PxShape* shape, PxU32 faceIndex)
{
if (faceIndex != 0xFFFFffff)
{
PxBaseMaterial* mat = shape->getMaterialFromInternalFaceIndex(faceIndex);
return mat ? (PhysicalMaterial*)mat->userData : nullptr;
}
else
{
PxMaterial* mat;
shape->getMaterials(&mat, 1);
return mat ? (PhysicalMaterial*)mat->userData : nullptr;
}
}
inline void P2C(const PxRaycastHit& hit, RayCastHit& result)
{
result.Point = P2C(hit.position);
result.Normal = P2C(hit.normal);
result.Distance = hit.distance;
result.Collider = hit.shape ? static_cast<PhysicsColliderActor*>(hit.shape->userData) : nullptr;
result.Material = hit.shape ? GetMaterial(hit.shape, hit.faceIndex) : nullptr;
result.FaceIndex = hit.faceIndex;
result.UV.X = hit.u;
result.UV.Y = hit.v;
@@ -250,6 +267,7 @@ inline void P2C(const PxSweepHit& hit, RayCastHit& result)
result.Normal = P2C(hit.normal);
result.Distance = hit.distance;
result.Collider = hit.shape ? static_cast<PhysicsColliderActor*>(hit.shape->userData) : nullptr;
result.Material = hit.shape ? GetMaterial(hit.shape, hit.faceIndex) : nullptr;
result.FaceIndex = hit.faceIndex;
result.UV = Vector2::Zero;
}

View File

@@ -4,16 +4,17 @@
#include "Types.h"
#include "Engine/Core/ISerializable.h"
#include "Engine/Scripting/ScriptingObject.h"
#include "Engine/Level/Tags.h"
/// <summary>
/// Physical materials are used to define the response of a physical object when interacting dynamically with the world.
/// </summary>
API_CLASS(Attributes = "ContentContextMenu(\"New/Physics/Physical Material\")")
class FLAXENGINE_API PhysicalMaterial final : public ISerializable
class FLAXENGINE_API PhysicalMaterial final : public ScriptingObject, public ISerializable
{
API_AUTO_SERIALIZATION();
DECLARE_SCRIPTING_TYPE_MINIMAL(PhysicalMaterial);
DECLARE_SCRIPTING_TYPE_WITH_CONSTRUCTOR_IMPL(PhysicalMaterial, ScriptingObject);
private:
void* _material = nullptr;

View File

@@ -10,6 +10,7 @@
struct PhysicsStatistics;
class PhysicsColliderActor;
class PhysicsScene;
class PhysicalMaterial;
class Joint;
class Collider;
class CollisionData;
@@ -132,7 +133,7 @@ DECLARE_ENUM_OPERATORS(RigidbodyConstraints);
/// <summary>
/// Raycast hit result data.
/// </summary>
API_STRUCT() struct RayCastHit
API_STRUCT(NoDefault) struct RayCastHit
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(RayCastHit);
@@ -141,6 +142,11 @@ API_STRUCT() struct RayCastHit
/// </summary>
API_FIELD() PhysicsColliderActor* Collider = nullptr;
/// <summary>
/// The physical material of the surface that was hit.
/// </summary>
API_FIELD() PhysicalMaterial* Material = nullptr;
/// <summary>
/// The normal of the surface the ray hit.
/// </summary>