To get to know more about this project, please check the Week 1 article where I introduce this project.
During Week 9, my main focus was to make torque work for Polygon vs Polygon. The issue that I had that made the torque not work correctly for the latter case is that my manifold generation was limited. I was only generating one point of contact, which worked fine for circles, but not for polygons. In most cases, we have a collision segment, which we can represent with 2 points.
Here are some code snippets of how I’m handling manifold generation for polygon vs polygon:
Below are the helper methods for clipping and finding the edge to use:
This article helped me a lot with figuring out how to do this.
After adding this, and debugging it, I noticed that there still are some edge cases where the clipping generates only one point of contact when it’s supposed to generate two, but I got over that for two reasons: First, I had an issue with inertia calculation for polygons, which was a more pressing matter, and second, this bug was not that frequent. I’m planning to handle this at a later time.
Coming back to inertia, the method that I was using to calculate the latter was giving me inconsistent values, so I decided to scrap that and follow another method.
What I did is I always took (0, 0) as the centroid, and then translate all of the vertices of the polygons accordingly. Which allowed me to do an easier calculation to get the inertia:
I revisited the torque calculations too by treating some of the edge cases.
Lastly, I updated the testbed, and the most prominent change was that added a random body spawner and a resetter.
Here’s the final product that I got:
You can always check the project on my GitHub.