Ashasekayi (Talk | contribs) |
Ashasekayi (Talk | contribs) |
||
Line 177: | Line 177: | ||
local min = 1.0;<br> | local min = 1.0;<br> | ||
local max = 10.0;<br> | local max = 10.0;<br> | ||
− | local duration = 5.0;<br> | + | local duration = 5.0; ''--lower value to speed up; raise to slow down''<br> |
local startTime;<br> | local startTime;<br> | ||
ball.SubscribeToEvents();<br><br> | ball.SubscribeToEvents();<br><br> | ||
Line 187: | Line 187: | ||
local animateBall = function()<br> | local animateBall = function()<br> | ||
local time = (Space.Time - startTime) / duration;<br> | local time = (Space.Time - startTime) / duration;<br> | ||
− | local newPos = Vector.New('''Space.Math.SmoothStep'''(min, max, time) + originalPos.x, originalPos.y, originalPos.z);<br> | + | local newPos = Vector.New('''Space.Math.SmoothStep'''(min, max, time)<br> |
+ | + originalPos.x, originalPos.y, originalPos.z);<br> | ||
ball.LocalPosition = newPos;<br> | ball.LocalPosition = newPos;<br> | ||
end<br><br> | end<br><br> |
The SMath class contains common math functions, see also SVector and SQuaternion for Vector and Quaternion related math functions.
Returns the constant value of Π (readonly).
Returns a random float between 0 and 1 (inclusive)
Returns a random float between min and max (inclusive)
Returns a random float between min (inclusive) and max (exclusive)
Returns the absolute value of 'val'
Returns the absolute value of 'val'
Returns the arc cosine value of 'val'
True if the difference between a and b is less than epsilon
Returns the arc sine value of 'val'
Returns the arc tangent value of 'val'
Returns the arc tangent of y/x
Returns the ceil value of 'val' as an integer
Clamps val between min and max, and returns the result
Clamps val between 0 and 1, and returns the result
Returns the closest power of two to val
Returns the cosine of val
Returns the difference in degrees between two values (e.g. 350' and 17' returns 27')
Returns e raised to val power.
Returns floor of val, converted to an int
Converts a colour value from Gamma to Linear Space (Pow 2.2)
Returns the percentage between a and b that 'val' is on a line (opposite of Lerp)
Returns true if val is a power of two
Interpolates between 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1
Interpolates between angles 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1
Interpolates between 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1, but unbounded (allowing higher/lower values)
Converts a colour value from Linear to Gamma Space (Pow 1/2.2)
Returns the natural logarithm for 'val'
Returns the logarithm of 'p' for 'val'
Returns the Log10 value for 'val'
Returns higher of 'a' or 'b'
Returns lower of 'a' or 'b'
Move value to target, but by no more than delta
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)
Move angle value to target, but by no more than delta
local sliderStopAngle = Space.Math.Pi; -- 180 degrees
local deltaChange = Space.Math.Pi/6; -- 30 degrees
local currentSliderPos = Space.Math.MoveTowardsAngle(sliderStartAngle, sliderStopAngle, deltaChange);
Space.Log(currentSliderPos);
-- prints 2.09439516067505 (moves forward 30 degrees to 120 degrees)
Return the next power of two larger or equal to val
Return 2D Perlin noise for coordinates x and y
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);
Return a value between 0 and length that oscillates upwards and back based on the position of 'val'
the return values would cycle forward through the range [0,5] then cycle backwards through the range [5,0] in order. --]]
local ball = Space.Host.ExecutingObject;
local originalPos = ball.LocalPosition;
ball.SubscribeToEvents();
-- The ball object oscillates back and forth on the x-axis.
local animateBall = function()
local value = Space.Time;
local length = 5.0;
local newPos = Vector.New(Space.Math.PingPong(value, length) + originalPos.x, originalPos.y, originalPos.z);
ball.LocalPosition = newPos;
end
Return x raised to y power
Return a value between 0 and length that returns to 0 after exceeding length based on 'val'
the return values would cycle forward through the range [0,5]. The cycle is repeated again from 0.--]]
local ball = Space.Host.ExecutingObject;
local originalPos = ball.LocalPosition;
ball.SubscribeToEvents();
-- The ball object moves forward on the x-axis.
-- Then, it repeats the same motion again from the beginning.
local animateBall = function()
local value = Space.Time;
local length = 5.0;
local newPos = Vector.New(Space.Math.Repeat(value, length) + originalPos.x, originalPos.y, originalPos.z);
ball.LocalPosition = newPos;
end
Returns the nearest integer value to val
-- print 4
Space.Log(Space.Math.Round(4.2));
-- print 4
Space.Log(Space.Math.Round(4.5));
-- print 4
Space.Log(Space.Math.Round(4.55));
-- print 5
Space.Log(Space.Math.Round(4.8));
-- print 5
Space.Log(Space.Math.Round(-4.2));
-- print -4
Space.Log(Space.Math.Round(-4.8));
Returns either 1 or -1 based on the sign of 'val'
Returns the sine of val
Similar to Lerp but moves slowly closer to the edges ('Spherical Lerp')
--[[ In this example, Space.Math.SmoothStep(min, max, time) returns values in the range [1,10] in order.
As values approach 10, the interpolation slows down. --]]
local ball = Space.Host.ExecutingObject;
local originalPos = ball.LocalPosition;
local min = 1.0;
local max = 10.0;
local duration = 5.0; --lower value to speed up; raise to slow down
local startTime;
ball.SubscribeToEvents();
local initStartTime = function()
startTime =Space.Time;
end
-- The ball jumps to min + originalPos, then it moves towards
-- max + originalPos while slowing down at the end.
local animateBall = function()
local time = (Space.Time - startTime) / duration;
local newPos = Vector.New(Space.Math.SmoothStep(min, max, time)
+ originalPos.x, originalPos.y, originalPos.z);
ball.LocalPosition = newPos;
end
ball.OnStart(initStartTime);
Returns the square root of val
Returns the tangent value of 'val'
|