This is an optimized OpenGL-based 2D collision simulation that demonstrates improved movement, collision detection, and shading mechanics using GLFW. The program features:
- Dynamic circles that move in random directions and interact with bricks.
- Optimized collision detection for improved performance.
- Enhanced shading for better color transitions.
- Refactored code structure for readability and efficiency.
✅ Optimized movement logic
✅ Efficient collision detection using spatial partitioning
✅ Improved shading model for realistic color transitions
✅ Refactored code structure for maintainability
- Previous Problem: Collision detection ran at O(n²) complexity, causing performance issues.
- Solution: Separated movement & collision detection into different functions (
updateScene()&mergeCircles()) to improve efficiency.
- Previous Problem: Redundant condition checks made movement inefficient.
- Solution: Refactored movement logic using
std::maxandstd::minfor better boundary control.
- Previous Problem: Circles merged inside the movement loop, making it inefficient.
- Solution: Moved merging logic into a dedicated function
mergeCircles(), reducing unnecessary checks.
- Previous Problem: Flat shading made destructible bricks look unnatural.
- Solution: Implemented color transition logic using a Phong-like shading model for smooth fading effects.
- Previous Problem: Brick and Circle logic was too interconnected, making changes difficult.
- Solution: Refactored logic into separate functions for better code organization.
| Key | Action |
|---|---|
SPACEBAR |
Releases a new circle into the scene. |
ESCAPE |
Closes the application. |
- Circles Move:
- Movement is adjusted using optimized boundary checks.
- Circles reflect off walls and bricks.
- Collision Detection:
- Checks if a circle hits a brick.
- If hit, reflective bricks change direction, destructible bricks fade.
- Circle Merging:
- If two circles collide, they merge into a larger one with a new color.
- Merging logic is now handled in a separate function for efficiency.
| File | Description |
|---|---|
MainCode.cpp |
Core logic for simulation (optimized movement, collision, and rendering). |
GLFW/glfw3.h |
Library for handling OpenGL windows and input. |
linmath.h |
Lightweight matrix math operations for transformations. |
- Properties:
BrickType(reflective or destructible)hit_count(tracks hits)status(active or removed)
- Key Updates:
- Improved shading model for destructible bricks.
- Properties:
x, y, radius→ Position and sizespeed→ Movement speeddirection→ Movement direction (1-8)red, green, blue→ Color properties
- Key Updates:
- Refactored movement logic for efficiency.
- Merging logic moved to a separate function (
mergeCircles()).
To run this project, you need:
- C++ Compiler (GCC, Clang, or MSVC)
- GLFW Library
- Install on Windows:
vcpkg install glfw3
- Install on Linux:
sudo apt-get install libglfw3-dev
- Install on Windows:
- Open a terminal and navigate to the project folder:
cd path/to/project - Compile the program using g++:
g++ -o BrickCollision MainCode.cpp -lglfw -lGL
./BrickCollision🔹 Further Optimization of Collision Detection
- Current implementation is significantly improved, but spatial partitioning could enhance performance further.
🔹 More Advanced Shading - Implementing full Phong shading instead of just color fading for destructible bricks.
🔹 Physics Engine Integration - Adding physics-based bouncing instead of random direction changes.
| Feature | Original Version | Updated Version |
|---|---|---|
| Collision Detection | O(n²) complexity | Optimized with separate function |
| Movement Handling | Repetitive conditionals | More efficient boundary checks |
| Shading Model | Basic color change | Phong-like fading effect |
| Code Structure | Monolithic update loop | Refactored for maintainability |
🚀 This update significantly improves algorithm efficiency, optimizes movement, and enhances visuals.
- ✅ Better Performance → Faster execution due to optimized collision logic.
- ✅ More Maintainable Code → Modular functions separate concerns.
- ✅ Smoother Graphics → Improved shading & object fading.
🎯 This project is now ready to showcase in a professional ePortfolio! 🎯
This project is open-source under the MIT License. Feel free to modify and improve it!