April 27 2024

4 min read

Trigonometry & Taxes

You know something? I never thought I'd struggle so much with trigonometry. It keeps coming back around over and over in every game project. I daresay that "trig" is the most useful math skill for gamedev.

There is an actual infinity between zero and one. In this normalized playground, I find myself returning again and again. Why does using trigonometry matter? How do these mathematical patterns affect gameplay? Lets observe together in the present challenge.

Critical Hits

A critical hit feels much better after being earned. In Elden Ring, you either have to position yourself in a backstab position, or stagger the opponent until they are vulnerable for a special critical strike.

I've personally cared less for the typical turn based random roll critical damage trigger. So in SteelPinion, we have a complex angles system that (you guessed it) can only be implemented with trigonometry!

Each ship will have various angles afforded to them for both defense and offense. Last week, we finally have a decent visual for possible attack angles, as well as critical hit angles too. We have 24 different attack arcs for critical hits, and 8 arcs for regular attacks.

card attack angle

The red zones represent that critical potential. So if you can get your target within the region, it has a much greater chance of doing excess damage. While it is still possible to get criticals from regular attack arcs, we want to give a way to enhance tactical decision making.

Multi-Card Display

As mentioned in the previous article #21, I've got an obsession with card designs. As both a fan and critic of many collectable card games (CCG/TCG), working out a functional yet attractive design for SteelPinion is perhaps overweighted.

Designing the card isn't enough; I want the full experience of holding the cards too. It is much easier to display UI elements in clean blocks or grid like alignment, but that feels really uniform and clean. You may think "what is wrong with clean UI?", to which I respond "nothing is wrong with clean UI!".

Steampunk style cannot be described with the word "clean", we need some asymmetry, we need some natural curves. We need the cards to be presented in a natural holding position to both match the style and meet the obsession.

Card Hand Angles

To get the hand to feel natural, we could use a sine curve, but that actually isn't asymmetrical enough on its own. In the case of card hand placement, we use a manually crafted spline. I am certain it is possible to represent this spline as a pure mathematical function, however, Unreal Engine having support for an entity like this first class is a nice quality of life enhancement for us. Meaning we can dodge figuring out the specific algorithm to reproduce this curve.

card hand spline curve

With this spline, we can very easily spawn cards along the path in 3d space avoiding some of the complex mathematical functions required to discover the precise position. We still have to compute the rotation (or how tilted the card is).

card hand spline curve

We still have to compute the rotation (or how tilted the card is). Trigonometry is avoided here by using a simple linear function based on the card's position in hand. If the card is in the center, then it should be rotated by zero.

card hand rotate logic card hand rotate logic B

Closing Thoughts

Taxes weren't even mentioned! Ahaha! The title was fun, however I don't actually want to pollute the diary with tax wine. I probably should elaborate more about the precise trig algorithms; indeed there is much more elaborated in the 4 minute video I did about it called "Trigonometry in Raw Shaders for Unreal 5 DEVLOG ISSUE 37". Gotta cut the article and get to this kids birthday party.

Join our newsletter!