From 67e6bf240522dc32d28351f5cb16344c4ba7d32a Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sat, 3 May 2025 19:40:15 +0100 Subject: [PATCH] add @victorfisac version of Physac --- .gitmodules | 3 + dynamic/raylib/__init__.pyi | 40 +++++----- dynamic/raylib/defines.py | 6 +- physac | 1 + pyray/__init__.pyi | 44 +++++------ raylib/__init__.pyi | 40 +++++----- raylib/defines.py | 6 +- raylib/physac.h.modified | 142 +++++++++++++++++++----------------- 8 files changed, 144 insertions(+), 138 deletions(-) create mode 160000 physac diff --git a/.gitmodules b/.gitmodules index 66ed17d..fd4dc26 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "raygui"] path = raygui url = https://github.com/raysan5/raygui.git +[submodule "physac"] + path = physac + url = https://github.com/victorfisac/Physac diff --git a/dynamic/raylib/__init__.pyi b/dynamic/raylib/__init__.pyi index fcfc1df..cdc6ce8 100644 --- a/dynamic/raylib/__init__.pyi +++ b/dynamic/raylib/__init__.pyi @@ -135,7 +135,7 @@ def CloseAudioDevice() -> None: ... @deprecated("Raylib no longer recommends the use of Physac library") def ClosePhysics() -> None: - """Close physics system and unload used memory.""" + """Unitializes physics pointers and closes physics loop thread.""" ... def CloseWindow() -> None: """Close window and unload OpenGL context.""" @@ -216,7 +216,7 @@ def DecompressData(compData: bytes,compDataSize: int,dataSize: Any,) -> bytes: ... @deprecated("Raylib no longer recommends the use of Physac library") def DestroyPhysicsBody(body: Any|list|tuple,) -> None: - """Destroy a physics body.""" + """Unitializes and destroy a physics body.""" ... def DetachAudioMixedProcessor(processor: Any,) -> None: """Detach audio stream processor from the entire audio pipeline.""" @@ -1605,7 +1605,7 @@ def InitAudioDevice() -> None: ... @deprecated("Raylib no longer recommends the use of Physac library") def InitPhysics() -> None: - """Initializes physics system.""" + """Initializes physics values, pointers and creates physics loop thread.""" ... def InitWindow(width: int,height: int,title: bytes,) -> None: """Initialize window and OpenGL context.""" @@ -1706,6 +1706,10 @@ def IsMusicValid(music: Music|list|tuple,) -> bool: def IsPathFile(path: bytes,) -> bool: """Check if a given path is a file or a directory.""" ... +@deprecated("Raylib no longer recommends the use of Physac library") +def IsPhysicsEnabled() -> bool: + """Returns true if physics thread is currently enabled.""" + ... def IsRenderTextureValid(target: RenderTexture|list|tuple,) -> bool: """Check if a render texture is valid (loaded in GPU).""" ... @@ -2389,10 +2393,6 @@ RL_TEXTURE_FILTER_TRILINEAR: int def Remap(value: float,inputStart: float,inputEnd: float,outputStart: float,outputEnd: float,) -> float: """.""" ... -@deprecated("Raylib no longer recommends the use of Physac library") -def ResetPhysics() -> None: - """Reset physics system (global variables).""" - ... def RestoreWindow() -> None: """Set window state: not minimized/maximized.""" ... @@ -2405,6 +2405,10 @@ def ResumeMusicStream(music: Music|list|tuple,) -> None: def ResumeSound(sound: Sound|list|tuple,) -> None: """Resume a paused sound.""" ... +@deprecated("Raylib no longer recommends the use of Physac library") +def RunPhysicsStep() -> None: + """Run physics step, to be used if PHYSICS_NO_THREADS is set in your main loop.""" + ... SCROLLBAR: int SCROLLBAR_SIDE: int SCROLLBAR_WIDTH: int @@ -2873,10 +2877,6 @@ def UpdateModelAnimationBones(model: Model|list|tuple,anim: ModelAnimation|list| def UpdateMusicStream(music: Music|list|tuple,) -> None: """Updates buffers for music streaming.""" ... -@deprecated("Raylib no longer recommends the use of Physac library") -def UpdatePhysics() -> None: - """Update physics system.""" - ... def UpdateSound(sound: Sound|list|tuple,data: Any,sampleCount: int,) -> None: """Update sound buffer with new data.""" ... @@ -4131,6 +4131,11 @@ class Image: mipmaps: int format: int KeyboardKey = int +class Mat2: + m00: float + m01: float + m10: float + m11: float class Material: shader: Shader maps: Any @@ -4157,11 +4162,6 @@ class Matrix: m7: float m11: float m15: float -class Matrix2x2: - m00: float - m01: float - m10: float - m11: float class Mesh: vertexCount: int triangleCount: int @@ -4246,15 +4246,15 @@ class PhysicsManifoldData: class PhysicsShape: type: PhysicsShapeType body: Any - vertexData: PhysicsVertexData radius: float - transform: Matrix2x2 + transform: Mat2 + vertexData: PolygonData PhysicsShapeType = int -class PhysicsVertexData: +PixelFormat = int +class PolygonData: vertexCount: int positions: list normals: list -PixelFormat = int class Quaternion: x: float y: float diff --git a/dynamic/raylib/defines.py b/dynamic/raylib/defines.py index a7e9902..fe706d3 100644 --- a/dynamic/raylib/defines.py +++ b/dynamic/raylib/defines.py @@ -186,12 +186,10 @@ RAYGUI_TEXTFORMAT_MAX_SIZE: int = 256 PHYSAC_MAX_BODIES: int = 64 PHYSAC_MAX_MANIFOLDS: int = 4096 PHYSAC_MAX_VERTICES: int = 24 -PHYSAC_DEFAULT_CIRCLE_VERTICES: int = 24 -PHYSAC_COLLISION_ITERATIONS: int = 100 +PHYSAC_CIRCLE_VERTICES: int = 24 +PHYSAC_COLLISION_ITERATIONS: int = 20 PHYSAC_PENETRATION_ALLOWANCE: float = 0.05 PHYSAC_PENETRATION_CORRECTION: float = 0.4 -PHYSAC_PI: float = 3.141592653589793 -PHYSAC_DEG2RAD = PHYSAC_PI / 180.0 PHYSAC_FLT_MAX: float = 3.402823466e+38 PHYSAC_EPSILON: float = 1e-06 GLFW_VERSION_MAJOR: int = 3 diff --git a/physac b/physac new file mode 160000 index 0000000..587b639 --- /dev/null +++ b/physac @@ -0,0 +1 @@ +Subproject commit 587b63926010593eedf29ef74e3aa22c1a507925 diff --git a/pyray/__init__.pyi b/pyray/__init__.pyi index 417129c..790a032 100644 --- a/pyray/__init__.pyi +++ b/pyray/__init__.pyi @@ -995,7 +995,7 @@ def close_audio_device() -> None: ... @deprecated("Raylib no longer recommends the use of Physac library") def close_physics() -> None: - """Close physics system and unload used memory.""" + """Unitializes physics pointers and closes physics loop thread.""" ... def close_window() -> None: """Close window and unload OpenGL context.""" @@ -1071,7 +1071,7 @@ def decompress_data(compData: str,compDataSize: int,dataSize: Any,) -> str: ... @deprecated("Raylib no longer recommends the use of Physac library") def destroy_physics_body(body: Any|list|tuple,) -> None: - """Destroy a physics body.""" + """Unitializes and destroy a physics body.""" ... def detach_audio_mixed_processor(processor: Any,) -> None: """Detach audio stream processor from the entire audio pipeline.""" @@ -2145,7 +2145,7 @@ def init_audio_device() -> None: ... @deprecated("Raylib no longer recommends the use of Physac library") def init_physics() -> None: - """Initializes physics system.""" + """Initializes physics values, pointers and creates physics loop thread.""" ... def init_window(width: int,height: int,title: str,) -> None: """Initialize window and OpenGL context.""" @@ -2246,6 +2246,10 @@ def is_music_valid(music: Music|list|tuple,) -> bool: def is_path_file(path: str,) -> bool: """Check if a given path is a file or a directory.""" ... +@deprecated("Raylib no longer recommends the use of Physac library") +def is_physics_enabled() -> bool: + """Returns true if physics thread is currently enabled.""" + ... def is_render_texture_valid(target: RenderTexture|list|tuple,) -> bool: """Check if a render texture is valid (loaded in GPU).""" ... @@ -2630,10 +2634,6 @@ def quaternion_transform(q: Vector4|list|tuple,mat: Matrix|list|tuple,) -> Vecto def remap(value: float,inputStart: float,inputEnd: float,outputStart: float,outputEnd: float,) -> float: """.""" ... -@deprecated("Raylib no longer recommends the use of Physac library") -def reset_physics() -> None: - """Reset physics system (global variables).""" - ... def restore_window() -> None: """Set window state: not minimized/maximized.""" ... @@ -2646,6 +2646,10 @@ def resume_music_stream(music: Music|list|tuple,) -> None: def resume_sound(sound: Sound|list|tuple,) -> None: """Resume a paused sound.""" ... +@deprecated("Raylib no longer recommends the use of Physac library") +def run_physics_step() -> None: + """Run physics step, to be used if PHYSICS_NO_THREADS is set in your main loop.""" + ... def save_file_data(fileName: str,data: Any,dataSize: int,) -> bool: """Save data to file from byte array (write), returns true on success.""" ... @@ -3021,10 +3025,6 @@ def update_model_animation_bones(model: Model|list|tuple,anim: ModelAnimation|li def update_music_stream(music: Music|list|tuple,) -> None: """Updates buffers for music streaming.""" ... -@deprecated("Raylib no longer recommends the use of Physac library") -def update_physics() -> None: - """Update physics system.""" - ... def update_sound(sound: Sound|list|tuple,data: Any,sampleCount: int,) -> None: """Update sound buffer with new data.""" ... @@ -4239,6 +4239,13 @@ class Image: self.height:int = height # type: ignore self.mipmaps:int = mipmaps # type: ignore self.format:int = format # type: ignore +class Mat2: + """Mat2 type (used for polygon shape rotation matrix).""" + def __init__(self, m00: float|None = None, m01: float|None = None, m10: float|None = None, m11: float|None = None): + self.m00:float = m00 # type: ignore + self.m01:float = m01 # type: ignore + self.m10:float = m10 # type: ignore + self.m11:float = m11 # type: ignore class Material: """Material, includes shader and maps.""" def __init__(self, shader: Shader|list|tuple|None = None, maps: Any|None = None, params: list|None = None): @@ -4270,13 +4277,6 @@ class Matrix: self.m7:float = m7 # type: ignore self.m11:float = m11 # type: ignore self.m15:float = m15 # type: ignore -class Matrix2x2: - """Matrix2x2 type (used for polygon shape rotation matrix).""" - def __init__(self, m00: float|None = None, m01: float|None = None, m10: float|None = None, m11: float|None = None): - self.m00:float = m00 # type: ignore - self.m01:float = m01 # type: ignore - self.m10:float = m10 # type: ignore - self.m11:float = m11 # type: ignore class Mesh: """Mesh, vertex data and vao/vbo.""" def __init__(self, vertexCount: int|None = None, triangleCount: int|None = None, vertices: Any|None = None, texcoords: Any|None = None, texcoords2: Any|None = None, normals: Any|None = None, tangents: Any|None = None, colors: str|None = None, indices: Any|None = None, animVertices: Any|None = None, animNormals: Any|None = None, boneIds: str|None = None, boneWeights: Any|None = None, boneMatrices: Any|None = None, boneCount: int|None = None, vaoId: int|None = None, vboId: Any|None = None): @@ -4371,13 +4371,13 @@ class PhysicsManifoldData: self.staticFriction:float = staticFriction # type: ignore class PhysicsShape: """.""" - def __init__(self, type: PhysicsShapeType|None = None, body: Any|None = None, vertexData: PhysicsVertexData|list|tuple|None = None, radius: float|None = None, transform: Matrix2x2|list|tuple|None = None): + def __init__(self, type: PhysicsShapeType|None = None, body: Any|None = None, radius: float|None = None, transform: Mat2|list|tuple|None = None, vertexData: PolygonData|list|tuple|None = None): self.type:PhysicsShapeType = type # type: ignore self.body:Any = body # type: ignore - self.vertexData:PhysicsVertexData = vertexData # type: ignore self.radius:float = radius # type: ignore - self.transform:Matrix2x2 = transform # type: ignore -class PhysicsVertexData: + self.transform:Mat2 = transform # type: ignore + self.vertexData:PolygonData = vertexData # type: ignore +class PolygonData: """.""" def __init__(self, vertexCount: int|None = None, positions: list|None = None, normals: list|None = None): self.vertexCount:int = vertexCount # type: ignore diff --git a/raylib/__init__.pyi b/raylib/__init__.pyi index fcfc1df..cdc6ce8 100644 --- a/raylib/__init__.pyi +++ b/raylib/__init__.pyi @@ -135,7 +135,7 @@ def CloseAudioDevice() -> None: ... @deprecated("Raylib no longer recommends the use of Physac library") def ClosePhysics() -> None: - """Close physics system and unload used memory.""" + """Unitializes physics pointers and closes physics loop thread.""" ... def CloseWindow() -> None: """Close window and unload OpenGL context.""" @@ -216,7 +216,7 @@ def DecompressData(compData: bytes,compDataSize: int,dataSize: Any,) -> bytes: ... @deprecated("Raylib no longer recommends the use of Physac library") def DestroyPhysicsBody(body: Any|list|tuple,) -> None: - """Destroy a physics body.""" + """Unitializes and destroy a physics body.""" ... def DetachAudioMixedProcessor(processor: Any,) -> None: """Detach audio stream processor from the entire audio pipeline.""" @@ -1605,7 +1605,7 @@ def InitAudioDevice() -> None: ... @deprecated("Raylib no longer recommends the use of Physac library") def InitPhysics() -> None: - """Initializes physics system.""" + """Initializes physics values, pointers and creates physics loop thread.""" ... def InitWindow(width: int,height: int,title: bytes,) -> None: """Initialize window and OpenGL context.""" @@ -1706,6 +1706,10 @@ def IsMusicValid(music: Music|list|tuple,) -> bool: def IsPathFile(path: bytes,) -> bool: """Check if a given path is a file or a directory.""" ... +@deprecated("Raylib no longer recommends the use of Physac library") +def IsPhysicsEnabled() -> bool: + """Returns true if physics thread is currently enabled.""" + ... def IsRenderTextureValid(target: RenderTexture|list|tuple,) -> bool: """Check if a render texture is valid (loaded in GPU).""" ... @@ -2389,10 +2393,6 @@ RL_TEXTURE_FILTER_TRILINEAR: int def Remap(value: float,inputStart: float,inputEnd: float,outputStart: float,outputEnd: float,) -> float: """.""" ... -@deprecated("Raylib no longer recommends the use of Physac library") -def ResetPhysics() -> None: - """Reset physics system (global variables).""" - ... def RestoreWindow() -> None: """Set window state: not minimized/maximized.""" ... @@ -2405,6 +2405,10 @@ def ResumeMusicStream(music: Music|list|tuple,) -> None: def ResumeSound(sound: Sound|list|tuple,) -> None: """Resume a paused sound.""" ... +@deprecated("Raylib no longer recommends the use of Physac library") +def RunPhysicsStep() -> None: + """Run physics step, to be used if PHYSICS_NO_THREADS is set in your main loop.""" + ... SCROLLBAR: int SCROLLBAR_SIDE: int SCROLLBAR_WIDTH: int @@ -2873,10 +2877,6 @@ def UpdateModelAnimationBones(model: Model|list|tuple,anim: ModelAnimation|list| def UpdateMusicStream(music: Music|list|tuple,) -> None: """Updates buffers for music streaming.""" ... -@deprecated("Raylib no longer recommends the use of Physac library") -def UpdatePhysics() -> None: - """Update physics system.""" - ... def UpdateSound(sound: Sound|list|tuple,data: Any,sampleCount: int,) -> None: """Update sound buffer with new data.""" ... @@ -4131,6 +4131,11 @@ class Image: mipmaps: int format: int KeyboardKey = int +class Mat2: + m00: float + m01: float + m10: float + m11: float class Material: shader: Shader maps: Any @@ -4157,11 +4162,6 @@ class Matrix: m7: float m11: float m15: float -class Matrix2x2: - m00: float - m01: float - m10: float - m11: float class Mesh: vertexCount: int triangleCount: int @@ -4246,15 +4246,15 @@ class PhysicsManifoldData: class PhysicsShape: type: PhysicsShapeType body: Any - vertexData: PhysicsVertexData radius: float - transform: Matrix2x2 + transform: Mat2 + vertexData: PolygonData PhysicsShapeType = int -class PhysicsVertexData: +PixelFormat = int +class PolygonData: vertexCount: int positions: list normals: list -PixelFormat = int class Quaternion: x: float y: float diff --git a/raylib/defines.py b/raylib/defines.py index a7e9902..fe706d3 100644 --- a/raylib/defines.py +++ b/raylib/defines.py @@ -186,12 +186,10 @@ RAYGUI_TEXTFORMAT_MAX_SIZE: int = 256 PHYSAC_MAX_BODIES: int = 64 PHYSAC_MAX_MANIFOLDS: int = 4096 PHYSAC_MAX_VERTICES: int = 24 -PHYSAC_DEFAULT_CIRCLE_VERTICES: int = 24 -PHYSAC_COLLISION_ITERATIONS: int = 100 +PHYSAC_CIRCLE_VERTICES: int = 24 +PHYSAC_COLLISION_ITERATIONS: int = 20 PHYSAC_PENETRATION_ALLOWANCE: float = 0.05 PHYSAC_PENETRATION_CORRECTION: float = 0.4 -PHYSAC_PI: float = 3.141592653589793 -PHYSAC_DEG2RAD = PHYSAC_PI / 180.0 PHYSAC_FLT_MAX: float = 3.402823466e+38 PHYSAC_EPSILON: float = 1e-06 GLFW_VERSION_MAJOR: int = 3 diff --git a/raylib/physac.h.modified b/raylib/physac.h.modified index fa35497..a17015d 100644 --- a/raylib/physac.h.modified +++ b/raylib/physac.h.modified @@ -4,8 +4,8 @@ * * DESCRIPTION: * -* Physac is a small 2D physics engine written in pure C. The engine uses a fixed time-step thread loop -* to simulate physics. A physics step contains the following phases: get collision information, +* Physac is a small 2D physics library written in pure C. The engine uses a fixed time-step thread loop +* to simluate physics. A physics step contains the following phases: get collision information, * apply dynamics, collision solving and position correction. It uses a very simple struct for physic * bodies with a position vector to be used in any 3D rendering API. * @@ -16,41 +16,49 @@ * If not defined, the library is in header only mode and can be included in other headers * or source files without problems. But only ONE file should hold the implementation. * -* #define PHYSAC_DEBUG -* Show debug traces log messages about physic bodies creation/destruction, physic system errors, -* some calculations results and NULL reference exceptions. +* #define PHYSAC_STATIC (defined by default) +* The generated implementation will stay private inside implementation file and all +* internal symbols and functions will only be visible inside that file. * -* #define PHYSAC_AVOID_TIMMING_SYSTEM -* Disables internal timming system, used by UpdatePhysics() to launch timmed physic steps, -* it allows just running UpdatePhysics() automatically on a separate thread at a desired time step. -* In case physics steps update needs to be controlled by user with a custom timming mechanism, -* just define this flag and the internal timming mechanism will be avoided, in that case, -* timming libraries are neither required by the module. +* #define PHYSAC_NO_THREADS +* The generated implementation won't include pthread library and user must create a secondary thread to call PhysicsThread(). +* It is so important that the thread where PhysicsThread() is called must not have v-sync or any other CPU limitation. +* +* #define PHYSAC_STANDALONE +* Avoid raylib.h header inclusion in this file. Data types defined on raylib are defined +* internally in the library and input management and drawing functions must be provided by +* the user (check library implementation for further details). +* +* #define PHYSAC_DEBUG +* Traces log messages when creating and destroying physics bodies and detects errors in physics +* calculations and reference exceptions; it is useful for debug purposes * * #define PHYSAC_MALLOC() -* #define PHYSAC_CALLOC() * #define PHYSAC_FREE() * You can define your own malloc/free implementation replacing stdlib.h malloc()/free() functions. * Otherwise it will include stdlib.h and use the C standard library malloc()/free() function. * -* COMPILATION: * -* Use the following code to compile with GCC: -* gcc -o $(NAME_PART).exe $(FILE_NAME) -s -static -lraylib -lopengl32 -lgdi32 -lwinmm -std=c99 +* NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations. +* NOTE 2: Physac requires static C library linkage to avoid dependency on MinGW DLL (-static -lpthread) * -* VERSIONS HISTORY: -* 1.1 (20-Jan-2021) @raysan5: Library general revision -* Removed threading system (up to the user) -* Support MSVC C++ compilation using CLITERAL() -* Review DEBUG mechanism for TRACELOG() and all TRACELOG() messages -* Review internal variables/functions naming for consistency -* Allow option to avoid internal timming system, to allow app manage the steps -* 1.0 (12-Jun-2017) First release of the library +* Use the following code to compile: +* gcc -o $(NAME_PART).exe $(FILE_NAME) -s -static -lraylib -lpthread -lopengl32 -lgdi32 -lwinmm -std=c99 +* +* VERY THANKS TO: +* - raysan5: helped with library design +* - ficoos: added support for Linux +* - R8D8: added support for Linux +* - jubalh: fixed implementation of time calculations +* - a3f: fixed implementation of time calculations +* - define-private-public: added support for OSX +* - pamarcos: fixed implementation of physics steps +* - noshbar: fixed some memory leaks * * * LICENSE: zlib/libpng * -* Copyright (c) 2016-2022 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5) +* Copyright (c) 2016-2025 Victor Fisac (github: @victorfisac) * * This software is provided "as-is", without any express or implied warranty. In no event * will the authors be held liable for any damages arising from the use of this software. @@ -68,39 +76,41 @@ * 3. This notice may not be removed or altered from any source distribution. * **********************************************************************************************/ -// Function specifiers in case library is build/used as a shared library (Windows) -// NOTE: Microsoft specifiers to tell compiler that symbols are imported/exported from a .dll -// Allow custom memory allocators +// #define PHYSAC_STATIC +// #define PHYSAC_NO_THREADS +// #define PHYSAC_STANDALONE +// #define PHYSAC_DEBUG //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- -// Data Types Structure Definition +// Types and Structures Definition +// NOTE: Below types are required for PHYSAC_STANDALONE usage //---------------------------------------------------------------------------------- -typedef enum PhysicsShapeType { PHYSICS_CIRCLE = 0, PHYSICS_POLYGON } PhysicsShapeType; +typedef enum PhysicsShapeType { PHYSICS_CIRCLE, PHYSICS_POLYGON } PhysicsShapeType; // Previously defined to be used in PhysicsShape struct as circular dependencies typedef struct PhysicsBodyData *PhysicsBody; -// Matrix2x2 type (used for polygon shape rotation matrix) -typedef struct Matrix2x2 { +// Mat2 type (used for polygon shape rotation matrix) +typedef struct Mat2 { float m00; float m01; float m10; float m11; -} Matrix2x2; -typedef struct PhysicsVertexData { - unsigned int vertexCount; // Vertex count (positions and normals) - Vector2 positions[24 /* Maximum number of vertex for polygons shapes*/]; // Vertex positions vectors - Vector2 normals[24 /* Maximum number of vertex for polygons shapes*/]; // Vertex normals vectors -} PhysicsVertexData; +} Mat2; +typedef struct PolygonData { + unsigned int vertexCount; // Current used vertex and normals count + Vector2 positions[24]; // Polygon vertex positions vectors + Vector2 normals[24]; // Polygon vertex normals vectors +} PolygonData; typedef struct PhysicsShape { - PhysicsShapeType type; // Shape type (circle or polygon) - PhysicsBody body; // Shape physics body data pointer - PhysicsVertexData vertexData; // Shape vertices data (used for polygon shapes) - float radius; // Shape radius (used for circle shapes) - Matrix2x2 transform; // Vertices transform matrix 2x2 + PhysicsShapeType type; // Physics shape type (circle or polygon) + PhysicsBody body; // Shape physics body reference + float radius; // Circle shape radius (used for circle shapes) + Mat2 transform; // Vertices transform matrix 2x2 + PolygonData vertexData; // Polygon shape vertices position and normals data (just used for polygon shapes) } PhysicsShape; typedef struct PhysicsBodyData { - unsigned int id; // Unique identifier + unsigned int id; // Reference unique identifier bool enabled; // Enabled dynamics state (collisions are calculated anyway) Vector2 position; // Physics body shape pivot Vector2 velocity; // Current linear velocity applied to position @@ -118,10 +128,10 @@ typedef struct PhysicsBodyData { 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, transform) + PhysicsShape shape; // Physics body shape information (type, radius, vertices, normals) } PhysicsBodyData; typedef struct PhysicsManifoldData { - unsigned int id; // Unique identifier + unsigned int id; // Reference unique identifier PhysicsBody bodyA; // Manifold first physics body reference PhysicsBody bodyB; // Manifold second physics body reference float penetration; // Depth of penetration from collision @@ -135,29 +145,25 @@ typedef struct PhysicsManifoldData { //---------------------------------------------------------------------------------- // Module Functions Declaration //---------------------------------------------------------------------------------- -// Physics system management - void InitPhysics(void); // Initializes physics system - void UpdatePhysics(void); // Update physics system - void ResetPhysics(void); // Reset physics system (global variables) - void ClosePhysics(void); // Close physics system and unload used memory - void SetPhysicsTimeStep(double delta); // Sets physics fixed time step in milliseconds. 1.666666 by default - void SetPhysicsGravity(float x, float y); // Sets physics global gravity force -// Physic body creation/destroy - PhysicsBody CreatePhysicsBodyCircle(Vector2 pos, float radius, float density); // Creates a new circle physics body with generic parameters - PhysicsBody CreatePhysicsBodyRectangle(Vector2 pos, float width, float height, float density); // Creates a new rectangle physics body with generic parameters - PhysicsBody CreatePhysicsBodyPolygon(Vector2 pos, float radius, int sides, float density); // Creates a new polygon physics body with generic parameters - void DestroyPhysicsBody(PhysicsBody body); // Destroy a physics body -// Physic body forces - void PhysicsAddForce(PhysicsBody body, Vector2 force); // Adds a force to a physics body - void PhysicsAddTorque(PhysicsBody body, float amount); // Adds an angular force to a physics body - void PhysicsShatter(PhysicsBody body, Vector2 position, float force); // Shatters a polygon shape physics body to little physics bodies with explosion force - void SetPhysicsBodyRotation(PhysicsBody body, float radians); // Sets physics body shape transform based on radians parameter -// Query physics info - PhysicsBody GetPhysicsBody(int index); // Returns a physics body of the bodies pool at a specific index - int GetPhysicsBodiesCount(void); // Returns the current amount of created physics bodies - int GetPhysicsShapeType(int index); // Returns the physics body shape type (PHYSICS_CIRCLE or PHYSICS_POLYGON) - int GetPhysicsShapeVerticesCount(int index); // Returns the amount of vertices of a physics body shape - Vector2 GetPhysicsShapeVertex(PhysicsBody body, int vertex); // Returns transformed position of a body shape (body position + vertex transformed position) +extern /* Functions visible from other files*/ void InitPhysics(void); // Initializes physics values, pointers and creates physics loop thread +extern /* Functions visible from other files*/ void RunPhysicsStep(void); // Run physics step, to be used if PHYSICS_NO_THREADS is set in your main loop +extern /* Functions visible from other files*/ void SetPhysicsTimeStep(double delta); // Sets physics fixed time step in milliseconds. 1.666666 by default +extern /* Functions visible from other files*/ bool IsPhysicsEnabled(void); // Returns true if physics thread is currently enabled +extern /* Functions visible from other files*/ void SetPhysicsGravity(float x, float y); // Sets physics global gravity force +extern /* Functions visible from other files*/ PhysicsBody CreatePhysicsBodyCircle(Vector2 pos, float radius, float density); // Creates a new circle physics body with generic parameters +extern /* Functions visible from other files*/ PhysicsBody CreatePhysicsBodyRectangle(Vector2 pos, float width, float height, float density); // Creates a new rectangle physics body with generic parameters +extern /* Functions visible from other files*/ PhysicsBody CreatePhysicsBodyPolygon(Vector2 pos, float radius, int sides, float density); // Creates a new polygon physics body with generic parameters +extern /* Functions visible from other files*/ void PhysicsAddForce(PhysicsBody body, Vector2 force); // Adds a force to a physics body +extern /* Functions visible from other files*/ void PhysicsAddTorque(PhysicsBody body, float amount); // Adds an angular force to a physics body +extern /* Functions visible from other files*/ void PhysicsShatter(PhysicsBody body, Vector2 position, float force); // Shatters a polygon shape physics body to little physics bodies with explosion force +extern /* Functions visible from other files*/ int GetPhysicsBodiesCount(void); // Returns the current amount of created physics bodies +extern /* Functions visible from other files*/ PhysicsBody GetPhysicsBody(int index); // Returns a physics body of the bodies pool at a specific index +extern /* Functions visible from other files*/ int GetPhysicsShapeType(int index); // Returns the physics body shape type (PHYSICS_CIRCLE or PHYSICS_POLYGON) +extern /* Functions visible from other files*/ int GetPhysicsShapeVerticesCount(int index); // Returns the amount of vertices of a physics body shape +extern /* Functions visible from other files*/ Vector2 GetPhysicsShapeVertex(PhysicsBody body, int vertex); // Returns transformed position of a body shape (body position + vertex transformed position) +extern /* Functions visible from other files*/ void SetPhysicsBodyRotation(PhysicsBody body, float radians); // Sets physics body shape transform based on radians parameter +extern /* Functions visible from other files*/ void DestroyPhysicsBody(PhysicsBody body); // Unitializes and destroy a physics body +extern /* Functions visible from other files*/ void ClosePhysics(void); // Unitializes physics pointers and closes physics loop thread /*********************************************************************************** * * PHYSAC IMPLEMENTATION