top of page

The project for this module worked to complement and synthesise the learning within the module itself. The project was a 2D physics engine created using C++ and OpenGL. The application can be downloaded here and the source code can be downloaded here.

2D Physics Engine

The physics engine provides collision detection and resolution capabilities between lines, circles, rectangles and squares. I initially planned to use DirectX for the project, but after some initial development I found the initialisation of the code to be comprehensive and complex. Knowing that time would be a limited and that there was a lot of functionality to implement for the project, I switched to using OpenGL fairly early on. The application has 2 circles, 2 rectangles, 2 squares and a line drawn, in order to show the different types of collisions possible. The top circle, rectangle and square can be moved around the screen using the ‘W’, ‘A’, ‘S’ and ‘D’ buttons. You can toggle between controlling each shape using the space bar. The shapes can be rotated by pressing the ‘T’ button. Various instructions are displayed on the top left of the screen. In order to avoid cluttering up the screen, the instructions are split. You can press the ‘I’ button to toggle between the different set of instructions. Different properties of the shapes also appear on the bottom left of the screen. A gravity value can be set that can be applied to all of the shapes. There is also a mass value than can be set as well as x and y velocities for each shape. The values can be modified suing the up and down buttons, and the different values can be iterated through using the tab button. Toggling between the different shapes with the space button will also switch between the properties for the 3 different shape types.

If 2 shapes collide, details will be displayed on the screen to give information about the collision. The type of collision will be given on the centre of the screen, indicated which types of shapes have collided. Different details will be shown on the top right of the screen. The depth of the collision is given as well as the x and y coordinates of the collision point, with the centre of the screen representing the origin point of (0, 0). A line will also be displays between the 2 shapes connecting them by their centre point and showing the direction of the collision. In order to resolve the collisions, the ‘R’ button can be toggled on and off. By default, this will cause the shape that is being controlled to gradually move away until it is out of the bounds of the other shape. This can also be used to resolve multiple shapes at once. The ‘R’ button can be left on, which will cause the collisions to be resolved dynamically as you move the shape. There are lines drawn at the border of the screen in order to keep shapes within the border of the window whenever collision resolution is enabled.

There are a few different methods to resolve collisions in the program. Pressing the right button toggles the instant resolution. With this enabled, the shape will immediately pop out of the bounds of the shape it is colliding with instead of moving gradually. Again, this can be left enabled, in order to keep a shape from moving within the bounds of another shape on the screen. Another option is the alt resolution, which can be toggled on or off with the left button. With this enabled, both shapes will move when a collision is resolved, in relation to their relative masses. Therefore, if 2 shapes that have collided have the same mass, they will move an equal amount in the opposite directions until they no longer collide. If one shape has a larger mass, it will move less, as the mass will influence how much effect one shape will have on the other. These 2 modes can be used together, allowing shapes to push each other around the screen when they collide, in relation to their respective masses.

Along with the different methods of collision resolution, there are some other features that can be toggled on and off. Gravity can be toggled on or off using the ‘G’ button. This can be modified to be larger or smaller or even to go in the other direction. The different velocities that the shapes are set to can be activated with the ‘V’ button. This will cause the shapes to move around the screen in directions influenced by the velocities they have been given. The movement of the shapes can also be influenced by whether gravity or collision resolution is enabled. If collision resolution is enabled, the shapes will bounce off each other as they move around the screen. The last but of functionality within the application is to toggle debug move. This can be done using the ‘Q’ button. With debug mode activated, the instructions are removed from the screen giving space to see details about the different types of shape collision. This way, if there are multiple types of collision, the details of all of the collisions can be seen without being drawn over each other.

bottom of page