# Finding the normal of a point on a sine wave

This topic is 711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey guys does anyone know how to find the normal of a point on a sine wave? i know that the slope (tangent) of a sine wave is the cosine wave, so i can use that to calculate the tangent at a point on a sine wave. However that is y/x and i don't know how to get the individual x and y components out of that. I figure once i have the x and the y components i can swap them and negate one to get the normal. Anyone know how to calculate this? Thank you! (PS im trying to find this out so i can use the normal to do lighting on a height map im manipulating in a vertex shader)

##### Share on other sites
Sine curve is parameterized as (x,sin(x)). A unit-length tangent is (1,cos(x))/sqrt(1+(cos(x))^2) = (tx,ty). A unit-length normal is (ty,-tx).

##### Share on other sites
Dave!

Thank you!

(rating++ for you)

##### Share on other sites
Darn i have a follow up question.

since a sine wave itself doesn't look like realistic waves, i multiple the output of sin by 0.125 to make it 1/8th as tall of a wave.

in the formula you gave, it works great for a full sized sine wave but not with the shrunken sine wave of course.

I tried replacing cos(x) with (cos(x)*.125) in the equation you gave but that didn't give the right results.

do you know how to take this into account?

Thank you again you have been an amazing help

##### Share on other sites
I'm not sure whether Dave's formula is right. It might be, then i just misunderstood it.

Here's how I do that:

We have a scaled sine function, t sin x

Now we differentiate it with respect to x so we get:
D(t sin x) = t D(sin x) = t cos x

From analytic geometry we know that slope of a tangent of a function (derivative) and it's direction angle a satisfy the relation: tan a = k

So we have: tan a = t cos x

Solving for a: a = arctan(t cos x)

Now we have the direction angle which unambigiously defines an unit length tangent vector s:
s = i cos a + j sin a

Knowing the tangent it's easy to get the normal:
n = -i sin a + j cos a

So the normal parametrizes as (-sin a, cos a) where a = arctan(t cos x)

Someone correct me if I did something wrong :)

##### Share on other sites
You can follow the same process Dave used for (x,sin(x)) to find the normal to any curve. Your new curve is (x, sin(x)/8), so a tangent vector is (1, cos(x)/8). Now you need to normalize it, which gives you (1,cos(x)/8)/sqrt(1+cos(x)^2/64). You finally flip the coordinates and change the sign to one of them to get a normal vector.

##### Share on other sites
Let:
y=f(x)
the derivative is:
dy/dx=f'(x)
A tangent's direction vector to f at x is:
(dx,dy)=(dx,f'(x)*dx)=dx(1,f'(x))
(1,f'(x)) is also a tangent's direction of f at x.
a normal vector to f at x is:
(f'(x),-1)

Now if I've understood well you have: f(x)=sin(a*x)
so:
f'(x)=a*cos(a*x)
A normal vector would be:
(a*cos(a*x),-1)
its norm is:
sqrt(1+a^2*cos^2(a*x))
So the normalised normal vector is:
(a*cos(a*x),-1)*1/sqrt(1+a^2*cos^2(a*x))
If we take a=1 the normalised normal vector will be:
(cos(x),-1)*1/sqrt(1+cos^2(x))
Which is what Dave Eberly gave.

##### Share on other sites
Oh, I accidentally read the parentheses wrong. Dave's formula is alright.

However, I understood that only the amplitude of sin was modified, not it's period
Quote:
 Original post by Atrix256since a sine wave itself doesn't look like realistic waves, i multiple the output of sin by 0.125 to make it 1/8th as tall of a wave.

So knighty's formula is calculated right but unfortunately it's not the one we're looking for here
Quote:
 Original post by knightyNow if I've understood well you have: f(x)=sin(a*x)

Substituting the correct(?) fuction to knighty's formula yields:
$\\ \textup{normal}(x, y) = (f'(x), -1)*\frac{1}{\sqrt{f'(x)^2 + (-1)^2}} = (t\, \textup{cos} \,x, -1)*\frac{1}{\sqrt{(t\, \textup{cos} \,x)^2 + 1}} = (t\, \textup{cos} \,x, -1)*\frac{1}{\sqrt{t^2\, \textup{cos}^2 \,x + 1}} = (\frac{t\, \textup{cos} \,x}{\sqrt{t^2\, \textup{cos}^2 \,x + 1}}, \frac{-1}{\sqrt{t^2\, \textup{cos}^2 \,x + 1}})$
whete t is the scaling multiplier (= 1/8 ?)

#EDIT:
It seems that the formula above calculates the right-hand normal that points "inside" the wawes. To calculate the left-hand normal vector (which has positive y-component) you should just multiply the components by -1.

##### Share on other sites
I didn't understand you well :)
You have y=f(x)=a*sin(x)
f'(x)=a*cos(x)
Which gives for the normalized normal vector (that's exactly what alvaro have given):
(a*cos(x),-1)*1/sqrt(a^2*cos^2(x)+1)
substitute a by 1/8 gives:
(cos(x)/8,-1)/sqrt(1+(cos(x))^2/64)

( cos^2(x)==(cos(x))^2. just to be clear )

SPuntte:
Quote:
 #EDIT:It seems that the formula above calculates the right-hand normal that points "inside" the wawes. To calculate the left-hand normal vector (which has positive y-component) you should just multiply the components by -1.

You'r right.

##### Share on other sites
Thank you guys, that seems to have done the trick (:

##### Share on other sites

This topic is 711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 18
• 19
• 11
• 9
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!