Browse Source

Up to PhysicsSystem initialization

Lucien Greathouse 1 year ago
parent
commit
a2055f9a37
4 changed files with 83 additions and 15 deletions
  1. 2 0
      HelloWorld/README.md
  2. 42 8
      HelloWorld/main.cpp
  3. 16 3
      JoltC/Functions.h
  4. 23 4
      JoltC/JoltC.cpp

+ 2 - 0
HelloWorld/README.md

@@ -0,0 +1,2 @@
+# HelloWorld in JoltC
+A port of the HelloWorld example from Jolt, but using JoltC.

+ 42 - 8
HelloWorld/main.cpp

@@ -21,11 +21,11 @@ typedef enum Hello_BroadPhaseLayers {
 	HELLO_BPL_COUNT,
 } Hello_BroadPhaseLayers;
 
-unsigned int Hello_GetNumBroadPhaseLayers(void* self) {
+unsigned int Hello_BPL_GetNumBroadPhaseLayers(void *self) {
 	return HELLO_BPL_COUNT;
 }
 
-JPC_BroadPhaseLayer Hello_GetBroadPhaseLayer(void* self, JPC_ObjectLayer inLayer) {
+JPC_BroadPhaseLayer Hello_BPL_GetBroadPhaseLayer(void *self, JPC_ObjectLayer inLayer) {
 	switch (inLayer) {
 	case HELLO_OL_NON_MOVING:
 		return HELLO_BPL_NON_MOVING;
@@ -39,11 +39,11 @@ JPC_BroadPhaseLayer Hello_GetBroadPhaseLayer(void* self, JPC_ObjectLayer inLayer
 }
 
 static JPC_BroadPhaseLayerInterfaceFns Hello_BPL = {
-	.GetNumBroadPhaseLayers = Hello_GetNumBroadPhaseLayers,
-	.GetBroadPhaseLayer = Hello_GetBroadPhaseLayer,
+	.GetNumBroadPhaseLayers = Hello_BPL_GetNumBroadPhaseLayers,
+	.GetBroadPhaseLayer = Hello_BPL_GetBroadPhaseLayer,
 };
 
-bool Hello_ShouldCollide(void* self, JPC_ObjectLayer inLayer1, JPC_BroadPhaseLayer inLayer2) {
+bool Hello_OVB_ShouldCollide(void *self, JPC_ObjectLayer inLayer1, JPC_BroadPhaseLayer inLayer2) {
 	switch (inLayer1) {
 	case HELLO_OL_NON_MOVING:
 		return inLayer2 == HELLO_BPL_MOVING;
@@ -57,7 +57,25 @@ bool Hello_ShouldCollide(void* self, JPC_ObjectLayer inLayer1, JPC_BroadPhaseLay
 }
 
 static JPC_ObjectVsBroadPhaseLayerFilterFns Hello_OVB = {
-	.ShouldCollide = Hello_ShouldCollide,
+	.ShouldCollide = Hello_OVB_ShouldCollide,
+};
+
+bool Hello_OVO_ShouldCollide(void *self, JPC_ObjectLayer inLayer1, JPC_ObjectLayer inLayer2) {
+	switch (inLayer1)
+	{
+	case HELLO_OL_NON_MOVING:
+		return inLayer2 == HELLO_OL_MOVING; // Non moving only collides with moving
+
+	case HELLO_OL_MOVING:
+		return true; // Moving collides with everything
+
+	default:
+		unreachable();
+	}
+}
+
+static JPC_ObjectLayerPairFilterFns Hello_OVO = {
+	.ShouldCollide = Hello_OVO_ShouldCollide,
 };
 
 int main() {
@@ -79,11 +97,27 @@ int main() {
 		.self = nullptr,
 	};
 
-	// create object_vs_object_layer_filter
+	JPC_ObjectLayerPairFilter object_vs_object_layer_filter = {
+		.fns = &Hello_OVO,
+		.self = nullptr,
+	};
 
 	JPC_PhysicsSystem* physics_system = JPC_PhysicsSystem_new();
 
-	// JPC_PhysicsSystem_Init();
+	const unsigned int cMaxBodies = 1024;
+	const unsigned int cNumBodyMutexes = 0;
+	const unsigned int cMaxBodyPairs = 1024;
+	const unsigned int cMaxContactConstraints = 1024;
+
+	JPC_PhysicsSystem_Init(
+		physics_system,
+		cMaxBodies,
+		cNumBodyMutexes,
+		cMaxBodyPairs,
+		cMaxContactConstraints,
+		broad_phase_layer_interface,
+		object_vs_broad_phase_layer_filter,
+		object_vs_object_layer_filter);
 
 	JPC_PhysicsSystem_delete(physics_system);
 	JPC_JobSystemThreadPool_delete(job_system);

+ 16 - 3
JoltC/Functions.h

@@ -67,6 +67,18 @@ typedef struct JPC_ObjectVsBroadPhaseLayerFilter {
 	void *self;
 } JPC_ObjectVsBroadPhaseLayerFilter;
 
+////////////////////////////////////////////////////////////////////////////////
+// ObjectLayerPairFilter
+
+typedef struct JPC_ObjectLayerPairFilterFns {
+	bool (*ShouldCollide)(void *self, JPC_ObjectLayer inLayer1, JPC_ObjectLayer inLayer2);
+} JPC_ObjectLayerPairFilterFns;
+
+typedef struct JPC_ObjectLayerPairFilter {
+	JPC_ObjectLayerPairFilterFns *fns;
+	void *self;
+} JPC_ObjectLayerPairFilter;
+
 ////////////////////////////////////////////////////////////////////////////////
 // PhysicsSystem
 
@@ -75,13 +87,14 @@ typedef struct JPC_PhysicsSystem JPC_PhysicsSystem;
 JPC_API JPC_PhysicsSystem* JPC_PhysicsSystem_new();
 JPC_API void JPC_PhysicsSystem_delete(JPC_PhysicsSystem* object);
 JPC_API void JPC_PhysicsSystem_Init(
+	JPC_PhysicsSystem* self,
 	uint inMaxBodies,
 	uint inNumBodyMutexes,
 	uint inMaxBodyPairs,
 	uint inMaxContactConstraints,
-	JPC_BroadPhaseLayerInterface inBroadPhaseLayerInterface);
-	// const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter,
-	// const ObjectLayerPairFilter &inObjectLayerPairFilter);
+	JPC_BroadPhaseLayerInterface inBroadPhaseLayerInterface,
+	JPC_ObjectVsBroadPhaseLayerFilter inObjectVsBroadPhaseLayerFilter,
+	JPC_ObjectLayerPairFilter inObjectLayerPairFilter);
 
 #ifdef __cplusplus
 }

+ 23 - 4
JoltC/JoltC.cpp

@@ -115,6 +115,26 @@ static JPC_ObjectVsBroadPhaseLayerFilter_Impl to_jph(JPC_ObjectVsBroadPhaseLayer
 	return JPC_ObjectVsBroadPhaseLayerFilter_Impl(in);
 }
 
+////////////////////////////////////////////////////////////////////////////////
+// JPC_ObjectLayerPairFilter
+
+class JPC_ObjectLayerPairFilter_Impl : public JPH::ObjectLayerPairFilter {
+public:
+	explicit JPC_ObjectLayerPairFilter_Impl(JPC_ObjectLayerPairFilter in) : fns(*in.fns), self(in.self) {}
+
+	bool ShouldCollide(JPH::ObjectLayer inLayer1, JPH::ObjectLayer inLayer2) const override {
+		return fns.ShouldCollide(self, inLayer1, inLayer2);
+	}
+
+private:
+	JPC_ObjectLayerPairFilterFns fns;
+	void* self;
+};
+
+static JPC_ObjectLayerPairFilter_Impl to_jph(JPC_ObjectLayerPairFilter in) {
+	return JPC_ObjectLayerPairFilter_Impl(in);
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // PhysicsSystem
 
@@ -132,13 +152,12 @@ JPC_API void JPC_PhysicsSystem_Init(
 	uint inMaxBodyPairs,
 	uint inMaxContactConstraints,
 	JPC_BroadPhaseLayerInterface inBroadPhaseLayerInterface,
-	JPC_ObjectVsBroadPhaseLayerFilter inObjectVsBroadPhaseLayerFilter)
-	// const ObjectLayerPairFilter &inObjectLayerPairFilter);
+	JPC_ObjectVsBroadPhaseLayerFilter inObjectVsBroadPhaseLayerFilter,
+	JPC_ObjectLayerPairFilter inObjectLayerPairFilter)
 {
 	auto impl_inBroadPhaseLayerInterface = to_jph(inBroadPhaseLayerInterface);
 	auto impl_inObjectVsBroadPhaseLayerFilter = to_jph(inObjectVsBroadPhaseLayerFilter);
-
-	JPH::ObjectLayerPairFilter impl_inObjectLayerPairFilter;
+	auto impl_inObjectLayerPairFilter = to_jph(inObjectLayerPairFilter);
 
 	to_jph(self)->Init(
 		inMaxBodies,