Jolt Physics Wrapper
- Posted by Icy_Viking Jun 15, 2023
- 872 views
Hello,
I've recently written a wrapper of the Jolt Physics Library. However when trying to make a example program, nothing shows up.
https://github.com/amerkoleci/JoltPhysicsSharp/blob/main/src/joltc/joltc.h - This is the Wrapper code that I made the wrapper from.
https://github.com/michal-z/zig-gamedev/blob/main/libs/zphysics/libs/JoltC/JoltPhysicsC_Tests.c - I tried converting the HelloWorld test to Eu code, which is the code below.
Notice the difference from JPH and JPC. I used the CSharp C port to write the wrapper. As I couldn't figure out how to make the Zig C port into a DLL. So that's why the wrapper uses JPH as its name calling convention for exporting.
https://katfile.com/rdhsy0f8i5c9/Jolt.zip.html - Includes the wrapper, attempted example and JoltC DLL.
I'm not sure if I'm overlooking something that is simple, or I screwed up somewhere else. A fresh pair of eyes may help.
https://github.com/jrouwe/JoltPhysics - The official CPP port of Jolt Physics.
--Hello World Example Jolt Physics include std/ffi.e include std/machine.e include jolt.e atom j = JPH_Init() if j = -1 then puts(1,"Failed to init Jolt Physics!\n") abort(0) end if atom sys = JPH_PhysicsSystem_Create() atom talloc = JPH_TempAllocator_Create(10 * 1024 * 1024) atom job_sys = JPH_JobSystemThreadPool_Create(2048,8,-1) constant max_bodies = 1024 constant num_body_mutexes = 0 constant max_body_pairs = 1024 constant max_contact_constraints = 1024 atom broad_phase_layer_interface = JPH_BroadPhaseLayerInterface_Create() atom broad_phase_layer_filter = JPH_ObjectVsBroadPhaseLayerFilter_Create() atom pair_filter = JPH_ObjectLayerPairFilter_Create() JPH_PhysicsSystem_Init(sys,max_bodies,num_body_mutexes,max_body_pairs,max_contact_constraints, broad_phase_layer_interface,broad_phase_layer_filter,pair_filter) atom body_act_list = JPH_BodyActivationListener_Create() JPH_PhysicsSystem_SetBodyActivationListener(sys,body_act_list) atom contact_list = JPH_ContactListener_Create() JPH_PhysicsSystem_SetContactListener(sys,contact_list) atom body_interface = JPH_PhysicsSystem_GetBodyInterface(sys) atom ground_vec3 = allocate_struct(JPH_Vec3,{100.0,1.0,100.0}) atom ground_quat = allocate_struct(JPH_Quat,{0.0,0.0,0.0,1.0}) atom ground_shape_settings = JPH_BoxShapeSettings_Create(ground_vec3,NULL) atom ground_shape = JPH_BoxShape_Create(ground_shape_settings,NULL) atom ground_settings = JPH_BodyCreationSettings_Create() atom ground_set = JPH_BodyCreationSettings_Create2(ground_settings, ground_vec3,ground_quat,JPH_MOTION_TYPE_STATIC, 0) atom ground_set2 = JPH_BodyCreationSettings_Create3(ground_shape,ground_vec3,ground_quat,JPH_MOTION_TYPE_STATIC,0) atom ground_body = JPH_BodyInterface_CreateBody(body_interface,ground_settings) atom ground_id = JPH_BodyInterface_AssignBodyID2(body_interface,ground_body,1) JPH_BodyInterface_AddBody(body_interface,ground_body,JPH_ACTIVATION_MODE_DONT_ACTIVATE) atom sphere_vec3 = allocate_struct(JPH_Vec3,{0.0,2.0,0.0}) atom sphere_quat = allocate_struct(JPH_Quat,{0.0,0.0,0.0,1.0}) atom sphere_rad = 0.5 atom sphere_shape_settings = JPH_SphereShapeSettings_Create(sphere_rad) atom sphere_shape = JPH_SphereShape_Create(sphere_rad) atom sphere_settings = JPH_BodyCreationSettings_Create() atom sphere_set = JPH_BodyCreationSettings_Create2(sphere_settings,sphere_vec3,sphere_quat,JPH_MOTION_TYPE_DYNAMIC,0) atom sphere_set2 = JPH_BodyCreationSettings_Create3(sphere_shape,sphere_vec3,sphere_quat,JPH_MOTION_TYPE_DYNAMIC,0) atom sphere_id = JPH_BodyInterface_CreateAndAddBody(body_interface,sphere_shape_settings,JPH_ACTIVATION_MODE_ACTIVATE) atom sphere_vel = allocate_struct(JPH_Vec3,{0.0,-5.0,0.0}) JPH_BodyInterface_SetLinearVelocity(body_interface,sphere_id,sphere_vel) JPH_PhysicsSystem_OptimizeBroadPhase(sys) atom step = 0 atom pos = allocate_struct(JPH_Vec3,{0,0,0}) sequence vel = {3} constant delta_time = 1.0 / 60.0 constant collision_steps = 1 constant integration_sub_steps = 1 while JPH_BodyInterface_IsActive(body_interface,sphere_id) do step = step + 1 JPH_BodyInterface_GetCenterOfMassPosition(body_interface,sphere_id,pos) JPH_BodyInterface_GetLinearVelocity(body_interface,sphere_id,vel) JPH_PhysicsUpdateError up = JPH_PhysicsSystem_Update(sys,delta_time,collision_steps,integration_sub_steps,talloc,job_sys) if up != JPH_PHYSICS_UPDATE_ERROR_NONE then puts(1,"No Physics!\n") --Not sure if this if statement is needed end if printf(1,"Step: %d\n Position %f, %f, %f,\n %f,%f,%f",{step,pos[1],pos[2],pos[3],vel[1],vel[2],vel[3]}) --nothing shows up on the command line. end while JPH_BodyInterface_RemoveBody(body_interface,sphere_id) JPH_BodyInterface_DestroyBody(body_interface,sphere_id) JPH_BodyInterface_RemoveBody(body_interface,ground_id) JPH_BodyInterface_DestroyBody(body_interface,ground_id) JPH_Shape_Destroy(ground_shape) JPH_BodyCreationSettings_Destroy(ground_settings) JPH_ContactListener_Destroy(contact_list) JPH_BodyActivationListener_Destroy(body_act_list) JPH_ObjectLayerPairFilter_Destroy(pair_filter) JPH_ObjectVsBroadPhaseLayerFilter_Destroy(broad_phase_layer_filter) JPH_BroadPhaseLayerInterface_Destroy(broad_phase_layer_interface) JPH_JobSystemThreadPool_Destroy(job_sys) JPH_TempAllocator_Destroy(talloc) JPH_PhysicsSystem_Destroy(sys) JPH_Shutdown()