Hi there, I’ve been playing around in Roblox studio, and I suddenly remembered a math function that I planned on using , which was math.pi. My knowledge of this is that it returns a number/constant(which is pi of course, 3.1415926…). However, this is all I see. All I understand is that it gives me a number and that’s it. So, I want this topic to help me understand what can be implemented with this, is this useful with other math functions? Please help out.
Meh. Not the most useful function in coding. However, this could be used to calculate the surcomference of a circle just from the radius/diameter. I could see this being useful if you want to know the circumference of a wheel.
Not as useful in Roblox where CFrame functions are provided for you, but in a lot of circumstances you would need to calculate vectors and angles using it.
I mean, that might not be the most useful question to ask. The value of pi comes in handy when you have a problem that needs it - not that you need to find places to put pi.
For example, I needed to rotate a vector (on a 2D plane). Using trig functions (which work in radians) I needed to get 45 degrees as radians. Which is math.pi/4
.
Which allows me to determine these diagonal edges on a wall rendering module.
I’ve used math.pi in many things, but one of my favorite use cases I’ve used it for is for the Titanic splitting into 2.
(in dev gif in 2018)
The logic behind wanting pi here is in conjunction with a sinusoidal function it creates a smooth wave to simulate the physics of a slow moving ship cracking at first. Then getting quickest while it’s falling, lastly slowing down as the water friction halts gravity’s pull + the stern is still full of air at this time.
I feel like this would come in handy with trig functions, or in general more math, you can’t really do much with only pi. Thanks for the help! (It would be great if more people demostrated this, but for now I’ll call it a day.)
As the matter of fact, we can’t say math.pi is not useful. Pi plays the key role!
In mathematics and computer science, we use radians, and meet math.pi, which is a lua constant storing precise but of course still rounded up infinite number π (3.14159…) a lot. Whenever we are working with whatever angles, CFrames and use trig. functions, we are actually using math.pi in the background constantly. Just take a look at this example:
cf = CFrame.new(15, 2, 12) * CFrame.Angles(math.rad(45), 0, 0)
We created a new coordinate frame and rotated it by 45 degrees. However, computer mechanisms can only work with radians, so we are using math.rad() method to convert degrees to radians. Using math.rad() is easier for those who prefer degrees. The above result is equivalent to
cf = CFrame.new(15, 2, 12) * CFrame.Angles(math.pi/4, 0, 0)
The formula to convert degrees to radians is
1° = π/180
@MrNicNac already mentioned use in trigonometry. I’m trying to explain more throughout this post.
If we care about micro performance optimization, it’s worth stating that it seems logical for math.pi to be faster. Lua, as opposed to some other programming languages, has an extremely fast math library and converting functions. I measured the calculation time six times for each method and removed the two most deviating outcomes. As it turns out
math.pi is 1.5 times faster than math.rad()
The difference is almost negligible, because process time is Xe-7s and math.pi even reached Xe-8s. In some cases it bypassing conversion helps us avoid even more added up potential micro floating point error.
It ultimately comes down to why radians are used in mathematics.
Why are radians more natural choice?
Let’s take a look at the unit circle. The unit circle is a normal circle, which has a center at the origin of coordinate system (0, 0) and has a radius exactly one unit long.
The unit circle is a circle centered at the origin (0,0) that has a radius of exactly one unit.
(Circle radians.gif by Lucas Vieira - left, Wikipedia - Radian)
An arc of a circle with the same length as the radius of the circle subtends an angle of 1 radian. The circumference subtends an angle of 2π radians. (Wikipedia - Radian, 2021-02-28)
We know that the circumference formula is
C = 2π*r
And since the radius of the unit circle is equal to 1 unit, it is very convenient to use radians. Because r, which represents radius parameter, is equal to 1, circumference of a circle is 2π radians, which is equal to 360 degrees.
On the other hand, there are many uses of pi. It is used whenever someone works with cylindrical and other round objects. Calculating round area, planetary motion (if you every decided to make a Roblox solar system ), oscillation (relatively common is Roblox, be it a simulation of pendulum or something else) and so on. Other than that, pi is commonly used in many formulas involving electricity, magnetism, motion physics, infinite product formulas etc.
You use math.pi to work in radians.
Radians are far more useful than degrees when coding or just doing maths in general. You can have whole numbers rather than numbers over 360. You can work in decimals.
Math.pi is literally one of the most useful functions. You need it whenever you are doing anything relating to rotation else you’d be forced to work in degrees.
It’s far better to express a full circle as 2pi than it is to express it as 360 degrees.
A very easy way to illustrate how useful radians is to making your life easier is, calculating the arc length of a sector.
If you are working in degrees, the formula is (Theta/360) * 2pi * radius length
If you are working in radians, the formula is just Theta * radius
Therefore, you’re far better off just using radians. And the above example shows that pi is nearly inescapable. Whenever you are doing literally anything relating to circles, you’re probably going to use math.pi.
Oh I see, so if I wanted to rotate something 180 degrees, I could just rotate it by pi radians since pi radians = 180 degrees. I think your post helped the most!