Physia2D: Week 5

To get to know more about this project, please check the Week 1 article where I introduce this project.

During Week 5, I focused mainly on finishing up the collisions detection, testing all of its cases and render the results. Creating the testbed was a big part of this week’s work, and the latter helped me debug my collisions detection.

I started by drawing circle and polygon shapes in the testbed. I’m using SFML as my OpenGL library. SFML is a cross-platform C++ multimedia library that provides access to graphics, input, audio, networking and the system. I chose this library because of its ease of use. The purpose of this project is not to learn OpenGL, but to use it to visualize the Physics engine.

To draw different kinds of bodies, I created a WorldRenderer class, which helps render all of the bodies in a world with a fair amount of customization, regardless of their shape.

Here is a code snippet from WorldRenderer:

2017-06-26 13_34_51-Physia2D - Microsoft Visual Studio

This method handles rendering a polygon to the screen.

After that, I went back to the collision detection module, and I added the Polygon to Polygon Collision. I used the Separating Axis Theorem (SAT) for this type of collision.

SAT states that: “If two convex objects are not penetrating, there exists an axis for which the projection of the objects will not overlap.

Pretty simple concept, here’s a diagram that illustrates how SAT works:


You can see there that all of the projections collide, if we had at least one projection that does not collide, that means that the polygons do not collide.

Here is my implementation in C++ for SAT:


For now, I’m banking on the fact that the polygons are convex since my SAT implementation assumes that the polygons are convex, and it’s not treating the case where the polygons are concave. There is a way to add support for concave polygons, but it’s a stretch goal for now.

I would recommend this article as a good read to understand how SAT works. I actually got the diagram explaining SAT from that article.

To test the different collisions, I added first an update method to the World which will go through the bodies and call their Update method, which updates the transform of the body proportionally to the elapsed time since the last frame. And in consequence, I added a game loop that calls the world Update after a certain fixed time, that depends on a fixed frame rate. The reason behind having the framerate fixed is to have a consistent output and also to avoid updating the world too frequently.

I created different bodies from different JSON files to simulate the different kinds of collisions. Here is the result for that:

You can always check the project on my GitHub.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by

Up ↑