Implementing a 2D Physics Engine using SFML from Scratch
Introduction
Hello everyone, and welcome back to my blog! Today, I am beyond excited to share my journey of building my own 2D physics engine from scratch, an endeavor I began just a month ago.
Now, you may be asking, "Why in the world would you build a physics engine when there are so many out there?" Well, the answer is two-fold: intellectual curiosity and the desire for customization.
You see, while existing engines like Box2D or Unity's built-in 2D physics engine are fantastic and well-optimized, they are also a bit like a black box. You're not entirely sure how they're doing what they're doing under the hood. By building my own engine, I've not only deepened my understanding of the complexities and intricacies of game physics but also gained the ability to tailor the engine precisely to my needs. Plus, there's something incredibly satisfying about creating something so fundamental from scratch.
For the technical folks among you, I've been using SFML (Simple and Fast Multimedia Library) for creating the window and drawing shapes. SFML is lightweight, versatile, and very beginner-friendly, making it the perfect choice for this project.
As of today, I'm proud to say that my engine is capable of doing quite a bit. It can detect collisions for any convex shape, forces can be applied to these shapes, rotations are possible, and I've even implemented friction. But that's not allβmy engine also features a ray casting class, allowing you to read distances from your main character (see video below), which is a crucial component for many game mechanics or robotics applications.
Here you see a video showcasing the final result of my engine.
Physics2D
So, if you're intrigued by the physics behind your favorite games, or just love a good DIY challenge, keep reading as we delve into the technical nuts and bolts of my 2D physics engine journey.
Since such a project covers a lot of material I divided this blog post into multiple parts listed below:
- Part I - Setting Up Math Classes
- Part II - Circle Collision Detection
- Part III - Collision Detection Between Circles and Polygons (coming soon)
- Part IV - Collisions Detections Between Polygons (coming soon)
- Part V - Implementing Gravity and Other Forces (no friction) (coming soon)
- Part VI - Resolving Collisions without Rotation
- Part VII - Improve Collision Accuracy (coming soon)
- Part VIII - Get the Contact Points (coming soon)
- Part VIV - Let's Apply Rotations (coming soon)
- Part X - Friction (coming soon)
- Part XI - Simulating a 2D Lidar using Ray Casting (coming soon)
Upcoming Versions of Physics2D
In future versions of Physics2D, I'm looking to get the Robot Operating System, or ROS, in the mix. ROS is a useful framework for developing control architectures for your robots that we've used in our magnecko project too. Then there's the deep reinforcement learning angle. I'm planning to tweak the code so you can easily test out different RL algorithms.
Below you find a small list containing other features that I want to implement.
- Air drag
- Collision detection with concave shapes
- Path-finding algorithm: The goal is to enable the developer to add autonomous agents that can find the shortest path to a target
- Add RL algorithms such as PPO and Q-Learning
- Springs
- Enable the use of textures
- Make it compatible with ROS Noetic and ROS2 Humble
- Enable noise on the Lidar sensor
- Allow more customization of the Lidar sensor
- Joint simulation
- Soft body physics
- 2D legged robots
- Wheeled robots
- Quadcopter
- β Implement a contact point class: Currently the drawing of these points happens in the game.cpp file. This file should only be used as a template for your games, but should not contain functionalities that belong to the Physics2D library.
- β Implement a text class to facilitate the drawing of text elements inside the game window and to reduce the code inside the game.cpp file