{"id":26491998,"url":"https://github.com/zig-gamedev/zphysics","last_synced_at":"2025-03-20T08:51:40.779Z","repository":{"id":270769882,"uuid":"883373427","full_name":"zig-gamedev/zphysics","owner":"zig-gamedev","description":"Zig build package, bindings and C API (JoltC) for https://github.com/jrouwe/JoltPhysics","archived":false,"fork":false,"pushed_at":"2025-01-18T12:53:03.000Z","size":1029,"stargazers_count":15,"open_issues_count":2,"forks_count":6,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-01-18T14:13:27.461Z","etag":null,"topics":["3d","bindings","gamedev","jolt-physics","physics","simulation","zig"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zig-gamedev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-04T21:13:35.000Z","updated_at":"2025-01-18T12:53:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"a241b601-214f-4e15-9f7b-031649593110","html_url":"https://github.com/zig-gamedev/zphysics","commit_stats":null,"previous_names":["zig-gamedev/zphysics"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zig-gamedev%2Fzphysics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zig-gamedev%2Fzphysics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zig-gamedev%2Fzphysics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zig-gamedev%2Fzphysics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zig-gamedev","download_url":"https://codeload.github.com/zig-gamedev/zphysics/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244583172,"owners_count":20476233,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["3d","bindings","gamedev","jolt-physics","physics","simulation","zig"],"created_at":"2025-03-20T08:51:40.241Z","updated_at":"2025-03-20T08:51:40.768Z","avatar_url":"https://github.com/zig-gamedev.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [zphysics](https://github.com/zig-gamedev/zphysics)\n\nZig build package, bindings and [C API](libs/JoltC) for [Jolt Physics](https://github.com/jrouwe/JoltPhysics).\n\nFor a simple sample applications please see [here](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples/physics_test_wgpu/src/physics_test_wgpu.zig).\n\n## Getting started\n\nExample `build.zig`:\n```zig\npub fn build(b: *std.Build) void {\n    const exe = b.addExecutable(.{ ... });\n\n    const zphysics = b.dependency(\"zphysics\", .{\n        .use_double_precision = false,\n        .enable_cross_platform_determinism = true,\n    });\n    exe.root_module.addImport(\"zphysics\", zphysics.module(\"root\"));\n    exe.linkLibrary(zphysics.artifact(\"joltc\"));\n}\n```\n\nNow in your code you may import and use `zphysics`:\n\n```zig\nconst zphy = @import(\"zphysics\");\n\npub fn main() !void {\n    try zphy.init(allocator, .{});\n    defer zphy.deinit();\n\n    // Create physics system\n    const physics_system = try zphy.PhysicsSystem.create(\n        ... // layer interfaces - please see sample application\n        .{\n            .max_bodies = 1024,\n            .num_body_mutexes = 0,\n            .max_body_pairs = 1024,\n            .max_contact_constraints = 1024,\n        },\n    );\n    defer physics_system.destroy();\n\n    // Create shape\n    const body_interface = physics_system.getBodyInterfaceMut();\n\n    const shape_settings = try zphy.BoxShapeSettings.create(.{ 1.0, 1.0, 1.0 });\n    defer shape_settings.release();\n\n    const shape = try shape_settings.createShape();\n    defer shape.release();\n\n    // Create body\n    const body_id = try body_interface.createAndAddBody(.{\n        .position = .{ 0.0, -1.0, 0.0, 1.0 },\n        .rotation = .{ 0.0, 0.0, 0.0, 1.0 },\n        .shape = shape,\n        .motion_type = .dynamic,\n        .object_layer = object_layers.non_moving,\n    }, .activate);\n    defer body_interface.removeAndDestroyBody(body_id);\n\n    physics_system.optimizeBroadPhase();\n\n    // Perform ray cast\n    {\n        const query = physics_system.getNarrowPhaseQuery();\n\n        var result = query.castRay(.{ .origin = .{ 0, 10, 0, 1 }, .direction = .{ 0, -20, 0, 0 } }, .{});\n        if (result.has_hit) {\n            // result.hit.body_id\n            // result.hit.fraction\n            // result.hit.sub_shape_id\n            ...\n        }\n    }\n\n    // Main loop\n    while (...) {\n        physics_system.update(1.0 / 60.0, .{});\n\n        // Draw all bodies\n        const bodies = physics_system.getBodiesUnsafe();\n        for (bodies) |body| {\n            if (!zphy.isValidBodyPointer(body)) continue;\n\n            const object_to_world = object_to_world: {\n                const position = zm.loadArr4(body.position);\n                const rotation = zm.loadArr4(body.rotation);\n                var xform = zm.matFromQuat(rotation);\n                xform[3] = position;\n                xform[3][3] = 1.0;\n                break :object_to_world xform;\n            };\n\n            // Issue a draw call\n            ...\n        }\n    }\n}\n```\n\n## Usage in a shared library\n\nThe `joltc` artifact can be built as a shared library by specifying the `shared` build option:\n\n```\n    const zphysics = b.dependency(\"zphysics\", .{\n        .shared = true,\n    });\n```\n\nIf your zig module uses `zphysics` and is itself part of a shared library that is reloaded at runtime, then some additional steps are required:\n\n- Before unloading the shared library, call `preUnload` to export the internal global state\n- After reloading the shared library, call `postReload` to import the internal state and update allocator vtables\n\nIf you use `registerTrace` or `registerAssertFailed`, these must also be called again to update their function pointers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzig-gamedev%2Fzphysics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzig-gamedev%2Fzphysics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzig-gamedev%2Fzphysics/lists"}