A simple OpenGL-based game where a ball moves around the screen, squashes on collisions, and interacts with a static rectangle. This project demonstrates rendering, movement, collision detection, and transformations in OpenGL.
-
Ball Movement:
- The ball moves in response to user clicks.
- The ball bounces off the screen edges and interacts with the static rectangle.
-
Squashing Effect:
- The ball squashes and stretches dynamically upon collisions with walls or the static object.
-
Static Object:
- A green rectangle is positioned on the screen.
- The ball detects and reacts to collisions with the rectangle.
-
OpenGL Basics:
- Rendering primitives like circles and rectangles.
- Transformations using the OpenGL matrix stack (translation, scaling).
- Shader programming for vertex and fragment shaders.
- C++ Compiler: Supports C++11 or later.
- OpenGL: Version 3.3 or later.
- GLFW: For windowing and input handling.
- GLAD: For loading OpenGL functions.
- GLM: For mathematical operations and transformations.
-
Clone the Repository:
git clone https://github.com/your-repo/ball-game.git cd ball-game -
Install Dependencies:
-
Compile the Code:
g++ main.cpp -o BallGame -lglfw -ldl -lGL
-
Run the Program:
./BallGame
This file contains the entire implementation:
-
Initialization:
- Sets up GLFW and GLAD for OpenGL context and function loading.
- Creates the shaders for rendering.
-
Structures:
Ball: Represents the moving ball, including position, velocity, radius, color, and squash factor.StaticObject: Represents the static rectangle, including position, size, and color.
-
Rendering:
- A vertex and fragment shader pipeline is used to render the ball and rectangle.
- Projection and model transformations are applied for proper positioning and scaling.
-
Collision Detection:
- AABB (Axis-Aligned Bounding Box) is used to detect collisions between the ball and the rectangle.
- Clicking: Clicking on the window sends the ball toward the clicked location.
- Bouncing: The ball bounces off screen edges and the static object.
- The ball squashes along the axis of collision and stretches perpendicular to it.
- The squash/stretch effect gradually recovers over time.
- A green rectangle is rendered on the screen and interacts with the ball when they collide.
- Left Click: Directs the ball to the clicked location.
- GLFW: Handles window creation and input.
- GLAD: Loads OpenGL functions.
- GLM: Provides vector and matrix math utilities.
- OpenGL 3.3: Used for rendering graphics.
- A turquoise window with:
- An orange ball that moves, bounces, and squashes upon collision.
- A static green rectangle that interacts with the ball.
- Add multiple static objects for the ball to interact with.
- Introduce scoring mechanisms for player interaction.
- Add textures to the ball and rectangle.
This project is licensed under the MIT License - see the LICENSE file for details.