• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

explain trig for a newb

Status
Not open for further replies.
Level 3
Joined
Apr 28, 2010
Messages
48
Hey guys, can someone give me the run down on how cos, sin, tan, etc work exactly? Like cos(camerayaw) for example. I can't figure out how it exactly calculates a cos. I am horrible at math, went to college and still never learned this stuff correctly. No time like the present. Can anyone give me the run down on what a function like that would return.
 
Level 10
Joined
Nov 28, 2008
Messages
655
uuuhhhh

SohCahToa => Some Old Hippy Caught Another Hippy Trippin' On Acid


Sin = Opposite / Hypotenuse
Cos = Adjacent / Hypotenuse
Tangent = Opposite / Adjacent

That means the angle.

It is used to solve for either lengths, or angles.

Say you have one side of a triangle, and the angle, you can then find either of the other sides.

So if you have the distance between points, or the height, or an angle, or any combination of those, it is very easy to get any of the other pieces of information you need.


It really isn't that hard, just go google Trig 101.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,286
You can approximate cos via a maclaurins series for it.
To do this its advisable to reduce the input to an angle between 0 and 2pi (any larger still works but ends up with insane numbers).

sin(x) = x - x^3/3! + x^5/5! - ..... etc infinitly
cos(x) = 1 - x^2/2! + x^4/4! - ..... etc infinitly
tan(x) = sin(x)/cos(x)

Due to the use of factorial, and exponenetial powers, by the 10th term the numers are so large it is practically impossible to use.

As you probably know, this is because they inherit properties from complex numbers. as you may have guessed from the above the maclaurin series for...
e^(x) = 1/0! + x/1! + x^2/2! + ..... etc infinitly
If you make it into a polar exponential, you get an imaginary sin part and a real cos part. As x becomes ix it is real every even power and imaganary every odd. As you cancle the odd imaginary bits out to get sin in a real form it becomes the series provided eariler. Cos is easy to get as all real terms in the series will attach to cos as it is the only real term.

This is not how computers calculate them, this would be far to slow and waste too many cycles for it to be usable outside of server farms or non real time stuff. Instead computers use an extreemly complex system of tables and corrections (really advanced maths) to compute an approximation to a sin and cos entry accuratly. As floating points (the usual way of storing decimals on computers) are nothing but approximations of a number, this works pretty well.

For speed the method is often built into the hardware, thus is invisible to software programmers.

Anyway, as you already know e^ix = cos(x)+isin(x). From that it is clear the corrections are logarithmic or exponential.

This is all university 1 stuff... I am fearing next year as I have to do maths again (engineering maths so it should be more practicle and less out of this world).

For example, firstly a few terms from the maclaurin series are evaluated to get an acccurate approximation of sin and cos at certain fixed values on run up (or built into software or whatever). Then you interperlate the 2 closest table entries to what you are after using a logarithmic correction. The result is an accurate (slightly less than the tables) approximation to the trig function you were after. As the approximation is more accurate than the floating points can represent, it works near perfectly however in high precision mathimatics or itterative maths (where a value is used to work out a next and so on) the approximation error can destroy any relevance of the output. Thus they need to use their own custom larger floating points and decimals as well as more accurate trig algerthims.
 
Level 13
Joined
Feb 28, 2007
Messages
477
This guy doesn't understand trig. So you teach him calculus? Really now...

Granted, I learned this stuff in high school and took the AP test on it, but before my junior year, this would have been nonsensical to me. Based on the fact that he doesn't know trig, it's safe to say that he has not learned Taylor series, or pretty much any other calculus yet. Just saying.

Besides, I fail to see how anyone would actually need to get values more accurate than the floating point limit in SC2, especially with the timer limit of 1/16 or 1/32, whichever it's at now.
 
Level 3
Joined
Apr 28, 2010
Messages
48
Well, I feel bad now that Dr. Super Good took all the time out to write all that. That, I was not being clear enough in my intentions for the topic. Suprisingly I think it answers another question however, I probably would of never asked. And, that is why units don't register on an X,Y plane when reaching speeds of 160+.

I did try to refresh my memory on how trig works because I was confused. Not on how the math is done, but how the math is implemented. Cos(CameraYaw). Where are the variables to make this an equation in the first place? You have cos(which is undefined?), and the angle in which the camera is facing(CamYaw). What other variables are making this equation complete if it is an equation at all.
 
Level 10
Joined
Nov 28, 2008
Messages
655
The world can be converted into triangles.

All shapes can be broken down into triangles. (Anything else that would need an arc comes from circles, which is also in Trig but that will be another discussion)

When we say the cos(angle), we mean that we want to "do" cosine, to that angle.

What does that mean?

Well, it is kinda like if I said (angle)^2

It means we are doing something to that angle.


Now, if I tell you that X * Y = Z
Then I tell you X and Z are equal to 5 and 10, you could easily figure out that Y = 2.

Trig is the same way.

tri.jpg

There is a triangle.


/*
I should clarify, it is a RIGHT triangle, meaning there is a 90 degree angle. all shapes can be broken down into right triangles and/or circles
*/


With the information of A, B, C, and either of the angles that is not the 90, I can find out the rest of the info.

So when it is said that:
sine = opposite / hypotenuse

That means that the sin of an angle is equal to the opposite side,, A would be opposite in that picture,, divided by the hypotenuse ,,that would be C, or the longest side on the triangle.

So it is the same as saying X * Y = Z
Sin(angle) = OppositeSide / Hypotenuse


So now if we have both sides, we can find the angle.
If we have one side and one angle, we can find the other side.

With all of the trig functions, sine, cosine, tangent, secant, cosecant, cotangent, we can use them to find any piece of the triangle based on any 2 other pieces.

So having the Yaw, which is the angle, if u have an distance, or height, or whatever becomes the other piece of the triangle, you can figure out the rest.

It is hard to explain over the internet...
 
Level 2
Joined
Dec 1, 2007
Messages
11
And thats what I gave you, a maclaurin series to calculate cos and explained to you how the computer works it out.

(I think) he doesn't want a literal method of calculating sin, cos, and tan. He wants to understand what the inputs and outputs mean, and thus "how" it calculates them. (Good job to tagg1080 for his explanation.)

But yeah, that is a linear approximation method of calculating sin, cos and tan. Math is fun. :)
 
Level 3
Joined
Apr 28, 2010
Messages
48
Ok, so I understand how to find cos, sin, tan. I just don't know where the variables are coming from. A,B,C.

Cos(CameraYaw)

This is how I see it.

63584298.jpg


Camera Yaw of 135, that is one line. How does it find or know which is what, Adjacent or Hypotenuse. Would the X plane count as the adjacent.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,286
He means how to use trig, not how trig is calculated, you guys need to work on your english as there is no way that those 2 mean the same thing.

Well trig is used basically to convert an angle (usually in radians) into a length of side of a triangle. As the hypotenuse of the right angled triangle is always 1 (cos(0) = 1) that means that sin returns the length of the opposite side (line not used to make measure the angle inputed). Cos returns the length of the side adjecten to the angle, the non hypotenuse side that is used when measuring the angle.

Tan is sin/cos aka the opposite/adjectent.
 
Level 9
Joined
Nov 28, 2008
Messages
704
In your picture, just draw a line from the end of the angled line to the x axis.

The opposite end is sin (sort of) and the line along the x axis is cos (sort of).

Trig is just a way of getting the lengths of x and y sides of a triangle.

500px-Unit_circle_angles.svg.png


In the picture, the (x, y) represents sin and cos. x is cos, y is sin.

I dont have time to go into anything huge, sorry. Maybe later I'll edit this post.
 
Level 2
Joined
Dec 1, 2007
Messages
11
Ok, so I understand how to find cos, sin, tan. I just don't know where the variables are coming from. A,B,C.

Cos(CameraYaw)

This is how I see it.

<img>

Camera Yaw of 135, that is one line. How does it find or know which is what, Adjacent or Hypotenuse. Would the X plane count as the adjacent.

Adjacent is the X plane/axis/length, opposite is the Y plane/axis/length, and hypotenuse is the vector/red line you're showing.

One way to use all these functions would be:

You want to move the camera, say, 11 "units" forward. You have the angle the camera is facing, let's call it CameraYaw. (Let's assume it's only a two-dimensional plane.) We'll say the current location of the camera is: CameraX, CameraY.


The new X of the location you want the camera is going to be:

NewCameraX = CameraX + (11 * cos(CameraYaw))

(or NewCameraX = the old CameraX + (the distance you want to travel * cos(the angle you want to travel)))


The new Y of the location you want the camera is going to be:

NewCameraY = CameraY + (11 * sin(CameraYaw))


Note that sin(CameraYaw) and cos(CameraYaw) could be negative, if the camera is not facing forward.
 
Status
Not open for further replies.
Top