Jump to content

View more

Image of the Day

New ninja for my game.
#screenshotsaturday #gamedev #indiedev #IndieDevHour
https://t.co/sJdnPuiVgh
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.


Sign up now

Finding the normal of a point on a sine wave

2: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
9 replies to this topic

#1 Atrix256   Members   

539
Like
0Likes
Like

Posted 26 October 2009 - 05:12 PM

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)

#2 Dave Eberly   Members   

1173
Like
1Likes
Like

Posted 26 October 2009 - 05:26 PM

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).

#3 Atrix256   Members   

539
Like
0Likes
Like

Posted 26 October 2009 - 05:36 PM

Dave!

Thank you!

(rating++ for you)

#4 Atrix256   Members   

539
Like
0Likes
Like

Posted 26 October 2009 - 07:32 PM

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

#5 SPuntte   Members   

158
Like
0Likes
Like

Posted 27 October 2009 - 12:31 AM

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 :)


#6 Álvaro   Members   

20816
Like
0Likes
Like

Posted 27 October 2009 - 01:36 AM

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.

#7 knighty   Members   

313
Like
0Likes
Like

Posted 27 October 2009 - 01:52 AM

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.

#8 SPuntte   Members   

158
Like
0Likes
Like

Posted 27 October 2009 - 02:42 AM

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 Atrix256
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.

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

Substituting the correct(?) fuction to knighty's formula yields:

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.

#9 knighty   Members   

313
Like
0Likes
Like

Posted 27 October 2009 - 02:43 AM

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:
I can't read your formulas :)
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.

#10 Atrix256   Members   

539
Like
0Likes
Like

Posted 27 October 2009 - 04:28 AM

Thank you guys, that seems to have done the trick (:




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.