Tricky stuff I tell ya, scaling these spheres properly. I asked ChatGPT for some help and it led me to the broken equation I’d already written, which works perfectly with two bodies but breaks when a third one is introduced.
Essentially, my problem is that when multiple bodies interact with one another within scaling distance, the attractee doesn’t understand which of the other attractors to base its new scale on. I needed a way to make the attractee aware of the full size it should scale to when multiple other attractors are nearby.
I tried rewriting the scaling algorithm to add an interpolated scale amount based on the attractee’s distance from a given attractor and time until they met in space. I got stuck calculating the time value since the acceleration of the attractee changes every frame. Researching how to calculate changes in acceleration taught me that the Rice Krispies slogan is based in science.
I decided to try a different method, but first needed to clean up distractions. I created a dedicated scaling script for testing purposes which removed all distracting gravitational forces. Then, in my player constructor, I added a dictionary with a key value pair of <Player, float>, with an attractor’s distance from the attractee as the float value in the pair. Then, inside a foreach loop, I added up interpolated scale values from all nearby attractors based on that distance.
The basic math:
Here’s the result:
It seems to work, but I still have issues with it. Because the formula relies on the original sizes of all bodies, the scaling doesn’t start until the objects are close to one another regardless of their sizes. I suspect that re-enabling gravity may mask this issue, since the objects will be pulled together more intensly as they increase in size. However, I think that if I can figure out how to control scale based on distance to surface rather than distance to a body’s center, then this will actually correct the problem. My game dev intuition told me I should re-enable gravity first to see how noticeable this problem really is though.
Note to self: stopGravityDistance being controlled globally. That’s not what is causing the issues here, but I do need to correct it. What is causing this issue, at least in part, is the two merged bodies having their velocity decreased to near zero despite a third body pulling on them. This is going to be another tricky case to solve, because while I do need two merged objects to slow down relative to each other, they should still be able to get drawn toward another object together. Ugh.
I have hope though. Because I’m never explicitly setting velocity, forces added from the gravitational pull of other bodies should still get added to a stationary one.
Tags: gamedev unity physics programming scaling debugging 3d