Physac
version with raylib 2.5

Physac是用PureC編寫的小型2D物理髮動機。發動機使用固定的時步線圈來模擬物理。物理步驟包含以下階段:獲取碰撞信息,應用動態,碰撞解決和位置校正。它使用一個非常簡單的結構,用於在任何3D渲染API中使用位置向量的物理體。
標題文件包含一些可調整的定義值,以適合用戶想要的結果,而結果最小的結果。這些價值中的大多數都對其用途進行了一些解釋。
注意:示例代碼使用Raylib編程庫創建程序窗口和渲染框架。
Physac需要Raylib。要獲得它,請按照下一步:
* 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.
物理結構包含所有動態信息和碰撞形狀。用戶應使用以下結構組件:
typedef struct * PhysicsBody {
unsigned int id ;
bool enabled ; // Enabled dynamics state (collisions are calculated anyway)
Vector2 position ; // Physics body shape pivot
Vector2 velocity ; // Current linear velocity applied to position
Vector2 force ; // Current linear force (reset to 0 every step)
float angularVelocity ; // Current angular velocity applied to orient
float torque ; // Current angular force (reset to 0 every step)
float orient ; // Rotation in radians
float staticFriction ; // Friction when the body has not movement (0 to 1)
float dynamicFriction ; // Friction when the body has movement (0 to 1)
float restitution ; // Restitution coefficient of the body (0 to 1)
bool useGravity ; // Apply gravity force to dynamics
bool isGrounded ; // Physics grounded on other body state
bool freezeOrient ; // Physics rotation constraint
PhysicsShape shape ; // Physics body shape information (type, radius, vertices, normals)
} * PhysicsBody ;標頭包含一些可自定義的定義值。我設置了使我最佳結果的價值觀。
#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.4fPhysac包含用於內存管理功能的定義(Malloc,免費),使用戶有機會實施自己的內存功能:
#define PHYSAC_MALLOC ( size ) malloc(size)
#define PHYSAC_FREE ( ptr ) free(ptr)用戶的Physac API功能可用如下:
// Initializes physics values, pointers and creates physics loop thread
void InitPhysics ( void );
// Returns true if physics thread is currently enabled
bool IsPhysicsEnabled ( void );
// Sets physics global gravity force
void SetPhysicsGravity ( float x , float y );
// Creates a new circle physics body with generic parameters
PhysicsBody CreatePhysicsBodyCircle ( Vector2 pos , float radius , float density );
// Creates a new rectangle physics body with generic parameters
PhysicsBody CreatePhysicsBodyRectangle ( Vector2 pos , float width , float height , float density );
// Creates a new polygon physics body with generic parameters
PhysicsBody CreatePhysicsBodyPolygon ( Vector2 pos , float radius , int sides , float density );
// Adds a force to a physics body
void PhysicsAddForce ( PhysicsBody body , Vector2 force );
// Adds a angular force to a physics body
void PhysicsAddTorque ( PhysicsBody body , float amount );
// Shatters a polygon shape physics body to little physics bodies with explosion force
void PhysicsShatter ( PhysicsBody body , Vector2 position , float force );
// Returns the current amount of created physics bodies
int GetPhysicsBodiesCount ( void );
// Returns a physics body of the bodies pool at a specific index
PhysicsBody GetPhysicsBody ( int index );
// Returns the physics body shape type (PHYSICS_CIRCLE or PHYSICS_POLYGON)
int GetPhysicsShapeType ( int index );
// Returns the amount of vertices of a physics body shape
int GetPhysicsShapeVerticesCount ( int index );
// Returns transformed position of a body shape (body position + vertex transformed position)
Vector2 GetPhysicsShapeVertex ( PhysicsBody body , int vertex );
// Sets physics body shape transform based on radians parameter
void SetPhysicsBodyRotation ( PhysicsBody body , float radians );
// Unitializes and destroy a physics body
void DestroyPhysicsBody ( PhysicsBody body );
// Unitializes physics pointers and closes physics loop thread
void ClosePhysics ( void );注意:在程序結束之前,需要在程序開始和closephysics()時調用initphysics()。在程序流期間關閉和初始化Physac不會影響或產生任何錯誤(可作為“重置”來破壞運行時用戶創建的任何身體的“重置”)。
Physac使用以下C庫進行內存管理,數學操作和一些調試功能:
它獨立於任何圖形引擎,並準備使用任何圖形API並使用頂點信息(查看示例繪製邏輯)在屏幕上繪製線路或形狀。例如,該頂點信息可用於OpenGL API GLVERTEX2F()。
順便說一句,我使用Raylib創建示例。此視頻遊戲編程庫用於處理輸入,窗口管理和圖形繪圖(使用OpenGL API)。