
Physac ist eine kleine 2D-Physik-Engine, die in reinem C geschrieben wurde. Der Motor verwendet eine feste Zeitschritt-Thread-Schleife, um die Physik zu simluieren. Ein Physikschritt enthält die folgenden Phasen: Kollisionsinformationen abrufen, Dynamik, Kollisionslösung und Positionskorrektur anwenden. Es verwendet eine sehr einfache Struktur für physische Körper mit einem Positionsvektor, der in einer 3D -Rendering -API verwendet wird.
Die Header -Datei enthält einige optimierbare Definition von Werten für die Ergebnisse, die der Benutzer mit minimalen schlechten Ergebnissen will. Die meisten dieser Werte werden mit einer kleinen Erklärung zu ihren Verwendungen kommentiert.
HINWEIS: Der Beispielcode verwendet die Raylib -Programmierbibliothek, um das Programmfenster und das Rendering -Framework zu erstellen.
Physac benötigt Raylib. Um es zu bekommen, befolgen Sie die nächsten Schritte:
* 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.
Die Physicsbody Struct enthält alle Dynamikinformationen und Kollisionsform. Der Benutzer sollte die folgenden Strukturkomponenten verwenden:
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 ;Der Header enthält einige anpassbare Definierwerte. Ich habe die Werte festgelegt, die mir die besten Ergebnisse versteckten.
#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 enthält Define für Speicherverwaltungsfunktionen (MALCOC, kostenlos), um dem Benutzer die Möglichkeit zu geben, seine eigenen Speicherfunktionen zu implementieren:
#define PHYSAC_MALLOC ( size ) malloc(size)
#define PHYSAC_FREE ( ptr ) free(ptr)Die für den Benutzer verfügbaren Physac -API -Funktionen sind die folgenden:
// 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 );HINWEIS: Initphysics () muss vor dem Ende des Programms bei Programmstart und Conephysics () aufgerufen werden. Das Schließen und Initialisieren von PhysAC während des Programmflusses beeinflusst oder erzeugt keinen Fehler (nützlich als "Reset", um einen erstellten Körper durch den Benutzer in der Laufzeit zu zerstören).
PhysAC verwendet die folgenden C -Bibliotheken für Speicherverwaltung, Mathematikoperationen und einige Debug -Funktionen:
Es ist unabhängig von jeder Grafik -Engine und ist bereit, jede Grafik -API zu verwenden und die Eckpunktinformationen (Beispiele für die Zeichnung der Logik anzusehen), um Linien oder Formen im Bildschirm zu zeichnen. Beispielsweise können diese Scheitelpunktinformationen in OpenGL API Glvertex2f () verwendet werden.
Übrigens verwende ich Raylib, um die Beispiele zu erstellen. Diese Videospiel -Programmierbibliothek wird verwendet, um Eingänge, Fensterverwaltung und Grafikzeichnung (mithilfe von OpenGL -API) zu verarbeiten.