Sign in to follow this  
Zido_Z

Understanding the sine function

Recommended Posts

So, I knew trigonometry would show up eventually in my time studying the math of game development, but I never was taught the applications of the sine, cos, or arc functions other than finding the values on a graph or simply determining the values of a triangle, which applies to circular functions.

Pretty much, I never knew how little I actually knew in applying the concepts.

So, I'm currently reading the XNA Game Studio Express book and on the first lesson, it is having me program an example program of having a tile of smileys (a texture), scroll at 50 pixels per second. The background of tiled smileys will oscillate up and down, varying its vertical position between 200 px up and 200 px down in a cycle that repeats roughly every 11 seconds, which is 33 degrees per second (something I would like to have explained mathematically why this relates to time). The vertical offset is calculated using the sine trigonometric function, which takes an angle in radians and returns a value between -1 and +1.

I'm not trying to understand how to do this code-wise since the book gives it out, but trying to understand why and how this works mathematically. Here's the code that the book gives:


protected Vector2 m_BkgrTileOffset = Vector2.Zero;

protected const float BkgrDegreesPerSec = 33.0f;

protected void DrawBackgroundTiles(SpriteBatch batch)
{
double degrees = BkgrDegreesPerSec * m_TotalSeconds;
double radians = MathHelper.ToRadians((float)degrees);

m_BkgrTileOffset.X += -50.0f * (float)m_ElapsedSeconds;
m_BkgrTileOffset.Y = (float)(200 * Math.Sin(radians));

}

Share this post


Link to post
Share on other sites
I personally believe that every important concept in math has an associated mental image.

There are two pictures you need in your head for sine and cosine.

The first is a plot of y=sin(x) and y=cos(x). Just generate these with a graphing calculator and look at them.

What's the amplitude? What's the period? What's the frequency? How would you scale these things?

The second mental image is the unit circle.

The angle above the horizontal is 'theta.' The point on the circle is (x,y). The relationship between them is x=cos(theta), y=sin(theta). Get that in your brain.

Finally, you need to "get" how these two images relate. Picture a point moving around the circle, and think about how its x and y coordinates evolve in time. this is what connects the second image to the first.

Heck, write little computer programs that animate these things for you and plot them. Not really joking.

Share this post


Link to post
Share on other sites
Thank you, Emergent. That little review actually made it a whole lot easier for me to realize what is going on. The more you go around the circle, the y increases. But when it reaches PI/2 (sin function), it starts to decrease, going past the 0 and into the negatives until it reaches full circle. And after studying the problems a bit, I realized that sine and cosine are used for keeping things within a range, while also allowing you to reach a "point" in a slowing manner.

So then, how does tan or the "arc" series trig come into the picture?

Share this post


Link to post
Share on other sites
tan of an angle simply equals y/x in Emergent's diagram.

ie.
sin(theta) = y
cos(theta) = x
tan(theta) = sin(theta)/cos(theta) = y/x


The 'arc' or 'a' functions are the inverses. If you have the values of x or y, they allow you to find the angle, theta.

arcsin(y) = theta
arccos(x) = theta
arctan(y/x) = theta

Hope it helps!

Share this post


Link to post
Share on other sites
I am in the same boat as the poster, never got a clear mental image of sin, cos and tan.

So sine is the sine wave.. the name cosine comes from complimenting sine?

it is a relationship where they change in unison.. yin and yang almost

sine goes up cosine goes down.. and vice versa

and tan is gotten by using both sin & cos

something like that?

Share this post


Link to post
Share on other sites
Quote:
Original post by B G
So sine is the sine wave.. the name cosine comes from complimenting sine?


I don't know where the name comes from, but that sounds as good as any explanation I've heard ("co" is usually used as a prefix in math for a "dual" object... I'm not going to elaborate on this here...)

Quote:

it is a relationship where they change in unison.. yin and yang almost

sine goes up cosine goes down.. and vice versa


Yeah, kind of like that. But we can actually be more precise. Have you studied any calculus? Are you familiar with derivatives? The derivative of sine is cosine. The derivative of cosine is negative sine.

I.e., if you're moving counterclockwise around the unit circle, and the line from where you are to the origin is horizontal, then your current velocity vector is vertical, and vice versa. Or, yet more precisely, your velocity is your position vector, rotated 90 degrees counterclockwise.

Quote:
and tan is gotten by using both sin & cos


Yes, but there's more...

Really, I left out the "original" mental image in my above explanation, which is of a triangle. This is the mental image that, as far as I understand, the ancient Greeks had when they thought about these things originally. Here it is:



Tangent is described by the third triangle.

It's useful because, if you have a point (x,y) in the first quadrant,
y/x = tan(theta)
so
theta = atan(y/x).

I.e., whenever you want to get an angle from a pair of coordinates, the tangent function is involved (that said, in practice there's some "which quadrant am I in?" logic you need, and in practical code this is handled by the atan2 function, which takes two arguments, x and y. It's worthwhile to work out how you'd build the atan2 function out of the atan function and some if statements.)

To keep these triangles straight in your head, you can remember the mnemonic "SOHCAHTOA" (the joke goes that this is the name of a Native American princess who studied geometry), which gives you
Sine = Opposite over Hypotenuse
Cosine = Adjacent over Hypotenuse
Tangent = Opposite over Adjacent
but frankly this whole thing smells too much like rote memorization to me; I personally think that if you just remember the mental images and the uses it'll come more naturally.

Share this post


Link to post
Share on other sites
I had heard sohcahtao before but it didn't really explain the guts of the mechanics to me.

The wavelength or graph diagram makes more sense now.

Been doing a little more reading.
So one radian is a triangle where all sides are equal to the radius?
6.28 approx radians being the maximum in a circle.
That means there are 57 degrees approx in 1 radian.

Thanks for the help and patience so far

Share this post


Link to post
Share on other sites
Quote:
Original post by B G
So one radian is a triangle where all sides are equal to the radius?


Not quite. Picture a "pie slice" of the unit circle. The "radians" system of angular measurement is this: If you ask me how large an angle is, I'll tell you how long the arc of that "pie slice" is.

E.g., 90 degrees is, in radians, the length of the arc of a quarter-circle (i.e., pi/2).

Quote:
6.28 approx radians being the maximum in a circle.
That means there are 57 degrees approx in 1 radian.


Yep.
6.28 ~= 2 PI = the circumference of the unit circle.
57 ~= pi/180 = (2 pi)/360 = the angle subtended by an arc of length 1 of the unit circle.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this