
PhysACは、純粋なCで書かれた小さな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.
Physicsbody structには、すべてのダイナミクス情報と衝突形状が含まれています。ユーザーは、次の構造コンポーネントを使用する必要があります。
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、free)の定義が含まれ、ユーザーに独自のメモリ関数を実装する機会をもたらします。
#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 );注:initphysics()は、プログラムが終了する前に、プログラム開始およびclosephysics()で呼び出す必要があります。プログラムフロー中にPhysACを閉じて初期化することは、エラーに影響を与えたり、エラーを生成したりしません(実行時にユーザーが作成したボディを破壊するために「リセット」として役立ちます)。
Physacは、メモリ管理、数学操作、いくつかのデバッグ機能に次のCライブラリを使用します。
グラフィックエンジンには独立しており、グラフィックスAPIを使用し、頂点情報を使用して(ロジックを描画する例を見てください)、画面にラインまたは形状を描画する準備ができています。たとえば、この頂点情報は、OpenGL API glvertex2f()で使用できます。
ちなみに、Raylibを使用して例を作成します。このビデオゲームプログラミングライブラリは、入力、ウィンドウ管理、グラフィックスの描画を処理するために使用されます(OpenGL APIを使用)。