Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Any good sources for Cos, Sine and Tan for Scripting. Wikipedia is to equation confusing?

Any good sources for Cos, Sine and Tan for Scripting. Wikipedia is to equation confusing.

Many of the sources I've looked up make the topic very.. very hard to learn.

4 comments

0

Do you mean just the basic concept of trigonometry? Because that's what you need to know in order to apply it to Roblox Lua.

0

ill take that give me "basic concept"

0

You can get knowledge of trigonometry from YouTube videos or high school classes (pre-calculus, calculus, or trigonometry).

0

Do you know any good youtube videos for it.

1 answer

✓ Accepted answer

Cosine and sine convert angles to horizontal and vertical components.

By convention (because of nice properties we get in calculus) we call 0 degrees to the right (+x), and positive angle moves counter clockwise (from +x to +y on the real plane).

Cosine, math.cos in Lua, corresponds (by convention) to the horizontal. Sine, math.sin, corresponds to the vertical.


Definition

Range & Appearance

Cosine and sine vary smoothly from -1 to 1, in a periodic manner. They correspond to points exactly 1 step away from the origin in a given direction. They look like each other; wavy lines; one is just half a wave off from the other.


Angle Conventions

Angles, in trigonometry, use radians. There are 2*PI radians (appx 6.28) in a circle instead of 360 degrees. Thus, for example, 90 degrees is PI/2 radians.

If you want to use degrees instead, math.rad converts degrees to radians and math.deg converts radians to degrees.

We said 0 points to the right and is +x and proceeds counterclockwise, so that it goes from +x to +y to -x to -y back to +x.

Cosine and Sine -- Unit Circle

print( math.cos(0), math.sin(0) )
-- 1 0

We get (1, 0)


If we go another 90 degrees (which is PI/2 radians):

print( math.cos( math.rad(90) ), math.sin( math.pi/2 ) )
-- 0 1

Repeating this, we can get any position on the unit circle. That is because we have the nice property that magnitude of the vector < cos(t), sin(t) > is always 1. That is, (cos(t))^2 + (sin(t))^2 = 1.


Tangent

Tangent, math.tan isn't very important. It usually only comes up as a convenience (particularly when doing calculus). It's defined as the quotient (division) of sine and cosine:

local t = math.random()
print( math.tan(t) , math.sin(t) / math.cos(t) )
-- 1.1160537925447	1.1160537925447

It corresponds to the slope of a line at a given angle. While this may be useful from time to time, because this fails to describe lines pointing in the -x direction and can't describe things directly up or down, it's usually better to just use the pair (cos, sin) instead of characterizing them by their tangent.


Polar Coordinates

In general, cos and sin are useful for converting from polar coordinates to rectangular coordinates.

If you have an angle t and a radius r, then

r * Vector3.new( math.cos(t), 0, math.sin(t) ) will correspond to the point with angle t (in radians) to the x axis and distance r from the origin.


Angle Between Vectors

There are lots of other properties that can use trigonometry, largely dealing with triangles. You'll spend a lot of time talking about them in school, but for the most part they don't turn out to be very useful in practice.

One often useful question is, "what is the angle between two vectors?"

We have a property that a . b = |a| * |b| * cos(t)

  • where |x| is the magnitude of x (x.magnitude in ROBLOX)
  • x . y is the dot product of x and y (x:dot(y) in ROBLOX)
  • t is the angle between them, in radians

Thus

a.unit:dot(b.unit) = cos(t) and t = arccos(a.unit:dot(b.unit)) where arccos is the inverse of cosine, specifically

angle = math.acos( a.unit:dot(b.unit) )

This discussion is preserved as it was. New answers, comments, and votes are closed.