
Physac เป็นเครื่องยนต์ฟิสิกส์ 2D ขนาดเล็กที่เขียนด้วย Pure C. เครื่องยนต์ใช้ห่วงเธรดระยะเวลาคงที่เพื่อซิมฟิสิกส์ฟิสิกส์ ขั้นตอนฟิสิกส์มีขั้นตอนต่อไปนี้: รับข้อมูลการชนใช้พลวัตการแก้ปัญหาการชนและการแก้ไขตำแหน่ง มันใช้โครงสร้างที่ง่ายมากสำหรับร่างกายฟิสิกส์ที่มีเวกเตอร์ตำแหน่งที่จะใช้ใน API การเรนเดอร์ 3 มิติใด ๆ
ไฟล์ส่วนหัวมีค่ากำหนดค่าที่ปรับแต่งได้บางส่วนเพื่อให้พอดีกับผลลัพธ์ที่ผู้ใช้ต้องการด้วยผลลัพธ์ที่ไม่ดีน้อยที่สุด ค่าเหล่านั้นส่วนใหญ่มีความคิดเห็นพร้อมคำอธิบายเล็กน้อยเกี่ยวกับการใช้งานของพวกเขา
หมายเหตุ: รหัสตัวอย่างใช้ไลบรารีการเขียนโปรแกรม 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.
Struct Physicsbody มีข้อมูลการเปลี่ยนแปลงทั้งหมดและรูปร่างการชน ผู้ใช้ควรใช้ส่วนประกอบโครงสร้างต่อไปนี้:
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 );หมายเหตุ: Initphysics () จะต้องมีการเรียกที่โปรแกรมเริ่มต้นและ closephysics () ก่อนที่โปรแกรมจะสิ้นสุดลง การปิดและเริ่มต้น Physac ในระหว่างการไหลของโปรแกรมจะไม่ส่งผลกระทบหรือสร้างข้อผิดพลาดใด ๆ (มีประโยชน์เป็น 'รีเซ็ต' เพื่อทำลายร่างกายที่สร้างขึ้นโดยผู้ใช้ในรันไทม์)
Physac ใช้ไลบรารี C ต่อไปนี้สำหรับการจัดการหน่วยความจำการดำเนินงานทางคณิตศาสตร์และคุณสมบัติการดีบักบางอย่าง:
มันเป็นอิสระกับเอ็นจิ้นกราฟิกใด ๆ และเตรียมพร้อมที่จะใช้กราฟิก API ใด ๆ และใช้ข้อมูลจุดยอด (ดูตัวอย่างลอจิกวาด) เพื่อวาดเส้นหรือรูปร่างในหน้าจอ ตัวอย่างเช่นข้อมูลจุดยอดนี้สามารถใช้ใน OpenGL API glvertex2F ()
โดยวิธีการที่ฉันใช้ Raylib เพื่อสร้างตัวอย่าง ไลบรารีการเขียนโปรแกรมวิดีโอเกมนี้ใช้เพื่อจัดการอินพุตการจัดการหน้าต่างและการวาดกราฟิก (ใช้ OpenGL API)