joltc
repository·main·Indexed 42 days ago
https://github.com/amerkoleci/joltcA C interface for the JoltPhysics engine that provides a stable C API for C/C++ developers. It includes bindings for initializing the engine via JPH_Init, configuring physics systems, managing collision filtering, creating rigid bodies with various shapes, and updating the simulation using JPH_PhysicsSystem_Update.
What's inside joltc
- joltc is a C interface for JoltPhysics. It allows developers to interact with the Jolt Physics engine using C-compatible bindings.
Set up the Physics System and Collision Filtering
mainA
JPH_PhysicsSystemrequires several configuration components to manage collisions and layers:- Object Layer Pair Filter: Defines which object layers are allowed to collide with each other. Use
JPH_ObjectLayerPairFilterTable_CreateandJPH_ObjectLayerPairFilterTable_EnableCollision. - Broad Phase Layer Interface: Maps object layers to broad-phase layers. Use
JPH_BroadPhaseLayerInterfaceTable_CreateandJPH_BroadPhaseLayerInterfaceTable_MapObjectToBroadPhaseLayer. - Object vs Broad Phase Layer Filter: Combines the previous two to filter collisions during the broad-phase stage. Use
JPH_ObjectVsBroadPhaseLayerFilterTable_Create.
Finally, configure
JPH_PhysicsSystemSettingswith these tables and pass them toJPH_PhysicsSystem_Create().// 1. Create Object Layer Filter JPH_ObjectLayerPairFilterTable* objectLayerPairFilterTable = JPH_ObjectLayerPairFilterTable_Create(numLayers); JPH_ObjectLayerPairFilterTable_EnableCollision(objectLayerPairFilterTable, layerA, layerB); // 2. Create Broad Phase Layer Interface JPH_BroadPhaseLayerInterface* broadPhaseLayerInterfaceTable = JPH_BroadPhaseLayerInterfaceTable_Create(numObjectLayers, numBroadPhaseLayers); JPH_BroadPhaseLayerInterfaceTable_MapObjectToBroadPhaseLayer(broadPhaseLayerInterfaceTable, objectLayer, broadPhaseLayer); // 3. Create Object vs Broad Phase Filter JPH_ObjectVsBroadPhaseLayerFilter* objectVsBroadPhaseLayerFilter = JPH_ObjectVsBroadPhaseLayerFilterTable_Create(broadPhaseLayerInterfaceTable, numBroadPhaseLayers, objectLayerPairFilterTable, numObjectLayers); // 4. Initialize Physics System JPH_PhysicsSystemSettings settings = {}; settings.broadPhaseLayerInterface = broadPhaseLayerInterfaceTable; settings.objectLayerPairFilter = objectLayerPairFilterTable; settings.objectVsBroadPhaseLayerFilter = objectVsBroadPhaseLayerFilter; JPH_PhysicsSystem* system = JPH_PhysicsSystem_Create(&settings);- Object Layer Pair Filter: Defines which object layers are allowed to collide with each other. Use
Create and add rigid bodies to the simulation
mainTo add a body to the physics world, follow these steps:
- Create a Shape: Use shape-specific creation functions like
JPH_BoxShape_Create,JPH_SphereShape_Create, orJPH_CapsuleShape_Create. - Configure Body Settings: Use
JPH_BodyCreationSettings_Create3to define the shape, position, rotation (identity if not specified), motion type (JPH_MotionType_StaticorJPH_MotionType_Dynamic), and the object layer. - Add to System: Use
JPH_BodyInterface_CreateAndAddBodyvia theJPH_BodyInterface(obtained from the physics system) to register the body. - Cleanup Settings: Always call
JPH_BodyCreationSettings_Destroyafter adding the body to prevent memory leaks.
Note:
JPH_BodyInterface_CreateAndAddBodyreturns aJPH_BodyIDused to reference the body in subsequent calls.// Create shape JPH_Vec3 halfExtents = { 100.0f, 1.0f, 100.0f }; JPH_BoxShape* shape = JPH_BoxShape_Create(&halfExtents, JPH_DEFAULT_CONVEX_RADIUS); // Create settings JPH_Vec3 position = { 0.0f, -1.0f, 0.0f }; JPH_BodyCreationSettings* settings = JPH_BodyCreationSettings_Create3( (const JPH_Shape*)shape, &position, nullptr, // Identity rotation JPH_MotionType_Static, Layers::NON_MOVING ); // Add to system JPH_BodyID bodyId = JPH_BodyInterface_CreateAndAddBody(bodyInterface, settings, JPH_Activation_DontActivate); // Cleanup settings JPH_BodyCreationSettings_Destroy(settings);- Create a Shape: Use shape-specific creation functions like
Update the physics simulation
mainThe physics simulation is advanced in discrete time steps using
JPH_PhysicsSystem_Update.Signature:
void JPH_PhysicsSystem_Update(JPH_PhysicsSystem* system, float time_step, uint32_t num_collision_steps, JPH_JobSystem* job_system)time_step: The amount of time to advance (e.g.,1.0f / 60.0f).num_collision_steps: The number of collision sub-steps. For stability, if yourtime_stepis larger than 1/60th of a second, you should perform multiple collision steps.job_system: A pointer to aJPH_JobSystem(e.g., created viaJPH_JobSystemThreadPool_Create) to enable multi-threaded physics updates.
const float deltaTime = 1.0f / 60.0f; const int collisionSteps = 1; // Step the world JPH_PhysicsSystem_Update(system, deltaTime, collisionSteps, jobSystem);Interact with bodies via JPH_BodyInterface
mainOnce a body is added to the system, use the
JPH_BodyInterfaceto manipulate it or query its state using itsJPH_BodyID.Common Operations:
- Set Velocity:
JPH_BodyInterface_SetLinearVelocity(bodyInterface, bodyId, &velocity) - Get Position:
JPH_BodyInterface_GetCenterOfMassPosition(bodyInterface, bodyId, &position) - Get Velocity:
JPH_BodyInterface_GetLinearVelocity(bodyInterface, bodyId, &velocity) - Check Activity:
JPH_BodyInterface_IsActive(bodyInterface, bodyId) - Remove Body:
JPH_BodyInterface_RemoveAndDestroyBody(bodyInterface, bodyId)
// Set velocity JPH_Vec3 velocity = { 0.0f, -5.0f, 0.0f }; JPH_BodyInterface_SetLinearVelocity(bodyInterface, sphereId, &velocity); // Query position JPH_RVec3 position; JPH_BodyInterface_GetCenterOfMassPosition(bodyInterface, sphereId, &position);- Set Velocity:
Initialize and shutdown the JoltPhysics engine
mainBefore using any JoltPhysics functionality, you must initialize the engine using
JPH_Init(). To clean up resources and shut down the engine, callJPH_Shutdown(). You can also set a custom trace handler for logging usingJPH_SetTraceHandler().if (!JPH_Init()) return 1; // Set a custom trace handler JPH_SetTraceHandler(TraceImpl); // ... use Jolt ... JPH_Shutdown();