It’s time for me to start playing with attraction physics. As always, I did a bit of Googling first before setting up my scene. Unity’s inbuilt gravity can be disabled or changed in project preferences. I disabled it because I wanted to start clean, and more importantly, be able to use Rigidbodies without hindrance.
Next, I started looking at different kinematics implementations. I was in a mood where I felt more like pasting code than understanding it, so I copied the first good-looking gravitational function I found, from this video. It used Newton’s gravitational equation:
I set up my scene with two spheres containing Rigidbody and sphere collider components, the latter of which were set up as triggers to allow object overlapping. I set up my two spheres to both attract one another and pressed play. Neither of them moved. That was because the gravitational constant G is such a small number, yet the masses of my spheres were a measly 1 mass unit. Cranking up the masses to 10,000,000 mass units got them going. I decided to modify the G value instead so I could keep the object masses at 1. Either way, I had a problem.
The classic infinite velocity problem! As the distance between objects becomes minimal, the force attracting them grows exponentially. I decided to work smarter, not harder, and created a thread on the Unity forums asking for suggestions. Then I took a walk to let my brain relax.
While walking, I realized an easy potential solution to test would be to disable gravitational force from being added to the spheres once they got within a certain distance of each other. That got me a bit closer to where I’d like to go, but it’s far from perfect.
Somebody on the forums suggested I add a counter-force when the spheres get close together. That’s a decent idea, but I hope there’s a good physics equation for it, perhaps some sort of fall-off formula.
The last thing I had to do was upload this project to GitHub so I can share it with my laptop. As always, I never remember the commands. As always, I turn to this guide. Unfortunately, I still had artifacts in my GitHub settings from working with Evan that caused hiccups in the tutorial. The root of the issue was that my main branch was called “main”, not “master”. Little things like that break everything when it comes to GitHub.