
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, 무료)을 정의하여 사용자에게 자체 메모리 기능을 구현할 수있는 기회를 제공합니다.
#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 사용)을 처리하는 데 사용됩니다.