|
| 1 | + |
| 2 | +#define PHYSAC_MAX_BODIES 64 // Maximum number of physic bodies supported |
| 3 | +#define PHYSAC_MAX_MANIFOLDS 4096 // Maximum number of physic bodies interactions (64x64) |
| 4 | +#define PHYSAC_MAX_VERTICES 24 // Maximum number of vertex for polygons shapes |
| 5 | +#define PHYSAC_DEFAULT_CIRCLE_VERTICES 24 // Default number of vertices for circle shapes |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +typedef enum PhysicsShapeType { PHYSICS_CIRCLE = 0, PHYSICS_POLYGON } PhysicsShapeType; |
| 11 | + |
| 12 | +// Previously defined to be used in PhysicsShape struct as circular dependencies |
| 13 | +typedef struct PhysicsBodyData *PhysicsBody; |
| 14 | + |
| 15 | + |
| 16 | +// Matrix2x2 type (used for polygon shape rotation matrix) |
| 17 | +typedef struct Matrix2x2 { |
| 18 | + float m00; |
| 19 | + float m01; |
| 20 | + float m10; |
| 21 | + float m11; |
| 22 | +} Matrix2x2; |
| 23 | + |
| 24 | +typedef struct PhysicsVertexData { |
| 25 | + unsigned int vertexCount; // Vertex count (positions and normals) |
| 26 | + Vector2 positions[PHYSAC_MAX_VERTICES]; // Vertex positions vectors |
| 27 | + Vector2 normals[PHYSAC_MAX_VERTICES]; // Vertex normals vectors |
| 28 | +} PhysicsVertexData; |
| 29 | + |
| 30 | +typedef struct PhysicsShape { |
| 31 | + PhysicsShapeType type; // Shape type (circle or polygon) |
| 32 | + PhysicsBody body; // Shape physics body data pointer |
| 33 | + PhysicsVertexData vertexData; // Shape vertices data (used for polygon shapes) |
| 34 | + float radius; // Shape radius (used for circle shapes) |
| 35 | + Matrix2x2 transform; // Vertices transform matrix 2x2 |
| 36 | +} PhysicsShape; |
| 37 | + |
| 38 | +typedef struct PhysicsBodyData { |
| 39 | + unsigned int id; // Unique identifier |
| 40 | + bool enabled; // Enabled dynamics state (collisions are calculated anyway) |
| 41 | + Vector2 position; // Physics body shape pivot |
| 42 | + Vector2 velocity; // Current linear velocity applied to position |
| 43 | + Vector2 force; // Current linear force (reset to 0 every step) |
| 44 | + float angularVelocity; // Current angular velocity applied to orient |
| 45 | + float torque; // Current angular force (reset to 0 every step) |
| 46 | + float orient; // Rotation in radians |
| 47 | + float inertia; // Moment of inertia |
| 48 | + float inverseInertia; // Inverse value of inertia |
| 49 | + float mass; // Physics body mass |
| 50 | + float inverseMass; // Inverse value of mass |
| 51 | + float staticFriction; // Friction when the body has not movement (0 to 1) |
| 52 | + float dynamicFriction; // Friction when the body has movement (0 to 1) |
| 53 | + float restitution; // Restitution coefficient of the body (0 to 1) |
| 54 | + bool useGravity; // Apply gravity force to dynamics |
| 55 | + bool isGrounded; // Physics grounded on other body state |
| 56 | + bool freezeOrient; // Physics rotation constraint |
| 57 | + PhysicsShape shape; // Physics body shape information (type, radius, vertices, transform) |
| 58 | +} PhysicsBodyData; |
| 59 | + |
| 60 | +typedef struct PhysicsManifoldData { |
| 61 | + unsigned int id; // Unique identifier |
| 62 | + PhysicsBody bodyA; // Manifold first physics body reference |
| 63 | + PhysicsBody bodyB; // Manifold second physics body reference |
| 64 | + float penetration; // Depth of penetration from collision |
| 65 | + Vector2 normal; // Normal direction vector from 'a' to 'b' |
| 66 | + Vector2 contacts[2]; // Points of contact during collision |
| 67 | + unsigned int contactsCount; // Current collision number of contacts |
| 68 | + float restitution; // Mixed restitution during collision |
| 69 | + float dynamicFriction; // Mixed dynamic friction during collision |
| 70 | + float staticFriction; // Mixed static friction during collision |
| 71 | +} PhysicsManifoldData, *PhysicsManifold; |
| 72 | + |
| 73 | + |
| 74 | +PHYSACDEF void InitPhysics(void); // Initializes physics system |
| 75 | +PHYSACDEF void UpdatePhysics(void); // Update physics system |
| 76 | +PHYSACDEF void ResetPhysics(void); // Reset physics system (global variables) |
| 77 | +PHYSACDEF void ClosePhysics(void); // Close physics system and unload used memory |
| 78 | +PHYSACDEF void SetPhysicsTimeStep(double delta); // Sets physics fixed time step in milliseconds. 1.666666 by default |
| 79 | +PHYSACDEF void SetPhysicsGravity(float x, float y); // Sets physics global gravity force |
| 80 | + |
| 81 | +// Physic body creation/destroy |
| 82 | +PHYSACDEF PhysicsBody CreatePhysicsBodyCircle(Vector2 pos, float radius, float density); // Creates a new circle physics body with generic parameters |
| 83 | +PHYSACDEF PhysicsBody CreatePhysicsBodyRectangle(Vector2 pos, float width, float height, float density); // Creates a new rectangle physics body with generic parameters |
| 84 | +PHYSACDEF PhysicsBody CreatePhysicsBodyPolygon(Vector2 pos, float radius, int sides, float density); // Creates a new polygon physics body with generic parameters |
| 85 | +PHYSACDEF void DestroyPhysicsBody(PhysicsBody body); // Destroy a physics body |
| 86 | + |
| 87 | +// Physic body forces |
| 88 | +PHYSACDEF void PhysicsAddForce(PhysicsBody body, Vector2 force); // Adds a force to a physics body |
| 89 | +PHYSACDEF void PhysicsAddTorque(PhysicsBody body, float amount); // Adds an angular force to a physics body |
| 90 | +PHYSACDEF void PhysicsShatter(PhysicsBody body, Vector2 position, float force); // Shatters a polygon shape physics body to little physics bodies with explosion force |
| 91 | +PHYSACDEF void SetPhysicsBodyRotation(PhysicsBody body, float radians); // Sets physics body shape transform based on radians parameter |
| 92 | + |
| 93 | +// Query physics info |
| 94 | +PHYSACDEF PhysicsBody GetPhysicsBody(int index); // Returns a physics body of the bodies pool at a specific index |
| 95 | +PHYSACDEF int GetPhysicsBodiesCount(void); // Returns the current amount of created physics bodies |
| 96 | +PHYSACDEF int GetPhysicsShapeType(int index); // Returns the physics body shape type (PHYSICS_CIRCLE or PHYSICS_POLYGON) |
| 97 | +PHYSACDEF int GetPhysicsShapeVerticesCount(int index); // Returns the amount of vertices of a physics body shape |
| 98 | +PHYSACDEF Vector2 GetPhysicsShapeVertex(PhysicsBody body, int vertex); // Returns transformed position of a body shape (body position + vertex transformed position) |
0 commit comments