|
|
Line 75: |
Line 75: |
| ''-- prints 20''}} | | ''-- prints 20''}} |
| | | |
− | {{ScriptFunction|float|MoveTowards|(float value, float target, float delta);|Move value to target, but by no more than delta}} | + | {{ScriptFunction|float|MoveTowards|(float value, float target, float delta);|Move value to target, but by no more than delta|5=local sliderStart = 5.0;<br> |
| + | local sliderStop = 10.0;<br> |
| + | local deltaChange = 1.0;<br><br> |
| + | local currentSliderPos = '''Space.Math.MoveTowards'''(sliderStart, sliderStop, deltaChange);<br> |
| + | Space.Log(currentSliderPos);<br> |
| + | ''-- prints 6 (moves forward 1)''<br><br><br> |
| + | local sliderStart = 5.0;<br> |
| + | local sliderStop = 10.0;<br> |
| + | local deltaChange = 7.0;<br><br> |
| + | local currentSliderPos = '''Space.Math.MoveTowards'''(sliderStart, sliderStop, deltaChange);<br> |
| + | Space.Log(currentSliderPos); |
| + | ''-- prints 10 (caps out at at the target value)''<br><br> |
| + | ''-- NOTE: Use negative delta to move away from target.''}} |
| + | |
| {{ScriptFunction|float|MoveTowardsAngle|(float value, float target, float delta);|Move angle value to target, but by no more than delta}} | | {{ScriptFunction|float|MoveTowardsAngle|(float value, float target, float delta);|Move angle value to target, but by no more than delta}} |
| {{ScriptFunction|int|NextPowerOfTwo|(int val);|Return the next power of two larger or equal to val}} | | {{ScriptFunction|int|NextPowerOfTwo|(int val);|Return the next power of two larger or equal to val}} |
Revision as of 04:26, 21 April 2017
The SMath class contains common math functions, see also SVector and SQuaternion for Vector and Quaternion related math functions.
Fields
Pi
Returns the constant value of Π (readonly).
Space.Log(Space.Math.Pi);
-- prints 3.14159274101257
Members
Random
Returns a random float between 0 and 1 (inclusive)
local randomNumber = Space.Math.Random();
Space.Log(randomNumber);
-- prints 0.689094245433807
RandomRange
float RandomRange
(float min, float max);
Returns a random float between min and max (inclusive)
local min = 100.0;
local max = 500.0;
local randomNumber = Space.Math.RandomRange(min, max );
Space.Log(randomNumber);
-- prints 0206.659149169922
RandomInteger
int RandomInteger
(int min, int max);
Returns a random float between min (inclusive) and max (exclusive)
local min = 50;
local max = 75;
local randomInteger = Space.Math.RandomInteger(min, max);
Space.Log(randomInteger);
-- prints 52
Abs
Returns the absolute value of 'val'
local value = -4.2
local absoluteNum = Space.Math.Abs(value);
Space.Log(absoluteNum);
-- prints 4 [BUG] should be 4.2
Abs
Returns the absolute value of 'val'
local value = -4
local absoluteNum = Space.Math.Abs(value);
Space.Log(absoluteNum);
-- prints 4
Acos
Returns the arc cosine value of 'val'
local value = 0.5;
local arcCosine = Space.Math.Acos(value);
Space.Log(arcCosine);
-- prints 1.04719758033752 (radians) which is 60 degrees
Approximately
bool Approximately
(float a, float b);
True if the difference between a and b is less than epsilon
local a = 5.0;
local b = 5;
local approx1 = Space.Math.Approximately(a, b);
Space.Log(approx1);
-- prints true
local a = 5.01;
local b = 5.0;
local approx2 = Space.Math.Approximately(a, b);
Space.Log(approx2);
-- prints false
local a = 5.01;
local b = 5.0;
local approx3 = Space.Math.Approximately(a, b);
Space.Log(approx3);
-- prints false
Asin
Returns the arc sine value of 'val'
local value = 0.5;
local arcSine = Space.Math.Asin(value);
Space.Log(arcSine);
-- prints 0.523598790168762 (radians) which is 30 degrees
Atan
Returns the arc tangent value of 'val'
local value = 1.732050808;
local arcTangent = Space.Math.Atan(value);
Space.Log(arcTangent);
-- prints 1.04719758033752 (radians) which is 60 degrees
Atan2
bool Atan2
(float y, float x);
Returns the arc tangent of y/x
local x = 0.5;
local y = 0.5;
local arcTangent = Space.Math.Atan2(y, x);
Space.Log(arcTangent);
-- prints 0.785398185253143
Ceil
Returns the ceil value of 'val' as an integer
Space.Log(Space.Math.Ceil(4.0));
-- prints 4
Space.Log(Space.Math.Ceil(4.2));
-- prints 5
Space.Log(Space.Math.Ceil(-4.2));
-- prints -4
Clamp
float Clamp
(float val, float min, float max);
Clamps val between min and max, and returns the result
local value = 100.0;
local min = 20.0;
local max = 82.0;
local clampedValue = Space.Math.Clamp(value, min, max);
Space.Log(clampedValue);
-- prints 82
Clamp01
float Clamp01
(float val);
Clamps val between 0 and 1, and returns the result
local value = -1.0;
local clampedValue = Space.Math.Clamp01(value);
Space.Log(clampedValue);
-- prints 0
ClosestPowerOfTwo
int ClosestPowerOfTwo
(int val);
Returns the closest power of two to val
local value = 33;
local powerOfTwo = Space.Math.ClosestPowerOfTwo(value);
Space.Log(powerOfTwo);
-- prints 32
Cos
Returns the cosine of val
local angle = 1.04719758033752; -- converts to 60 degrees
local cosine = Space.Math.Cos(angle);
Space.Log(cosine);
-- prints 0.499999970197678
local angle2 = Space.Math.Pi/3; -- converts to 60 degrees
local cosine2 = Space.Math.Cos(angle2);
Space.Log(cosine2);
-- prints 0.499999970197678
DeltaAngle
float DeltaAngle
(float current, float target);
Returns the difference in degrees between two values (e.g. 350' and 17' returns 27')
Space.Log(Space.Math.DeltaAngle(350.0, 17.0));
-- prints 27
Exp
Returns e raised to val power.
Space.Log(Space.Math.Exp(3.0));
-- prints 20.0855369567871
Floor
Returns floor of val, converted to an int
Space.Log(Space.Math.Floor(4.0));
-- prints 4
Space.Log(Space.Math.Floor(4.2));
-- prints 4
Space.Log(Space.Math.Floor(-4.2));
-- prints -5
GammaToLinearSpace
float GammaToLinearSpace
(float val);
Converts a colour value from Gamma to Linear Space (Pow 2.2)
Space.Log(Space.Math.GammaToLinearSpace(0.5));
-- prints 0.214041143655777
InverseLerp
float InverseLerp
(float a, float b, float val);
Returns the percentage between a and b that 'val' is on a line (opposite of Lerp)
local sliderStart = 0.0;
local sliderStop = 100.0;
local currentSliderPos = 75.0;
local percentage = Space.Math.InverseLerp(sliderStart, sliderStop, currentSliderPos);
Space.Log(percentage);
-- prints 0.75
IsPowerOfTwo
bool IsPowerOfTwo
(int val);
Returns true if val is a power of two
Space.Log(Space.Math.IsPowerOfTwo(55));
-- prints False
Space.Log(Space.Math.IsPowerOfTwo(32));
-- prints True
Lerp
float Lerp
(float a, float b, float val);
Interpolates between 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1
local sliderStart = 0.0;
local sliderStop = 100.0;
local percentage = 0.75;
local currentSliderPos = Space.Math.Lerp(sliderStart, sliderStop, percentage);
Space.Log(currentSliderPos);
-- prints 75
LerpAngle
float LerpAngle
(float a, float b, float val);
Interpolates between angles 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1
local sliderStartAngle = 0.0;
local sliderStopAngle = Space.Math.Pi;-- 180 degrees
local percentage = 0.50;
local currentSliderPos = Space.Math.LerpAngle(sliderStartAngle, sliderStopAngle, percentage);
Space.Log(currentSliderPos);
-- prints 1.57079637050629 (90 degrees)
LerpUnclamped
float LerpUnclamped
(float a, float b, float val);
Interpolates between 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1, but unbounded (allowing higher/lower values)
local sliderStart = 0.0;
local sliderStop = 100.0;
local percentage = 2.0;
local currentSliderPos = Space.Math.LerpUnclamped(sliderStart, sliderStop, percentage);
Space.Log(currentSliderPos);
-- prints 200
LinearToGammaSpace
float LinearToGammaSpace
(float val);
Converts a colour value from Linear to Gamma Space (Pow 1/2.2)
Space.Log(Space.Math.LinearToGammaSpace(0.214041143655777));
-- prints 0.5
Log
Returns the natural logarithm for 'val'
Space.Log(Space.Math.Log(30.0));
-- prints 3.40119743347168
Log
float Log
(float val, float p);
Returns the logarithm of 'p' for 'val'
Space.Log(Space.Math.Log(4.0, 2.0));
-- prints 2
Log10
Returns the Log10 value for 'val'
Space.Log(Space.Math.Log10(100.0));
-- prints 2
Max
float Max
(float a, float b);
Returns higher of 'a' or 'b'
Space.Log(Space.Math.Max(20.0, 100.0));
-- prints 100
Min
float Min
(float a, float b);
Returns lower of 'a' or 'b'
Space.Log(Space.Math.Min(20.0, 100.0));
-- prints 20
MoveTowards
float MoveTowards
(float value, float target, float delta);
Move value to target, but by no more than delta
local sliderStart = 5.0;
local sliderStop = 10.0;
local deltaChange = 1.0;
local currentSliderPos = Space.Math.MoveTowards(sliderStart, sliderStop, deltaChange);
Space.Log(currentSliderPos);
-- prints 6 (moves forward 1)
local sliderStart = 5.0;
local sliderStop = 10.0;
local deltaChange = 7.0;
local currentSliderPos = Space.Math.MoveTowards(sliderStart, sliderStop, deltaChange);
Space.Log(currentSliderPos);
-- prints 10 (caps out at at the target value)
-- NOTE: Use negative delta to move away from target.
MoveTowardsAngle
float MoveTowardsAngle
(float value, float target, float delta);
Move angle value to target, but by no more than delta
No example provided yet
NextPowerOfTwo
int NextPowerOfTwo
(int val);
Return the next power of two larger or equal to val
No example provided yet
PerlinNoise
float PerlinNoise
(float x, float y);
Return 2D Perlin noise for coordinates x and y
-- Lua Translation of Unity C# Documentation
local ball = Space.Host.ExecutingObject;
ball.SubscribeToEvents();
-- animates this object to move upwards on the y axis with slight random movement
local animateBall = function()
local heightScale = 0.1; -- controls speed of movement
local xScale = 1.0; -- shifts x position on perlin noise plane
local height = heightScale * Space.Math.PerlinNoise(Space.Time * xScale, 0.0);
local pos = ball.LocalPosition;
pos.y = pos.y + height;
ball.LocalPosition = pos;
Space.Log(pos.y);
end
ball.OnUpdate(animateBall);
-- NOTE: If you need a guaranteed range of [0,1], make sure to clamp the value with Space.Math.Clamp01.
PingPong
float PingPong
(float val, float length);
Return a value between 0 and length that oscillates upwards and back based on the position of 'val'
No example provided yet
Pow
float Pow
(float x, float y);
Return x raised to y power
No example provided yet
Repeat
float Repeat
(float val, float length);
Return a value between 0 and length that returns to 0 after exceeding length based on 'val'
No example provided yet
Round
Returns the nearest integer value to val
No example provided yet
Sign
Returns either 1 or -1 based on the sign of 'val'
No example provided yet
Sin
Returns the sine of val
No example provided yet
SmoothStep
float SmoothStep
(float from, float to, float val);
Similar to Lerp but moves slowly closer to the edges ('Spherical Lerp')
No example provided yet
Sqrt
Returns the square root of val
No example provided yet
Tan
Returns the tangent value of 'val'
No example provided yet
Scripting Portal
|
|
Common
|
|
|
Key Classes
|
|
|
Helper Classes
|
|
|
Scripting samples and tutorials
|
|
|