Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How does sin and cos work?

Asked by 9 years ago

What do sin and cos do and how can I use them in scripts or loops?

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The sine and cosine functions are mathematical functions. Lua gives them the names math.sin and math.cos.

When graphed, they look like a wavy line.

For an angle t from the positive x-axis (counter-clockwise from the right on a traditional math plot), sin(t) is the y component of the line of length 1 at angle t and cos(t) is the x component (the previously linked gif also illustrates this).

The maximum value of both sine and cosine is 1. The minimum value is -1.

Note that they use radians instead of degrees. While degrees use 360 to form a full circle, radians use 2pi (2*?). Lua includes math.pi as a pi constant; it also gives you math.rad(degs) which converts degs to radians.


Basically, cosine and sine are ways to do math involving angles instead of just positions in space.

They show up when you have an angle but you need an (x, y). (By convention x is usually cosine, y is usually sine)

They come up when you deal with circles or triangles. Also, if you want something to just wiggle back and forth, they are a really good way to do that!


They have nice properties. The derivative of sin(x) is cos(x) and the derivative of cos(x) is -sin(x).

sin(x)^2 + cos(x)^2 = 1, called the Pythagorean identity.

sin(x + pi/2) = cos(x)

and more.


Examples using sine and cosine (use of these functions is usually called trigonometry or trig for short)

-- Plot a pentagon
for i = 1, 5 do
    angle = math.rad( 360 / 5 * i )
    -- first 1/5 the way around,
    -- then 2/5, then 3/5, 4/5, 5/5
    radius = 10
    position = Vector3.new( math.cos(angle) , 0 , math.sin(angle) ) * radius
    local p = Instance.new("Part",workspace)
    p.Anchored = true
    p.BrickColor = BrickColor.new("Bright blue")
    p.Size = Vector3.new(1, 1.2, 1)
    p.Position = position
end
Ad
Log in to vote
-1
Answered by 9 years ago

http://lua-users.org/wiki/MathLibraryTutorial take a look there.

Answer this question