Add Material to RayCastHit for surface detection logic
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
#include <ThirdParty/PhysX/foundation/PxBounds3.h>
|
#include <ThirdParty/PhysX/foundation/PxBounds3.h>
|
||||||
#include <ThirdParty/PhysX/characterkinematic/PxExtended.h>
|
#include <ThirdParty/PhysX/characterkinematic/PxExtended.h>
|
||||||
#include <ThirdParty/PhysX/PxShape.h>
|
#include <ThirdParty/PhysX/PxShape.h>
|
||||||
|
#include <ThirdParty/PhysX/PxMaterial.h>
|
||||||
#include <ThirdParty/PhysX/PxQueryReport.h>
|
#include <ThirdParty/PhysX/PxQueryReport.h>
|
||||||
|
|
||||||
namespace physx
|
namespace physx
|
||||||
@@ -233,12 +234,28 @@ inline float RadPerSToRpm(float v)
|
|||||||
return v * (30.0f / PI);
|
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)
|
inline void P2C(const PxRaycastHit& hit, RayCastHit& result)
|
||||||
{
|
{
|
||||||
result.Point = P2C(hit.position);
|
result.Point = P2C(hit.position);
|
||||||
result.Normal = P2C(hit.normal);
|
result.Normal = P2C(hit.normal);
|
||||||
result.Distance = hit.distance;
|
result.Distance = hit.distance;
|
||||||
result.Collider = hit.shape ? static_cast<PhysicsColliderActor*>(hit.shape->userData) : nullptr;
|
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.FaceIndex = hit.faceIndex;
|
||||||
result.UV.X = hit.u;
|
result.UV.X = hit.u;
|
||||||
result.UV.Y = hit.v;
|
result.UV.Y = hit.v;
|
||||||
@@ -250,6 +267,7 @@ inline void P2C(const PxSweepHit& hit, RayCastHit& result)
|
|||||||
result.Normal = P2C(hit.normal);
|
result.Normal = P2C(hit.normal);
|
||||||
result.Distance = hit.distance;
|
result.Distance = hit.distance;
|
||||||
result.Collider = hit.shape ? static_cast<PhysicsColliderActor*>(hit.shape->userData) : nullptr;
|
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.FaceIndex = hit.faceIndex;
|
||||||
result.UV = Vector2::Zero;
|
result.UV = Vector2::Zero;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,17 @@
|
|||||||
|
|
||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
#include "Engine/Core/ISerializable.h"
|
#include "Engine/Core/ISerializable.h"
|
||||||
|
#include "Engine/Scripting/ScriptingObject.h"
|
||||||
#include "Engine/Level/Tags.h"
|
#include "Engine/Level/Tags.h"
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Physical materials are used to define the response of a physical object when interacting dynamically with the world.
|
/// Physical materials are used to define the response of a physical object when interacting dynamically with the world.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
API_CLASS(Attributes = "ContentContextMenu(\"New/Physics/Physical Material\")")
|
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();
|
API_AUTO_SERIALIZATION();
|
||||||
DECLARE_SCRIPTING_TYPE_MINIMAL(PhysicalMaterial);
|
DECLARE_SCRIPTING_TYPE_WITH_CONSTRUCTOR_IMPL(PhysicalMaterial, ScriptingObject);
|
||||||
private:
|
private:
|
||||||
void* _material = nullptr;
|
void* _material = nullptr;
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
struct PhysicsStatistics;
|
struct PhysicsStatistics;
|
||||||
class PhysicsColliderActor;
|
class PhysicsColliderActor;
|
||||||
class PhysicsScene;
|
class PhysicsScene;
|
||||||
|
class PhysicalMaterial;
|
||||||
class Joint;
|
class Joint;
|
||||||
class Collider;
|
class Collider;
|
||||||
class CollisionData;
|
class CollisionData;
|
||||||
@@ -132,7 +133,7 @@ DECLARE_ENUM_OPERATORS(RigidbodyConstraints);
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Raycast hit result data.
|
/// Raycast hit result data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
API_STRUCT() struct RayCastHit
|
API_STRUCT(NoDefault) struct RayCastHit
|
||||||
{
|
{
|
||||||
DECLARE_SCRIPTING_TYPE_NO_SPAWN(RayCastHit);
|
DECLARE_SCRIPTING_TYPE_NO_SPAWN(RayCastHit);
|
||||||
|
|
||||||
@@ -141,6 +142,11 @@ API_STRUCT() struct RayCastHit
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
API_FIELD() PhysicsColliderActor* Collider = nullptr;
|
API_FIELD() PhysicsColliderActor* Collider = nullptr;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The physical material of the surface that was hit.
|
||||||
|
/// </summary>
|
||||||
|
API_FIELD() PhysicalMaterial* Material = nullptr;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The normal of the surface the ray hit.
|
/// The normal of the surface the ray hit.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user