I want to add the method ‘wave:getNormal(x, z, t)’, where x and z are coordinates and t is the time in seconds. The method should return a unit normal of a given point on the mathematical function specified in ‘wave:getValue(x, z, t)’. ‘self.CONFIG’ contains some wave parameters like HEIGHT and LENGTH. This is the relevant code:
local wave = {}
-- This method is used for giving a direction to the wave
function wave:getP(x, z, t)
return Vector2.new(Vector2.new(x, z):Dot(self.config.DIRECTION), Vector2.new(z, x):Dot(self.config.DIRECTION)) * math.pi / self.config.LENGTH
end
-- This method is used to get the height of the wave
function wave:getValue(x, z, t)
local LENGTH = self.config.LENGTH
local p = self:getP(x, z, t)
-- This is the maths function that should be related to wave:getNormal(x, z, t)
return math.sin(p.x - t * self.config.SPEED) * math.sin((p.x + p.y) / 5) * self.config.HEIGHT + self.config.BIAS
end
function wave:getNormal(x, z, t)
-- Not sure how to calculate this
end
The problem is that I do not know how to calculate the unit normal. All I know is that it has something to do with derivatives and the tangent line of the wave’s maths function. Here is an illustration of what I want to calculate, where the blue part is my wave:
I have tried to mess around with derivatives, but I have come to the conclusion that I do not have enough knowledge about derivatives and vectorial equations.
I should probably post this on a maths forum, but I thought I can at least try to ask it here.