The incomprehensibility of math in game development


I get stuck on math quite often. It's very common for me to come up with an idea for my games that requires some math that I really suck at, and I fall down a rabbit hole while trying to solve it. Math is hard, but it is soooo much fun to figure it out. This is a short story about failure and learning.

I'm a 34 year old, largely self-taught developer, who never touched calculus at the university, but I really feel like I'm missing some basic knowledge here. My most recent mathematical success is this:


This is an image of how the collider of my new Hazard objects overlap with the visuals. I can't make elliptical colliders by scaling a spherical collider, and I want to just use primitives instead (simple geometry like sphere and boxes), as I expect I'll probably introduce a large amount of object with colliders, and I think that would save performance a bit. Premature optimisation, but the problem seemed easy enough. So, I figured that I could just stretch and  rotate a primitive cylindrical collider based on the major axis of the visual, stretch-able sphere, but I didn't know the math to go from one to the other!

First, I tried setting the angle from the semi-major axis. At the time, I didn't know I was looking for the semi-major axis, as I don't even have the vocabulary. The semi-major axis is apparently the length from the center of the ellipsis to any of the two farthest points on the edge. All I knew was, that I'd like to set the width and the rotation in such a way, that the rotated collider would have the same cross-section at the plane of my game, as the visual model of the stretching circle. And I had no idea what to do.

As most developers know, researching you problem is 80% of development, but math is so much harder to research than engines, frameworks and behaviours. Usually, you can google one or two keywords from your scripts and learn from there, but math as a discipline is contained within a strange vocabulary and logical framework, so there's no wonder a lot of people are intimidated by it. It feels like you need to know the solution to your problem before you can google it, and that doesn't provide a friendly learning environment.

Usually, I try to just use brute force trial-and-error, plotting in whatever seems like it might work, tweaking the variables. The scale-profile seemed like it would need to increase slowly, then faster, then slowly at the end. Which seemed like a sine-wave, and so I tried that, but it didn't work - the scale was jumping and jittering and was completely off:


For this, I went with my gut, which simple translated to:

visualScale.z = Mathf.Sin(angle);

I tried Cos, Asin, Acos and Tan, all wave-like functions, just trying to find something that might work - which felt faster than reading and googling for weird terms that I didn't understand. Nothing worked, so I started googling ellipsis, cross-section and cylindre. One of the top hits is Wikipedia, and later, I would find my solution right there, but I wanted to skip to a more specific description, and charged into the familiar waters of stackexchange and the like. I found some very concrete problems with very exact solutions, but I did not find what I was looking for. Focal points, eccentricities and representing vectors as single numbers rather than two-dimensional numbers (or something?) got me super confused, and I finally read that damn Wikipedia article in Ellipsis, which lead me to an article on Cylinders, where I found the formula I needed:

{\displaystyle a={\frac {r}{\sin \alpha }}.} 

(ignore the dot, it's just a copy-paste artefact..)

This seemed straight forward. The semi-major axis "a" equals the radius divided by sine of the angle. Great! But that didn't work in Unity.

At this point I had figured out that Mathf.Sin takes radians instead of degrees (despite the documentation saying otherwise), so for this attempt, my code looked like this:

visualScale.z = 0.5f / Mathf.Sin(angle * Mathf.Deg2Rad);

It felt like I was close, but I couldn't google my way to an easy answer, and the math terms were getting to me, so I resorted to blind hacking. Maybe you can see the solution right there, but sometimes you go blind while working on a problem. I tried Cos, I tried moving the variables and I even tried reading again, but it just didn't click. Eventually, I gave up and accepted that my Hazards would just be perfect circles.

But - the animation had gotten stuck in my mind, and after sleeping on it a few times, I still felt like I was too close to let it go. So I went back and stared at it. It was just off by a little - the scale was maybe too small and the animation was out of sync. Reversed. So! I figured that I needed to set the entire length of the cylinder, not just the semi-major axis, and multiplied by two - then, I reversed the angle by subtracting it from 90, because that was the range of change I'd gathered from playing with sine. And it worked!

visualScale.z = (0.5f / Mathf.Sin ( (90 - angle) * Mathf.Deg2Rad )) * 2;

Now, writing up a devlog on this, I realized I could actually do some basic math and reduce the simple function to:

visualScale.z = Mathf.Sin ( (90 - angle) * Mathf.Deg2Rad );

And voila - it looked good and seemed simple. Stupid math. I'm pretty happy about it, but I am fairly certain that I wasted too much time on it and I might as well have used a custom collider and just stretched the whole thing.

Despite this problem being very simple and the math being very available when I started to narrow in my search terms, this was way too difficult to get right. If I knew the math to solve this issue, I wouldn't need to look, and once I started to look, I couldn't understand the results I found. This seems like a major problem with how the terminology of math is constructed. To a math literate, the most elegant representation of an explanation is favourable, because you know what every element means, and you just need a reminder, or a direction. The main point in this example, is how the angle in the formula I found in the Wikipedia article, was incomprehensible to me - and I didn't stop to question it. Of course I should have, but the language surrounding it didn't help at all:

angle  between the secant plane and cylinder axis

I tried looking up secant plane and figured out what the cylinder axis was, but it was too broad for my immediate understanding, and I really didn't have the cognitive stamina to figure out the basic terms of geometry at that point. When looking up math, I would really love to see the skill tree that I need to follow if I am to undertand the problems I'm at.

Stupid math.

Get Gravity Break Prototype

Download NowName your own price

Comments

Log in with itch.io to leave a comment.

The feature I did all this for was scrapped a few weeks later. It took me a few days of messing around. It was worth it.