Derivatives: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
No edit summary
>NXTBoy
No edit summary
 
(26 intermediate revisions by 3 users not shown)
Line 1: Line 1:
__TOC__
== Introduction ==
This tutorial is intended to introduce you to the mathematical notion of derivatives.
== How will this page help me ==


==Goal==
This page will help smooth out your [[Terrain Generation|Terrains]], if you use mathematical formulae to create them.  In ROBLOX, however, we do not work with mathematical "points", but "bricks", which occupy space.  Therefore, in order to smooth out terrains and functions, you will want to know the slopes of your functions... to know the slopes of your terrains.
This page will help smooth out your [[Terrain Generation|Terrains]], if you use mathematical formulae to create them.  In ROBLOX, however, we do not work with mathematical "points", but "bricks", which occupy space.  Therefore, in order to smooth out terrains and functions, you will want to know the slopes of your functions... to know the slopes of your terrains.


[[Image:Derivative.PNG|thumb|An example of how you can use Derivatives]]
[[Image:Derivative.PNG|thumb|150px|An example of how you can use Derivatives]]
 
== What is a derivative? ==


==Introduction==
A derivative is the slope of a function, or, phrased differently, the difference in the height divided by the difference in width at that point.
A derivative is the slope of a function, or, phrased differently, the difference in the height divided by the difference in width at that point.


The [[Ramps]] article provides a method on how to rotate a brick:
The [[Ramps]] article provides a method on how to rotate a brick:


<pre>
{{lua|=
game.Workspace.slope.CFrame=CFrame.new(Vector3.new(0,100,0)) * CFrame.fromAxisAngle(Vector3.new(0,0,1), math.pi/2)
game.Workspace.slope.CFrame = CFrame.new(Vector3.new(0, 100, 0))
</pre>
                            * CFrame.fromAxisAngle(Vector3.new(0, 0, 1), math.pi / 2)
}}


Here we have the brick being created in its position in the first CFrame, and the rotation in the second CFrame.  This is where the derivative comes in.  In the script below, z=-x^2.  The derivative for this is -2*x, which you see in the script below.
Here we have the brick being created in its position in the first CFrame, and the rotation in the second CFrame.  This is where the derivative comes in.  In the script below, z=-x^2.  The derivative for this is -2*x. (Why does -x work better than -2*x???)


<pre>
{{lua|=
x=0
function f(x)            return -x^2 end
z=0
function f_derivative(x) return 2*x end
for i = -2.5,2.5, .01 do
x=i
z=-x^2


p = Instance.new("Part")
for x = -1.8, 1.8, .01 do
p.CFrame=CFrame.new(Vector3.new(100*x+100, 100*z+100, 100))*CFrame.fromAxisAngle(Vector3.new(0,0,1), 2*x*math.pi/180)
    local y = f(x)
    local dydx = f_derivative(x)
p.Size = Vector3.new(1,1,1)
    local slopeAngle = math.atan(dydx)
p.Anchored = true
p.BottomSurface = "Smooth"
p.TopSurface = "Smooth"
p.Parent = game.Workspace
p.BrickColor = BrickColor.new(217)


    local p = Instance.new('Part')
    p.CFrame = CFrame.new(100 * Vector.new(x, y, 0))
            * CFrame.fromAxisAngle(Vector3.new(0, 0, 1), slopeAngle)
    p.Size = Vector3.new(1, 10, 1)
    p.Anchored = true
    p.BottomSurface = 'Smooth'
    p.TopSurface = 'Smooth'
    p.Parent = game.Workspace
    p.BrickColor = BrickColor.new(217)
end
end
</pre>
</pre>}}


== What about trigonometric functions? ==
You can smooth out trigonometric functions, too. 


Yes, you can smooth out trigonometric functions, too. 
[[Image:Derivative2.PNG|thumb|150px|Sin wave (yes, it's flipped around)]]


[[Image:Derivative2.PNG|thumb|Sin wave (yes, it's flipped around)]]
{{Example|<pre>
function f(x)            return math.sin(x) end
function f_derivative(x) return math.cos(x) end


<pre>
for x = -2.5, 2.5, .01 do
x=0
    local y = f(x)
z=0
    local dydx = f_derivative(x)
for i = -2.5,2.5, .01 do
    local slopeAngle = math.atan(dydx)
x=i
y=math.sin(i) -- here is the formula


p = Instance.new("Part")
    local p = Instance.new('Part')
p.CFrame=CFrame.new(Vector3.new(10*x, 10*y, 10))*CFrame.fromAxisAngle(Vector3.new(0,0,1), math.cos(i))
    p.CFrame = CFrame.new(Vector3.new(10 * x, 10 * y, 10)) * CFrame.fromAxisAngle(Vector3.new(0, 0, 1), slopeAngle)  
--[[cos is the derivative of sin --]]
p.Size = Vector3.new(1,1,1)
p.Anchored = true
p.BottomSurface = "Smooth"
p.TopSurface = "Smooth"
p.Parent = game.Workspace
p.BrickColor = BrickColor.new(217)


    --cos is the derivative of sin
    p.Size = Vector3.new(1,1,1)
    p.Anchored = true
    p.BottomSurface = 'Smooth'
    p.TopSurface = 'Smooth'
    p.Parent = game.Workspace
    p.BrickColor = BrickColor.new(217)
end
end
</pre>
</pre>}}


== 3D terrain ==
==3D Terrain==


[[Image:Smooth.PNG|thumb|Smoothed out 3D terrain]]
[[Image:Smooth.PNG|thumb|150px|Smoothed out 3D terrain]]


This is the sinx+siny formula from [[Terrain Generation]], only smoothed out now.  (This needs to be double checked.)
This is the sinx+siny formula from [[Terrain Generation]], only smoothed out now.  (This needs to be double checked.)


<pre>
{{Example|<pre>
x=0
local x, y = 0, 0
z=0
for i = -2,2, .1 do
for i = -2,2, .1 do
for j = -2,2, .1 do
    for j = -2,2, .1 do
 
        y = math.sin(i) + math.sin(j) -- here is the formula
y=math.sin(i)+math.sin(j) -- here is the formula
 
p = Instance.new("Part")
p.CFrame=CFrame.new(Vector3.new(10*i, 10*y, 10*j))*CFrame.fromAxisAngle(Vector3.new(0,0,1), math.cos(i)+math.cos(j))
p.Size = Vector3.new(1,1,1)
p.Anchored = true
p.BottomSurface = "Smooth"
p.TopSurface = "Smooth"
p.Parent = game.Workspace
p.BrickColor = BrickColor.new(217)


        local p = Instance.new('Part')
        p.CFrame=CFrame.new(Vector3.new(10 * i, 10 * y, 10 * j)) * CFrame.fromAxisAngle(Vector3.new(0, 0, 1), math.cos(i) + math.cos(j))
        p.Size = Vector3.new(1,1,1)
        p.Anchored = true
        p.BottomSurface = "Smooth"
        p.TopSurface = "Smooth"
        p.Parent = game.Workspace
        p.BrickColor = BrickColor.new(217)
    end
end
end
end
</pre>}}
</pre>
 
== See Also ==
 
[[Ramps]]
 
[[Terrain Generation]]


[http://en.wikipedia.org/wiki/Derivative Wikipedia article on Derivatives]
==See Also==
*[[Ramps]]
*[[Terrain Generation]]
*[http://en.wikipedia.org/wiki/Derivative Wikipedia article on Derivatives]


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]
[[Category:Mathematical Methods]]

Latest revision as of 13:31, 11 March 2012

Goal

This page will help smooth out your Terrains, if you use mathematical formulae to create them. In ROBLOX, however, we do not work with mathematical "points", but "bricks", which occupy space. Therefore, in order to smooth out terrains and functions, you will want to know the slopes of your functions... to know the slopes of your terrains.

An example of how you can use Derivatives

Introduction

A derivative is the slope of a function, or, phrased differently, the difference in the height divided by the difference in width at that point.

The Ramps article provides a method on how to rotate a brick:

game.Workspace.slope.CFrame = CFrame.new(Vector3.new(0, 100, 0))
                            * CFrame.fromAxisAngle(Vector3.new(0, 0, 1), math.pi / 2)

Here we have the brick being created in its position in the first CFrame, and the rotation in the second CFrame. This is where the derivative comes in. In the script below, z=-x^2. The derivative for this is -2*x. (Why does -x work better than -2*x???)

function f(x)            return -x^2 end
function f_derivative(x) return 2*x end

for x = -1.8, 1.8, .01 do
    local y = f(x)
    local dydx = f_derivative(x)
    local slopeAngle = math.atan(dydx)

    local p = Instance.new('Part')
    p.CFrame = CFrame.new(100 * Vector.new(x, y, 0))
             * CFrame.fromAxisAngle(Vector3.new(0, 0, 1), slopeAngle)
    p.Size = Vector3.new(1, 10, 1)
    p.Anchored = true
    p.BottomSurface = 'Smooth'
    p.TopSurface = 'Smooth'
    p.Parent = game.Workspace
    p.BrickColor = BrickColor.new(217)
end
</pre>

You can smooth out trigonometric functions, too.

Sin wave (yes, it's flipped around)
Example
function f(x)            return math.sin(x) end
function f_derivative(x) return math.cos(x) end

for x = -2.5, 2.5, .01 do
    local y = f(x)
    local dydx = f_derivative(x)
    local slopeAngle = math.atan(dydx)

    local p = Instance.new('Part')
    p.CFrame = CFrame.new(Vector3.new(10 * x, 10 * y, 10)) * CFrame.fromAxisAngle(Vector3.new(0, 0, 1), slopeAngle) 

    --cos is the derivative of sin

    p.Size = Vector3.new(1,1,1)
    p.Anchored = true
    p.BottomSurface = 'Smooth'
    p.TopSurface = 'Smooth'
    p.Parent = game.Workspace
    p.BrickColor = BrickColor.new(217)
end


3D Terrain

Smoothed out 3D terrain

This is the sinx+siny formula from Terrain Generation, only smoothed out now. (This needs to be double checked.)

Example
local x, y = 0, 0
for i = -2,2, .1 do
    for j = -2,2, .1 do
        y = math.sin(i) + math.sin(j) -- here is the formula

        local p = Instance.new('Part')
        p.CFrame=CFrame.new(Vector3.new(10 * i, 10 * y, 10 * j)) * CFrame.fromAxisAngle(Vector3.new(0, 0, 1), math.cos(i) + math.cos(j)) 
        p.Size = Vector3.new(1,1,1)
        p.Anchored = true
        p.BottomSurface = "Smooth"
        p.TopSurface = "Smooth"
        p.Parent = game.Workspace
        p.BrickColor = BrickColor.new(217)
    end
end


See Also