All Projects → victorfisac → Physac

victorfisac / Physac

2D physics header-only library for videogames developed in C using raylib library.

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Physac

Py3ODE
Port of PyODE for Python 3
Stars: ✭ 29 (-80.79%)
Mutual labels:  physics, physics-engine, 2d
Pydy
Multibody dynamics tool kit.
Stars: ✭ 232 (+53.64%)
Mutual labels:  dynamics, simulation, physics
Physics3d
A 3D physics engine
Stars: ✭ 101 (-33.11%)
Mutual labels:  physics-engine, simulation, physics
elasty
A research-oriented elastic body simulator
Stars: ✭ 173 (+14.57%)
Mutual labels:  simulation, physics, dynamics
Pioneer
A game of lonely space adventure
Stars: ✭ 979 (+548.34%)
Mutual labels:  simulation, physics
Imagine Old
Modeling and simulations using computational graphs
Stars: ✭ 25 (-83.44%)
Mutual labels:  simulation, physics
Matter Js
a 2D rigid body physics engine for the web ▲● ■
Stars: ✭ 12,522 (+8192.72%)
Mutual labels:  physics-engine, physics
Gala
Galactic and gravitational dynamics in Python
Stars: ✭ 73 (-51.66%)
Mutual labels:  dynamics, physics
Awesome Robotics
A curated list of awesome links and software libraries that are useful for robots.
Stars: ✭ 478 (+216.56%)
Mutual labels:  simulation, physics
Spirit
Atomistic Spin Simulation Framework
Stars: ✭ 67 (-55.63%)
Mutual labels:  simulation, physics
Ign Gazebo
Open source robotics simulator. Through Ignition Gazebo users have access to high fidelity physics, rendering, and sensor models. Additionally, users and developers have multiple points of entry to simulation including a graphical user interface, plugins, and asynchronous message passing and services. Ignition Gazebo is derived from Gazebo, and represents over 16 years of development and experience in robotics and simulation. This library is part of the Ignition Robotics project.
Stars: ✭ 81 (-46.36%)
Mutual labels:  simulation, physics
Reactphysics3d
Open source C++ physics engine library in 3D
Stars: ✭ 730 (+383.44%)
Mutual labels:  physics-engine, simulation
Su2
SU2: An Open-Source Suite for Multiphysics Simulation and Design
Stars: ✭ 731 (+384.11%)
Mutual labels:  simulation, physics
Game Dogfight
Air to air combat game, created in Python 3 using HARFANG 3D.
Stars: ✭ 41 (-72.85%)
Mutual labels:  simulation, physics
Dart
Dynamic Animation and Robotics Toolkit
Stars: ✭ 596 (+294.7%)
Mutual labels:  dynamics, simulation
Dmech
3D physics engine for D
Stars: ✭ 70 (-53.64%)
Mutual labels:  physics-engine, simulation
Specs Physics
nphysics integration for the Specs entity component system
Stars: ✭ 94 (-37.75%)
Mutual labels:  physics-engine, physics
Webots
Webots Robot Simulator
Stars: ✭ 1,324 (+776.82%)
Mutual labels:  physics-engine, simulation
Pydy Tutorial Human Standing
PyDy tutorial materials for MASB 2014, PYCON 2014, and SciPy 2014/2015.
Stars: ✭ 135 (-10.6%)
Mutual labels:  dynamics, simulation
Planck.js
2D JavaScript Physics Engine
Stars: ✭ 4,149 (+2647.68%)
Mutual labels:  2d, physics-engine

Physac

Physac is a small 2D physics engine 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.

The header file includes some tweakable define values to fit the results that the user wants with a minimal bad results. Most of those values are commented with a little explanation about their uses.

Note: The example code uses raylib programming library to create the program window and rendering framework.

Installation

Physac requires raylib. To get it, follow the next steps:

* 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.

Physac API

The PhysicsBody struct contains all dynamics information and collision shape. The user should use the following structure components:

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;

The header contains a few customizable define values. I set the values that gived me the best results.

#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.4f

Physac contains defines for memory management functions (malloc, free) to bring the user the opportunity to implement its own memory functions:

#define     PHYSAC_MALLOC(size)             malloc(size)
#define     PHYSAC_FREE(ptr)                free(ptr)

The Physac API functions availables for the user are the following:

// 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);

Note: InitPhysics() needs to be called at program start and ClosePhysics() before the program ends. Closing and initializing Physac during the program flow doesn't affect or produces any error (useful as a 'reset' to destroy any created body by user in runtime).

Dependencies

Physac uses the following C libraries for memory management, math operations and some debug features:

  • stdlib.h - Memory allocation [malloc(), free(), srand(), rand()].
  • stdio.h - Message logging (only if PHYSAC_DEBUG is defined) [printf()].
  • math.h - Math operations functions [cos(), sin(), fabs(), sqrtf()].

It is independent to any graphics engine and prepared to use any graphics API and use the vertices information (look at examples Drawing logic) to draw lines or shapes in screen. For example, this vertices information can be use in OpenGL API glVertex2f().

By the way, I use raylib to create the examples. This videogames programming library is used to handle inputs, window management and graphics drawing (using OpenGL API).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].