{"id":13732111,"url":"https://github.com/victorfisac/Physac","last_synced_at":"2025-05-08T06:31:21.753Z","repository":{"id":44346038,"uuid":"70338024","full_name":"victorfisac/Physac","owner":"victorfisac","description":"2D physics header-only library for videogames developed in C using raylib library.","archived":false,"fork":false,"pushed_at":"2024-10-20T15:16:31.000Z","size":7448,"stargazers_count":444,"open_issues_count":0,"forks_count":30,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-20T16:29:55.041Z","etag":null,"topics":["2d","2d-physics-engine","c","dynamics","physics","physics-engine","simulation","threading"],"latest_commit_sha":null,"homepage":"http://www.victorfisac.com/physac","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/victorfisac.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":"2016-10-08T14:52:18.000Z","updated_at":"2024-10-20T15:16:34.000Z","dependencies_parsed_at":"2024-10-20T16:44:35.913Z","dependency_job_id":null,"html_url":"https://github.com/victorfisac/Physac","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorfisac%2FPhysac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorfisac%2FPhysac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorfisac%2FPhysac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorfisac%2FPhysac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/victorfisac","download_url":"https://codeload.github.com/victorfisac/Physac/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224707724,"owners_count":17356393,"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":["2d","2d-physics-engine","c","dynamics","physics","physics-engine","simulation","threading"],"created_at":"2024-08-03T02:01:46.635Z","updated_at":"2024-11-14T23:31:09.667Z","avatar_url":"https://github.com/victorfisac.png","language":"C","funding_links":[],"categories":["Physics"],"sub_categories":[],"readme":"\u003cimg src=\"https://github.com/victorfisac/Physac/blob/master/icon/physac_256x256.png\"\u003e\n\n# Physac\n\nPhysac is a small 2D physics engine written in pure C. The engine uses a fixed time-step thread loop to simluate physics.\nA physics step contains the following phases: get collision information, apply dynamics, collision solving and position correction. It uses a very simple struct for physic bodies with a position vector to be used in any 3D rendering API.\n\nThe header file includes some tweakable define values to fit the results that the user wants with a minimal bad results. Most of those values are commented with a little explanation about their uses.\n\n_Note: The example code uses raylib programming library to create the program window and rendering framework._\n\nInstallation\n-----\n\nPhysac requires raylib. To get it, follow the next steps:\n\n    * Go to [raylib](https://www.github.com/raysan5/raylib) and clone the repository.\n    * Ensure to pull the last changes of 'master' branch.\n    * Use code inside examples header comments to compile and execute.\n\nPhysac API\n-----\n\nThe PhysicsBody struct contains all dynamics information and collision shape. The user should use the following structure components:\n```c\ntypedef struct *PhysicsBody {\n    unsigned int id;\n    bool enabled;                   // Enabled dynamics state (collisions are calculated anyway)\n    Vector2 position;               // Physics body shape pivot\n    Vector2 velocity;               // Current linear velocity applied to position\n    Vector2 force;                  // Current linear force (reset to 0 every step)\n    float angularVelocity;          // Current angular velocity applied to orient\n    float torque;                   // Current angular force (reset to 0 every step)\n    float orient;                   // Rotation in radians\n    float staticFriction;           // Friction when the body has not movement (0 to 1)\n    float dynamicFriction;          // Friction when the body has movement (0 to 1)\n    float restitution;              // Restitution coefficient of the body (0 to 1)\n    bool useGravity;                // Apply gravity force to dynamics\n    bool isGrounded;                // Physics grounded on other body state\n    bool freezeOrient;              // Physics rotation constraint\n    PhysicsShape shape;             // Physics body shape information (type, radius, vertices, normals)\n} *PhysicsBody;\n```\nThe header contains a few customizable define values. I set the values that gived me the best results.\n\n```c\n#define     PHYSAC_MAX_BODIES               64\n#define     PHYSAC_MAX_MANIFOLDS            4096\n#define     PHYSAC_MAX_VERTICES             24\n#define     PHYSAC_CIRCLE_VERTICES          24\n\n#define     PHYSAC_COLLISION_ITERATIONS     100\n#define     PHYSAC_PENETRATION_ALLOWANCE    0.05f\n#define     PHYSAC_PENETRATION_CORRECTION   0.4f\n```\n\nPhysac contains defines for memory management functions (malloc, free) to bring the user the opportunity to implement its own memory functions:\n\n```c\n#define     PHYSAC_MALLOC(size)             malloc(size)\n#define     PHYSAC_FREE(ptr)                free(ptr)\n```\n\nThe Physac API functions availables for the user are the following:\n\n```c\n// Initializes physics values, pointers and creates physics loop thread\nvoid InitPhysics(void);\n\n// Returns true if physics thread is currently enabled\nbool IsPhysicsEnabled(void);\n\n// Sets physics global gravity force\nvoid SetPhysicsGravity(float x, float y);\n\n// Creates a new circle physics body with generic parameters\nPhysicsBody CreatePhysicsBodyCircle(Vector2 pos, float radius, float density);\n\n// Creates a new rectangle physics body with generic parameters\nPhysicsBody CreatePhysicsBodyRectangle(Vector2 pos, float width, float height, float density);\n\n// Creates a new polygon physics body with generic parameters\nPhysicsBody CreatePhysicsBodyPolygon(Vector2 pos, float radius, int sides, float density);\n\n// Adds a force to a physics body\nvoid PhysicsAddForce(PhysicsBody body, Vector2 force);\n\n// Adds a angular force to a physics body\nvoid PhysicsAddTorque(PhysicsBody body, float amount);\n\n// Shatters a polygon shape physics body to little physics bodies with explosion force\nvoid PhysicsShatter(PhysicsBody body, Vector2 position, float force);\n\n// Returns the current amount of created physics bodies\nint GetPhysicsBodiesCount(void);\n\n// Returns a physics body of the bodies pool at a specific index\nPhysicsBody GetPhysicsBody(int index);\n\n// Returns the physics body shape type (PHYSICS_CIRCLE or PHYSICS_POLYGON)\nint GetPhysicsShapeType(int index);\n\n// Returns the amount of vertices of a physics body shape\nint GetPhysicsShapeVerticesCount(int index);\n\n// Returns transformed position of a body shape (body position + vertex transformed position)\nVector2 GetPhysicsShapeVertex(PhysicsBody body, int vertex);\n\n// Sets physics body shape transform based on radians parameter\nvoid SetPhysicsBodyRotation(PhysicsBody body, float radians);\n\n// Unitializes and destroy a physics body\nvoid DestroyPhysicsBody(PhysicsBody body);\n\n// Unitializes physics pointers and closes physics loop thread\nvoid ClosePhysics(void);\n```\n_Note: InitPhysics() needs to be called at program start and ClosePhysics() before the program ends. Closing and initializing Physac during the program flow doesn't affect or produces any error (useful as a 'reset' to destroy any created body by user in runtime)._\n\nDependencies\n-----\n\nPhysac uses the following C libraries for memory management, math operations and some debug features:\n\n   *  stdlib.h - Memory allocation [malloc(), free(), srand(), rand()].\n   *  stdio.h  - Message logging (only if PHYSAC_DEBUG is defined) [printf()].\n   *  math.h   - Math operations functions [cos(), sin(), fabs(), sqrtf()].\n\n**It is independent to any graphics engine** and prepared to use any graphics API and use the vertices information (look at examples Drawing logic) to draw lines or shapes in screen. For example, this vertices information can be use in OpenGL API glVertex2f().\n\nBy the way, I use [raylib](http://www.raylib.com) to create the examples. This videogames programming library is used to handle inputs, window management and graphics drawing (using OpenGL API).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvictorfisac%2FPhysac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvictorfisac%2FPhysac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvictorfisac%2FPhysac/lists"}