1. Jolt Physics Wrapper
- Posted by Icy_Viking Jun 15, 2023
- 962 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()
2. Re: Jolt Physics Wrapper
- Posted by mitgedanken Jun 26, 2023
- 776 views
[...] As I couldn't figure out how to make the Zig C port into a DLL. [...]
I'm not sure I understood you correctly.
What do you mean by "As I couldn't figure out how [...]"?
There is a manual that tells you how to do it.
- https://ziglang.org/documentation/0.10.1/
- Building a Library
- zig build-lib file.zig -dynamic
3. Re: Jolt Physics Wrapper
- Posted by Icy_Viking Jun 26, 2023
- 783 views
[...] As I couldn't figure out how to make the Zig C port into a DLL. [...]
I'm not sure I understood you correctly.
What do you mean by "As I couldn't figure out how [...]"?
There is a manual that tells you how to do it.
- https://ziglang.org/documentation/0.10.1/
- Building a Library
- zig build-lib file.zig -dynamic
I tried that after installing Zig and still couldn't get it to work. Apparently there's a bug with it, currently.
4. Re: Jolt Physics Wrapper
- Posted by ghaberek (admin) Jun 30, 2023
- 984 views
- Last edited Aug 14, 2023
I was able to build the zig-gamedev library with GCC using the following Makefile. This works for me on Windows 10 and Ubuntu 20.04.
That being said, I haven't actually tested anything and Jolt seems to make heavy use of threads so it may not be stable for use with Euphoria at this time.
# Recursive wildcard function https://stackoverflow.com/a/18258352 rwildcard = $(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d)) SOURCES = $(filter-out %/JoltPhysicsC_Extensions.cpp,$(call rwildcard,libs,*.cpp)) OBJECTS = $(patsubst %.cpp,%.o,$(SOURCES)) ifeq ($(OS),Windows_NT) TARGET = joltc.dll else TARGET = libjoltc.so endif CXX = g++ CXXFLAGS = -std=c++17 -Ilibs -fPIC -O2 -s -DJPH_DEBUG_RENDERER LDFLAGS = -shared ifdef BITS32 CXXFLAGS += -m32 LDFLAGS += -m32 else ifdef BITS64 CXXFLAGS += -m64 LDFLAGS += -m64 endif all : $(TARGET) $(TARGET) : $(OBJECTS) $(CXX) $(LDFLAGS) -o $@ $^ $(OBJECTS) : %.o : %.cpp ; $(CXX) $(CXXFLAGS) -o $@ -c $< ifeq ($(OS),Windows_NT) clean : @del /S $(subst /,\,$(OBJECTS) $(TARGET)) >NUL 2>&1 || (exit 0) else clean : @rm -f $(OBJECTS) $(TARGET) endif .PHONY : all clean
One thing I noticed building with GCC is that this line in JoltPhysicsC_Extensions.cpp causes a build error (and is in general a big no-no) so I've filtered it out. I don't think it's necessary.
// We do this because we add some low-level functions which need access to private fields. // Also, we static assert offsets of some private fields (see bottom of this file). #define private public
You should be able to build on Windows using TDM-GCC with these steps:
> git clone ~https://github.com/michal-z/zig-gamedev > cd zig-gamedev\libs\zphysics (save Makefile to zphysics directory) > mingw32-make
If you want to make separate 32-bit and 64-bit libraries, you can do something like this:
(build a separate 32-bit library) > mingw32-make BITS32=1 TARGET=joltc32.dll > mingw32-make clean (build a separate 64-bit library) > mingw32-make BITS64=1 TARGET=joltc64.dll > mingw32-make clean
Don't forget to add -j4 to run build faster in parallel. (Use your number of CPU cores.)
-Greg
5. Re: Jolt Physics Wrapper
- Posted by Icy_Viking Jul 01, 2023
- 764 views
You son of a gun, Greg! It worked! I was able to compile JoltC in a DLL using your tips. I was having a lot of trouble trying to compile/build it using Zig. Note that JoltC is still a work in progress, but I can try and make a wrapper and see if its feasible with Euphoria. I also notice that Bullet physics, well the C port of Bullet physics is also part of the zig game dev package. I'm working on upgrading my SFML wrapper, after that I'll start work on JoltC wrapper and see if its hopefully feasible. Thanks for helping!