I must have shut my machine off yesterday before testing the code on differently sized spheres because it worked perfectly off the bat today. The constructor did the trick 😀

I decided to visualize the attraction radius next. There are many ways to draw a circle in the renderer. I went with the simplest option, using a circular sprite. My initial testing showed me that I needed to scale the sprite game object at 0.4x to get the circle to overlap perfectly with the sphere. With that in mind, I got to coding a scaler for the sprite based on the sphere’s attraction radius, which is based on the sphere’s size and a global attraction radius multiplier.

I quickly realized that I had oversimplified the math for calculating the attraction radius. I wanted attraction to ensue when a sphere crosses into the attraction radius of another sphere. That meant that instead of comparing the distance between the spheres to the attracting sphere’s attraction radius, I had to also factor in the radius of the attractee sphere, which is equal to one of its local scale parameters divided by two. Here’s the result:

I didn’t anticipate the object’s following the momentum of the faster object when the collision takes place, but I kinda like it. I consider the effect one of the perks of using good math in my code.


Tags: gamedev unity physics vfx programming math