Today was a reinvent the wheel type of day where I tried to come up with a proper algorithm for the attraction between bodies. I didn’t work alone though—I got some help from my super-mechanic buddy Mr. ChatGPT. We conversed for hours, trying to deduce a proper algorithm to attract two objects toward a middle point with an overshoot past the middle that decreases in size each time the objects cross. ChatGPT is a great assistant, but not a leader—it’s only as smart as I am. If I can’t describe what I want to build, ChatGPT won’t do extra thinking for me. That was a problem, because I couldn’t get the algorithm to work the way I had in mind.
Eventually I ran out of gas for the day and left my station feeling frustrated. Still, I obtained some useful experience with ChatGPT, and even picked up a few tricks from its recommendations to me. For example, it showed me that I can determine whether two objects are moving towards or away from each another using the dot product of each one’s relative velocity and their distance apart. I also realized an obvious programming trick that I never considered before, which is that if I need to “remember” a bool value across frame updates, I shouldn’t reassign its value right away. Instead, I can use the bool-determining conditions in conjunction with the bool’s current state to check for a change in value. Here’s an example of what I mean.
void GetRemainderSign(int A, int B) {
int remainder = A - B;
if (!isPositive && remainder > 0 || isPositive && remainder < 0) {
// isPositive has changed state
isPositive = !isPositive;
}
}
Tomorrow, I will reconsider the fact that my original energy ball algorithm has a very realistic attraction effect and attempt to adjust its code to factor in an additional attractive force (from another energy ball). Following my original metaphor, I have a good wheel already—I just need some better rims. If that still doesn’t work, then perhaps I’ll look into inertia as a way to slow down the spheres.
Tags: gamedev programming physics algorithms chatgpt csharp unity