# Trigonometric Functions

## Prerequisites

1. Basic understanding of scripting in Roblox:
• Know how to access and read properties of instances.
• Understand arithmetic operations in LuaU.
• Familiarity with functions, variables, and more.
1. Basic understanding of elementary mathematics
• Knowledge of geometry and analytic geometry.

### What is trigonometry?

Trigonometry is the mathematical study of the relationships between the angles and sides of triangles, geometric structures composed of three points. Triangles are prevalent in geometry, making them a crucial concept for programmers to understand. In Roblox, trigonometry finds applications in scripting animations, general movement, and physics calculations.

#### What is an angle

An angle represents a measurement of rotation, indicating the difference in direction from the start to the end while rotating around a point (vertex). Angles are commonly measured in degrees, ranging from 0° to 360°, or in radians, as discussed later.

#### Trigonometric functions

For right-angled triangles, characterized by a 90° angle, trigonometric functions come into play. These triangles consist of three sides: the Adjacent side (closest to the angle), the Opposite side (opposite the angle), and the Hypotenuse (opposite the 90° angle).

The relationships between these sides and the angle θ (Greek: Theta) are described by three fundamental trigonometric functions:

1. Sine:

sin(θ) = Opposite side / Hypotenuse

1. Cosine:

cos(θ) = Adjacent side / Hypotenuse

1. Tangent:

tan(θ) = Opposite side / Adjencent side

These functions allow us to find the relationship between two sides of a right-angled triangle for any angle θ.

Understanding the use of trigonometric functions is crucial as they enable the conversion from degrees to a unit proportion, allowing us to determine the actual side lengths in triangles.

##### Unit circle

The trigonometric functions are best visualized with a unit circle, which is a circle with a radius of 1 centered in (0,0). Here we visualize a right angled triangle, where the adjacent side to angle θ, is called cos, and the opposite side is called sin. This is because these are the sides that the functions cos(θ) and sin(θ) correspond to.

Since the hypotenuse is the radius of the circle, 1, then the its length is also 1. This means that we can use the angle to measure the value of sine and cosine of the angle, by finding their lengths in the triangle.

Tangent is represented by the tangentline to the circle, at the point where the hypotenuse intersects with the circle, and its length can also be measured.

The angle θ, creates a right angled triangle in the unit circle, with the radius.

I meantioned earlier that there exists a different method of indexing angles, called radians. For the roblox trigonometric functions, it is typically assumed that the value is given in radians, and so it is important to understand what this means.

Radians measure angles based on the radius of a circle. If you travel along the edge of a circle for a distance equal to the length of the radius, you've covered one radian. We know that the circumference of a circle is = `2*pi*r`, and so half of the circle is `pi radians`, and the complete circle is `2*pi radians`.

In roblox, you would use the following function to convert an angle given in degrees, to radians.

``````math.rad(angle) : number
``````

### Applications of trigonometry in Roblox

The following are functions relating to the basic trigonometry that we have just learned, in roblox.

``````math.sin(angle) : number -- assumed to be in radians -- Sine of an angle
math.cos(angle) : number -- assumed to be in radians -- Cosine of an angle
math.tan(angle) : number -- assumed to be in radians -- Tangent of an angle

math.deg(angle) : number -- convert from radians to degrees
``````

These can be used to perform mathematical calculations in regards to angles. An example of this, is the function below, the purpose of which is to return a point, which lies in relation to a given point on a circle, offset by an amount of the given angle. This could for example, be used to create a camera script, which is centered on a single point, that it rotates around `angle` steps at a time.

``````-- Function to rotate a 2D point around a center by a given angle
local function Get2DPoint(center, angle, point)
-- Extract components. dx means difference in x and dy is the difference in y
local dx = point.X - center.X
local dy = point.Y - center.Y

-- Apply 2D rotation using trigonometric functions
local rotatedX = math.cos(angle) * dx - math.sin(angle) * dy + center.X
local rotatedY = math.sin(angle) * dx + math.cos(angle) * dy + center.Y

-- Return the rotated point as a Vector2
return Vector2.new(rotatedX, rotatedY)
end

``````

We first find the difference in x and y, between the center and the point. dx and dy, create a rightangled triangle, with the radius, between point and center, as the hypotenuse. We then use trigonometric functions, multiplied by these two sides, to find the x and y coordinates of the rotated point. We also add back the center coordinates, to offset the point so it fits on the circle. Finally, we create a Vector2 structure, with the x and y coordinates, and return it.

I know it can be a bit hard to understand exactly how we got this formula, since the road to it deals with a bit more advanced topics, but I wanted to include a very practical example of the trigonometric functions in action, that you could use in your games too.

Another example is the function below, which calculates the gravitational force's impact on a projectile, which is thrown in a given angle, with a given velocity, in 2 dimensions.

``````-- Function to simulate projectile motion
local function SimulateProjectile(projectile, angle, initialVelocity)
local gravity = -9.8 -- Earth's gravity
local time = 0.1

-- A loop that continues, so long as the projectile is in the air
while projectile.Position.Y > 0 do

-- Apply 2D movement using trigonometric functions
local horizontalVelocity = initialVelocity * math.cos(angle)
local verticalVelocity = initialVelocity * math.sin(angle) + gravity * time

-- Set the position
projectile.Position += Vector3.new(horizontalVelocity, verticalVelocity, 0)

wait(0.1)
time = time + 0.1
end
end