joltc

repository·main·Indexed 42 days ago

https://github.com/amerkoleci/joltc

A 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.

Tokens
2K
Snippets
5
Records
6
Agent score
3%

What's inside joltc

  1. Set up the Physics System and Collision Filtering

    main

    A JPH_PhysicsSystem requires several configuration components to manage collisions and layers:

    1. Object Layer Pair Filter: Defines which object layers are allowed to collide with each other. Use JPH_ObjectLayerPairFilterTable_Create and JPH_ObjectLayerPairFilterTable_EnableCollision.
    2. Broad Phase Layer Interface: Maps object layers to broad-phase layers. Use JPH_BroadPhaseLayerInterfaceTable_Create and JPH_BroadPhaseLayerInterfaceTable_MapObjectToBroadPhaseLayer.
    3. 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_PhysicsSystemSettings with these tables and pass them to JPH_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);
  2. Create and add rigid bodies to the simulation

    main

    To add a body to the physics world, follow these steps:

    1. Create a Shape: Use shape-specific creation functions like JPH_BoxShape_Create, JPH_SphereShape_Create, or JPH_CapsuleShape_Create.
    2. Configure Body Settings: Use JPH_BodyCreationSettings_Create3 to define the shape, position, rotation (identity if not specified), motion type (JPH_MotionType_Static or JPH_MotionType_Dynamic), and the object layer.
    3. Add to System: Use JPH_BodyInterface_CreateAndAddBody via the JPH_BodyInterface (obtained from the physics system) to register the body.
    4. Cleanup Settings: Always call JPH_BodyCreationSettings_Destroy after adding the body to prevent memory leaks.

    Note: JPH_BodyInterface_CreateAndAddBody returns a JPH_BodyID used 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);
  3. Update the physics simulation

    main

    The 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 your time_step is larger than 1/60th of a second, you should perform multiple collision steps.
    • job_system: A pointer to a JPH_JobSystem (e.g., created via JPH_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);
  4. Interact with bodies via JPH_BodyInterface

    main

    Once a body is added to the system, use the JPH_BodyInterface to manipulate it or query its state using its JPH_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);
  5. Initialize and shutdown the JoltPhysics engine

    main

    Before using any JoltPhysics functionality, you must initialize the engine using JPH_Init(). To clean up resources and shut down the engine, call JPH_Shutdown(). You can also set a custom trace handler for logging using JPH_SetTraceHandler().

    if (!JPH_Init()) return 1;
    
    // Set a custom trace handler
    JPH_SetTraceHandler(TraceImpl);
    
    // ... use Jolt ...
    
    JPH_Shutdown();