Hello, developers!
I know math-related functions are very important in scripting.
But I don’t know when I should use them.
In the case of math.pi, I heard that it is used to calculate the parabolic trajectory when scripting grenades, but it seems that it can actually be replaced by math.sin.
I know it might be a bit burdensome, but I would like to know about the usage examples, detailed usage methods, and definitions of each math-related functions.
Any help would be greatly appreciated!
Thank you.
1 Like
math.pi is used in any function that should require the use of pi (obviously), pi is most commonly used to get the surface area of a circle which is π * radius²
Cosine is also used in parabolic trajectory as shown on the Wikipedia page for
Parabolic Trajectories
Basically math.pi is just π
math.huge is the largest number generatable by LUA, in the most common usage is when you need to use a number infinitely large.
math.sin & cos are sine waves, cos is inbetween a sines wave.
math.clamp clamps a number given between a minimum and a maximum.
math.lerp is linear interpolation, which is a + (b - a) * t, A is the start point, B is the end point, and T is the time, 0 is the start point, 1 being the end point. So 0.5 would be in the middle between the two.
Those are the main ones I use on a common basis, there’s a lot more built in math functions that you can check out here
2 Likes
Thank you for replying, and can you please explain about the other situation when I should use math.pi?
I know it is commonly used to get the surface area of a circle but I can’t find when it uses more commonly in games.
And it will be more helpful if you explain about the example situations when you commonly use them include math.sin, math.cos, etc.
Because I already know those math-related functions’ informations but I can’t find commonly used situations.
1 Like
I also use math.pi to place equally-spaced parts around a cycle, for example if I want to make a circular lobby with portals, or an animation with parts rotating around a character.
It’s very useful for any graphics that require cycles and need to be dynamic.
1 Like
That’s interesting. Then how do you use math.pi to make it do that in scripting? And do you often use functions like math.sin and math.cos? If so, when and how do you use them?
1 Like
everything is quite simple - based on basic functions. math.rad(180) - what is it? in essence it looks like this:
local math = {
pi =3,1415
rad = function(angle: number): number
return angle * (math.pi / 180)
end
— other math functions
}
This is the simplest example of a math-related function. And probably the most used one.
(sorry for no tabs, I writed it from phone)
2 Likes
That makes sense. But I want to know when and how developers use those functions. Could you please tell me when you use it?
1 Like
Yes, of course. I will update this message later, when I am free. Now I will just tell about math.rad - this function is used for example to rotate objects. Everyone knows that this happens in radians. That’s why when you want to rotate an object and write CFrame.Angles, you should enter radians in the arguments - if anything, I will explain. In radians, a circle is something like 6.2902, that is, 2pi. so when you rotate an object you have to convert 90 degrees to radians, which is about 1.5 something, or half a pi.
TweenService:Create(obj, tweeninfo.new(1), CFrame = obj.CFrame * CFrame.Angles(0, math.rad(90),0) will rotate obj, here we need to use radians. I am ready to say more, I wrote my engine with 3D rendering in C language. So that you understand - everything you see is also mathematical operations. rotation of the angle? - this is a change of the matrix (see in mathematics). Change position - change matrix. Etc
Here’s an example function using pi.
local function surface_area_of_circle( radius )
return math.pi * ( radius ^ 2 )
end
I use sin and cos for things like viewmodel bobbing, or anything that requires a smooth circular or wave like movement.
math.rad( 90 ), this converts a number in degrees to radians which are used in any CFrame setting.
math.deg( 1 ) this converts a number in radians to degrees which can be used in Vectors or other degree based things.
1 Like