Game Development Reference
In-Depth Information
function angleBetween(ptA, ptB)
local angle = math.deg(math.atan2(ptA.y - ptB.y, ptA.x - ptB.x))
angle = angle + 90
return angle
end
Keeping a Number in a Given Range
There could be a scenario when you might want to keep a value within a given range. In this way,
rather than having several if...then statements for each value, we can create a function that keeps
the value within a range.
return math.min(math.max(low, theVal), high)
if...then statements, looks like this:
if theVal < low then
return low
end
if theVal > high then
return high
end
return theVal
end
Performing Linear Interpolation
No matter what framework you use for your game, the game might have some form of transition—for
example, a fade in or a movement. The simplest form way to accomplish a transition is to apportion
a value over time equally (linearly). Using the following function, you can retrieve a value over a
given time by designating the starting and ending values. Here, we are calling the retrieved value
thePercent , which is a value between 0 and 1, indicating the state of progress—0 being the start and
1 being completed.
function getLinearInterpolation(theStartValue, theEndValue, thePercent)
return (theStartValue + (theEndValue - theStartValue) * thePercent)
end
Getting the Sign of a Value
Sometimes it's important to determine if a value is a negative number of not. It is easy to check
whether a number is less than 0, but when performing a calculation, it might not be possible to use
if...then statements. Instead, you can create a function. To keep things organized, you should
add it to the math namespace.
 
Search WWH ::




Custom Search