Correct Formulae For A Sinusoid?

I think I have the correct formulae but I am unsure. On the Wikipedia Article is specifies 4 variables of a Sinusoid; Amplitude, ordinary frequency, angular frequency and phase. My code has phase in radians and no ordinary frequency. This may be right but I do not know.

Code

local RunService = game:GetService("RunService")

local PropertiesTable = {
	Amplitude = 1,
	OrdinaryFrequency = 1,
	AngularFrequency = 1,
	Phase = 0
}

RunService.Heartbeat:Connect(function()
	local Amplitude = PropertiesTable.Amplitude
	local OrdinaryFrequency = PropertiesTable.OrdinaryFrequency
	local AngularFrequency = PropertiesTable.AngularFrequency
	local Phase = PropertiesTable.Phase
	
	local Sinusoid = Amplitude * math.sin(AngularFrequency * tick() + math.rad(Phase))
end)

I am confused because AngularFrequency = 2πf
I am correct or do I need to change this?

Is Angular Frequency == 2 * math.pi * OrdinaryFrequency?

the OrdinaryFrequency-dependent formula is often used.

local RunService = game:GetService("RunService")

local PropertiesTable = {
	Amplitude = 1,
	OrdinaryFrequency = 1,
--	AngularFrequency = 1,
	Phase = 0
}

RunService.Heartbeat:Connect(function()
	local Amplitude = PropertiesTable.Amplitude
--	local OrdinaryFrequency = PropertiesTable.OrdinaryFrequency
	local AngularFrequency = 2 * math.pi * PropertiesTable.OrdinaryFrequency
	local Phase = PropertiesTable.Phase

	local Sinusoid = Amplitude * math.sin(AngularFrequency * tick() + math.rad(Phase))
end)
1 Like

Hi!

I am a little short on time, but figure I can still give you a brief explanation. I believe it’s best for you if you go to a library and borrow a high school or, even better, college level textbook. Alternatively, you could watch explanations on You Tube, although they are usually more useful for revisions and visualizing rather than completely explaining yourself the theory. To really understand the basics, you should take a look at linear function and dive into quadratic functions. Along with that you will surely meet exponential, logarithmic, square root and other functions. Two prerequisites to understanding sine waves is also a basic knowledge of function transformations, different quadratic equation forms (standard, factorized and vertex), as well as some basic physics.

Original sine wave formula:

f(t) = A • sin(2πf • t + θ)

I’m sure you already know the meaning of amplitude, but anyway, it is

“the extent or range of a quantity, property, process, or phenomenon: such as the the extent of a vibratory movement (as of a pendulum) measured from the mean position to an extreme.”
(Amplitude Definition & Meaning - Merriam-Webster)

If the contents inside brackets in the above function would not be multiplied by A (amplitude), the result would be the same as multiplying it by 1, which means A is equal to one by default. In case amplitude is equal to zero, that means that there is no amplitude, and thus both peak values lie on the same line (minimum - trough and maximum - crest).

f(t) = 0 • sin(2πf • t + θ)  or  f(t) = 0

With A being equal to one, m = -1 and M = 1 (peak values).

I’ll skip 2π because it’s sort of a scaling factor, except it’s worth to mention that in mathematics and computer science we use radians, because they are a natural choice. See the post below and perhaps other posts around the Dev Forum. When working with radians, one radian represents the length of an arch on unit circle (unit of 1), hence 2*math.pi means circumference.

image

(Amplitude - Assignment Point)

Frequency is “the number of times that a periodic function repeats the same sequence of values during a unit variation of the independent variable.”
(Frequency Definition & Meaning - Merriam-Webster)

So frequency represents the number of cycles per second. The higher the frequency, the more dense the waves seem on the graph and the shorter is wave length and the smaller is the period.

(image by Wikinana38, Frequency - Wikipedia)

What if frequency is 1? That means the point on curve travels around one cycle per second. What if frequency is 0? Sinusoidal doesn’t exist, because

math.sin(0) = 0

What about theta? That’s called phase: a horizontal shift that determines where the sine wave is when t = 0 (time at starting point). It’s sort of an offset from the original sine wave position on coordinate system. It’s worth noting that positive phase shift moves the whole function to the left.

Phase is additional argument, exactly like the fourth one, vertical offset. In linear function, it is recognized as n variable.

-- Linear function
f(x) = kx + n

-- Periodic function
f(x) = A • sin(B • (x - C)) + D

-- A --> amplitude
-- B --> 2π/B = period
-- C --> phase (horizontal offset)
-- D --> vertical offset

I accidentally wrote x + C before and it took me a while to figure out what I was missing. :sweat_smile:

f(x) = sin(x)    ;    a = b = 1, c = d = 0

I would suggest using Desmos Graphing Calculator. It’s truly useful. I found it almost essential when learning myself, and I still do today.

Try adding the following into the first slot.

f\left(x\right)=A\cdot\sin\left(B\cdot\left(x-C\right)\right)\ +D

C and D values should be set to 0 for the start, and A and B values should be both set to 1. That’s the sin(x) function.

Insert sliders and play with them. Observe how sinusoidal behaves. Perhaps check the values, and later on change X-axis steps by clicking on monkey wrench (top right corner). Modify the step to math.pi/2.

image

Hopefully this gives you a head start.

What about some useful videos?

The equation of a wave | Physics | Khan Academy - YouTube (Khan Academy)
Sine waves explained through a story - YouTube (Tibees)
… and more.

I completely support what @MillaGamerF proposed, so the following is their (her) solution, but has deprecated tick() replaced for os.clock() and angular frequency defined outside Heartbeat loop. In the near future, the successor of heartbeat will probably be the prepared PostSimulation, although I don’t know what the difference is.

local RunService = game:GetService("RunService")

local PropertiesTable = {
	Amplitude = 1,
	OrdinaryFrequency = 1,
	-- AngularFrequency = 1,
	Phase = 0
}

--[[
	If properties in PropertiesTable are constant, define all
	values outside the loop (or those you won't be changing).
	In both cases, allocate the variables in advance.
]]
local amplitude, freqency, phase, sinusoid

RunService.Heartbeat:Connect(function()
	amplitude = PropertiesTable.Amplitude
	phase = PropertiesTable.Phase
	freqency = 2 *math.pi *PropertiesTable.OrdinaryFrequency
	
	sinusoid = amplitude * math.sin(freqency * os.clock() + math.rad(phase))
end)

Have a nice day!

PS: You may find this basic post useful: How can I use math.pi In Game Development? - #7 by EssenceExplorer.

2 Likes