Boat wave function, inverse of sine problem

I have created the function ‘wave()’ as part of a boat script to generate a changing sine wave value. The function is attached to a Runservice.Stepped:wait() and runs every Stepped. After calculating an appropriate sine value, the function will calculate a position value, then will alter a parts position, to make it go up or down depending on the sine value.

local waveVal = 0
local increment = 0.02
local floatDepth = 2
local waterOffset = Vector3.new(0,2,0)

function wave()
	waveVal = waveVal + increment

	if waveVal >= (math.pi * 2) then
		waveVal = 0
	end

	local newVal = math.sin(waveVal)
	float.Position = Vector3.new(0, 0 + (newVal * floatDepth), 0) + waterOffset
end

I want to do this on the client once the client takes control of the boat, though the client has no access to the last ‘waveVal’ value used to change the ship position, it results in an instant position change once they take control. I am able to find the last waveVal value from creating a reversed equation, and using the position of the boat.

-- Server position equation
local newVal = math.sin(waveVal)
float.Position = Vector3.new(0, 0 + (newVal * floatDepth), 0) + waterOffset

-- Reversed equation to get newVal and waveVal
newVal = ((float.Position.y - waterOffset.y)/floatDepth)
waveVal = math.asin(newVal) --Reverse the sine.. Correct? you would think, but no

The reversed newVal equation works and gives me the correct value, though when I attempt to reverse the sine to get waveVal they are not the same, the original waveVal and reverse waveVal work, until they pass a certain value.

I’m convinced I’m just not inversing the math.asin(newVal) correctly. I have ran out of ideas on how I can fix this, and I really don’t want to have to use remote events on something which could be done with math.

Problem is probably because of the nature of sine being a periodic function where different inputs can yield the same answer, but not in reverse. Arcsine makes the assumption that the output of sine are all unique, which isn’t the case, so it will only give you a single solution for each domain. In other words, multiple domains can yield the same range, but in reverse that one range can only yield a single domain.

From Wikipedia:

Since none of the six trigonometric functions are one-to-one, they must be restricted in order to have inverse functions. Therefore, the result ranges of the inverse functions are proper (i.e. strict) subsets of the domains of the original functions.

Take this example:

sin(0) = 0
sin(math.pi) = 0
sin(math.pi*2) = 0
sin(math.pi*3) = 0
sin(math.pi*4) = 0
...

There are an infinite number of domains that will give you zero as the answer. However, trying to reverse this function with arcsine:

asin(0) = 0

It can only give you zero, because there can only be one exact answer. Zero is indeed one of the infinite many solutions, but it’s not the only one and it likely isn’t the one you’re looking for.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.