This project simulates a large number of moving balls within a defined space using a QuadTree for efficient spatial partitioning and collision detection. The visualization is handled using OpenCV.
The simulation creates and animates 200,000 balls of different types (e.g., aggressive) that move randomly within a defined window. A QuadTree is utilized to optimize the detection of collisions between these balls.
To run this code, you need:
Ensure you have the necessary libraries installed and properly linked in your Visual Studio 2020 project.
- Ball: Represents each ball with properties such as diameter, position, type, direction, speed, and a method to move the ball.
- Rectangle: A helper class that defines a rectangular area and checks if balls are contained within or intersect with other rectangles.
- QuadTree: Implements the QuadTree data structure to efficiently manage and query ball positions.
createBalls
: Generates a specified number of balls and adds them to the global vector.moveBalls
: Updates the position of each ball based on its direction and speed.checkCollisions
: Checks for collisions between balls using the QuadTree.createMap
: Renders the current state of the simulation on a blank image.display
: Displays the rendered image using OpenCV.
- Open the project in Visual Studio 2020.
- Ensure all dependencies are correctly configured.
- Build the project.
- Run the executable. A window titled "Simulation" will display the moving balls.
- QuadTree Optimization: The QuadTree structure significantly improves the performance of collision detection by reducing the number of comparisons needed.
- Parallel Processing: Leveraging OpenMP allows multiple threads to check for collisions concurrently, enhancing the speed of the simulation.
- Dynamic Movement: Balls change direction randomly, simulating realistic behavior.