Physics Engine

- Beta

Physics Engine

A small 2D Physics Engine developed in C++

About

This project was developed as part of the coursework at SAE Institute Geneva. The objective was to create a robust 2D physics engine in C++ that simulates various physical phenomena. This involved implementing key features such as force application to objects, collision detection, triggers, and friction. Additionally, we used SDL2 (Simple DirectMedia Layer) for rendering graphics and integrated ImGui for a user interface to facilitate debugging and visualisation.

Goals


First Scene: Planet System

Planet System

Description

The first scene features a Planet System, where circles representing planets orbit around a central star. Users can click anywhere in the scene to spawn additional planets.

Learning Points

#include <xmmintrin.h>
    template <>
    FourVec2f FourVec2<float>::operator+(const FourVec2<float>& other) const
    {
        FourVec2<float> result;
        __m128 x1 = _mm_loadu_ps(x.data());
        __m128 x2 = _mm_loadu_ps(other.x.data());
        __m128 y1 = _mm_loadu_ps(y.data());
        __m128 y2 = _mm_loadu_ps(other.y.data());

        __m128 x_res = _mm_add_ps(x1, x2);
        __m128 y_res = _mm_add_ps(y1, y2);

        _mm_storeu_ps(result.x.data(), x_res);
        _mm_storeu_ps(result.y.data(), y_res);

        return result;
    }

Second Scene: Trigger System

Trigger System

Description

The Trigger System showcases circles and Axis-Aligned Bounding Boxes (AABBs) moving around the window. These shapes bounce off the edges of the window and change colour upon intersection, illustrating basic trigger mechanics.

Learning Points


Third Scene: Collision System

Collision System

Description

The Collision System builds on the previous trigger system. Instead of merely detecting intersections, shapes now bounce off each other and change colour each time they collide, simulating real-world physics interactions.

Learning Points


Fourth Scene: Friction System

Friction System

Description

In the Friction System, an AABB serves as the ground, and users can spawn objects above it. Circles have a random bounciness, while AABBs have none. The system incorporates friction, causing objects to slow down and eventually stop when moving on the ground.

Issue

Unfortunately, this scene is currently bugged. The friction is present and working (as seen on the gif, the shapes slow on the x-axis), but shapes pass through the ground or each other after stopping.

Learning Points


Conclusion

This project has been a valuable learning experience in physics simulation and C++ programming. Developing the physics engine has deepened my understanding of the practical applications of physics in game development and I intend to fix the remaining bugs and continue working on it.

[Github Project]
GitHub Repo