Skip to content

Commit

Permalink
Filter types for NarrowPhaseQuery::CastRay
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed May 13, 2024
1 parent 6568c60 commit bc6d651
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 9 deletions.
56 changes: 51 additions & 5 deletions JoltC/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ typedef struct JPC_RayCastResult {
JPC_SubShapeID SubShapeID2;
} JPC_RayCastResult;

typedef struct JPC_Body JPC_Body;

////////////////////////////////////////////////////////////////////////////////
// VertexList == Array<Float3> == std::vector<Float3>

Expand Down Expand Up @@ -214,6 +216,52 @@ JPC_API JPC_BroadPhaseLayerInterface* JPC_BroadPhaseLayerInterface_new(

JPC_API void JPC_BroadPhaseLayerInterface_delete(JPC_BroadPhaseLayerInterface* object);

////////////////////////////////////////////////////////////////////////////////
// BroadPhaseLayerFilter

typedef struct JPC_BroadPhaseLayerFilterFns {
bool (*ShouldCollide)(const void *self, JPC_BroadPhaseLayer inLayer);
} JPC_BroadPhaseLayerFilterFns;

typedef struct JPC_BroadPhaseLayerFilter JPC_BroadPhaseLayerFilter;

JPC_API JPC_BroadPhaseLayerFilter* JPC_BroadPhaseLayerFilter_new(
const void *self,
JPC_BroadPhaseLayerFilterFns fns);

JPC_API void JPC_BroadPhaseLayerFilter_delete(JPC_BroadPhaseLayerFilter* object);

////////////////////////////////////////////////////////////////////////////////
// ObjectLayerFilter

typedef struct JPC_ObjectLayerFilterFns {
bool (*ShouldCollide)(const void *self, JPC_ObjectLayer inLayer);
} JPC_ObjectLayerFilterFns;

typedef struct JPC_ObjectLayerFilter JPC_ObjectLayerFilter;

JPC_API JPC_ObjectLayerFilter* JPC_ObjectLayerFilter_new(
const void *self,
JPC_ObjectLayerFilterFns fns);

JPC_API void JPC_ObjectLayerFilter_delete(JPC_ObjectLayerFilter* object);

////////////////////////////////////////////////////////////////////////////////
// BodyFilter

typedef struct JPC_BodyFilterFns {
bool (*ShouldCollide)(const void *self, JPC_BodyID inBodyID);
bool (*ShouldCollideLocked)(const void *self, const JPC_Body *inBodyID);
} JPC_BodyFilterFns;

typedef struct JPC_BodyFilter JPC_BodyFilter;

JPC_API JPC_BodyFilter* JPC_BodyFilter_new(
const void *self,
JPC_BodyFilterFns fns);

JPC_API void JPC_BodyFilter_delete(JPC_BodyFilter* object);

////////////////////////////////////////////////////////////////////////////////
// ObjectVsBroadPhaseLayerFilter

Expand Down Expand Up @@ -522,8 +570,6 @@ JPC_API JPC_BodyCreationSettings* JPC_BodyCreationSettings_new();
////////////////////////////////////////////////////////////////////////////////
// Body

typedef struct JPC_Body JPC_Body;

JPC_API JPC_BodyID JPC_Body_GetID(const JPC_Body* self);
JPC_API JPC_BodyType JPC_Body_GetBodyType(const JPC_Body* self);
JPC_API bool JPC_Body_IsRigidBody(const JPC_Body* self);
Expand Down Expand Up @@ -725,9 +771,9 @@ typedef struct JPC_NarrowPhaseQuery JPC_NarrowPhaseQuery;
typedef struct JPC_NarrowPhaseQuery_CastRayArgs {
JPC_RRayCast Ray;
JPC_RayCastResult Result;
// BroadPhaseLayerFilter
// ObjectLayerFilter
// BodyFilter
JPC_BroadPhaseLayerFilter *BroadPhaseLayerFilter;
JPC_ObjectLayerFilter *ObjectLayerFilter;
JPC_BodyFilter *BodyFilter;
} JPC_NarrowPhaseQuery_CastRayArgs;

JPC_API bool JPC_NarrowPhaseQuery_CastRay(const JPC_NarrowPhaseQuery* self, JPC_NarrowPhaseQuery_CastRayArgs* args);
Expand Down
94 changes: 90 additions & 4 deletions JoltC/JoltC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,88 @@ JPC_API JPC_ObjectVsBroadPhaseLayerFilter* JPC_ObjectVsBroadPhaseLayerFilter_new
return to_jpc(new JPC_ObjectVsBroadPhaseLayerFilterBridge(self, fns));
}

////////////////////////////////////////////////////////////////////////////////
// BroadPhaseLayerFilter

class JPC_BroadPhaseLayerFilterBridge final : public JPH::BroadPhaseLayerFilter {
public:
explicit JPC_BroadPhaseLayerFilterBridge(const void *self, JPC_BroadPhaseLayerFilterFns fns) : self(self), fns(fns) {}

virtual bool ShouldCollide(JPH::BroadPhaseLayer inLayer) const override {
return fns.ShouldCollide(self, to_jpc(inLayer));
}

private:
const void* self;
JPC_BroadPhaseLayerFilterFns fns;
};

OPAQUE_WRAPPER(JPC_BroadPhaseLayerFilter, JPC_BroadPhaseLayerFilterBridge)
DESTRUCTOR(JPC_BroadPhaseLayerFilter)

JPC_API JPC_BroadPhaseLayerFilter* JPC_BroadPhaseLayerFilter_new(
const void *self,
JPC_BroadPhaseLayerFilterFns fns)
{
return to_jpc(new JPC_BroadPhaseLayerFilterBridge(self, fns));
}

////////////////////////////////////////////////////////////////////////////////
// ObjectLayerFilter

class JPC_ObjectLayerFilterBridge final : public JPH::ObjectLayerFilter {
public:
explicit JPC_ObjectLayerFilterBridge(const void *self, JPC_ObjectLayerFilterFns fns) : self(self), fns(fns) {}

virtual bool ShouldCollide(JPH::ObjectLayer inLayer) const override {
return fns.ShouldCollide(self, inLayer);
}

private:
const void* self;
JPC_ObjectLayerFilterFns fns;
};

OPAQUE_WRAPPER(JPC_ObjectLayerFilter, JPC_ObjectLayerFilterBridge)
DESTRUCTOR(JPC_ObjectLayerFilter)

JPC_API JPC_ObjectLayerFilter* JPC_ObjectLayerFilter_new(
const void *self,
JPC_ObjectLayerFilterFns fns)
{
return to_jpc(new JPC_ObjectLayerFilterBridge(self, fns));
}

////////////////////////////////////////////////////////////////////////////////
// BodyFilter

class JPC_BodyFilterBridge final : public JPH::BodyFilter {
public:
explicit JPC_BodyFilterBridge(const void *self, JPC_BodyFilterFns fns) : self(self), fns(fns) {}

virtual bool ShouldCollide(const JPH::BodyID &inBodyID) const override {
return fns.ShouldCollide(self, to_jpc(inBodyID));
}

virtual bool ShouldCollideLocked(const JPH::Body &inBody) const override {
return fns.ShouldCollideLocked(self, to_jpc(&inBody));
}

private:
const void* self;
JPC_BodyFilterFns fns;
};

OPAQUE_WRAPPER(JPC_BodyFilter, JPC_BodyFilterBridge)
DESTRUCTOR(JPC_BodyFilter)

JPC_API JPC_BodyFilter* JPC_BodyFilter_new(
const void *self,
JPC_BodyFilterFns fns)
{
return to_jpc(new JPC_BodyFilterBridge(self, fns));
}

////////////////////////////////////////////////////////////////////////////////
// JPC_ObjectLayerPairFilter

Expand Down Expand Up @@ -1342,12 +1424,16 @@ 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;

JPC_BroadPhaseLayerFilterBridge* bplFilter = to_jph(args->BroadPhaseLayerFilter);
JPC_ObjectLayerFilterBridge* olFilter = to_jph(args->ObjectLayerFilter);
JPC_BodyFilterBridge* bodyFilter = to_jph(args->BodyFilter);

bool hit = to_jph(self)->CastRay(
to_jph(args->Ray),
result
// BroadPhaseLayerFilter
// ObjectLayerFilter
// BodyFilter
result,
*bplFilter,
*olFilter,
*bodyFilter
);

if (hit) {
Expand Down

0 comments on commit bc6d651

Please sign in to comment.