Browse Source

Implement ShapeFilter and Shape UserData functions

Lucien Greathouse 9 months ago
parent
commit
ed025e6a44
2 changed files with 100 additions and 29 deletions
  1. 45 24
      JoltC/Functions.h
  2. 55 5
      JoltC/JoltC.cpp

+ 45 - 24
JoltC/Functions.h

@@ -195,6 +195,33 @@ typedef struct JPC_IndexedTriangleList JPC_IndexedTriangleList;
 JPC_API JPC_IndexedTriangleList* JPC_IndexedTriangleList_new(const JPC_IndexedTriangle* storage, size_t len);
 JPC_API void JPC_IndexedTriangleList_delete(JPC_IndexedTriangleList* object);
 
+////////////////////////////////////////////////////////////////////////////////
+// Shape -> RefTarget
+
+typedef struct JPC_Shape JPC_Shape;
+
+JPC_API uint32_t JPC_Shape_GetRefCount(const JPC_Shape* self);
+JPC_API void JPC_Shape_AddRef(const JPC_Shape* self);
+JPC_API void JPC_Shape_Release(const JPC_Shape* self);
+
+JPC_API uint64_t JPC_Shape_GetUserData(const JPC_Shape* self);
+JPC_API void JPC_Shape_SetUserData(JPC_Shape* self, uint64_t userData);
+
+JPC_API JPC_ShapeType JPC_Shape_GetType(const JPC_Shape* self);
+JPC_API JPC_ShapeSubType JPC_Shape_GetSubType(const JPC_Shape* self);
+
+JPC_API JPC_Vec3 JPC_Shape_GetCenterOfMass(const JPC_Shape* self);
+
+////////////////////////////////////////////////////////////////////////////////
+// CompoundShape -> Shape -> RefTarget
+
+typedef struct JPC_CompoundShape JPC_CompoundShape;
+
+JPC_API uint32_t JPC_CompoundShape_GetSubShapeIndexFromID(
+	const JPC_CompoundShape* self,
+	JPC_SubShapeID inSubShapeID,
+	JPC_SubShapeID* outRemainder);
+
 ////////////////////////////////////////////////////////////////////////////////
 // TempAllocatorImpl
 
@@ -280,6 +307,23 @@ JPC_API JPC_BodyFilter* JPC_BodyFilter_new(
 
 JPC_API void JPC_BodyFilter_delete(JPC_BodyFilter* object);
 
+////////////////////////////////////////////////////////////////////////////////
+// ShapeFilter
+
+typedef struct JPC_ShapeFilterFns {
+	bool (*ShouldCollide)(const void *self, const JPC_Shape *inShape2, JPC_SubShapeID inSubShapeIDOfShape2);
+
+	// virtual bool			ShouldCollide([[maybe_unused]] const Shape *inShape1, [[maybe_unused]] const SubShapeID &inSubShapeIDOfShape1, [[maybe_unused]] const Shape *inShape2, [[maybe_unused]] const SubShapeID &inSubShapeIDOfShape2) const
+} JPC_ShapeFilterFns;
+
+typedef struct JPC_ShapeFilter JPC_ShapeFilter;
+
+JPC_API JPC_ShapeFilter* JPC_ShapeFilter_new(
+	const void *self,
+	JPC_ShapeFilterFns fns);
+
+JPC_API void JPC_ShapeFilter_delete(JPC_ShapeFilter* object);
+
 ////////////////////////////////////////////////////////////////////////////////
 // ObjectVsBroadPhaseLayerFilter
 
@@ -493,30 +537,6 @@ typedef struct JPC_String JPC_String;
 JPC_API void JPC_String_delete(JPC_String* self);
 JPC_API const char* JPC_String_c_str(JPC_String* self);
 
-////////////////////////////////////////////////////////////////////////////////
-// Shape -> RefTarget
-
-typedef struct JPC_Shape JPC_Shape;
-
-JPC_API uint32_t JPC_Shape_GetRefCount(const JPC_Shape* self);
-JPC_API void JPC_Shape_AddRef(const JPC_Shape* self);
-JPC_API void JPC_Shape_Release(const JPC_Shape* self);
-
-JPC_API JPC_ShapeType JPC_Shape_GetType(const JPC_Shape* self);
-JPC_API JPC_ShapeSubType JPC_Shape_GetSubType(const JPC_Shape* self);
-
-JPC_API JPC_Vec3 JPC_Shape_GetCenterOfMass(const JPC_Shape* self);
-
-////////////////////////////////////////////////////////////////////////////////
-// CompoundShape -> Shape -> RefTarget
-
-typedef struct JPC_CompoundShape JPC_CompoundShape;
-
-JPC_API uint32_t JPC_CompoundShape_GetSubShapeIndexFromID(
-	const JPC_CompoundShape* self,
-	JPC_SubShapeID inSubShapeID,
-	JPC_SubShapeID* outRemainder);
-
 ////////////////////////////////////////////////////////////////////////////////
 // TriangleShapeSettings
 
@@ -930,6 +950,7 @@ typedef struct JPC_NarrowPhaseQuery_CastRayArgs {
 	const JPC_BroadPhaseLayerFilter *BroadPhaseLayerFilter;
 	const JPC_ObjectLayerFilter *ObjectLayerFilter;
 	const JPC_BodyFilter *BodyFilter;
+	const JPC_ShapeFilter *ShapeFilter;
 } JPC_NarrowPhaseQuery_CastRayArgs;
 
 JPC_API bool JPC_NarrowPhaseQuery_CastRay(const JPC_NarrowPhaseQuery* self, JPC_NarrowPhaseQuery_CastRayArgs* args);

+ 55 - 5
JoltC/JoltC.cpp

@@ -471,6 +471,36 @@ JPC_API JPC_BodyFilter* JPC_BodyFilter_new(
 	return to_jpc(new JPC_BodyFilterBridge(self, fns));
 }
 
+////////////////////////////////////////////////////////////////////////////////
+// ShapeFilter
+
+class JPC_ShapeFilterBridge final : public JPH::ShapeFilter {
+public:
+	explicit JPC_ShapeFilterBridge(const void *self, JPC_ShapeFilterFns fns) : self(self), fns(fns) {}
+
+	virtual bool ShouldCollide(const JPH::Shape *inShape2, const JPH::SubShapeID &inSubShapeIDOfShape2) const override {
+		if (fns.ShouldCollide == nullptr) {
+			return true;
+		}
+
+		return fns.ShouldCollide(self, to_jpc(inShape2), to_jpc(inSubShapeIDOfShape2));
+	}
+
+private:
+	const void* self;
+	JPC_ShapeFilterFns fns;
+};
+
+OPAQUE_WRAPPER(JPC_ShapeFilter, JPC_ShapeFilterBridge)
+DESTRUCTOR(JPC_ShapeFilter)
+
+JPC_API JPC_ShapeFilter* JPC_ShapeFilter_new(
+	const void *self,
+	JPC_ShapeFilterFns fns)
+{
+	return to_jpc(new JPC_ShapeFilterBridge(self, fns));
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // JPC_ObjectLayerPairFilter
 
@@ -677,6 +707,14 @@ JPC_API void JPC_Shape_Release(const JPC_Shape* self) {
 	to_jph(self)->Release();
 }
 
+JPC_API uint64_t JPC_Shape_GetUserData(const JPC_Shape* self) {
+	return to_jph(self)->GetUserData();
+}
+
+JPC_API void JPC_Shape_SetUserData(JPC_Shape* self, uint64_t userData) {
+	to_jph(self)->SetUserData(userData);
+}
+
 JPC_API JPC_ShapeType JPC_Shape_GetType(const JPC_Shape* self) {
 	return to_jpc(to_jph(self)->GetType());
 }
@@ -1673,6 +1711,8 @@ JPC_API void JPC_BodyInterface_InvalidateContactCache(JPC_BodyInterface *self, J
 JPC_API bool JPC_NarrowPhaseQuery_CastRay(const JPC_NarrowPhaseQuery* self, JPC_NarrowPhaseQuery_CastRayArgs* args) {
 	JPH::RayCastResult result;
 
+	JPH::RayCastSettings settings;
+
 	JPH::BroadPhaseLayerFilter defaultBplFilter{};
 	const JPH::BroadPhaseLayerFilter* bplFilter = &defaultBplFilter;
 	if (args->BroadPhaseLayerFilter != nullptr) {
@@ -1691,16 +1731,26 @@ JPC_API bool JPC_NarrowPhaseQuery_CastRay(const JPC_NarrowPhaseQuery* self, JPC_
 		bodyFilter = to_jph(args->BodyFilter);
 	}
 
-	bool hit = to_jph(self)->CastRay(
+	JPH::ShapeFilter defaultShapeFilter{};
+	const JPH::ShapeFilter* shapeFilter = &defaultShapeFilter;
+	if (args->ShapeFilter != nullptr) {
+		shapeFilter = to_jph(args->ShapeFilter);
+	}
+
+	JPH::ClosestHitCollisionCollector<JPH::CastRayCollector> collector;
+
+	to_jph(self)->CastRay(
 		to_jph(args->Ray),
-		result,
+		settings,
+		collector,
 		*bplFilter,
 		*olFilter,
-		*bodyFilter
-	);
+		*bodyFilter,
+		*shapeFilter);
 
+	bool hit = collector.HadHit();
 	if (hit) {
-		args->Result = to_jpc(result);
+		args->Result = to_jpc(collector.mHit);
 	}
 
 	return hit;