June 2, 2016

## Math: The Hypotenuse and PI

We left off with one issue in the previous article: with our CharacterController, the ball does not spin as it moves forward.
So, we need to simulate its rotation. While with a RigidBody all that was done to us automatically by Unity’s physics engine, we now have to manually spin the ball. To achieve that, we need some simple math.
The goal of this post is to show you how some good old math goes a long way in game development, and its always good to learn some math, specially trigonometry, at least the basic stuff!
Ok, let’s start with our friend, PI (π). What is that number?
Let’s imagine a circle. We are interested in two parameters: the circle’s circumference and its diameter, as shown here:
Imagine you have a circle made of rope, and you have a stick the size of your circle’s diameter. If you lay that rope straight, you’ll end up having the circumference of that circle. Well, PI is how many times the stick fits into the straight rope. In other terms, PI is how many times the diameter fits into the circumference. And that is 3.1416 times.
So what does that mean to us? Simple. We know that our ball will make a full 360 spin for every 3.1416 world units it advances. So a code similar to this one will make it spin realistically enough:

// Calculate the distance traveled this frame Vector3 newPos = transform.position; float distanceTraveledThisFrame = Vector3.Distance(newPos, previousPos); // Calculate how many PI‘s we moved in the last frame float piRatio = distanceTraveledThisFrame / Mathf.PI; // Now, apply the rotation multiplying it by a full rotation (360f) ball.Rotate(piRatio * 360f, 0, 0, Space.Self);That wasn’t that hard, and it was a practical example on when you need math. Here’s another: our tile floor is based in a quad that is -just because we decided- exactly 1 world unit width and 1 world unit length, so it’s a square. We can safely say our game is based on a square grid. Now, square grids are extremely easy to handle in many, many ways, but it creates an issue for us: a square measuring 1 world unit means the distance between a side to another in a straight line is 1. So, if we instantiated a tile, to instantiate the next, we simply move forward by 1 unit, and we are in the exact position to place it. But, when we start turning we see an issue: when we rotate to the right, if we move 1 unit forward, we will be exactly in the right place to put the next tile, but we’ll no longer be in our “grid”. Check out the following image: you can see how each tile starts at a grid intersection. But the last one, the rotated one, will leave us starting outside our “grid”. This does not necessarily means a problem, but if we think ahead a bit, we realize we will be having pickups in the game. And following a grid makes pickup spawning much easier because having the pickups, the tiles, and the ball follow a grid guarantees to us the pickups will be reachable and not missed by a tiny bit. So how do we solve it so we can be in grid intersections the whole time? Well, another mathematical term comes to help: The Hypotenuse. The hypotenuse is the longest side of a right-sided triangle. As shown in the picture, if we imaginary extend that triangle and add another one to form a square, we can say the hypotenuse is the diagonal. And, thanks to the math again, we can find out how much the hypotenuse value is in our 1 world unit square. We calculate it using the Pythagorean theorem, which for us it says (in our case and in horrible, horrible terms) the diagonal of our square is the square root of the sum of two of our four sides. Since our sides measure 1, our equation is:

diagonal = square root of 1 + 1 or diagonal = square root of 2 which is roughly 1.4142So, if we want to keep our tiles on grid intersections, the length of the tile for rotations needs to be 1.4142 in length instead of just 1. Here’s how it looks with the new “long” tile: As you can see, there’s a lot to learn from math. And trust me, using Unity (or any other game engine) really solves tons of problems. I come from a time where you had to code your own physics (collisions, sliding, slope climbing, etc.). The important message of this post is to realize how game programming involves a lot of disciplines from the exact sciences (such as math and physics), the arts (design, modeling, sounds, music, movie direction), the social sciences (economics, history), etc. Now, before we go any further into programming, I want to share a life saviour: Git. Don’t forget to follow us on twitter for news regarding articles and game development [twitter-follow screen_name=’indelvestudios’]

Spell Run – The backstage of a game | indelve

June 2, 2016 @ 3:14 am

[…] Math: The Hypotenuse and PI […]

Spell Run – Physics: Rigidbody vs CharacterController | indelve

June 3, 2016 @ 12:31 am

[…] So we’ll need to make the ball rotate in two axis: X axis so the ball rotates as it moves forward (like a car wheel), and on the Y axis when it needs to rotate to the left or to the right. We deal with that in our next article. […]

Creating an automated level tester | indelve

June 7, 2016 @ 5:15 pm

[…] we mentioned in previous chapters like this one, working with a grid goes a long way. And once more comes back to aid us […]

Spell Run – Physics: Rigidbody vs CharacterController – Indelve

June 30, 2016 @ 1:00 pm

[…] So we’ll need to make the ball rotate in two axis: X axis so the ball rotates as it moves forward (like a car wheel), and on the Y axis when it needs to rotate to the left or to the right. We deal with that in our next article. […]