Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

Physac

> 编程语言
Open source

2D physics header-only library for videogames developed in C using raylib library.

606 stars0 likes1 views
WebsiteGitHub

About

2D physics header-only library for videogames developed in C using raylib library.

Physac

Physac is a small 2D physics engine written in pure C. The engine uses a fixed time-step thread loop to simluate physics. A 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.

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

Note: The example code uses raylib programming library to create the program window and rendering framework.

Installation

Physac requires raylib. To get it, follow the next steps:

* Go to [raylib](https://www.github.com/raysan5/raylib) and clone the repository.
* Ensure to pull the last changes of 'master' branch.
* Use code inside examples header comments to compile and execute.

Physac API

The PhysicsBody struct contains all dynamics information and collision shape. The user should use the following structure components:

…

The header contains a few customizable define values. I set the values that gived me the best results.

c
#define     PHYSAC_MAX_BODIES               64
#define     PHYSAC_MAX_MANIFOLDS            4096
#define     PHYSAC_MAX_VERTICES             24
#define     PHYSAC_CIRCLE_VERTICES          24

#define     PHYSAC_COLLISION_ITERATIONS     100
#define     PHYSAC_PENETRATION_ALLOWANCE    0.05f
#define     PHYSAC_PENETRATION_CORRECTION   0.4f

Physac contains defines for memory management functions (malloc, free) to bring the user the opportunity to implement its own memory functions:

c
#define     PHYSAC_MALLOC(size)             malloc(size)
#define     PHYSAC_FREE(ptr)                free(ptr)

The Physac API functions availables for the user are the following:

…

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

Dependencies

Physac uses the following C libraries for memory management, math operations and some debug features:

  • stdlib.h - Memory allocation [malloc(), free(), srand(), rand()].
  • stdio.h - Message logging (only if PHYSAC_DEBUG is defined) [printf()].
  • math.h - Math operations functions [cos(), sin(), fabs(), sqrtf()].

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().

By the way, I use raylib to create the examples. This videogames programming library is used to handle inputs, window management and graphics drawing (using OpenGL API).

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C2d2d-physics-enginecdynamics

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言