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