A lightweight physics simulation engine written in C/C++. This library provides fundamental physics calculations including gravitational forces, electric forces, and basic entity movement simulations.
- Advanced Entity System: Create and manage physical entities with mass, charge, position, velocity, acceleration, and rotational properties
- Universal Gravitation: Calculate gravitational forces between celestial bodies
- Electrostatic Forces: Compute electric forces between charged particles
- Magnetic Field Support: Apply magnetic forces using Lorentz force law
- 3D Physics: Support for 3-dimensional position, velocity, and acceleration vectors using Vector and Quaternion types
- Entity Manager: C++ class for managing multiple entities with batch operations and collision detection
- OpenGL Graphics Support: Real-time 3D visualization with camera, lighting, and texture support
- Model Loading: OBJ model loading support via custom Model class
- Sphere Rendering: Built-in sphere renderer for physics visualization
- Mathematical Library: Comprehensive vector operations and quaternion utilities
- Skybox Rendering: High dynamic range (HDR) skybox support for realistic environments
- Collision Detection: Sphere collision detection and response between entities
- Physics Logging: Comprehensive logging system for simulation data
- CMake Build System: Easy compilation and integration with other projects
- vcpkg Integration: Modern dependency management with vcpkg
CPhysics/
├── include/ # Header files
│ ├── core/ # Core physics components
│ │ ├── collider.h # Collision detection
│ │ ├── entity.h # Entity definitions
│ │ ├── entity_manager.h # Entity management (C++)
│ │ ├── field.h # Field calculations
│ │ ├── movement.h # Movement and kinematics
│ │ └── time_flow.h # Time flow management
│ ├── graphics/ # Graphics components
│ │ └── OpenGL/ # OpenGL implementations
│ │ ├── gl_camera.h # Camera system
│ │ ├── gl_cube.h # Cube rendering
│ │ ├── gl_model.h # OBJ model loading (C++)
│ │ ├── gl_skybox.h # Skybox rendering
│ │ ├── gl_sphere.hpp # Sphere rendering (C++)
│ │ └── gl_texture.h # Texture management
│ ├── mathlib/ # Mathematical utilities
│ │ ├── Vector.h # Vector operations
│ │ └── Quaternion.h # Quaternion operations
│ ├── cphysics.h # Main library header
│ ├── constant.h # Physical constants
│ ├── error_codes.h # Error code definitions
│ └── plog.h # Physics logging system
├── src/ # Source files
│ ├── core/ # Core physics implementations
│ ├── graphics/ # Graphics implementations
│ │ └── OpenGL/ # OpenGL implementations
│ ├── mathlib/ # Mathematical utilities
│ └── plog.c # Physics logging implementation
├── resources/ # Resource files
│ ├── models/ # 3D models (OBJ format)
│ ├── skybox/ # Skybox HDR textures
│ └── textures/ # Texture files
├── doc/ # Documentation
│ ├── Entity.md # Entity system documentation
│ ├── Field.md # Field calculations documentation
│ ├── Formulas.md # Physics formulas reference
│ └── Movement.md # Movement system documentation
├── vcpkg_installed/ # vcpkg dependencies
├── CMakeLists.txt # Build configuration
├── vcpkg.json # vcpkg manifest
├── vcpkg-configuration.json # vcpkg configuration
├── LICENSE # MIT License
├── main.cpp # Demo application
└── readme.md # This file
- CMake (version 3.10 or higher)
- C/C++ compiler (GCC, Clang, or MSVC)
- vcpkg package manager
- OpenGL libraries (for graphics features)
The project uses vcpkg for dependency management. Required packages:
- GLFW (window and input management)
- GLEW (OpenGL extensions)
# Install vcpkg if not already installed
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.bat # Windows
# or ./bootstrap-vcpkg.sh # Linux/macOS
# Install dependencies
./vcpkg install glfw3:x64-windows
./vcpkg install glew:x64-windows# Clone or navigate to the project directory
cd cPhysics
# Create build directory
mkdir build && cd build
# Configure with CMake (specify vcpkg toolchain)
cmake .. -DCMAKE_TOOLCHAIN_FILE=[vcpkg-root]/scripts/buildsystems/vcpkg.cmake
# Build the project
cmake --build .
# Run the demo
./cPhysics#include "include/cphysics.h"
#include "include/core/entity_manager.h"
int main(void) {
// Initialize physics logging
plog_init("simulation.log");
// Create entity manager
EntityManager entity_manager;
// Add entities using the manager
entity_manager.addEntity("Earth", 5.972e24, 0.0,
{0.0, 0.0, 0.0}, {0.0, 0.0, 0.0},
6371000.0, false);
entity_manager.addEntity("Moon", 7.348e22, 0.0,
{3.844e8, 0.0, 0.0}, {0.0, 1022.0, 0.0},
1737400.0, false);
// Simulation loop
double dt = 0.001;
for (int step = 0; step < 1000; step++) {
// Update all physics
entity_manager.updateAllPhysics(dt);
// Check for collisions
entity_manager.checkCollisions();
// Log state
for (Entity* entity : entity_manager) {
plog_entity_state(entity, step);
}
}
// Clean up
plog_close();
return 0;
}new_entity(): Create a new physical entity with rotational propertiesget_position(),get_velocity(),get_acceleration(): Access entity stateset_entity_position(),set_entity_velocity(): Modify entity stateget_euclidean_distance(): Calculate distance between two entitiesget_linear_momentum(): Calculate linear momentum vector
apply_universal_gravitation(): Apply gravitational force between two entitiesapply_electric_force(): Apply electric force between charged particlesapply_force(): Apply arbitrary force to an entityapply_torque(): Apply torque to an entity
apply_gravitational_field(): Apply uniform gravitational fieldapply_electric_field(): Apply uniform electric fieldapply_magnetic_field(): Apply magnetic field using Lorentz force
new_time_flow(): Create a new time flow configurationadvance_time(): Advance simulation timeget_simulation_time(): Get current simulation time
check_sphere_collision(): Check collision between two spherical entitiesresolve_sphere_collision(): Resolve collision between entitiesapply_collision_response(): Apply collision response to entity array
plog_init(): Initialize physics logging systemplog_entity_state(): Log entity state to fileplog_close(): Close logging system
typedef struct Entity {
char name[256]; // Entity identifier
double mass; // Mass in kilograms (kg)
double charge; // Electric charge in coulombs (C)
Vector position; // 3D position vector
Vector velocity; // 3D velocity vector
Vector acceleration; // 3D acceleration vector
Quaternion quaternion; // Orientation quaternion
Vector angular_velocity; // Angular velocity vector
Vector angular_acceleration; // Angular acceleration vector
double moment_of_inertia; // Moment of inertia scalar
double coefficient_of_restitution; // Elasticity coefficient (0.0-1.0)
bool rigid_body; // Rigid body flag
bool is_static; // Static object flag
} Entity;typedef struct Vector {
double x;
double y;
double z;
} Vector;typedef struct {
float w; // Scalar (real) component
float x; // i-component
float y; // j-component
float z; // k-component
} Quaternion;class EntityManager {
public:
void addEntity(const std::string& name, double mass, double charge,
const Vector& position, const Vector& velocity,
double radius = 0.5, bool is_static = false);
bool removeEntity(const std::string& name);
Entity* findEntity(const std::string& name);
size_t getEntityCount() const;
void updateAllPhysics(double delta_time);
void applyGravityField(double magnitude, const Vector& direction);
void applyElectricField(double magnitude, const Vector& direction);
void applyMagneticField(double magnitude, const Vector& direction);
void checkCollisions();
bool saveToFile(const std::string& filename);
bool loadFromFile(const std::string& filename);
// Iterator support
std::vector<Entity*>::iterator begin();
std::vector<Entity*>::iterator end();
};The library includes commonly used physical constants:
- Gravitational constant (G): 6.67430e-11 m³/kg/s²
- Coulomb's constant (K): 8.987551787e9 N·m²/C²
- Speed of light (c): 3e8 m/s
- Pi (π): 3.14159265358979323846
The project includes a demo application in main.cpp that demonstrates:
- OpenGL window creation and rendering
- Entity creation and management with EntityManager
- Sphere collision simulation
- Camera controls (WASD + mouse)
- HDR skybox rendering
- OBJ model loading and rendering
- Real-time physics updates
- WASD - Move camera
- Q/E - Move up/down
- Mouse - Look around
- SPACE - Pause/Resume simulation
- R - Reset simulation
- ESC - Exit
Detailed documentation is available in the doc/ directory:
- Entity System Documentation - Complete guide to entity management
- Field System Documentation - Field calculations and applications
- Movement System Documentation - Movement and rotation dynamics
- Physics Formulas Reference - Mathematical foundations of the engine
- Quaternion-based orientation representation
- Angular velocity and acceleration calculations
- Moment of inertia support
- Rigid body physics simulation
- Configurable time scaling
- Simulation time tracking
- Time step control for numerical stability
- Comprehensive state logging
- Simulation data export
- Debugging and analysis support
- Camera system with mouse look
- Sphere rendering for physics visualization
- OBJ model loading support
- HDR skybox rendering
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
Planned features include:
- Advanced collision detection (AABB, OBB)
- Multi-body simulations with N-body problem solvers
- Numerical integration methods (Runge-Kutta, Verlet)
- Fluid dynamics simulations
- Thermodynamic systems
- Quantum mechanics extensions