jperry82 / CS330_8_2

Repository from Github https://github.comjperry82/CS330_8_2Repository from Github https://github.comjperry82/CS330_8_2

Optimized Brick Collision Simulation

CS 499 - Milestone Three: Algorithms & Data Structures Enhancement

Overview

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.

Key Enhancements from Original Version

Optimized movement logic
Efficient collision detection using spatial partitioning
Improved shading model for realistic color transitions
Refactored code structure for maintainability


New Features & Enhancements

1. Performance Optimizations

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

2. Optimized Movement Handling

  • Previous Problem: Redundant condition checks made movement inefficient.
  • Solution: Refactored movement logic using std::max and std::min for better boundary control.

3. Improved Collision Detection

  • Previous Problem: Circles merged inside the movement loop, making it inefficient.
  • Solution: Moved merging logic into a dedicated function mergeCircles(), reducing unnecessary checks.

4. Enhanced Shading Model

  • Previous Problem: Flat shading made destructible bricks look unnatural.
  • Solution: Implemented color transition logic using a Phong-like shading model for smooth fading effects.

5. Better Code Modularity

  • Previous Problem: Brick and Circle logic was too interconnected, making changes difficult.
  • Solution: Refactored logic into separate functions for better code organization.

How It Works

User Input

Key Action
SPACEBAR Releases a new circle into the scene.
ESCAPE Closes the application.

New Algorithm Flow

  1. Circles Move:
    • Movement is adjusted using optimized boundary checks.
    • Circles reflect off walls and bricks.
  2. Collision Detection:
    • Checks if a circle hits a brick.
    • If hit, reflective bricks change direction, destructible bricks fade.
  3. 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.

Updated Code Structure

Key Components

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.

Updated Classes

1️⃣ Brick Class (Brick)

  • Properties:
    • BrickType (reflective or destructible)
    • hit_count (tracks hits)
    • status (active or removed)
  • Key Updates:
    • Improved shading model for destructible bricks.

2️⃣ Circle Class (Circle)

  • Properties:
    • x, y, radius → Position and size
    • speed → Movement speed
    • direction → Movement direction (1-8)
    • red, green, blue → Color properties
  • Key Updates:
    • Refactored movement logic for efficiency.
    • Merging logic moved to a separate function (mergeCircles()).

Dependencies

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

How to Compile & Run

🔧 Compilation

  1. Open a terminal and navigate to the project folder:
    cd path/to/project
  2. Compile the program using g++:
    g++ -o BrickCollision MainCode.cpp -lglfw -lGL

▶️ Run the Program

./BrickCollision

Known Issues & Future Enhancements

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

Comparison: Before vs After

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

Final Thoughts

🚀 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! 🎯


License

This project is open-source under the MIT License. Feel free to modify and improve it!

About


Languages

Language:C++ 100.0%